Public Karma log

Started by finnhack, March 22, 2004, 10:42:53 AM

Previous topic - Next topic

finnhack

Hi!

On my YaBBSE I had some kind of a public Karma log, in other words it simply showed the members who had given what kind of karma (+/-) to whom. Like:

Today 16:25 UserX kicked UserY in the butt   (combined with a good or bad smiley)

For som reason (don't know if they are sadistic or what) but my boardmembers well give me no peace until I bring them this feature again.

The code i used on YSE was about this:


//get the data from the Karmalog
$karma_result = mysql_query("SELECT * FROM {$db_prefix}log_karma ORDER BY logTime DESC");
while($row_karmas = mysql_fetch_array($karma_result))
{

// get the users real names
$target_result = mysql_query("SELECT realName FROM {$db_prefix}members WHERE ID_MEMBER=$row_karmas[ID_TARGET] LIMIT 1");
$row_target = mysql_fetch_array($target_result);

$executor_result = mysql_query("SELECT realName FROM {$db_prefix}members WHERE ID_MEMBER=$row_karmas[ID_EXECUTOR] LIMIT 1");
$row_executor = mysql_fetch_array($executor_result);

$actiontime = timeformat( $row_karmas[logTime] );
  if ( $row_karmas[action] == "+" ) {
      $nicepic = mt_rand (1, 16);
$feeling = "good" . $nicepic . ".gif";
$color = "#ff6600";
$actext = "";
  }
  else {
  $notnicepic = mt_rand (1, 31);
  $feelis = "bad" . $notnicepic . ".gif";
  $color = "#0066ff";
  $actext = "smites";
  }
and so on...


I already tried to convert it, but it wasn't that easy. It would be very nice if I could get some help with at least these questions:

1) in what way has the method to call the database changed from YSE to SMF? I guess mysql_query won't work anymore?

2) Can I place theese kind of code directly into a template, or does it have to be placed in a file in the source directory? I do not yet fully understand how I can get something I have added to a source file to echo on a certain template. In YSE I added this Karma feature so it showed up on the 'Who is doing what' page (mod for YSE).

I'm really sorry if these kind of questions have been answered a million times before.




pulpitfire

1) i think all the queries are in Karma.php, in your source files.

2) you could place the code into the template, but it would be more consistent to place it into your source-code.

to get something from your source code to echo on the template, you simply define a variable in the source-code, then call it in the template.

e.g. source.php 
$karma = x + y;


source.template.php
echo $karma;


finnhack

OK! Thanks for giving this feature some time. I'll go digging into the source then, and see what will happen. Probably you will hear more from me  :)

[Unknown]

This code be optimized like this:

