moderation.module 4.64 KB
Newer Older
Dries's avatar
 
Dries committed
1 2 3 4 5 6
<?php

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

include_once "includes/common.inc";
Dries's avatar
 
Dries committed
7
include_once "includes/node.inc";
Dries's avatar
 
Dries committed
8 9 10 11 12 13

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

function moderation_count() {
Dries's avatar
 
Dries committed
14 15
  global $status;
  $result = db_query("SELECT COUNT(nid) FROM nodes WHERE status = '$status[queued]'");
Dries's avatar
 
Dries committed
16 17 18 19 20 21 22 23 24
  return ($result) ? db_result($result, 0) : 0;
}

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

function moderation_vote($id, $vote) {
Dries's avatar
 
Dries committed
25
  global $status, $user;
Dries's avatar
 
Dries committed
26 27 28 29 30 31 32 33

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

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

Dries's avatar
 
Dries committed
34
    if ($node = node_get_object(nid, $id)) {
Dries's avatar
 
Dries committed
35
      if (node_post_threshold($node) <= $node->score) {
Dries's avatar
 
Dries committed
36 37
        node_save(array(nid => $id, pid => $node->pid, type => $node->type, status => $status[posted]));
        watchdog("message", "posted node '$node->title'");
Dries's avatar
 
Dries committed
38 39
      }
      else if (node_dump_threshold($node) >= $node->score) {
Dries's avatar
 
Dries committed
40 41
        node_save(array(nid => $id, pid => $node->pid, type => $node->type, status => $status[dumped]));
        watchdog("message", "dumped node '$node->title'");
Dries's avatar
 
Dries committed
42 43
      }
      else if (node_timout_threshold($node) <= $node->votes) {
Dries's avatar
 
Dries committed
44 45
        node_save(array(nid => $id, pid => $node->pid, type => $node->type, status => $status[expired]));
        watchdog("message", "expired node '$node->title'");
Dries's avatar
 
Dries committed
46 47 48 49 50 51
      }
    }
  }
}

function moderation_overview() {
Dries's avatar
 
Dries committed
52
  global $status, $theme, $user;
Dries's avatar
 
Dries committed
53

Dries's avatar
 
Dries committed
54
  $result = db_query("SELECT n.*, u.userid FROM nodes n LEFT JOIN users u ON n.author = u.id WHERE n.status = '$status[queued]'");
Dries's avatar
 
Dries committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

  $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 {
Dries's avatar
 
Dries committed
78 79 80 81 82
    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>". check_output($node->log, 1) ."</P>";
    }

Dries's avatar
 
Dries committed
83 84 85 86 87 88 89 90 91
    // 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();
Dries's avatar
 
Dries committed
92
    node_view($node, 0);
Dries's avatar
 
Dries committed
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
    $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;
    }
  }
}

?>