--- nmoderation.module.orig 2006-08-18 14:47:32.000000000 +1000 +++ nmoderation.module 2006-08-18 16:58:03.000000000 +1000 @@ -10,6 +10,11 @@ define('NMODERATION_CACHE_TYPE', 'points'); define('NMODERATION_TAG', 'vote'); +//Pre-sets viewing thresholds. Would probably be good to generalise these +define('NMODERATION_THRESHOLD_HIGH', '2'); +define('NMODERATION_THRESHOLD_LOW', '1'); +define('NMODERATION_THRESHOLD_ALL', '0'); + /** * Implementation of hook_help(). */ @@ -51,8 +56,8 @@ function nmoderation_menu($may_cache) { 'callback' => 'nmoderation_list_all', 'weight' => 5, 'access' => user_access('administer node moderation votes')); } else { - if (arg(0) == 'node' && is_numeric(arg(1))) { - $access = user_access('administer node moderation votes') || user_access('view node moderation votes'); + if (arg(0) == 'node' && is_numeric(arg(1))) { + $access = user_access('administer node moderation votes') || user_access('view node moderation votes'); $items[] = array('path' => 'node/'. arg(1). '/nmoderation', 'title' => t('votes'), 'type' => MENU_LOCAL_TASK, 'callback' => 'nmoderation_list_node', 'weight' => 5, 'access' => $access, 'callback arguments' => array(arg(1))); } @@ -527,17 +532,19 @@ function nmoderation_delete_confirm_subm // will either promote or unpublish or do nothing, depending on the curent score for this node function nmoderation_set_promote($content_id, $content_type = 'node') { - $result = votingapi_get_voting_result($content_type, $content_id, NMODERATION_CACHE_TYPE, NMODERATION_TAG, 'sum'); - $node = node_load($content_id); - // have to load terms in order that they don't get lost upon saving the node. - - if ($result->value < variable_get('nmoderation_threshold_unpublish', 4)) { - $node->status = 0; - node_save($node); - } - elseif ($result->value > variable_get('nmoderation_threshold_promote', 4)) { - $node->promote = 1; - node_save($node); + if( variable_get('nmoderation_do_promotion', 'no') == 'yes') { + $result = votingapi_get_voting_result($content_type, $content_id, NMODERATION_CACHE_TYPE, NMODERATION_TAG, 'sum'); + $node = node_load($content_id); + // have to load terms in order that they don't get lost upon saving the node. + + if ($result->value < variable_get('nmoderation_threshold_unpublish', 4)) { + $node->status = 0; + node_save($node); + } + elseif ($result->value > variable_get('nmoderation_threshold_promote', 4)) { + $node->promote = 1; + node_save($node); + } } } @@ -551,7 +558,7 @@ function nmoderation_list_unvoted($conte ); $cols = 2; $title = t('my unmoderated nodes'); - $types = implode("','", variable_get('nmoderation_node_types', array_flip(node_get_types()))); + $types = implode("','", variable_get('nmoderation_node_types', array_flip(nmoderation_node_get_types()))); $sql = "SELECT n.nid, n.created, u.name, n.title, n.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {votingapi_vote} v ON n.nid = v.content_id AND v.content_type = '$content_type' AND v.uid = $user->uid WHERE n.type IN ('$types') AND n.status = 1 AND ISNULL(v.vote_id)"; $min = time()-24*3600*variable_get('nmoderation_vote_interval', 7); $sql .= " AND n.created > $min"; @@ -679,39 +686,72 @@ function nmoderation_list_node($content_ function nmoderation_settings() { $promote = 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)); - $unpublish = drupal_map_assoc(array(-1, -2, -3, -4, -5, -6, -7, -8, -8, -10, -11, -12, -13, -14, -15, -20, -25, -30)); + $unpublish = drupal_map_assoc(array(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -20, -25, -30)); $interval = drupal_map_assoc(range(1,60)); - + $form['nmoderation_node_types'] = array( '#type' => 'select', '#title' => t('Node Types'), - '#default_value' => variable_get('nmoderation_node_types', node_get_types()), - '#options' => node_get_types(), + '#default_value' => variable_get('nmoderation_node_types', nmoderation_node_get_types()), + '#options' => nmoderation_node_get_types(), '#description' => t('Select the node types upon which accept node moderation votes.'), '#extra' => 0, '#multiple' => 1, ); - $form['nmoderation_threshold_promote'] = array( + $form['nmoderation_promote_settings'] = array( + '#type' => 'fieldset', + '#title' => 'Promotion settings'); + $form['nmoderation_promote_settings']['nmoderation_do_promotion'] = array( + '#type' => 'radios', + '#title' => t('Promote/Unpublish on thresholds'), + '#default_value' => variable_get('nmoderation_do_promotion', 'no'), + '#options' => array ( + 'yes' => t('Yes'), + 'no' => t('No') + ), + '#description' => t('Select this if you want posts to be promoted or unpublished when the thresholds below are reached'), + ); + $form['nmoderation_promote_settings']['nmoderation_threshold_promote'] = array( '#type' => 'select', '#title' => t('Promote threshold'), '#default_value' => variable_get('nmoderation_threshold_promote', 4), '#options' => $promote, '#description' => t('When a post exceeds this moderation score, it is immediately promoted to the front page.'), ); - $form['nmoderation_threshold_unpublish'] = array( + $form['nmoderation_promote_settings']['nmoderation_threshold_unpublish'] = array( '#type' => 'select', '#title' => t('Unpublish threshold'), '#default_value' => variable_get('nmoderation_threshold_unpublish', -2), '#options' => $unpublish, '#description' => t('When a post drops below this score, it is immediately unpublished.'), ); - $form['nmoderation_vote_interval'] = array( + $form['nmoderation_promote_settings']['nmoderation_vote_interval'] = array( '#type' => 'select', '#title' => t('Voting Interval'), '#default_value' => variable_get('nmoderation_vote_interval', 7), '#options' => $interval, '#description' => t('The number of days since the creation of a node when we allow voting.'), ); + $form['nmoderation_hide_comments'] = array( + '#type' => 'radios', + '#title' => t('Hide comments below threshold'), + '#default_value' => variable_get('nmoderation_hide_comments', 'no'), + '#options' => array ( + 'yes' => t('Yes'), + 'no' => t('No') + ), + ); + $form['nmoderation_default_threshold'] = array( + '#type' => 'radios', + '#title' => t('Default Viewing Threshold'), + '#default_value' => variable_get('nmoderation_default_threshold', NMODERATION_THRESHOLD_HIGH), + '#options' => array ( + NMODERATION_THRESHOLD_HIGH => t('View only high quality content'), + NMODERATION_THRESHOLD_LOW => t('View all non-hidden content'), + NMODERATION_THRESHOLD_ALL => t('View all content') + ), + '#description' => t('Choose the default level of content for users to view.') + ); return $form; } @@ -722,8 +762,18 @@ function nmoderation_is_votable($node) { if (user_access('administer node moderation votes')) { return TRUE; } - else { - $access = user_access('nmoderate nodes') && ($node->uid != $user->uid) && (time()-$node->created < 3600*24*variable_get('nmoderation_vote_interval', 7)); + else { //hack to allow comment type to be used as well + if (isset($node->created)) { + $createdtime = $node->created; + } + elseif (isset($node->timestamp)) { + $createdtime = $node->timestamp; + } + else { + $createdtime = 0; //TODO - set this to 0 or time()? + } + + $access = user_access('nmoderate nodes') && ($node->uid != $user->uid) && (time()-$createdtime < 3600*24*variable_get('nmoderation_vote_interval', 7)); return $access; } } @@ -732,7 +782,7 @@ function nmoderation_is_votable($node) { function nmoderation_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { switch ($op) { case 'view': - if ($page && nmoderation_is_votable($node) && in_array($node->type, variable_get('nmoderation_node_types', node_get_types()))) { + if ($page && nmoderation_is_votable($node) && in_array($node->type, variable_get('nmoderation_node_types', nmoderation_node_get_types()))) { global $user; $vote = votingapi_get_vote('node', $node->nid, NMODERATION_VOTE_TYPE, NMODERATION_TAG, $user->uid); @@ -746,12 +796,76 @@ function nmoderation_nodeapi(&$node, $op $node->body .= drupal_get_form('nmoderation_moderation_form', $frm); } } + + break; + + case 'insert': + votingapi_recalculate_results('node', $node->nid, TRUE); break; } } +/* Moderation for comments + * vik 13-8-06 + */ + +function nmoderation_comment(&$comment, $op) { + switch ($op) { + case 'view': + + //Only show objects above a certain threshold + $result = db_query("SELECT value FROM {votingapi_cache} WHERE content_type = 'comment' and content_id = '%d'", $comment->cid); + if($result) { + $ro = db_fetch_object($result); + $score = $ro->value; + } else { + $score = 0; + } + global $user; + if(isset($user->viewing_threshold)) { + $threshold = $user->viewing_threshold; + } else { + $threshold = variable_get('nmoderation_default_threshold', NMODERATION_THRESHOLD_HIGH); + } + + if($score < $threshold && variable_get('nmoderation_hide_comments', 'no')== 'yes') { + //Should make this comment into something more useful + $comment->comment = 'This comment has been hidden by the moderator. To view it, change your viewing threshold here.'; + $comment->subject = '[comment hidden]'; + } else { + + //Add voting box to comment if applicable + if (nmoderation_is_votable($comment) && in_array('comment', variable_get('nmoderation_node_types', nmoderation_node_get_types()))) { + $vote = votingapi_get_vote('comment', $comment->cid, NMODERATION_VOTE_TYPE, NMODERATION_TAG, $user->uid); + + if ($vote->vote_id) { + $title = t('Edit vote'); + } + else { + $title = t('Add vote'); + } + if ($frm = nmoderation_get_moderation_form('comment', $comment->cid, $vote, $title)) { + $comment->comment .= drupal_get_form('nmoderation_moderation_form' . $comment->cid, $frm, 'nmoderation_moderation_form'); + } + } + } + break; + case 'insert': + //for some reason, $comment->cid doesn't work, so using [] instead + votingapi_recalculate_results('comment', $comment['cid'], TRUE); + break; + } +} + + function nmoderation_votingapi_calculate(&$cache, $votes, $content_type, $content_id) { - if ($content_type == 'node' && nmoderation_is_votable(node_load($content_id))) { + if ($content_type == 'node') { + $content = node_load($content_id); + } elseif ($content_type == 'comment') { + $content = _comment_load($content_id); + } + + if ($content && nmoderation_is_votable($content)) { //loop through the $votes collection and calculate the weighted value. foreach($votes as $vote) { if ($vote->value_type == NMODERATION_VOTE_TYPE && $vote->tag == NMODERATION_TAG) { @@ -761,5 +875,100 @@ function nmoderation_votingapi_calculate $cache[NMODERATION_TAG][NMODERATION_CACHE_TYPE]['sum'] += nmoderation_get_value($vote->value, $account); } } + } + + //add initial node score + if($content->uid != 0) { //Not anonymous user + $initial_scores = variable_get('nmoderation_roles', array()); + + $initial_value = 0; + + //Go through user roles to see if higher value has been set + $user = user_load(array('uid' => $content->uid)); + foreach(array_keys($user->roles) as $rid) { + if($initial_scores[$rid] > $initial_value) { + $initial_value = $initial_scores[$rid]; + } + } + } + //Actually set the initial value + $cache[NMODERATION_TAG][NMODERATION_CACHE_TYPE]['sum'] += $initial_value; +} + +function nmoderation_node_get_types() { + $types = node_get_types(); + $types['comment'] = 'comment'; + return $types; +} + +function nmoderation_user($type, $edit, &$user, $category = NULL) { + if ($type == 'form' && $category == 'account') { + // when user tries to edit his own data + $form['moderation_settings'] = array( + '#type' => 'fieldset', + '#title' => t('Moderation settings'), + '#weight' => 5); + $form['moderation_settings']['viewing_threshold'] = array( + '#type' => 'radios', + '#title' => t('Viewing Threshold'), + '#default_value' => isset($edit['viewing_threshold']) ? $edit['viewing_threshold'] : variable_get('nmoderation_default_threshold', NMODERATION_THRESHOLD_HIGH), + '#options' => array ( + NMODERATION_THRESHOLD_HIGH => t('View only high quality content'), + NMODERATION_THRESHOLD_LOW => t('View all non-hidden content'), + NMODERATION_THRESHOLD_ALL => t('View all content') + ), + '#description' => t('Choose what level of content you wish to view.')); + + return $form; + } +} + +//Creates a table to get view based on VotingAPI's cached score +function nmoderation_views_tables() { + $tables["nmoderation_votingapi_cache"] = array( + "name" => "votingapi_cache", + "provider" => "nmoderation", + "join" => array( + "left" => array( + "table" => "node", + "field" => "nid" + ), + "right" => array( + "field" => "content_id" + ), + "extra" => array( + 'content_type' => 'node', + ), + ), + "filters" => array( + "score" => array( + 'field' => 'value', + 'name' => "VotingAPI: Node Score", + 'operator' => 'views_handler_operator_gtlt', + 'help' => t("Filter by the cached score of the node. Use ***CURRENT_THRESHOLD*** for the current user's threshold"), + ), + ), + ); + return $tables; +} + +function nmoderation_views_query_substitutions($view) { + global $user; + if(isset($user->viewing_threshold)) { + $threshold = $user->viewing_threshold; + } else { + $threshold = variable_get('nmoderation_default_threshold', NMODERATION_THRESHOLD_HIGH); + } + return array("***CURRENT_THRESHOLD***" => intval($threshold)); +} + +//This should be unnecessary now - not deleting it in case it comes in handy later +function nmoderation_views_handler_filter_score_threshold($op, $filter, $filterinfo, &$query) { + global $user; + if(isset($user->viewing_threshold)) { + $threshold = $user->viewing_threshold; + } else { + $threshold = variable_get('nmoderation_default_threshold', NMODERATION_THRESHOLD_HIGH); } + $query->add_where("node.nid IN (SELECT content_id FROM {votingapi_cache} WHERE content_type = 'node' AND value >= '%d')", $threshold); }