summaryrefslogtreecommitdiff
path: root/modules/rating.module
blob: 563876bd090c451635bdc86c0e5002662e7dac0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

$module = array("cron" => "rating_cron",
                "help" => "rating_help",
                "page" => "rating_page",
                "block" => "rating_block");

function rating_cron() {
  global $status;

  $period = 5184000;  // 60 days
  $number = 30;       // 30 comments
  $offset = 5;        // 5 comments

  $r1 = db_query("SELECT id, userid FROM users");
  while ($rating = db_fetch_object($r1)) {
    unset($bonus); unset($votes); unset($score); unset($value); unset($weight);

    $r2 = db_query("SELECT COUNT(nid) AS number FROM nodes WHERE author = '$rating->id' AND (". time() ." - timestamp < $period) AND status = '$status[posted]'");
    if ($story = db_fetch_object($r2)) {
      $bonus += $story->number / 2;
    }

    $r3 = db_query("SELECT COUNT(nid) AS number FROM nodes WHERE author = '$rating->id' AND (". time() ." - timestamp < $period) AND status = '$status[dumped]'");
    if ($story = db_fetch_object($r3)) {
      $bonus -= $story->number / 2;
    }

    $r4 = db_query("SELECT score, votes FROM comments WHERE author = '$rating->id' AND (". time() ." - timestamp < $period) ORDER BY timestamp LIMIT $number", 1);
    while ($comment = db_fetch_object($r4)) {
      $weight++;
      $score += $weight * $comment->score;
      $votes += $weight * $comment->votes;
    }

    if ($weight >= $offset && $votes > 0) {
      $value = ($score + $weight) / $votes + $bonus;
      print "update $rating->id : $value<BR>";
      db_query("UPDATE users SET rating = '$value' WHERE id = '$rating->id'");
    }
  }
}

function rating_help() {
 ?>
  <H3>User rating</H3>
  <P>The rating cron will periodically calculate an overall rating of each user's contributed value that is a time-weighted average of his or her comments ratings with an additional bonus for the nodes he or she contributed.  The system can be best compared with <A HREF="http://slashcode.com/">SlashCode</A>'s karma and is - in fact - even more similar to <A HREF="http://scoop.kuro5hin.org/">Scoop</A>'s mojo implementation.</P>
  <P>I won't elaborate on all the funny math involved and it suffices to say that the actual weighting is done in such a way:</P>
  <OL>
   <LI>that comments with a lot of votes count more then comments with only one or two votes.</LI>
   <LI>that newer comments count for more then older comments.</LI>
  </OL>
  <P>The idea of (1) is that it favors comments that more people voted on, and thus whose rating is more likely to be accurate or justified.</P>
  <P>The latter (2) makes the user rating that comes out of the calulations temporary, based on users' most recent activity and responsive to their current state.  This is accomplished by taking each user's last 30 comments, or however many he or she posted in the last 60 days - whatever comes first.</P>
  <P>Additionally, users that posted one or more succesful nodes in the last 60 days gain extra bonus points which will boost up their overall rating.</P>
 <?php
}

function rating_list($limit) {
  $result = db_query("SELECT userid, rating FROM users ORDER BY rating DESC LIMIT $limit");

  $output .= "<TABLE CELLPADDING=\"1\" CELLSPACING=\"1\">\n";
  while ($account = db_fetch_object($result)) {
    $output .= "<TR><TD ALIGN=\"right\">". ++$i ."</TD><TD>". format_username($account->userid) ."</TD><TD>". check_output($account->rating) ."</TD></TR>";
  }
  $output .= "</TABLE>\n";
  return $output;
}

function rating_page() {
  global $theme;
  $theme->header();
  $theme->box("Top 100 users", rating_list(100));
  $theme->footer();
}

function rating_block() {
  $block[0][subject] = "Top 10:<BR>users";
  $block[0][content] = rating_list(10);
  $block[0][info] = "Top 10: users";
  return $block;
}

?>