queue.module 9.06 KB
Newer Older
1
<?php
2
// $Id$
3

4 5 6 7
function queue_help($section) {
  $output = "";

  switch ($section) {
8
    case 'admin/system/modules#description':
9
      $output = t("Enables content to be moderated by the community.");
10 11
      break;
    case 'admin/system/modules/queue':
12
      $output = t("The queue provides a way for your users to vote on submitted content. This is called <strong>moderation</strong>. Users can moderate a post up (give it a point), or down (subtract a point). The settings below give you control over how many points are required for the status of a post to be automatically changed. See individual items for details.");
13 14
      break;
  }
15
  return $output;
16
}
Dries's avatar
Dries committed
17

18
function queue_settings() {
19 20
  $post_and_expire = drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100));
  $dump = drupal_map_assoc(array(-1, -2, -3, -4, -5, -6, -7, -8, -8, -10, -11, -12, -13, -14, -15, -20, -25, -30));
Dries's avatar
Dries committed
21

22 23 24
  $output .= form_select(t("Post threshold"), "queue_threshold_post", variable_get("queue_threshold_post", 4), $post_and_expire, t("When a post gets this number of moderation points, it is <strong>promoted to the front page</strong> automatically."));
  $output .= form_select(t("Dump threshold"), "queue_threshold_dump", variable_get("queue_threshold_dump", -2), $dump, t("When a post drops below this number of points, its status is changed to <strong>unpublished</strong>."));
  $output .= form_select(t("Expiration threshold"), "queue_threshold_expire", variable_get("queue_threshold_expire", 8), $post_and_expire, t("When a post gets this number of points, its status is changed to <strong>unpublished</strong>."));
25
  $output .= form_item(t("Show comments"), form_checkbox(t("Enabled"), "queue_show_comments", 1, variable_get("queue_show_comments", 1)), t("Tick the box to show comments below the moderation form."));
Dries's avatar
Dries committed
26

Dries's avatar
Dries committed
27
  return $output;
28 29
}

30
function queue_perm() {
Dries's avatar
Dries committed
31 32 33 34
  return array("access submission queue");
}

function queue_link($type) {
35 36
  $links = array();

37 38
  if ($type == "system") {
    if (user_access("access submission queue")) {
39
      menu("queue", t("submission queue"), "queue_page", 1);
40
    }
Dries's avatar
Dries committed
41 42
  }

43
  return $links;
44 45
}

Dries's avatar
Dries committed
46
function queue_count() {
47
  $result = db_query("SELECT COUNT(nid) FROM {node} WHERE moderate = 1");
48 49 50
  return ($result) ? db_result($result, 0) : 0;
}

Dries's avatar
Dries committed
51
function queue_score($id) {
52
  $result = db_query("SELECT score FROM {node} WHERE nid = %d", $id);
53 54 55
  return ($result) ? db_result($result, 0) : 0;
}

Dries's avatar
Dries committed
56
function queue_vote($node, $vote) {
57
  global $user;
58

Dries's avatar
Dries committed
59 60
  if (!field_get($node->users, $user->uid)) {
    // Update submission's score- and votes-field:
61
    db_query("UPDATE {node} SET score = score $vote, votes = votes + 1, users = '". field_set($node->users, $user->uid, $vote) ."' WHERE nid = '$node->nid'");
62

Dries's avatar
Dries committed
63 64
    // Reload the updated node from the database:
    $node = node_load(array("nid" => $node->nid));
65

66 67 68 69 70
    $terms = module_invoke('taxonomy', 'node_get_terms', $node->nid, 'tid');
    foreach ($terms as $term) {
      $node->taxonomy[] = $term->tid;
    }

71
    if (variable_get("queue_threshold_post", 4) <= $node->score) {
72 73 74
      $node->moderate = 0;
      $node->promote = 1;
      node_save($node);
Dries's avatar
Dries committed
75
      watchdog("special", "moderation: approved '$node->title'");
Dries's avatar
Dries committed
76
    }
Dries's avatar
Dries committed
77
    else if (variable_get("queue_threshold_dump", -2) >= $node->score) {
Dries's avatar
Dries committed
78 79
      if ($node->revisions) {
        node_revision_rollback($node, end(node_revision_list($node)));
Dries's avatar
Dries committed
80
        watchdog("special", "moderation: declined '$node->title' (rollback)");
81
      }
Dries's avatar
Dries committed
82
      else {
83 84 85
        $node->moderate = 0;
        $node->status = 0;
        node_save($node);
Dries's avatar
Dries committed
86
        watchdog("special", "moderation: declined '$node->title'");
Dries's avatar
Dries committed
87 88
      }
    }
89
    else if (variable_get("queue_threshold_expire", 8) <= $node->votes) {
Dries's avatar
Dries committed
90 91 92
      if ($node->revisions) {
        node_revision_rollback($node, end(node_revision_list($node)));
        watchdog("special", "moderation: expired '$node->title' (rollback)");
93
      }
Dries's avatar
Dries committed
94
      else {
95 96 97
        $node->moderate = 0;
        $node->status = 0;
        node_save($node);
Dries's avatar
Dries committed
98
        watchdog("special", "moderation: expired '$node->title'");
99 100 101 102 103
      }
    }
  }
}

