summaryrefslogtreecommitdiff
path: root/modules/queue.module
blob: 8593bec83d0011f593a48a09c3d9d69e9b53f08f (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
// $Id$

function queue_system($field){
  $system["description"] = t("Enables content to be moderated by the community.");
  return $system[$field];
}

function queue_conf_options() {
  $threshold_post = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90, 100 => 100);
  $threshold_dump = array(-1 => -1, -2 => -2, -3 => -3, -4 => -4, -5 => -5, -6 => -6, -7 => -7, -8 => -8, -9 => -9, -10 => -10, -11 => -11, -12 => -12, -13 => -13, -14 => -14, -15 => -15, -20 => -20, -25 => -25, -30 => -30);
  $threshold_expire = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90, 100 => 100);

  $output .= form_select(t("Post threshold"), "queue_threshold_post", variable_get("queue_threshold_post", 4), $threshold_post, t("If new submissions are subject to moderation, select a post threshold."));
  $output .= form_select(t("Dump threshold"), "queue_threshold_dump", variable_get("queue_threshold_dump", -2), $threshold_dump, t("If new submissions are subject to moderation, select a dump threshold."));
  $output .= form_select(t("Expiration threshold"), "queue_threshold_expire", variable_get("queue_threshold_expire", 8), $threshold_expire, t("If new submissions are subject to moderation, select an expiration threshold."));
  $output .= form_select(t("Show comments"), "queue_show_comments", variable_get("queue_show_comments", 1), array(0 => "Disabled", 1 => "Enabled"), t("If enabled comments will be shown below the moderation form."));

  return $output;
}

function queue_perm() {
  return array("access submission queue");
}

