News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Post Karma

Started by nend, September 22, 2012, 01:10:38 PM

Previous topic - Next topic

nend

Link to Mod

This mod adds two new links to the Karma system which you can see in each post. These links contain the number of smites and applauds a post has.

This mod has no configuration since it relies on SMF's karma system settings.

License
QuoteCopyright (c) 2012, Russell Najar (SiberInc)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the SiberInc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Releases
0.1
- First build and initial upload

0.2
* Small change in installation. Doesn't affect modification once installed.

NanoSector

Nice mod, I'm going to use this on my own site :)

Going to make a small change to it though to only show/work on the first post of a topic. That'll make it ideal for my site :)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

tMicky


nend

Thanks,

Expected a little more comments with all the downloads. Later on when I have enough time I will post my "Trending" block which looks at the karma per post and displays which post are trending. You can add this in a PHP block in your portal. Will post later though because I have to go soon.

nend

Here is the trending post function.

function mini_trendingPosts($num_recent = 5)
{
global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
global $modSettings, $smcFunc;

//Check cache
if($context['user']['is_logged'] && ($data = cache_get_data('g'.$user_info['groupString'].'trendingPostscache', 1800)) != null) {
return $data;
} else if (!$context['user']['is_logged'] && ($data = cache_get_data('uguesttrendingPostscache', 1800)) != null) {
return  $data;
}

// Find all the posts in distinct topics.  Newer ones will have higher IDs.
$request = $smcFunc['db_query']('substring', '
SELECT
m.poster_time, m.id_topic, m.id_member, m.id_msg, b.id_board, b.name AS board_name, t.num_replies, t.num_views,
IFNULL(mem.real_name, m.poster_name) AS poster_name, mem.avatar, m.body AS body, m.smileys_enabled, m.icon, tb.subject, tb.body AS tbody,
IFNULL(a.id_attach, 0) AS ID_ATTACH, a.filename, a.attachment_type as attachmentType
FROM {db_prefix}messages AS m
INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member AND a.attachment_type!=3)
LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
LEFT JOIN {db_prefix}messages AS tb ON (tb.id_msg = t.id_first_msg)
WHERE m.poster_time > {int:time_back}
AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
AND t.approved = {int:is_approved}
AND m.approved = {int:is_approved}' : '') . '
ORDER BY m.karma_good - m.karma_bad DESC
LIMIT {int:numrecent}' ,
array(
'current_member' => $user_info['id'],
'include_boards' => empty($include_boards) ? '' : $include_boards,
'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
'min_message_id' => $modSettings['maxMsgID'] - 35 * min($num_recent, 5),
'is_approved' => 1,
'numrecent' => $num_recent,
'time_back' => time() - 2599277,
)
);

$posts = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$shortSub = null;
$row['body'] = strip_tags(str_replace('<br />', ' ', parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg'])));
if ($smcFunc['strlen']($row['body']) > 170)
$row['body'] = $smcFunc['substr']($row['body'], 0, 170) . '...';
if ($smcFunc['strlen']($row['subject']) > 45)
$shortSub = $smcFunc['substr']($row['subject'], 0, 45) . '...';

// Censor the subject.
censorText($row['subject']);
censorText($row['body']);

if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
$icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';

// Build the array.
$posts[] = array(
'board' => array(
'id' => $row['id_board'],
'name' => $row['board_name'],
'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>',
),
'topic' => $row['id_topic'],
'poster' => array(
'id' => $row['id_member'],
'name' => $row['poster_name'],
'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
),
'readmore' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new">Read More</a>',
'avatar' => $row['avatar'] == '' ? ($row['ID_ATTACH'] > 0 ? '<img src="' . (empty($row['attachmentType']) ? $scripturl . '?action=dlattach;attach=' . $row['ID_ATTACH'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $row['filename']) . '" alt="&nbsp;"  />' : '') : (stristr($row['avatar'], 'http://') ? '<img src="' . $row['avatar'] . '" alt="&nbsp;" />' : '<img src="' . $modSettings['avatar_url'] . '/' . $smcFunc['htmlspecialchars']($row['avatar']) . '" alt="&nbsp;" />'),
'subject' => $row['subject'],
'replies' => $row['num_replies'],
'views' => $row['num_views'],
'short_subject' => shorten_subject($row['subject'], 25),
'preview' => $row['body'],
'time' => protimeformat($row['poster_time']),
'timestamp' => forum_time(true, $row['poster_time']),
'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow"' . (isset($shortSub) ? ' title="' . $row['subject'] .'">' . $shortSub : '>' . $row['subject']) . '</a>',
// Retained for compatibility - is technically incorrect!
'new' => !empty($row['is_read']),
'is_new' => empty($row['is_read']),
'new_from' => $row['new_from'],
'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '"  />',
);
}
$smcFunc['db_free_result']($request);

// Make cache
if($context['user']['is_logged']) {
cache_put_data('g'.$user_info['groupString'].'trendingPostscache', $posts, 1800);
} else {
cache_put_data('uguesttrendingPostscache', $posts, 1800);
}

return $posts;
}


You can use this function to return the trending post within the time frame of time_back.

Jade Elizabeth

OH MY GOD I LOVE YOU SO MUCH.

BUT how does it work? Will it work with Karma Description and Limit Karma?
http://mods.simplemachines.org/index.php?mod=192
http://custom.simplemachines.org/mods/index.php?mod=1308
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

tMicky

Quote from: nend on September 28, 2012, 12:42:23 PM
Here is the trending post function.

You can use this function to return the trending post within the time frame of time_back.

Thank you. I do have a question, are we supposed to change something in the time_back area, to make the posts show. I added the block, but no post is listed?

CreativeITWorld.com

Had a problem:

Quote1.   Execute Code   db.php   
   2.   Execute Modification   ./Sources/Karma.php   Test successful
   3.   Execute Modification   ./Sources/Display.php   Test failed
      1.   Add After   ./Sources/Display.php   Test successful
      2.   Add After   ./Sources/Display.php   Test failed

   4.   Execute Modification   ./Themes/default/Display.template.php   Test successful

This is a heavily modified forum, so if nobody else reports the same problem, you will know that the installation problem is due to conflicts.

Arantor

Well, the conflicts aren't there on a vanilla 2.0 install, which means it's due to conflicts...

nend

Quote from: CreativeITWorld.com on October 12, 2012, 07:33:03 PM
Had a problem:

Quote1.   Execute Code   db.php   
   2.   Execute Modification   ./Sources/Karma.php   Test successful
   3.   Execute Modification   ./Sources/Display.php   Test failed
      1.   Add After   ./Sources/Display.php   Test successful
      2.   Add After   ./Sources/Display.php   Test failed

   4.   Execute Modification   ./Themes/default/Display.template.php   Test successful

This is a heavily modified forum, so if nobody else reports the same problem, you will know that the installation problem is due to conflicts.
The install code was tested on a vanilla 2.0 board without any problems.

The two lines in Display.php is the portion added to the query and the portion to add the information to the array. The mod will not display karma per post without this.

I recommend to do a manual install or pay someone to manually install it for you.

I don't recommend to go further with the install via the package manager, I doubt it would cause any problems but it wouldn't add any benefits/features without that portion of the code working.

The choice is yours.

rlh2006

I started using this mod about a week ago. I like it but there are some minor issues that need a coding fix.

Issue I: Guest Browsers

Logged-in users see post karma (normally) as:



Guests sees the post karma in an odd way:




And there is an error constantly showing up in my log, which I think is related to guests browsing topics

Error: 8: Undefined index: karma
File: forum/Themes/default/Display.template.php (main sub template - eval?)
Line: 498
Line Text ==>498: if ($message['member']['karma']['allow'])


Issue II: Posts that Remain from Deleted User Accounts

When logged-in users browse a post left over from a deleted account the following error is generated:

Error: 8: Undefined index: karma
File: forum/Themes/default/Display.template.php (main sub template - eval?)
Line: 498
Line Text ==>498: if ($message['member']['karma']['allow'])


If any Simple Machines Hero could give a possible coding fix here I would surely appreciate it.

368 Urban Terror avatars for your site or forum.
The avatars pack is 11.6 mbs. Direct free download for the avatars pack is right here: urt_avatars.zip This avatar pack is 1up Clan's way of contributing to both the Urban Terror gaming community and the Simple Machines community. Due to the size of the pack, SMF has not approved it.

nend

Your theme may be using a custom Display.template.php which may be causing the errors and causing the text to miss align.

Basically saying, post your Display.template.php, something is wrong in it.  ;D

Jade Elizabeth

Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

NanoSector

Set up a test installation and see :P

Looking at the code it doesn't appear to do anything special with them.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

rlh2006

Quote from: nend on October 14, 2012, 01:14:05 PM
Your theme may be using a custom Display.template.php which may be causing the errors and causing the text to miss align.

Basically saying, post your Display.template.php, something is wrong in it.  ;D
Ok. I had a coder look this over and these changes were applied to my site.
Note: on my site guests can't post or give karma.

Open Themes/default/Display.template.php  its the default theme (because the theme I am using doesn't have its own and loads the file from the default theme).

find:



// Karma Per Post
if ($message['member']['karma']['allow'])
echo '
<li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=modifykarma;sa=applaud;uid=', $message['member']['id'], ';topic=', $context['current_topic'], '.' . $context['start'], ';m=', $message['id'], ';', $context['session_var'], '=', $context['session_id'], '">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</a></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=modifykarma;sa=smite;uid=', $message['member']['id'], ';topic=', $context['current_topic'], '.' . $context['start'], ';m=', $message['id'], ';', $context['session_var'], '=', $context['session_id'], '">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</a></li>';


and below that part, add the following:


elseif ($context['user']['is_guest'])
echo '
<ul class="reset smalltext quickbuttons"><li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=login">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</a></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=login">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</a></li></ul>';



So the final product looks like this:



// Karma Per Post
if ($message['member']['karma']['allow'])
echo '
<li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=modifykarma;sa=applaud;uid=', $message['member']['id'], ';topic=', $context['current_topic'], '.' . $context['start'], ';m=', $message['id'], ';', $context['session_var'], '=', $context['session_id'], '">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</a></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=modifykarma;sa=smite;uid=', $message['member']['id'], ';topic=', $context['current_topic'], '.' . $context['start'], ';m=', $message['id'], ';', $context['session_var'], '=', $context['session_id'], '">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</a></li>';

elseif ($context['user']['is_guest'])
echo '
<ul class="reset smalltext quickbuttons"><li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=login">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</a></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=login">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</a></li></ul>';

else
echo '
<li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><span style="padding-left:20px;display:block;height:20px;line-height:18px;float:left;">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</span></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><span style="padding-left:20px;display:block;height:20px;line-height:18px;float:left;">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</span></li>';
// Can we restore topics?
368 Urban Terror avatars for your site or forum.
The avatars pack is 11.6 mbs. Direct free download for the avatars pack is right here: urt_avatars.zip This avatar pack is 1up Clan's way of contributing to both the Urban Terror gaming community and the Simple Machines community. Due to the size of the pack, SMF has not approved it.

rlh2006

Also, if you do not allow guest posting and you have deleted accounts from prior members but left their posts then you will receive an error related to that, so this fix will resolve that:

// Karma Per Post
if (!$message['member']['is_guest'] && $message['member']['karma']['allow'])
echo '
<li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=modifykarma;sa=applaud;uid=', $message['member']['id'], ';topic=', $context['current_topic'], '.' . $context['start'], ';m=', $message['id'], ';', $context['session_var'], '=', $context['session_id'], '">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</a></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=modifykarma;sa=smite;uid=', $message['member']['id'], ';topic=', $context['current_topic'], '.' . $context['start'], ';m=', $message['id'], ';', $context['session_var'], '=', $context['session_id'], '">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</a></li>';

elseif (!$message['member']['is_guest'] && $context['user']['is_guest'])
echo '
<ul class="reset smalltext quickbuttons"><li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=login">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</a></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=login">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</a></li></ul>';

elseif (!$message['member']['is_guest'])
echo '
<li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><span style="padding-left:20px;display:block;height:20px;line-height:18px;float:left;">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</span></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><span style="padding-left:20px;display:block;height:20px;line-height:18px;float:left;">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</span></li>';

// Can we restore topics?
368 Urban Terror avatars for your site or forum.
The avatars pack is 11.6 mbs. Direct free download for the avatars pack is right here: urt_avatars.zip This avatar pack is 1up Clan's way of contributing to both the Urban Terror gaming community and the Simple Machines community. Due to the size of the pack, SMF has not approved it.

impreza

Nice addition, for sure come in handy. thanks
Portal ToTemat.pl - treści w postaci artykułów i filmów tematycznych.

nend

Quote from: rlh2006 on October 15, 2012, 02:16:28 PM
Also, if you do not allow guest posting and you have deleted accounts from prior members but left their posts then you will receive an error related to that, so this fix will resolve that:

// Karma Per Post
if (!$message['member']['is_guest'] && $message['member']['karma']['allow'])
echo '
<li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=modifykarma;sa=applaud;uid=', $message['member']['id'], ';topic=', $context['current_topic'], '.' . $context['start'], ';m=', $message['id'], ';', $context['session_var'], '=', $context['session_id'], '">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</a></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=modifykarma;sa=smite;uid=', $message['member']['id'], ';topic=', $context['current_topic'], '.' . $context['start'], ';m=', $message['id'], ';', $context['session_var'], '=', $context['session_id'], '">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</a></li>';

elseif (!$message['member']['is_guest'] && $context['user']['is_guest'])
echo '
<ul class="reset smalltext quickbuttons"><li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=login">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</a></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><a href="', $scripturl, '?action=login">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</a></li></ul>';

elseif (!$message['member']['is_guest'])
echo '
<li style="background: url(', $settings['images_url'], '/post/thumbup.gif) no-repeat 0 0;"><span style="padding-left:20px;display:block;height:20px;line-height:18px;float:left;">', $modSettings['karmaApplaudLabel'], $message['karma']['good'], '</span></li>
<li style="background: url(', $settings['images_url'], '/post/thumbdown.gif) no-repeat 0 0;"><span style="padding-left:20px;display:block;height:20px;line-height:18px;float:left;">', $modSettings['karmaSmiteLabel'], $message['karma']['bad'], '</span></li>';

// Can we restore topics?


Been a little busy to go over the code but yeah guest posting and deleted members may throw some errors. Will look into that to see what needs to be done to fix it if a fix is needed.  ;)

Lazarevics

#18
here the full corrected package (tested)
thx rlh2006!

nend

Thanks,

I completely forgot about that bug. I have been doing this one off from memory as I haven't set up any tracking for such mods. I might set up a repo, anybody is welcomed to add to this mod, hence the open source license.

However to avoid confusion if anyone does want to release a variant on the mod site of any of my mods please make sure it adds a completely new feature, otherwise don't because it may cause too much confusion.

That being said, please feel free to modify, distribute or whatever you like. Remember you can't sell the code due to the license. Anyways have fun with it.

Advertisement: