/**
* Modify score for a node based on the flag and account roles.
*
* @param $nid
* Node id the flag is applied to.
* @param $newflag
* The flag to be applied.
* @param $account
* The user who is flagging.
*/
function flagging_flag_modify_score($nid, $newflag, $account) {
// maps flag type to flagging_node_score column.
$score_table_columns = array(
FLAGGING_BREAKING_NEWS => 'flag_bn_score',
FLAGGING_BREAKING_NEWS_HEAVY => 'flag_bn_score',
FLAGGING_GOOD_STUFF => 'flag_gs_score',
FLAGGING_NEWS_WANTED => 'flag_nw_score',
FLAGGING_NEEDS_IMPROVEMENT => 'flag_ni_score',
FLAGGING_FISHY => 'flag_fishy_score',
FLAGGING_SPAM => 'flag_spam_score',
);
$score_modifier = NULL;
$scores = flagging_scores(); // information about all our scores.
$snewflag = ltrim($newflag, '-'); // saves lots of effort later on.
foreach ($account->roles as $rid => $rname) { // get highest score of all user roles.
if ($scores[$rid][$snewflag] && abs($scores[$rid][$snewflag]) > abs($score_modifier)) {
$score_modifier = $scores[$rid][$snewflag];
}
} // if we don't have a score, check the defaults and use that. else, settle on NULL. bugger.
$score_modifier = !$score_modifier ? ($scores[0][$snewflag] ? $scores[0][$snewflag] : NULL) : $score_modifier;
if ($score_modifier) { // insert or modify the existing score.
$score_modifier = ($newflag > 0) ? $score_modifier : -$score_modifier;
$result = db_query('SELECT %s FROM {flagging_node_score} WHERE nid = %d', $score_table_columns[$snewflag], $nid);
if (db_num_rows($result)) { // check if there is a row for that nid.
db_query('UPDATE {flagging_node_score} SET %s = %s + %d WHERE nid = %d', $score_table_columns[$snewflag], $score_table_columns[$snewflag], $score_modifier, $nid);
}
else {
db_query('INSERT INTO {flagging_node_score} (nid, %s) VALUES (%d, %d)', $score_table_columns[$snewflag], $nid, $score_modifier);
}
$current_score = db_result($result);
}
$current_score = isset($current_score) ? $current_score + $score_modifier : $score_modifier;
$cid = 'node_load:'. $nid;
// regardless of the flag type, negative scores are always bad,
// and postive scores are always good. nothing happens to Good Stuff
// stories (other not-here code filters for it as necessary).
switch ($snewflag) {
case FLAGGING_BREAKING_NEWS:
case FLAGGING_BREAKING_NEWS_HEAVY:
db_query('UPDATE {node} SET sticky = %d WHERE nid = %d', $current_score >= 10, $nid);
cache_clear_all($cid, 'cache_node');
cache_clear_all();
break;
case FLAGGING_NEEDS_IMPROVEMENT:
case FLAGGING_FISHY:
db_query('UPDATE {node} SET promote = %d WHERE nid = %d', $current_score >= 0, $nid);
cache_clear_all($cid, 'cache_node');
cache_clear_all();
break;
case FLAGGING_SPAM:
db_query('UPDATE {node} SET status = %d WHERE nid = %d', $current_score >= 0, $nid);
cache_clear_all($cid, 'cache_node');
cache_clear_all();
$n = (object)array('nid' => $nid, 'status' => $current_score >= 0);
np_newsroom_actions_update($n);
break;
}
}
No comments:
Post a Comment