SMF Community Helpers > Mod Development
[MOD]Karma Per Message
nend:
This is a work in progress that I am sharing. I just finished the backend portion today, well sort of I still need to edit the query to get the karma information per post but it is saving the information to the database.
Database
Run Sql
--- Code: ---ALTER TABLE `smf_messages` ADD `karma_bad` SMALLINT( 5 ) NOT NULL DEFAULT '0' AFTER `approved` ,
ADD `karma_good` SMALLINT( 5 ) NOT NULL DEFAULT '0' AFTER `karma_bad` ;
--- End code ---
Sources/Karma.php
Find
--- Code: --- $smcFunc['db_insert']('replace',
'{db_prefix}log_karma',
array('action' => 'int', 'id_target' => 'int', 'id_executor' => 'int', 'log_time' => 'int'),
array($dir, $_REQUEST['uid'], $user_info['id'], time()),
array('id_target', 'id_executor')
);
// Change by one.
updateMemberData($_REQUEST['uid'], array($dir == 1 ? 'karma_good' : 'karma_bad' => '+'));
--- End code ---
Add After
--- Code: --- if (!empty($_REQUEST['m'])) {
// Change by one, message karma
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET '.($dir == 1 ? 'karma_good = karma_good' : 'karma_bad = karma_bad').' + 1
WHERE id_msg = {int:id_message}',
array(
'id_message' => $_REQUEST['m']
)
);
}
--- End code ---
Find
--- Code: --- // It was recently changed the OTHER way... so... reverse it!
if ($dir == 1)
updateMemberData($_REQUEST['uid'], array('karma_good' => '+', 'karma_bad' => '-'));
else
updateMemberData($_REQUEST['uid'], array('karma_bad' => '+', 'karma_good' => '-'));
--- End code ---
Replace
--- Code: --- // It was recently changed the OTHER way... so... reverse it!
if ($dir == 1) {
updateMemberData($_REQUEST['uid'], array('karma_good' => '+', 'karma_bad' => '-'));
if (!empty($_REQUEST['m'])) {
// Change by one, message karma
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET karma_good = karma_good + 1, karma_bad = karma_bad - 1
WHERE id_msg = {int:id_message}',
array(
'id_message' => $_REQUEST['m']
)
);
}
} else {
updateMemberData($_REQUEST['uid'], array('karma_bad' => '+', 'karma_good' => '-'));
if (!empty($_REQUEST['m'])) {
// Change by one, message karma
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET karma_good = karma_good - 1, karma_bad = karma_bad + 1
WHERE id_msg = {int:id_message}',
array(
'id_message' => $_REQUEST['m']
)
);
}
}{/code]
--- End code ---
Orangine:
so in other words it's a Like/Dislike for posts?
nend:
Basically yes, but this one works off SMF Karma system, I don't know how the others work.
All I have to do now is edit the query for the display and edit the template and it is finished. Basically very minimal. I wish there was a integration hook right there though.
nend:
Here is for Sources/Display.php
Find
--- Code: --- // What? It's not like it *couldn't* be only guests in this topic...
if (!empty($posters))
loadMemberData($posters);
$sticky_sort = $sticky_post != '' ? "id_msg != " . $sticky_post : '';
$messages_request = $smcFunc['db_query']('', '
SELECT
id_msg, icon, subject, poster_time, poster_ip, id_member, modified_time, modified_name, body,
smileys_enabled, poster_name, poster_email, approved,
id_msg_modified < {int:new_from} AS is_read
FROM {db_prefix}messages
WHERE id_msg IN ({array_int:message_list})
ORDER BY ' .$sticky_sort. ' id_msg' . (empty($options['view_newest_first']) ? '' : ' DESC'),
array(
'message_list' => $messages,
'new_from' => $topicinfo['new_from'],
)
);
--- End code ---
Replace
--- Code: --- // What? It's not like it *couldn't* be only guests in this topic...
if (!empty($posters))
loadMemberData($posters);
$sticky_sort = $sticky_post != '' ? "id_msg != " . $sticky_post : '';
$messages_request = $smcFunc['db_query']('', '
SELECT
id_msg, icon, subject, poster_time, poster_ip, id_member, modified_time, modified_name, body,
smileys_enabled, poster_name, poster_email, approved, karma_good, karma_bad,
id_msg_modified < {int:new_from} AS is_read
FROM {db_prefix}messages
WHERE id_msg IN ({array_int:message_list})
ORDER BY ' .$sticky_sort. ' id_msg' . (empty($options['view_newest_first']) ? '' : ' DESC'),
array(
'message_list' => $messages,
'new_from' => $topicinfo['new_from'],
)
);
--- End code ---
Find
--- Code: --- // Run BBC interpreter on the message.
$message['body'] = parse_bbc($message['body'], $message['smileys_enabled'], $message['id_msg']);
// Compose the memory eat- I mean message array.
$output = array(
'attachment' => loadAttachmentContext($message['id_msg']),
'alternate' => $counter % 2,
'id' => $message['id_msg'],
'href' => $scripturl . '?topic=' . $topic . '.msg' . $message['id_msg'] . '#msg' . $message['id_msg'],
'link' => '<a href="' . $scripturl . '?topic=' . $topic . '.msg' . $message['id_msg'] . '#msg' . $message['id_msg'] . '" rel="nofollow">' . $message['subject'] . '</a>',
'member' => &$memberContext[$message['id_member']],
'icon' => $message['icon'],
'icon_url' => $settings[$context['icon_sources'][$message['icon']]] . '/post/' . $message['icon'] . '.gif',
'subject' => $message['subject'],
'time' => timeformat($message['poster_time']),
'timestamp' => forum_time(true, $message['poster_time']),
'counter' => $counter,
'modified' => array(
'time' => timeformat($message['modified_time']),
'timestamp' => forum_time(true, $message['modified_time']),
'name' => $message['modified_name']
),
--- End code ---
Add After
--- Code: --- 'karma' => array(
'good' => $message['karma_good'],
'bad' => $message['karma_bad'],
),
--- End code ---
Orangine:
is it fully working?
Navigation
[0] Message Index
[#] Next page
Go to full version