summaryrefslogtreecommitdiff
path: root/modules/queue.module
blob: 8dd4d2a5a2c4dc28f3850aacb3c8b76c7231b9ac (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
<?php
// $Id$

function queue_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), 2419200 => format_interval(2419200), 1000000000 => t("Never"));
  $output .= form_select(t("Discard entries older than"), "queue_clear", variable_get("queue_clear", 604800), $period, t("The time nodes should be kept in the submission queue.  Older entries will be automatically discarded.  Requires crontab."));  return $output;
}

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

function queue_link($type) {
  if ($type == "menu" && user_access("access submission queue")) {
    $links[] = "<a href=\"module.php?mod=queue\">". t("submission queue") ."</a> (<FONT COLOR=\"red\">". queue_count() ."</FONT>)";
  }

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

function queue_cron() {
  // TODO: use 'created' or 'changed' ?
  // db_query("UPDATE node SET status = 0 WHERE moderate = 1 AND ". time() ." - timestamp > ". variable_get("queue_clear", 604800));
}

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($id, $vote) {
  global $user;

  if ($node = node_load(array(nid => $id))) {

    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 = $id");

      $node = node_load(array(nid => $id, type => $node->type));

      if (variable_get($node->type ."_post", 4) <= $node->score) {
        node_save($node, array("nid", "status" => 1, "moderate" => 0));
        watchdog("special", "node: posted '$node->title' - moderation");
      }
      else if (variable_get($node->type ."_dump", -2) >= $node->score) {
        node_save($node, array("nid", "status" => 1, "moderate" => 0));
        watchdog("special", "node: dumped '$node->title' - moderation");
      }
      else if (variable_get($node->type ."_expire", 8) <= $node->votes) {
        node_save($node, array("nid", "status" => 0, "moderate" => 0));
        watchdog("special", "node: expired '$node->title' - moderation");
      }
    }
  }
}

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");

  $content .= "<table border=\"0\" cellspacing=\"4\" cellpadding=\"4\">";
  $content .= " <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)) $content .= " <tr><td><a href=\"module.php?mod=queue&op=view&id=$node->nid\">". check_output($node->title) ."</a></td><td align=\"center\">". format_name($node) ."</td><td align=\"center\">". check_output($node->type) ."</td><td align=\"center\">". queue_score($node->nid) ."</td></tr>";
    else $content .= " <tr><td><a href=\"module.php?mod=queue&op=view&id=$node->nid\">". check_output($node->title) ."</a></td><td align=\"center\">". format_name($node) ."</td><td align=\"center\">". check_output($node->type) ."</td><td align=\"center\"><a href=\"module.php?mod=queue&op=view&id=$node->nid\">". t("vote") ."</a></td></tr>";
  }
  $content .= "</table>";

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

function queue_view($id) {
  global $theme, $user;

  $node = node_load(array(nid => $id));

  if ($user->uid == $node->uid || field_get($node->users, $user->uid)) {
    drupal_goto("node.php?id=$node->nid");
  }
  else {

    /*
    ** The keys of this associative array are displayed in each submission's
    ** selection box whereas the corresponding values represent the
    ** mathematical calculation to be performed to update a comment's value.
    */
    $queue_votes = array("neutral (+0)" => "+ 0", "post it (+1)" => "+ 1", "dump it (-1)" => "- 1");

    // TODO: this is where the upcoming versioning system should come in
    if ($n = node_load(array("nid" => $node->pid))) {
      $output .= " ". t("The above node is a proposed update of an existing node:") ." \"<a href=\"node.php?id=$n->nid\">". check_output($n->title) ."</a>\".";
    }

    // TODO: this is where the upcoming versioning system should come in
    if ($node->log) {
      $output .= " ". t("The log message to accompany this submission is given below:") ."<p><i>". check_output($node->log, 1) ."</i></p>";
    }

    // moderation form:
    $output .= "<form action=\"module.php?mod=queue\" method=\"post\">";
    foreach ($queue_votes as $key=>$value) $options .= "  <option value=\"$value\">$key</option>";
    $output .= " <select name=\"vote\">$options</select>";
    $output .= " <input type=\"hidden\" name=\"id\" value=\"$node->nid\">";
    $output .= " <input type=\"submit\" name=\"op\" value=\"Vote\">";
    $output .= "</form>";

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

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

  if ($user->uid && user_access("access submission queue")) {
    switch($op) {
      case "Vote";
        queue_vote(check_input($id), check_input($vote));
        // fall through:
      case "view":
        queue_view(check_input($id));
        break;
      default:
        queue_overview();
        break;
    }
  }
  else {
    $theme->header();
    $theme->box(t("Moderation queue"), message_access());
    $theme->footer();
  }
}

?>