// Get the data from the Karma log.
$karma_result = db_query("
   SELECT lk.ID_TARGET, lk.ID_EXECUTOR, lk.logTime, lk.action, memt.realName AS targetName, meme.realName AS executorName
   FROM {$db_prefix}log_karma AS lk, {$db_prefix}members AS memt, {$db_prefix}members AS meme
   WHERE memt.ID_MEMBER = lk.ID_TARGET
      AND meme.ID_MEMBER = lk.ID_EXECUTOR
   ORDER BY logTime DESC", __FILE__, __LINE__);
$context['karma_actions'] = array();
while ($row_karmas = mysql_fetch_array($karma_result))
{
   $actiontime = timeformat($row_karmas['logTime']);

   if ($row_karmas[action] == "+") {
      $nicepic = mt_rand (1, 16);
      $feeling = "good" . $nicepic . ".gif";
      $color = "#ff6600";
      $actext = "";
  }
  else {
     $notnicepic = mt_rand (1, 31);
     $feelis = "bad" . $notnicepic . ".gif";
     $color = "#0066ff";
     $actext = "smites";
  }

   $context['karma_actions'][] = array(
       ...
   );
}


That's how I'd do it, anyhow.

-[Unknown]

finnhack


[Unknown]

Oops, almost forgot... this won't work:

if ($row_karmas[action] == "+") {

It should be, instead:

if ($row_karmas[action] == 1) {

If I remember right, which I probably do ^_^.

-[Unknown]

finnhack

Hi!

Really BIG THANKS for the help! I got it working now. I attach the code if anyone want to try it (this is a simplified version without any smileys or so). Believe it or not but this kind of public karma log might spice up the discussions on your board  ;)


// Get the data from the Karma log.
$karma_result = db_query("
   SELECT lk.ID_TARGET, lk.ID_EXECUTOR, lk.logTime, lk.action, memt.realName AS targetName,
meme.realName AS executorName
   FROM {$db_prefix}log_karma AS lk, {$db_prefix}members AS memt, {$db_prefix}members AS meme
   WHERE memt.ID_MEMBER = lk.ID_TARGET
      AND meme.ID_MEMBER = lk.ID_EXECUTOR
   ORDER BY logTime DESC", __FILE__, __LINE__);

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

while ($row_karmas = mysql_fetch_array($karma_result))
{
   $actiontime = timeformat($row_karmas['logTime']);

   //good or bad karma
   if ($row_karmas['action'] == 1) {
      $actext = 'applauds';
  }
  else {
     $actext = 'smites';
  }
  //send it to the template
   $context['karma_actions'][] = array(
        'executor' => $row_karmas['executorName'],
        'target' => $row_karmas['targetName'],
        'action' => $actext,
        'time' => $actiontime
   );
}

mysql_free_result($karma_result);


joseph_1970

Hi guys.  Sorry to pick up on the old post, but I was hoping you could clarify this code and where to place it in the simplemachines code.  We have a someone messing with the karma and would like to show everyone who it is. 

Thanks!


[Unknown]

This code can be put on a separate page that includes SSI.php... how much programming knowledge do you have?

-[Unknown]

joseph_1970

I don't really know how to answer that, but I seem to be able to get around alright.  Understand what you meant with creating another page, and including the SSI.php (that's easy).  I have created an e-com site, but it was pretty straight forward, imo.  I don't understand the entire structure of calls with this forum, although I haven't spent much time studying it.

If you explain it generally, I might/shold be able to follow.  I will figure it out if your explanation is clear, due diligence and all.  :)  I just don't know how it actually works with integration.  I didn't know if it was going to be a separate page that will be clicked by users to see only things about karma or how it integrates.  Sorry for my ignorance. 

If I now the what to expect in terms of how this actually outputs, it will help me reverse things and follow it more I think. 

I don't want to be a pain your backside either! 


[Unknown]

Well, this just fills a contectual variable.

You can put it in, after including SSI.php, and then do something like this:

print_r($context['karma_actions']);

At the end.  This will actually output something.

-[Unknown]

joseph_1970

Oh, I will give that a shot tomorrow. 

Thanks. 

Aquilo

hey this is cool! ;D

I'm still building my front page and this may be a good addition! thanks!

joseph_1970 you could just past this in SSI.php at the end! ;D

function ssi_karmaActions($max_listing = 5, $output_method = 'echo')
{
global $db_prefix;

// Get the data from the Karma log.
$karma_result = db_query("
SELECT lk.ID_TARGET, lk.ID_EXECUTOR, lk.logTime, lk.action, memt.realName AS targetName, meme.realName AS executorName
FROM {$db_prefix}log_karma AS lk, {$db_prefix}members AS memt, {$db_prefix}members AS meme
WHERE memt.ID_MEMBER = lk.ID_TARGET
AND meme.ID_MEMBER = lk.ID_EXECUTOR
ORDER BY logTime DESC
LIMIT $max_listing", __FILE__, __LINE__);

$return = array();
// Build an array to return karama action data.
while ($row_karmas = mysql_fetch_assoc($karma_result))
$return[] = array(
'executor' => $row_karmas['executorName'],
'target' => $row_karmas['targetName'],
'action' => ($row_karmas['action'] == 1) ? 'applauds' : 'smites',
'time' => timeformat($row_karmas['logTime'])
);

mysql_free_result($karma_result);

// return the array?
if ($output_method != 'echo' || empty($return))
return $return;

// print as html?
foreach ($return as $data)
echo $data['executor'], ' ', $data['action'], ' ', $data['target'], ' ', $data['time'], '<br />';

unset($return);
}

dracomiconia2

Does it work in beta 5?

NukeWorker.com

#14
Yes.

I got [Unk]'s code to work, but I didn't like the formatting.

I couldn't get the other code to work in SSI, for some reason (Must have been something I did wrong, I would like to see this added to the official SSI, and documented).

I hacked at it a little , and got it wo work.  This is what I used:

<? include ('[path to SMF]/forum/SSI.php');
global $db_prefix;
$karma_result = db_query("
SELECT lk.ID_TARGET, lk.ID_EXECUTOR, lk.logTime, lk.action, memt.realName AS targetName, meme.realName AS executorName
FROM {$db_prefix}log_karma AS lk, {$db_prefix}members AS memt, {$db_prefix}members AS meme
WHERE memt.ID_MEMBER = lk.ID_TARGET
AND meme.ID_MEMBER = lk.ID_EXECUTOR
ORDER BY logTime DESC
LIMIT 60", __FILE__, __LINE__);
$return = array();
while ($row_karmas = mysql_fetch_assoc($karma_result))
$return[] = array(
'executor' => $row_karmas['executorName'],
'target' => $row_karmas['targetName'],
'action' => ($row_karmas['action'] == 1) ? 'applauds' : 'smites',
'time' => timeformat($row_karmas['logTime'])
);
mysql_free_result($karma_result);
foreach ($return as $data)
echo $data['executor'], ' ', $data['action'], ' ', $data['target'], ' ', $data['time'], '<br />';
unset($return);
?>


I saved the text above in a file in my forum (SMF) directory named 'karma_log.php'.

Include for shtml pages:
<!-- insert karmic activity -->
<!--#include virtual="forum/karma_log.php" -->

Include for php pages (forum template):

<!-- insert karmic activity -->
<? include ("http://www.nukeworker.com/forum/karma_log.php"); ?>



You can see it here:

http://www.nukeworker.com/karmalog.shtml

Of course, you should change [path to SMF]/forum   info to your path.

finnhack

Hello!

I have added the code into the who-action, because I feel that karmalog is closely related to the list of what people are doing on the forum.

This was done by adding the code in reply #6 to the Who.php file, and something like this to the Who.template-file:


echo '
</table>
';
echo '

<table cellpadding="3" cellspacing="0" border="0" width="100%" class="tborder">
<tr class="titlebg">
<td colspan="2" width="26%">Empty</td>
<td width="20%">', $txt['who_time'], '</td>
<td>', $txt['who_action'], '</td>
</tr>';

foreach ($context['karma_actions'] as $karma_action)
        {
echo '<tr class="windowbg', $alternate ? '2' : '', '"><td> </td><td></td><td>
', $karma_action['time'], '</td><td><font color="', $karma_action['coloor'],
'">', $karma_action['executor'], ' ',
$karma_action['action'], ' ', $karma_action['target'], '</font></td></tr>';
$alternate = !$alternate;
}
echo '</table>';


The first columne in the table is not in use yet, so it should be removed...

[Unknown]


echo '
</table>';

echo '

<table cellpadding="3" cellspacing="0" border="0" width="100%" class="tborder">
<tr class="titlebg">
<td width="20%">', $txt['who_time'], '</td>
<td>', $txt['who_action'], '</td>
</tr>';

foreach ($context['karma_actions'] as $karma_action)
        {
echo '
<tr class="windowbg', $alternate ? '2' : '', '">
<td>', $karma_action['time'], '</td>
<td style="color: ', $karma_action['color'], ';">', $karma_action['executor'], ' ', $karma_action['action'], ' ', $karma_action['target'], '</td>
</tr>';

$alternate = !$alternate;
}

echo '
</table>';


Looks a bit cleaner this way, imho.

-[Unknown]

mi_amigo

Gents, I'm a coding Dumbo. I wonder if someone could lead me by the hand and show me how to use this as a stand-alone page please?

TIA.

mi_amigo

Hold the answers, I found a clue in the SSI tutorial. Thanks to Lamper.

Elijah Bliss


[Unknown]


<?php

include ('[path to SMF]/SSI.php');

global 
$db_prefix;

$karma_result db_query("
   SELECT lk.ID_TARGET, lk.ID_EXECUTOR, lk.logTime, lk.action, memt.realName AS targetName, meme.realName AS executorName
   FROM 
{$db_prefix}log_karma AS lk, {$db_prefix}members AS memt, {$db_prefix}members AS meme
   WHERE memt.ID_MEMBER = lk.ID_TARGET
      AND meme.ID_MEMBER = lk.ID_EXECUTOR
   ORDER BY logTime DESC
   LIMIT 60"
__FILE____LINE__);
$return = array();
while (
$row_karmas mysql_fetch_assoc($karma_result))
   
$return[] = array(
      
'executor' => $row_karmas['executorName'],
      
'target' => $row_karmas['targetName'],
      
'action' => $row_karmas['action'] == 'applauds' 'smites',
      
'time' => timeformat($row_karmas['logTime'])
   );
mysql_free_result($karma_result);

foreach (
$return as $data)
   echo 
$data['executor'], ' '$data['action'], ' '$data['target'], ' '$data['time'], '<br />';

unset(
$return);

?>


Looks okay to me, what doesn't work about it?

-[Unknown]

NukeWorker.com

#21
Just an update, I added a little color, and made the member names links to their profile.


<?php

include ('/homepages/~/~/htdocs/forum/SSI.php');

global 
$db_prefix;

$karma_result db_query("
   SELECT lk.ID_TARGET, lk.ID_EXECUTOR, lk.logTime, lk.action, memt.realName AS targetName, meme.realName AS executorName, meme.ID_MEMBER as executorID, memt.ID_MEMBER as targetID
   FROM 
{$db_prefix}log_karma AS lk, {$db_prefix}members AS memt, {$db_prefix}members AS meme
   WHERE memt.ID_MEMBER = lk.ID_TARGET
      AND meme.ID_MEMBER = lk.ID_EXECUTOR
   ORDER BY logTime DESC
   LIMIT 100"
__FILE____LINE__);
$return = array();
while (
$row_karmas mysql_fetch_assoc($karma_result))
   
$return[] = array(
      
'executor' => $row_karmas['executorName'],
      
'executorID' => $row_karmas['executorID'],
      
'target' => $row_karmas['targetName'],
      
'targetID' => $row_karmas['targetID'],
      
'action' => $row_karmas['action'] == '<font color=green>applauds' '<font color=#CC0000>smites',
      
'time' => timeformat($row_karmas['logTime'])
   );
mysql_free_result($karma_result);

foreach (
$return as $data)
   echo 
'<a href='$scripturl'?action=profile;u='$data['executorID'], '><font color=#000000>'$data['executor'], '</font></a> '$data['action'], ' <a href='$scripturl'?action=profile;u='$data['targetID'], '>'$data['target'], '</a></font> '$data['time'], '<br />';

unset(
$return);

?>



Here is a new link to my version in action: http://www.nukeworker.com/forum/karmalog.shtml

I actually have two versions.  That one which is a full log, and a mini-log that I display on pages, that has just the 5 most recent, without the times.
See that here: (bottom left)  http://www.nukeworker.com/

Vedm

I tried NukeWorker.com's function (thanks, man!), and it seems like it writes only a last action of one member about another. I mean, I "smite" someone, check the log - it logged, then I "applaud" the same one member, and there's only my last "applaud" action written in log, "smite" disappeared.

What's wrong? Is it my DNA or can it be fixed somehow else? ::)
Thanks in advance.
My Forum runs under SMF 1.0 RC1

[Unknown]

If you smite and then applaud, it undos the smite.

-[Unknown]

Vedm

Quote from: [Unknown] on September 27, 2004, 05:42:15 PMIf you smite and then applaud, it undos the smite.

Err... Does this mean that if I smite someone in one topic, and then applaud him in other - resulting izn't zero, but +1??? It's auwful! :)

How can I make the karma log write ALL the changes and keep them forever (or some last 500) in table? It is strange that such a nice feature isn't fully implemented yet. ;)
My Forum runs under SMF 1.0 RC1

[Unknown]

It is fully implemented as far as how it was meant to, and is meant to, work.....

-[Unknown]

Tristan Perry

#26
Quote from: NukeWorker.com on August 16, 2004, 12:05:39 AM
Just an update, I added a little color, and made the member names links to their profile.


[Edited for space]

Thanks for writing this, I'm using it, although how do you stop SMF from overwriting the logs in log_karma?

NukeWorker.com

Quote from: Tau Online on October 05, 2004, 02:00:08 PM
Quote from: NukeWorker.com on August 16, 2004, 12:05:39 AM
Just an update, I added a little color, and made the member names links to their profile.


[Edited for space]

Thanks for writing this, I'm using it, although how do you stop SMF from overwriting the logs in log_karma?

Thats how it is meant to be.

octopus22

may be i missed something :)

i think will be useful to make karmalog available per user, and have word "karma" in post as link to it.

also, it's a good idea to make available smite and applaud karma with reasen. may be make additional links to new page with textbox for that?

why not to make this feature built-in in official release of SMF?

Advertisement: