You may know that I'm working on a karma MOD that will eventually be two-sided, with an aspect of mod/admin-controlled karma and with an aspect of user-given karma not unlike a "thanks" MOD.
But I've hit a wall I can't seem to avoid nor tear down: I have a "functions_karpost.php" file, and I have code changes in "viewtopic.php", but they do not seem to work together.
I'm sure it's a stupid part of the functions file that's missing, but at the same time, I've based this code on the custom profile fields code (/include/functions_profile_fields.php and viewtopic.php)? So why it doesn't work is beyond me.
Here's the code I'm talking about:
- Code: Select all
<?php
class karma
{
function generate_karma_template($mode, &$row, $user_id = 0)
{
global $db, $user;
if ($mode == 'grab')
{
error_reporting(E_ALL);
$sql = 'SELECT *
FROM ' . USERS_TABLE . "
WHERE user_id = $user_id";
$result = $db->sql_query($sql);
$karma_data = array();
while ($ka_row = $db->sql_fetchrow($result))
{
$karma_data[$ka_row['karma']] = $ka_row;
}
$db->sql_freeresult($result);
return $ka_row['karma'];
}
else if ($mode == 'show')
{
$tpl_karma = array();
$tpl_karma['row'] = $tpl_karma['blockrow'] = array();
$karma_value = $this->get_karma($ka_row);
$tpl_karma['row'] = array(
'karma' => $karma_value,
'KARMA_NAME' => $user->lang['KARMA'],
);
$tpl_karma['blockrow'] = array(
'KARMA_FIELD_VALUE' => $karma_value,
'KARMA_NAME' => $user->lang['KARMA'],
);
return $tpl_karma;
}
else
{
trigger_error('Wrong mode for karma', E_USER_ERROR);
}
}
function get_karma(&$ka_row)
{
global $user;
$karma_value = $ka_row['karma'];
$positive_karmas = array
(
1 => 0,
2 => 0,
3 => 0,
4 => 0,
5 => 0,
6 => 1,
7 => 1,
8 => 1,
9 => 1,
10 => 1,
11 => 1,
12 => 1,
13 => 2,
14 => 2,
15 => 2,
16 => 2,
17 => 2,
18 => 2,
19 => 2,
20 => 2,
21 => 2,
22 => 3,
23 => 3,
24 => 3,
25 => 3,
26 => 3,
27 => 3,
28 => 3,
29 => 3,
30 => 3,
31 => 3,
32 => 3,
33 => 4,
34 => 4,
35 => 4,
36 => 4,
37 => 4,
38 => 4,
39 => 4,
40 => 4,
41 => 4,
42 => 4,
43 => 4,
44 => 4,
45 => 4,
46 => 5,
47 => 5,
48 => 5,
49 => 5,
50 => 5,
51 => 6,
52 => 6,
53 => 6,
54 => 6,
55 => 6,
56 => 6,
57 => 6,
58 => 6,
59 => 6,
60 => 6,
61 => 6,
62 => 6,
63 => 6,
64 => 6,
65 => 6,
66 => 6,
67 => 6,
68 => 7,
69 => 7,
70 => 7,
71 => 7,
72 => 7,
73 => 7,
74 => 7,
75 => 7,
76 => 7,
77 => 7,
78 => 7,
79 => 7,
80 => 7,
81 => 7,
82 => 7,
83 => 7,
84 => 7,
85 => 7,
86 => 7,
87 => 8,
88 => 8,
89 => 8,
90 => 8,
91 => 8,
92 => 8,
93 => 8,
94 => 8,
95 => 8,
96 => 8,
97 => 8,
98 => 8,
99 => 8,
100 => 9
);
$negative_karmas = array
(
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
8 => 7,
10 => 8
);
if (isset($positive_karmas[$karma_value]))
{
$poster_karma = $user->lang['karma_positive'][$karma_value];
}
elseif (isset($negative_karmas[-$karma_value]))
{
$poster_karma = $user->lang['karma_negative'][-$karma_value];
}
else
{
if ($karma_value < 0)
$poster_karma = $user->lang['karma_negative'][8];
else
$poster_karma = $user->lang['karma_positive'][9];
$poster_karma = $user->lang['karma_negative'][0];
}
return $poster_karma;
}
}
?>
Viewtopic changes:
- Code: Select all
// Load custom profile fields
if ($config['load_cpf_viewtopic'])
{
include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
$cp = new custom_profile();
// Grab all profile fields from users in id cache for later use - similar to the poster cache
$profile_fields_cache = $cp->generate_profile_fields_template('grab', $id_cache);
}
// Load karma field
include($phpbb_root_path . 'includes/functions_karpost.' . $phpEx);
$ka = new karma();
// Grab karma field from users in id cache for later use - similar to the poster cache
$karma_field_cache = $ka->generate_karma_template('grab', $id_cache);
[?]
if ($config['load_cpf_viewtopic'])
{
$cp_row = (isset($profile_fields_cache[$poster_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields_cache[$poster_id]) : array();
}
$ka_row = array();
$ka_row = (isset($karma_field_cache[$poster_id])) ? $ka->generate_karma_template('show', $row, $karma_field_cache[$poster_id]) : array();
[?]
'S_CUSTOM_FIELDS' => (isset($cp_row['row']) && sizeof($cp_row['row'])) ? true : false,
'S_KARMA' => (isset($ka_row['row']) && sizeof($ka_row['row'])) ? true : false,
[?]
if (isset($cp_row['row']) && sizeof($cp_row['row']))
{
$postrow = array_merge($postrow, $cp_row['row']);
}
$postrow = array_merge($postrow, $tpl_karma['row']);
// Dump vars into template
$template->assign_block_vars('postrow', $postrow);
if (!empty($cp_row['blockrow']))
{
foreach ($cp_row['blockrow'] as $field_data)
{
$template->assign_block_vars('postrow.custom_fields', $field_data);
}
}
if (!empty($tpl_karma['blockrow']))
{
foreach ($tpl_karma['blockrow'] as $karma_data)
{
$template->assign_block_vars('postrow', array(
'KARMA_VALUE' => '100') // this part was intended as a test to see if it actually worked? which it didn't
);
}
}
And the problem is that viewtopic tells me that "tpl_karma" is an undefined variable. And on top of that, when I was using code closer to the custom profile fields code, it told me the same thing for $ka_row
So what's the problem exactly? Anyone with any ideas?




Well, I might as well say that young developers are often smarter 



