summaryrefslogtreecommitdiff
path: root/modules/moderation.module
blob: 4df29e080dd358f04be0add9224d3f5ba5fea325 (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
118
<?php

$module = array("menu" => "moderation_menu",
                "page" => "moderation_page");

include_once "includes/common.inc";
include_once "includes/node.inc";

function moderation_menu() {
  return array("<A HREF=\"module.php?mod=moderation\">". t("moderation queue") ."</A> (<FONT COLOR=\"red\">". moderation_count() ."</FONT>)");
}

function moderation_count() {
  global $status;
  $result = db_query("SELECT COUNT(nid) FROM node WHERE status = '$status[queued]'");
  return ($result) ? db_result($result, 0) : 0;
}

function moderation_score($id) {
  $result = db_query("SELECT score FROM node WHERE nid = '$id'");
  return ($result) ? db_result($result, 0) : 0;
}

function moderation_vote($id, $vote) {
  global $status, $user;

  if (!user_get($user, "history", "n$id")) {
    // Update submission's score- and votes-field:
    db_query("UPDATE node SET score = score $vote, votes = votes + 1 WHERE nid = $id");

    // Update user's history record:
    $user = user_set($user, "history", "n$id", $vote);

    if ($node = node_get_object(nid, $id)) {
      if (node_post_threshold($node) <= $node->score) {
        node_save(array(nid => $id, pid => $node->pid, type => $node->type, status => $status[posted]));
        watchdog("message", "posted node '$node->title'");
      }
      else if (node_dump_threshold($node) >= $node->score) {
        node_save(array(nid => $id, pid => $node->pid, type => $node->type, status => $status[dumped]));
        watchdog("message", "dumped node '$node->title'");
      }
      else if (node_timout_threshold($node) <= $node->votes) {
        node_save(array(nid => $id, pid => $node->pid, type => $node->type, status => $status[expired]));
        watchdog("message", "expired node '$node->title'");
      }
    }
  }
}

function moderation_overview() {
  global $status, $theme, $user;

  $result = db_query("SELECT n.*, u.userid FROM node n LEFT JOIN users u ON n.author = u.id WHERE n.status = '$status[queued]'");

  $content .= "<TABLE BORDER=\"0\" CELLSPACING=\"4\" CELLPADDING=\"4\">\n";
  $content .= " <TR BGCOLOR=\"$bgcolor1\"><TH>". t("Subject") ."</TH><TH>". t("Author") ."</TH><TH>". t("Type") ."</TH><TH>". t("Score") ."</TH></TR>\n";
  while ($node = db_fetch_object($result)) {
    if ($user->id == $node->author || user_get($user, "history", "n$node->nid")) $content .= " <TR><TD><A HREF=\"module.php?mod=moderation&op=view&id=$node->nid\">". check_output($node->title) ."</A></TD><TD ALIGN=\"center\">". format_username($node->userid) ."</TD><TD ALIGN=\"center\">". check_output($node->type) ."</TD><TD ALIGN=\"center\">". moderation_score($node->nid) ."</TD></TR>\n";
    else $content .= " <TR><TD><A HREF=\"module.php?mod=moderation&op=view&id=$node->nid\">". check_output($node->title) ."</A></TD><TD ALIGN=\"center\">". format_username($node->userid) ."</TD><TD ALIGN=\"center\">". check_output($node->type) ."</TD><TD ALIGN=\"center\"><A HREF=\"module.php?mod=moderation&op=view&id=$node->nid\">". t("vote") ."</A></TD></TR>\n";
  }
  $content .= "</TABLE>\n";

  $theme->header();
  $theme->box(t("Moderation queue"), $content);
  $theme->footer();
}

function moderation_node($id) {
  global $theme, $user, $moderation_votes;

  $node = node_get_object(nid, $id);

  if ($user->id == $node->author || user_get($user, "history", "n$node->nid")) {
    header("Location: node.php?id=$node->nid");
  }
  else {
    if ($node->pid && $n = node_get_object("nid", $node->pid)) {
      if ($node->pid) $output .= " ". t("The above node is a suggested update for an existing node:") ." \"<A HREF=\"node.php?id=$n->nid\">". check_output($n->title) ."</A>\".";
      if ($node->log) $output .= " ". t("The log message to accompany this update is given below:") ."<P><I>". check_output($node->log, 1) ."</I></P>";
    }

    // moderation form:
    $output .= "<FORM ACTION=\"module.php?mod=moderation\" METHOD=\"post\">\n";
    foreach ($moderation_votes as $key=>$value) $options .= "  <OPTION VALUE=\"$value\">$key</OPTION>\n";
    $output .= "<SELECT NAME=\"vote\">$options</SELECT>\n";
    $output .= "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$node->nid\">\n";
    $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Vote\">\n";
    $output .= "</FORM>\n";

    $theme->header();
    node_view($node, 0);
    $theme->box(t("Moderate"), $output);
    $theme->footer();
  }
}

function moderation_page() {
  global $id, $op, $user, $vote;

  if ($user->id) {
    $user = user_load($user->userid);

    switch($op) {
      case "Vote";
        moderation_vote(check_input($id), check_input($vote));
        // fall through:
      case "view":
        moderation_node(check_input($id));
        break;
      default:
        moderation_overview();
        break;
    }
  }
}

?>