News:

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

Main Menu

Showing Highest and Lowest Karma Rating in Stats Page

Started by Tristan Perry, October 24, 2004, 05:05:33 AM

Previous topic - Next topic

Tristan Perry

Hello,
  This small mod adds the top 5 karma ratings, and the lowest 5 karma ratings to the stats page. You can see an example of it here:
http://forums.tauonline.org/index.php?action=stats

Update: This has been updated on 4/12/04. The changes are:
  • If you have karma disabled on your forum, this mod won't take effect, if you have it on 'Enable Karma Total' this mod will show the highest and lowest total karma, and if you have it on 'Enable Karma Positive/Negative' this mod will show the members with the highest positive karma, and the members with the lowest negative karma. Thanks to NiXnAx for helping make the showing of karma totals display correctly.
  • The members' names are now linked to their profile. Thanks to Burpee for helping out with this



Here's what to change:

In /Sources/Stats.php
Find:

// Activity by month.

Add above it:
// Top 5 Karma Rating
$karma_results = db_query("
SELECT realName, karmaGood, ID_MEMBER FROM {$db_prefix}members ORDER BY karmaGood DESC LIMIT 5", __FILE__, __LINE__);

$context['karma'] = array();

while( $row_karma = mysql_fetch_assoc($karma_results) )
{
$context['gkarma'][] = array(
'Karma' => $row_karma['karmaGood'],
'Link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_karma['ID_MEMBER'] . '">' . $row_karma['realName'] . '</a>'
);
}

// Bottom 5 Karma Rating
$karma_results = db_query("
SELECT realName, karmaBad, ID_MEMBER FROM {$db_prefix}members ORDER BY karmaBad DESC LIMIT 5", __FILE__, __LINE__);

$context['bkarma'] = array();

while( $row_karma = mysql_fetch_assoc($karma_results) )
{
$context['bkarma'][] = array(
'Link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_karma['ID_MEMBER'] . '">' . $row_karma['realName'] . '</a>',
'Karma' => $row_karma['karmaBad']
);
}

// Highest Total Karma
$karma_results = db_query("
SELECT karmaGood, karmaBad, realName, ID_MEMBER FROM {$db_prefix}members ORDER BY FLOOR(karmaGood-karmaBad) DESC LIMIT 5", __FILE__, __LINE__);

$context['htkarma'] = array();

while( $row_karma = mysql_fetch_assoc($karma_results) )
{
$context['htkarma'][] = array(
'Link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_karma['ID_MEMBER'] . '">' . $row_karma['realName'] . '</a>',
'Good' => $row_karma['karmaGood'],
'Bad' => $row_karma['karmaBad']
);
}

// Lowest karma total
$karma_results = db_query("
SELECT karmaGood, karmaBad, realName, ID_MEMBER FROM {$db_prefix}members ORDER BY FLOOR(karmaGood-karmaBad) ASC LIMIT 5", __FILE__, __LINE__);

$context['ltkarma'] = array();

while( $row_karma = mysql_fetch_assoc($karma_results) )
{
$context['ltkarma'][] = array(
'Link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_karma['ID_MEMBER'] . '">' . $row_karma['realName'] . '</a>',
'Good' => $row_karma['karmaGood'],
'Bad' => $row_karma['karmaBad']
);
}


In /Themes/(ThemeFolder)/Stats.template.php
Find:

foreach ($context['top_time_online'] as $poster)
echo '
<tr>
<td width="60%" valign="top">', $poster['link'], '</td>
<td width="20%" align="left" valign="top">', $poster['time_online'] > 0 ? '<img src="' . $settings['images_url'] . '/bar.gif" width="' . $poster['time_percent'] . '" height="15" alt="" border="0" />' : '&nbsp;', '</td>
<td width="20%" align="right" valign="top" nowrap="nowrap">', $poster['time_online'], '</td>
</tr>';
echo '
</table>
</td>
</tr><tr>
<td class="catbg" colspan="4"><b>', $txt['smf_stats_5'], '</b></td>
</tr><tr>
<td class="windowbg" width="20" valign="middle" align="center"><img src="', $settings['images_url'], '/stats_history.gif" border="0" width="20" height="20" alt="" /></td>
<td class="windowbg2" colspan="4">';


Replace with:
foreach ($context['top_time_online'] as $poster)
            echo '
                                                <tr>
                                                        <td width="60%" valign="top">', $poster['link'], '</td>
                                                        <td width="20%" align="left" valign="top">', $poster['time_online'] > 0 ? '<img src="' . $settings['images_url'] . '/bar.gif" width="' . $poster['time_percent'] . '" height="15" alt="" border="0" />' : '&nbsp;', '</td>
                                                        <td width="20%" align="right" valign="top" nowrap="nowrap">', $poster['time_online'], '</td>
                                                </tr>';
        echo ' </table></td></tr>';

if( $modSettings['karmaMode'] == 1 )
{
echo '<tr>
<td class="catbg" colspan="2">Highest Karma Total</td>
<td class="catbg" colspan="2">Lowest Karma Total</td>
</tr>
<tr>
<td class="windowbg" colspan="1">
<img src="'. $settings['images_url']. '/stats_posters.gif" width="20" height="20" />
</td>
<td class="windowbg2">';

foreach($context['htkarma'] as $htkarma)
{
$total = $htkarma['Good'] - $htkarma['Bad'];
echo '<table border="0" cellpadding="1" cellspacing="0" width="100%"><tr>
<td class="windowbg2" width="80%" align="left">'.
$htkarma['Link']
.'</td>
<td class="windowbg2" width="20%" align="right">'.$total.'</td>
</tr></table>';
}

echo'</td><td class="windowbg" colspan="1">
<img src="'. $settings['images_url']. '/stats_posters.gif" width="20" height="20" />
</td>
<td class="windowbg2">';


foreach($context['ltkarma'] as $ltkarma)
{
$total = $ltkarma['Good'] - $ltkarma['Bad'];
echo '<table border="0" cellpadding="1" cellspacing="0" width="100%"><tr>
<td class="windowbg2" width="80%" align="left">'.
$ltkarma['Link']
.'</td>
<td class="windowbg2" width="20%" align="right">'.$total.'</td>
</tr></table>';
}
}

if( $modSettings['karmaMode'] == 2)
{
echo '<tr>
<td class="catbg" colspan="2">Highest Karma Rating</td>
<td class="catbg" colspan="2">Lowest Karma Rating</td>
</tr>
<tr>
<td class="windowbg" colspan="1">
<img src="'. $settings['images_url']. '/stats_posters.gif" width="20" height="20" />
</td>
<td class="windowbg2">';

foreach($context['gkarma'] as $gkarma)
{
echo '<table border="0" cellpadding="1" cellspacing="0" width="100%"><tr>
<td class="windowbg2" width="80%" align="left">'.
$gkarma['Link']
.'</td>
<td class="windowbg2" width="20%" align="right">+'.$gkarma['Karma'].'</td>
</tr></table>';
}

echo'</td><td class="windowbg" colspan="1">
<img src="'. $settings['images_url']. '/stats_posters.gif" width="20" height="20" />
</td>
<td class="windowbg2">';

foreach($context['bkarma'] as $bkarma)
{
echo '<table border="0" cellpadding="1" cellspacing="0" width="100%">
<tr valign="top"><td class="windowbg2" width="80%" align="left">'.
$bkarma['Link']
.'</td>
<td class="windowbg2" width="20%" align="right">-'.$bkarma['Karma'].'</td>
</tr></table>';
}
}

echo'</tr></td><tr><td class="catbg" colspan="4"><b>', $txt['smf_stats_5'], '</b></td>
                        </tr>
<tr>
                                <td class="windowbg" width="20" valign="middle" align="center"><img src="', $settings['images_url'], '/stats_history.gif" border="0" width="20" height="20" alt="" /></td>
                                <td class="windowbg2" colspan="4">';




If you want it to show more (Or less) stats then:
In /Sources/Stats.php
Find the following:

SELECT realName, karmaGood, ID_MEMBER FROM {$db_prefix}members ORDER BY karmaGood DESC LIMIT 5

SELECT realName, karmaBad, ID_MEMBER FROM {$db_prefix}members ORDER BY karmaBad DESC LIMIT 5

SELECT karmaGood, karmaBad, realName, ID_MEMBER FROM {$db_prefix}members ORDER BY FLOOR(karmaGood-karmaBad) DESC LIMIT 5

SELECT karmaGood, karmaBad, realName, ID_MEMBER FROM {$db_prefix}members ORDER BY FLOOR(karmaGood-karmaBad) ASC LIMIT 5 5

And replace the number bolded above to however many results you want shown.



That's it! Please say if you have any questions/queries about this,
TauOnline.Org

A.M.A

Really sorry .. real life is demanding my full attention .. will be back soon hopefully :)

Tristan Perry

Quote from: A.M.A on October 24, 2004, 11:16:48 AM
nice tip .. thanks for sharing it.
Thanks and your welcome  :) Also whatever happened to the links to all these tips? They were in the sticky, but now they aren't.. Was it too much work?

A.M.A

nope .. just thinking it is not hard to find a tip in here.
Really sorry .. real life is demanding my full attention .. will be back soon hopefully :)

Tristan Perry

Quote from: A.M.A on October 24, 2004, 11:56:55 AM
nope .. just thinking it is not hard to find a tip in here.
Ah right.  :) Will it come back if this board gets bigger?

