News:

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

Main Menu

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


Advertisement: