summaryrefslogtreecommitdiff
path: root/modules/rating.module
blob: dd00aafb90d075bc16aa7439086e34721ce262b6 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php

function rating_perm() {
  return array("access user ratings");
}

function rating_link($type) {
  if ($type == "page" && user_access("access user ratings")) {
    $links[] = "<a href=\"module.php?mod=rating\">". t("user ratings") ."</a>";
  }

  return $links ? $links : array();
}

function rating_conf_options() {
  $period = array(3600 => format_interval(3600), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600), 1000000000 => "Never");
  $output .= form_select("Update interval", "rating_cron_time" , variable_get("rating_cron_time", 86400), $period, "The update interval for the user ratings.  Requires crontab.");

  $weight = array("Disabled", 1, 2, 3, 4, 5, 6, 7, 9, 10);
  foreach (module_list() as $name) {
    if (module_hook($name, "user")) {
      $output .= form_select("Weight of a $name", "rating_weight_$name", variable_get("rating_weight_$name", 0), $weight, "The weight of a $name.");
    }
  }
  return $output;
}

function rating_cron() {
  if (time() - variable_get("rating_cron_last", 0) > variable_get("rating_cron_time", time())) {
    variable_set("rating_cron_last", time());

    $r1 = db_query("SELECT uid FROM users ORDER BY rating DESC");
    while ($account = db_fetch_object($r1)) {
      db_query("UPDATE users SET rating = '". rating_gravity($account->uid) ."' WHERE uid = '$account->uid'");
      $rating[$account->uid] = ++$i;
    }

    db_query("DELETE FROM rating");

    $r2 = db_query("SELECT uid FROM users ORDER BY rating DESC");
    while ($account = db_fetch_object($r2)) {
      db_query("INSERT INTO rating (uid, new, old) VALUES ('$account->uid', '". ++$j ."', '". $rating[$account->uid] ."')");
    }
  }
}

function rating_help() {
 ?>
  <P>The rating cron will periodically calculate each user's gravity, the overall time-weighted rating of each user's contributions.</P>
 <?
}

function rating_gravity($id) {
  global $status;

  $period = 5184000;    // maximum 60 days
  $number = 30;         // maximum 30 comments

  $r1 = db_query("SELECT nid, type FROM node WHERE author = '$id' AND (". time() ." - timestamp < $period) AND status = '$status[posted]'");
  while ($node = db_fetch_object($r1)) {
    $bonus += variable_get("rating_weight_$node->type", 0);
  }

  $r2 = db_query("SELECT nid, type FROM node WHERE author = '$id' AND (". time() ." - timestamp < $period) AND status = '$status[dumped]'");
  while ($node = db_fetch_object($r1)) {
    $bonus -= variable_get("rating_weight_$node->type", 0);
  }

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

  $bonus += $weight / 5;
*/

  return ($votes ? ($score + $weight) / $votes + $bonus : $bonus);
}

function rating_list($limit) {
  $result = db_query("SELECT u.rating, u.name, u.uid, r.* FROM users u LEFT JOIN rating r ON u.uid = r.uid ORDER BY u.rating DESC LIMIT $limit");

  $output .= "<TABLE CELLPADDING=\"1\" CELLSPACING=\"1\">\n";
  while ($account = db_fetch_object($result)) {
    $ranking = $account->old - $account->new;
    $output .= "<TR><TD ALIGN=\"right\">". ++$i .".</TD><TD>". format_name($account) ."</TD><TD ALIGN=\"right\">". check_output($account->rating) ."</TD><TD>(". ($ranking < 0 ? "" : "+") ."$ranking)</TD></TR>";
  }
  $output .= "</TABLE>\n";
  return $output;
}

function rating_page() {
  global $user, $theme;

  $theme->header();

  if (user_access("access user ratings")) {
    $theme->box(t("Top 100 users"), rating_list(100));
  }
  else {
    $theme->box(t("Access denied"), message_access());
  }

  $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;
}

?>