GiNi3D



Burpee

Hmm... would it be possible to show the lowest and highest totals, instead of the votes casted upon each user?

PS: Nice trick, Tau :D

Tristan Perry

#8
Quote from: Burpee on November 16, 2004, 08:55:36 AM
Hmm... would it be possible to show the lowest and highest totals, instead of the votes casted upon each user?

PS: Nice trick, Tau :D
Yeah it's possible, it's just that at the time of making the code, I couldn't be bothered to show the totals! I'll edit my first post soon to show the rating, or the totals depending on what setting you choose  :)



EDIT: It appears it's not as easy as I thought it'd be.. I've got the main bit of this script working, I just can't get an ordered list to show up... 

E.g: (Taken from my forum when testing this script)
Brother Edwin      -3
Orion                   -1
Fire Eater         -1
Feugan AM      -1
misfits Tau punk -2

... I'm not good enough with arrays right now to complete this... I can give you the code that I have done at the moment, although it doesn't show an ordered list and so is kind of pointless.. I'll keep working on this, although I doubt I can do this at the moment... Sorry  :-[ If anyone wants to help out, It'd be appreciated

Burpee

Hmm... perhaps you should share the code you have right now so people can try and help you...
I probably can't help you, but ofcourse I'm willing to give it a try :P

Tristan Perry

