queue.module
<?php
function queue_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Allows content to be moderated by the community.');
case 'admin/settings/queue':
return t("<p>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.</p>");
}
}
function queue_settings() {
$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));
$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>.'));
$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.'));
return $output;
}
function queue_perm() {
return array('access submission queue');
}
function queue_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'queue', 'title' => t('submission queue'),
'callback' => 'queue_page',
'access' => user_access('access submission queue'),
'weight' => 1);
}
return $items;
}
function queue_count() {
$result = db_query('SELECT COUNT(nid) FROM {node} WHERE moderate = 1');
return ($result) ? db_result($result, 0) : 0;
}
function queue_score($nid) {
$result = db_query('SELECT SUM(vote) FROM {queue} WHERE nid = %d', $nid);
return ($result) ? db_result($result, 0) : 0;
}
function queue_vote($node, $vote) {
global $user;
if (!isset($node->voters[$user->uid])) {
db_query("INSERT INTO {queue} (nid, uid, vote) VALUES (%d, %d, %d)", $node->nid, $user->uid, $vote);
$terms = module_invoke('taxonomy', 'node_get_terms', $node->nid, 'tid');
foreach ($terms as $term) {
$node->taxonomy[] = $term->tid;
}
if (variable_get('queue_threshold_post', 4) <= $node->score) {
$node->moderate = 0;
$node->promote = 1;
node_save($node);
watchdog('content', t('Moderation: approved %title.', array('%title' => theme('placeholder', $node->title))));
drupal_set_message(t('The post is promoted.'));
}
else if (variable_get('queue_threshold_dump', -2) >= $node->score) {
if ($node->revisions) {
node_revision_rollback($node, end(node_revision_list($node)));
watchdog('content', t('Moderation: declined %title (rollback).', array('%title' => theme('placeholder', $node->title))));
drupal_set_message(t('The post has been declined and the previous version has been restored.'));
}
else {
$node->moderate = 0;
$node->status = 0;
node_save($node);
watchdog('content', t('Moderation: declined %title.', array('%title' => theme('placeholder', $node->title))));
drupal_set_message(t('The post has been declined.'));
}
}
else if (variable_get('queue_threshold_expire', 8) <= $node->votes) {
if ($node->revisions) {
node_revision_rollback($node, end(node_revision_list($node)));
watchdog('content', t('Moderation: expired %title (rollback).', array('%title' => theme('placeholder', $node->title))));
drupal_set_message(t('The post has expired and the previous version has been restored.'));
}
else {
$node->moderate = 0;
$node->status = 0;
node_save($node);
watchdog('content', t('Moderation: expired %title.', array('%title' => theme('placeholder', $node->title))));
drupal_set_message(t('The post has expired.'));
}
}
$node = node_load(array('nid' => $node->nid), NULL, TRUE);
}
}
function queue_overview() {
global $user;
$header = array(array('data' => t('Subject')), array('data' => t('Author')), array('data' => t('Type')), array('data' => t('Score')));
$sql = 'SELECT n.nid, n.title, n.type, u.name, u.uid, SUM(IF(q.uid = %d, 1, 0)) AS voted, SUM(q.vote) AS score FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {queue} q ON n.nid = q.nid WHERE n.moderate = 1 GROUP BY n.nid, n.title, n.type, u.name, u.uid, q.uid, q.vote';
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql('SELECT COUNT(n.nid) FROM {node} n INNER JOIN {queue} q ON n.nid = q.nid WHERE n.moderate = 1');
$result = pager_query($sql, 10, 0, $sql_count, $user->uid);
while ($node = db_fetch_object($result)) {
if ($user->uid == $node->uid || $node->voted) {
$rows[] = array(array('data' => l($node->title, 'queue/'. $node->nid), 'class' => 'title'), array('data' => format_name($node), 'class' => 'name'), array('data' => node_invoke($node, 'node_name'), 'class' => 'type'), array('data' => $node->score, 'class' => 'score'));
}
else {
$rows[] = array(array('data' => l($node->title, 'queue/'. $node->nid), 'class' => 'title'), array('data' => format_name($node), 'class' => 'name'), array('data' => node_invoke($node, 'node_name'), 'class' => 'type'), array('data' => l(t('vote'), 'queue/'. $node->nid), 'class' => 'score'));
}
}
if ($pager = theme('pager', NULL, 10, 0, tablesort_pager())) {
$rows[] = array(array('data' => $pager, 'colspan' => '4'));
}
if (!$rows) {
$rows[] = array(array('data' => t('No posts available in queue.'), 'colspan' => '4'));
}
$output = '<div id="queue">';
$output .= theme('table', $header, $rows);
$output .= '</div>';
drupal_set_title(t('Submission queue'));
print theme('page', $output);
}
function queue_view($nid) {
global $user;
$op = $_POST['op'];
$edit = $_POST['edit'];
$votes = array('0' => t('neutral (+0)'), '1' => t('post it (+1)'), '-1' => t('dump it (-1)'));
$node = node_load(array('nid' => $nid, 'moderate' => 1));
if ($node) {
if ($user->uid != $node->uid && !isset($node->voters[$user->uid])) {
if ($op == t('Vote') && $votes[$edit['vote']]) {
queue_vote($node, $edit['vote']);
$output = t('Your vote has been recorded.');
}
else {
$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>';
$output .= form_select(t('Your vote'), 'vote', '', $votes);
$output .= form_hidden('id', $node->nid);
$output .= form_submit(t('Vote'));
$output = form($output);
}
}
$output .= node_view($node);
$output = theme('box', t('Moderate'), $output);
if ($node->comment && variable_get('queue_show_comments', 1)) {
$output .= module_invoke('comment', 'render', $node);
}
print theme('page', $output);
}
else {
drupal_not_found();
}
}
function queue_page($nid = 0) {
if ($nid) {
queue_view($nid);
}
else {
queue_overview();
}
}
function queue_block($op = 'list', $delta = 0) {
global $user;
if ($op == 'list') {
$blocks[0]['info'] = t('Moderation results');
return $blocks;
}
else if ($op == 'view') {
if (user_access('access submission queue') && (arg(0) == 'queue') || arg(0) == 'node') {
if ($user->uid) {
if (arg(0) == 'queue') {
$id = arg(1);
}
else {
$id = arg(2);
}
$node = node_load(array('nid' => $id));
if (($user->uid == $node->uid || isset($node->voters[$user->uid])) && $node->moderate == 1) {
foreach ($node->voters as $uid => $vote) {
$account = user_load(array('uid' => $uid));
$output .= t('%user voted %vote', array('%user' => format_name($account), '%vote' => $vote)) .'<br />';
}
$block['subject'] = t('Moderation results');
$block['content'] = $output ? $output : t('This node has not yet been moderated.');
}
}
}
return $block;
}
}
function queue_nodeapi(&$node, $op) {
switch ($op) {
case 'load':
$result = db_query("SELECT uid, vote FROM {queue} WHERE nid = %d", $node->nid);
$node->voters = array();
$node->score = 0;
while ($voter = db_fetch_object($result)) {
$node->voters[$voter->uid] = $voter->vote;
$node->score += $voter->vote;
}
break;
case 'validate':
if ($node->nid && $node->moderate) {
$node->score = 0;
$node->voters = array();
$node->votes = 0;
}
break;
case 'insert':
if ($node->moderate) {
db_query("INSERT INTO {queue} (nid, uid) VALUES (%d, %d)", $node->nid, $node->uid);
}
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.'));
}
break;
}
}
?>