Dries's avatar
Dries committed
104
function queue_overview() {
105
  global $user;
106

107
  $header = array(array("data" => t("subject")), array("data" => t("author")), array("data" => t("type")), array("data" => t("score")));
108

109 110 111
  $sresult = pager_query("SELECT n.*, u.name, u.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.moderate = 1", 10, 0);

  while ($node = db_fetch_object($sresult)) {
Dries's avatar
Dries committed
112
    if ($user->uid == $node->uid || field_get($node->users, $user->uid)) {
113
      $rows[] = array(array("data" => l($node->title, "queue/$node->nid"), "class" => "title"), array("data" => format_name($node), "class" => "name"), array("data" => module_invoke($node->type, "node", "name"), "class" => "type"), array("data" => queue_score($node->nid), "class" => "score"));
Dries's avatar
Dries committed
114 115
    }
    else {
116
      $rows[] = array(array("data" => l($node->title, "queue/$node->nid"), "class" => "title"), array("data" => format_name($node), "class" => "name"), array("data" => module_invoke($node->type, "node", "name"), "class" => "type"), array("data" => l(t("vote"), "queue/$node->nid"), "class" => "score"));
Dries's avatar
Dries committed
117
    }
118
  }
Dries's avatar
Dries committed
119

120 121
  if ($pager = theme("pager", NULL, 10, 0, tablesort_pager())) {
   $rows[] = array(array("data" => $pager, "colspan" => 4));
122
  }
123 124 125 126

  $output  = "<div id=\"queue\">";
  $output .= theme("table", $header, $rows);
  $output .= "</div>";
127

128
  drupal_set_title(t("Submission queue"));
129

130
  print theme("page", $output);
131 132
}

Dries's avatar
Dries committed
133
function queue_view($nid) {
134 135 136 137
  global $user;

  $op = $_POST["op"];
  $edit = $_POST["edit"];
138

Dries's avatar
Dries committed
139 140 141 142 143 144 145 146 147 148 149
  /*
  ** 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));
150

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

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

159 160 161 162 163 164
      $output = t("Your vote has been recorded.");
    }
    else {
      /*
      ** Display some explanation or voting guidelines:
      */
165

166
      $output .= "<p>". t("When new content is submitted it goes into the submission queue.  Registered users with the appropriate permission can access this queue and vote whether they think the content should be approved or not.  When enough people vote to approve the content it is displayed on the front page.  On the other hand, if enough people vote to drop it, the content will disappear.") ."</p>";
167

168 169 170
      /*
      ** Display a voting form:
      */
171

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

176
      $output = form($output);
177
    }
178
  }
Dries's avatar
Dries committed
179

180
  $output .= node_view($node);
181
  if ($output) {
182
    $output .= theme("box", t("Moderate"), $output);
183 184
  }
  if ($node->comment && variable_get("queue_show_comments", 1)) {
185
    $output .= module_invoke("comment", "render", $node);
186
  }
187
  print theme("page", $output);
188 189
}

Dries's avatar
Dries committed
190
function queue_page() {
191
  global $user, $vote;
192

193 194
  if (arg(1)) {
    queue_view(arg(1));
195
  }
Dries's avatar
Dries committed
196
  else {
197
    queue_overview();
Dries's avatar
Dries committed
198
  }
199
}
200

201
function queue_block($op = "list", $delta = 0) {
202 203
  global $user;

204 205 206 207 208
  if ($op == "list") {
    $blocks[0]["info"] = t("Moderation results");
    return $blocks;
  }
  else {
209
    if (user_access("access submission queue") && (arg(0) == "queue") || arg(0) == "node") {
210
      if ($user->uid) {
211 212 213 214 215 216
        if (arg(0) == "queue") {
          $id = arg(1);
        }
        else {
          $id = arg(2);
        }
217
        $node = node_load(array("nid" => $id));
218 219 220 221 222 223 224
        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 />";
            }
225
          }
226

227 228 229 230
          $block["subject"] = t("Moderation results");
          $block["content"] = $output ? $output : t("This node has not yet been moderated.");

        }
231 232 233 234
      }
    }
    return $block;
  }
235 236
}

237 238
function queue_nodeapi(&$node, $op, $arg = 0) {
  switch ($op) {
239 240 241
    case 'fields':
      return array('score', 'users', 'votes');
    case 'validate':
242 243 244
      if ($node->nid && $node->moderate) {
        // Reset votes when node is updated:
        $node->score = 0;
245
        $node->users = '';
246
        $node->votes = 0;
247 248
      }
      break;
249 250 251 252 253 254 255 256 257 258 259 260 261
    case 'insert':
    case 'update':
      if ($node->moderate && user_access("access submission queue")) {
        drupal_set_message(t('The post is queued for approval. You can check the votes in the <a href="%queue">submission queue</a>.', array('%queue' => url('queue'))));
      }
      else if ($node->moderate) {
        drupal_set_message(t('The post is queued for approval. The editors will decide whether it should be published.'));
      }
      else {
        drupal_set_message(t('The post is published.'));
      }
      break;
   }
262
}
263
?>