#10
Quote from: Burpee on November 16, 2004, 04:36:20 PM
Hmm... perhaps you should share the code you have right now so people can try and help you...
I probably can't help you, but ofcourse I'm willing to give it a try :P
I've updated my first post to do this  :)

BigMike

Wow, I tried doing this on my forum a while back and just gave up. Having a max and min is nice, but I was just trying to show the max listings. All you would have to do is sum their max and mins and then sort the highest n number of returns, and store those into a new array. Then run a counter from 0 to the number of names -1 and you would have it.

I dont have time right now, but when I do I'll see if I can figure it out. But Im only going to figure out how to show the Higest totals, not the lowest..

Here's your code running on my forum: http://board.marlincrawler.com/index.php?action=stats
Thanks for the code and I'll see what I can do...

Tristan Perry

Quote from: BigMike on November 17, 2004, 05:31:07 PM
Wow, I tried doing this on my forum a while back and just gave up. Having a max and min is nice, but I was just trying to show the max listings. All you would have to do is sum their max and mins and then sort the highest n number of returns, and store those into a new array. Then run a counter from 0 to the number of names -1 and you would have it.

I dont have time right now, but when I do I'll see if I can figure it out. But Im only going to figure out how to show the Higest totals, not the lowest..

Here's your code running on my forum: http://board.marlincrawler.com/index.php?action=stats
Thanks for the code and I'll see what I can do...
Cool, thanks  :) Also I'm glad you liked the code  :)

ozmafans

#13
Hey this is great and all, but I have a question:

http://www.ozmafans.com/forum [nofollow]

People seem to show up multiple times.

For example: "stefanie" has the highest at +123, but also the lowest at -118 ? How is this possible?

I just read some of the comments, I understand now. If someone could make this a real feature, that includes BOTH totals and the original stats (as both are interesting) I would love it.

NiXnAx

To get the lowest total karma correct, you have to use this query: SELECT karmaGood, karmaBad, realName FROM smf_members ORDER BY (karmaGood-karmaBad) ASC LIMIT 5", __FILE__, __LINE__);

ozmafans

Quote from: NiXnAx on December 02, 2004, 11:53:29 AM
To get the lowest total karma correct, you have to use this query: SELECT karmaGood, karmaBad, realName FROM smf_members ORDER BY (karmaGood-karmaBad) ASC LIMIT 5", __FILE__, __LINE__);

that worked perfectly (karmaBad-karmaGood) as well, for the Top 5 posters.

ozmafans

sorry for double-replying, but I am noticing now that after increasing the count to 10, the stats are no longer in order.

To see for yourself, www.ozmafans.com/forum [nofollow]

NiXnAx

Quote from: ozmafans on December 02, 2004, 01:35:04 PM
Quote from: NiXnAx on December 02, 2004, 11:53:29 AM
To get the lowest total karma correct, you have to use this query: SELECT karmaGood, karmaBad, realName FROM smf_members ORDER BY (karmaGood-karmaBad) ASC LIMIT 5", __FILE__, __LINE__);

that worked perfectly (karmaBad-karmaGood) as well, for the Top 5 posters.

You only need to change ASC to DESC to get the top 5 karma total.

ozmafans

Quote from: NiXnAx on December 02, 2004, 02:45:14 PM
Quote from: ozmafans on December 02, 2004, 01:35:04 PM
Quote from: NiXnAx on December 02, 2004, 11:53:29 AM
To get the lowest total karma correct, you have to use this query: SELECT karmaGood, karmaBad, realName FROM smf_members ORDER BY (karmaGood-karmaBad) ASC LIMIT 5", __FILE__, __LINE__);

that worked perfectly (karmaBad-karmaGood) as well, for the Top 5 posters.

You only need to change ASC to DESC to get the top 5 karma total.
Thank you for that tip, I know very little about php/sql.

The problem seems to be that say I had -100 karma points given to me, and 100 positive karma points given to me.

karmaBad (100) - karmaGood (100) = -200, when it should really be 0. Is there an absolute value function?

NiXnAx

when you remove good karma from bad karma, you usually get negative karma. (that is supposed when you are nice on the forum), and int (the value of karma, stored in the database cannot be negative) ant that is the reason there is two karma colums. So when you substract 100 (goodkarma) from 100 (badkarma) you get, well you guessed it, 0.

Advertisement: