From 74d3c351ce30e351f171c215ce97817550c9f5ea Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Sat, 3 Mar 2001 11:44:51 +0000
Subject: [PATCH] - Took all "rating logic" out of the account.module and moved
 it into   a seperate module called "rating.module".  This should allow people
   to experiment with different rating heuristics/algorithms.

- The rating module also generates a "Top 100 users" page, see:
     http://drop.org/module.php?mod=rating

- Adjusted ./scripts/php-clean to ignore png files.
  (I should do it the way around and make it ignore everything but
  our php files.)
---
 modules/account.module | 65 +----------------------------------
 modules/rating.module  | 78 ++++++++++++++++++++++++++++++++++++++++++
 scripts/php-clean      |  4 +--
 3 files changed, 81 insertions(+), 66 deletions(-)
 create mode 100644 modules/rating.module

diff --git a/modules/account.module b/modules/account.module
index 098934992cd2..9a67a897f043 100644
--- a/modules/account.module
+++ b/modules/account.module
@@ -1,60 +1,14 @@
 <?
 
-$module = array("cron" => "account_cron",
-                "help" => "account_help",
+$module = array("help" => "account_help",
                 "find" => "account_find",
-                "block" => "account_block",
                 "admin" => "account_admin");
 
-
-function account_cron_ratings() {
-  $period = 5184000;  // 60 days
-  $number = 30;       // 30 comments
-  $offset = 5;        // 5 comments
-
-  $r1 = db_query("SELECT id, userid FROM users");
-  while ($account = db_fetch_object($r1)) {
-    unset($bonus); unset($votes); unset($score); unset($value); unset($weight);
-
-    $r2 = db_query("SELECT COUNT(id) AS number FROM stories WHERE author = $account->id AND (". time() ." - timestamp < $period) AND status = 2");
-    if ($story = db_fetch_object($r2)) {
-      $bonus = $story->number;
-    }
-
-    $r3 = db_query("SELECT score, votes FROM comments WHERE author = $account->id AND (". time() ." - timestamp < $period) ORDER BY timestamp LIMIT $number");
-    while ($comment = db_fetch_object($r3)) {
-      $weight++;
-      $score += $weight * $comment->score;
-      $votes += $weight * $comment->votes;
-    }
-
-    if ($weight >= $offset && $votes > 0) {
-      $value = ($score + $weight) / $votes + $bonus;
-      db_query("UPDATE users SET rating = '$value' WHERE id = $account->id");
-    }
-  }
-}
-
-function account_cron() {
-  // update user ratings:
-  account_cron_ratings();
-}
-
 function account_help() {
  ?>
   <P>The account-module is responsible for maintaining the user database. It automatically handles tasks like registration, authentication, access rights, password retrieval, user settings and much more.</P>
   <P>The required administration can be accomplished through the "account" interface of the administration section.  From here administrators can get a quick overview of all registered users and view/edit specific accounts using the links provided.  Some useful operations include blocking specific accounts (e.g. a troublesome user) and giving/taking administration permissions.  Note that you should only give these permissions to people you trust!</P>
   <P>Check the documentation page for detailed information about user management.</P>
-  <H3>User rating</H3>
-  <P>The account 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 stories 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 stories in the last 60 days gain extra bonus points which will boost up their overall rating.</P>
  <?
 }
 
@@ -208,23 +162,6 @@ function account_view($name) {
   }
 }
 
-function account_block() {
-  $result = db_query("SELECT userid, rating FROM users ORDER BY rating DESC LIMIT 10");
-
-  $content .= "<TABLE CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
-  $content .= "<TR><TH>Username</TH><TH>Rating</TH></TR>\n";
-  while ($account = db_fetch_object($result)) {
-    $content .= "<TR><TD>". format_username($account->userid) ."</TD><TD>". check_output($account->rating) ."</TD></TR>";
-  }
-  $content .= "</TABLE>\n";
-
-  $block[0]["subject"] = "Top 10:<BR>users";
-  $block[0]["content"] = $content;
-  $block[0]["info"] = "Top 10: users";
-
-  return $block;
-}
-
 function account_admin() {
   global $op, $edit, $order, $name;
 
diff --git a/modules/rating.module b/modules/rating.module
new file mode 100644
index 000000000000..26c06fab136e
--- /dev/null
+++ b/modules/rating.module
@@ -0,0 +1,78 @@
+<?
+
+$module = array("cron" => "rating_cron",
+                "help" => "rating_help",
+                "page" => "rating_page",
+                "block" => "rating_block");
+
+function rating_cron() {
+  $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(id) AS number FROM stories WHERE author = $rating->id AND (". time() ." - timestamp < $period) AND status = 2");
+    if ($story = db_fetch_object($r2)) {
+      $bonus = $story->number;
+    }
+
+    $r3 = db_query("SELECT score, votes FROM comments WHERE author = $rating->id AND (". time() ." - timestamp < $period) ORDER BY timestamp LIMIT $number");
+    while ($comment = db_fetch_object($r3)) {
+      $weight++;
+      $score += $weight * $comment->score;
+      $votes += $weight * $comment->votes;
+    }
+
+    if ($weight >= $offset && $votes > 0) {
+      $value = ($score + $weight) / $votes + $bonus;
+      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 stories 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 stories in the last 60 days gain extra bonus points which will boost up their overall rating.</P>
+ <?
+}
+
+function rating_list($limit) {
+  $result = db_query("SELECT userid, rating FROM users ORDER BY rating DESC LIMIT $limit");
+
+  $output .= "<TABLE CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
+  $output .= "<TR><TH>Username</TH><TH>Rating</TH></TR>\n";
+  while ($account = db_fetch_object($result)) {
+    $output .= "<TR><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;
+}
+
+?>
\ No newline at end of file
diff --git a/scripts/php-clean b/scripts/php-clean
index fe5c589d3bbb..5c14c37215a3 100644
--- a/scripts/php-clean
+++ b/scripts/php-clean
@@ -1,5 +1,5 @@
 #!/bin/sh
 find . -name "*~" -type f | xargs rm -f
 find . -name "DEADJOE" -type f | xargs rm -f
-find . -name "*" -type f | grep -v ".gif" | grep -v ".jpg" | grep -v ".tgz" | xargs perl -wi -pe 's/\s+$/\n/'
-find . -name "*" -type f | grep -v ".gif" | grep -v ".jpg" | grep -v ".tgz" | xargs perl -wi -pe 's/\t/  /g'
+find . -name "*" -type f | grep -v ".gif" | grep -v ".jpg" | grep -v ".png" | grep -v ".tgz" | xargs perl -wi -pe 's/\s+$/\n/'
+find . -name "*" -type f | grep -v ".gif" | grep -v ".jpg" | grep -v ".png" | grep -v ".tgz" | xargs perl -wi -pe 's/\t/  /g'
-- 
GitLab