function queue_link($type) {
  if ($type == "menu.view" && user_access("access submission queue")) {
    $links[] = l(t("view submissions"), "queue", array("title" => t("Moderate the content in the submission queue."))) ." (<span style=\"color: red;\">". queue_count() ."</span>)";
  }

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

function queue_count() {
  $result = db_query("SELECT COUNT(nid) FROM node WHERE moderate = 1");
  return ($result) ? db_result($result, 0) : 0;
}

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

function queue_vote($node, $vote) {
  global $user;

  if (!field_get($node->users, $user->uid)) {

    // Update submission's score- and votes-field:
    db_query("UPDATE node SET score = score $vote, votes = votes + 1, users = '". field_set($node->users, $user->uid, $vote) ."' WHERE nid = '$node->nid'");

    // Reload the updated node from the database:
    $node = node_load(array("nid" => $node->nid));

    if (variable_get("queue_threshold_post", 3) <= $node->score) {
      node_save($node, array_merge(array("nid", "moderate" => 0), module_invoke($node->type, "save", "approve", $node)));
      watchdog("special", "moderation: approved '$node->title'");
    }
    else if (variable_get("queue_threshold_dump", -2) >= $node->score) {
      if ($node->revisions) {
        node_revision_rollback($node, end(node_revision_list($node)));
        watchdog("special", "moderation: declined '$node->title' (rollback)");
      }
      else {
        node_save($node, array_merge(array("nid", "moderate" => 0), module_invoke($node->type, "save", "decline", $node)));
        watchdog("special", "moderation: declined '$node->title'");
      }
    }
    else if (variable_get("queue_threshold_expire", 6) <= $node->votes) {
      if ($node->revisions) {
        node_revision_rollback($node, end(node_revision_list($node)));
        watchdog("special", "moderation: expired '$node->title' (rollback)");
      }
      else {
        node_save($node, array_merge(array("nid", "moderate" => 0), module_invoke($node->type, "save", "decline", $node)));
        watchdog("special", "moderation: expired '$node->title'");
      }
    }
  }
}

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

  $result = db_query("SELECT n.*, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.moderate = 1");

  $output .= "<table border=\"0\" cellspacing=\"4\" cellpadding=\"4\">";
  $output .= " <tr><th>". t("Subject") ."</th><th>". t("Author") ."</th><th>". t("Type") ."</th><th>". t("Score") ."</th></tr>";
  while ($node = db_fetch_object($result)) {
    if ($user->uid == $node->uid || field_get($node->users, $user->uid)) {
      $output .= " <tr><td>". l($node->title, "queue/$node->nid") ."</td><td align=\"center\">". format_name($node) ."</td><td align=\"center\">". module_invoke($node->type, "node", "name") ."</td><td align=\"center\">". queue_score($node->nid) ."</td></tr>";
    }
    else {
      $output .= " <tr><td>". l($node->title, "queue/$node->nid") ."</td><td align=\"center\">". format_name($node) ."</td><td align=\"center\">". module_invoke($node->type, "node", "name") ."</td><td align=\"center\">". l(t("vote"), "queue/$node->nid") ."</td></tr>";
    }

    if ($node->teaser) {
      $output .= " <tr><td colspan=\"4\"><div style=\"margin-left: 40px; margin-bottom: 20px;\">". check_output($node->teaser) ."</div></td></tr>";
    }
  }
  $output .= "</table>";

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

function queue_view($nid) {
  global $op, $edit, $theme, $user;

  /*
  ** An associative array with the possible voting options:
  */

  $votes = array("+ 0" => t("neutral (+0)"), "+ 1" => t("post it (+1)"), "- 1" => t("dump it (-1)"));

  /*
  ** Load the node from the database:
  */

  $node = node_load(array("nid" => $nid, "moderate" => 1));

  if ($user->uid != $node->uid && !field_get($node->users, $user->uid)) {
    if ($op == t("Vote") && $votes[$edit["vote"]]) {
      /*
      ** If it is a valid vote, record it.
      */

      queue_vote($node, $edit["vote"]);

      $output = t("Your vote has been recorded.");
    }
    else {
      /*
      ** Display some explanation or voting guidelines:
      */

      $output .= "<p>". t("When new content get submitted it goes to the submission queue.  Most, if not all, registered users can access this queue and can vote whether they think the content should be approved or not.  When enough people vote to approve the content, it is pushed over the threshold and up it goes.  On the other hand, when too many people voted to drop some content, the content will get trashed.") ."</p>";

      /*
      ** Display a voting form:
      */

      $output .= form_select(t("Your vote"), "vote", "", $votes);
      $output .= form_hidden("id", $node->nid);
      $output .= form_submit(t("Vote"));

      $output = form($output);
    }
  }

  $theme->header();
  node_view($node);
  if ($output) {
    $theme->box(t("Moderate"), $output);
  }
  if ($node->comment && variable_get("queue_show_comments", 1)) {
    module_invoke("comment", "render", $node);
  }
  $theme->footer();
}

function queue_page() {
  global $user, $theme, $vote;

  if ($user->uid && user_access("access submission queue")) {
    if (arg(1)) {
      queue_view(check_input(arg(1)));
    }
    else {
      queue_overview();
    }
  }
  else {
    $theme->header();
    $theme->box(t("Moderation queue"), message_access());
    $theme->footer();
  }
}

function queue_block($op = "list", $delta = 0) {
  if ($op == "list") {
    $blocks[0]["info"] = t("Moderation results");
    return $blocks;
  }
  else {
    if (user_access("access submission queue") && (substr_count(request_uri(), url("queue")) || substr_count(request_uri(), url()))) {
      global $user, $id;
      if ($user->uid) {
        $node = node_load(array("nid" => $id));
      }
      if (($user->uid == $node->uid || field_get($node->users, $user->uid)) && $node->moderate == 1) {
        foreach (explode(",", $node->users) as $vote) {
          if ($vote) {
            $data = explode("=", $vote);
            $account = user_load(array("uid" => $data[0]));
            $output .= format_name($account) ." voted '$data[1]'.<br />";
          }
        }

        $block["subject"] = t("Moderation results");
        $block["content"] = $output ? $output : t("This node has not been moderated yet.");
      }
    }
    elseif ((user_access("access submission queue") || user_access("administer blocks")) && (substr_count(request_uri(), url("user")) || substr_count(request_uri(), url("admin")))) {
      $block["subject"] = t("Moderation results");
    }

    return $block;
  }
}

?>