| 1 |
<?php
|
| 2 |
// $Id:
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_help().
|
| 10 |
*/
|
| 11 |
function user_karma_offensive_flag_help($path, $arg) {
|
| 12 |
switch ($path) {
|
| 13 |
case 'admin/modules#description':
|
| 14 |
// This description is shown in the listing at admin/modules.
|
| 15 |
return t('User karma - remove karma for bad content, marked as offensive n times');
|
| 16 |
}
|
| 17 |
}
|
| 18 |
|
| 19 |
##################################################################
|
| 20 |
# START OF SETTINGS FORM
|
| 21 |
##################################################################
|
| 22 |
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Add a new setting to the user_karma_offensive_flag panel.
|
| 26 |
*
|
| 27 |
* @return
|
| 28 |
* The altered config form
|
| 29 |
*/
|
| 30 |
function user_karma_offensive_flag_form_alter(&$form, $form_state, $form_id) {
|
| 31 |
switch ($form_id) {
|
| 32 |
case 'user_karma_peerreview_admin_settings':
|
| 33 |
$form['lose_karma']['user_karma_peerreview_receive_offensive_number'] = array(
|
| 34 |
'#type' => 'textfield',
|
| 35 |
'#title' => t('How much offensive flags are needed to reputate a post as offensive'),
|
| 36 |
'#size' => 4,
|
| 37 |
'#maxlength' => 4,
|
| 38 |
'#default_value' => variable_get('user_karma_peerreview_receive_offensive_number', 5),
|
| 39 |
);
|
| 40 |
$form['lose_karma']['user_karma_peerreview_receive_five_offensive'] = array(
|
| 41 |
'#type' => 'textfield',
|
| 42 |
'#title' => t('How much karma will be removed when your post is marked as offensive'),
|
| 43 |
'#size' => 4,
|
| 44 |
'#maxlength' => 4,
|
| 45 |
'#default_value' => variable_get('user_karma_peerreview_receive_five_offensive', 100),
|
| 46 |
);
|
| 47 |
break;
|
| 48 |
}
|
| 49 |
}
|
| 50 |
|
| 51 |
##################################################################
|
| 52 |
# HOOK TO INTERCEPT THE FLAGGING
|
| 53 |
##################################################################
|
| 54 |
/**
|
| 55 |
* Intercept the flag on a node and calculate if it's five or more.
|
| 56 |
* If so, launch a karma recalculation.
|
| 57 |
*
|
| 58 |
* @return
|
| 59 |
*
|
| 60 |
*/
|
| 61 |
function user_karma_offensive_flag_flag($event, $flag, $content_id, $account) {
|
| 62 |
if ($flag->name == "abuse_node") {
|
| 63 |
// Get the user id that has created the flagged content
|
| 64 |
$query = "SELECT u.uid FROM {users} u LEFT JOIN {node} n ON u.uid=n.uid WHERE n.nid = %d";
|
| 65 |
$user_id = db_result(db_query($query,$content_id));
|
| 66 |
// Make it more efficient, detect how many flag for this content
|
| 67 |
$query = "SELECT fc.count FROM {flag_counts} fc LEFT JOIN {flags} f ON fc.fid = f.fid WHERE fc.content_id = %d AND f.name = 'abuse_node'";
|
| 68 |
$flag_count = db_result(db_query($query,$content_id));
|
| 69 |
if ($flag_count >= variable_get('user_karma_peerreview_receive_offensive_number', '5')) {
|
| 70 |
user_karma_msg("Recalculating karma for $user_id");
|
| 71 |
user_karma_calculate_karma($user_id);
|
| 72 |
}
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
|
| 77 |
##################################################################
|
| 78 |
# HOOK TO RETURN A KARMA VALUE FRO OFFENSIVE NODES
|
| 79 |
##################################################################
|
| 80 |
/**
|
| 81 |
* Calculate karma on-the-fly calling every nodes created by the user.
|
| 82 |
*
|
| 83 |
* It takes from the database the multiplier for offensive flags.
|
| 84 |
*
|
| 85 |
* @return
|
| 86 |
* The value of the karma calculated
|
| 87 |
*/
|
| 88 |
function user_karma_offensive_flag_user_karma_partial($uid) {
|
| 89 |
|
| 90 |
// If the module is not active, then just always return 0
|
| 91 |
if ( ! variable_get('user_karma_peerreview_activate', FALSE)) {
|
| 92 |
user_karma_msg("This karma plugin is not active. returning '0'");
|
| 93 |
return 0;
|
| 94 |
}
|
| 95 |
|
| 96 |
user_karma_msg("In 'user_karma_offensive_flag_user_karma_partial'");
|
| 97 |
|
| 98 |
// Set the query
|
| 99 |
//$query = "SELECT SUM(vv.value) FROM {votingapi_vote} vv LEFT JOIN {node} n ON vv.content_id = n.nid WHERE vv.value > '0' AND vv.content_type in ($content_types_sql) AND vv.value_type in ($value_types_sql) AND n.uid = %d ". user_karma_sql_duration($days, "n.created");
|
| 100 |
$query = "SELECT COUNT(*) FROM {flags} f LEFT JOIN {flag_counts} fc ON f.fid = fc.fid LEFT JOIN {node} n ON fc.content_id=n.nid WHERE fc.count >= %d AND f.name = 'abuse_node' AND n.uid = %d";
|
| 101 |
|
| 102 |
user_karma_msg("Query: $query");
|
| 103 |
|
| 104 |
// Return the value
|
| 105 |
$result = db_result(
|
| 106 |
db_query($query,variable_get('user_karma_peerreview_receive_offensive_number', 5), $uid
|
| 107 |
)
|
| 108 |
);
|
| 109 |
|
| 110 |
$karma = 0 - (variable_get('user_karma_peerreview_receive_five_offensive', 1) * $result);
|
| 111 |
|
| 112 |
user_karma_msg("Returning offensive karma: $karma");
|
| 113 |
|
| 114 |
return $karma;
|
| 115 |
|
| 116 |
}
|
| 117 |
|
| 118 |
##################################################################
|
| 119 |
# HOOKS TO TRIGGER VOTE RECALCULATION IF CERTAIN EVENTS HAPPEN
|
| 120 |
##################################################################
|
| 121 |
|
| 122 |
/**
|
| 123 |
* This function is the super-hook for hook_votingapi_update and _insert,
|
| 124 |
* since it's identical as far as karma is concerned. If a node is
|
| 125 |
* voted, this function will trigger the karma recalculation.
|
| 126 |
*
|
| 127 |
* @param $op
|
| 128 |
* Not really used (yet). It tells the function if it was an
|
| 129 |
* insert or an update
|
| 130 |
* @param $v
|
| 131 |
* The vote object
|
| 132 |
* @value
|
| 133 |
* The vote value. This holds the "new" value of the vote if it was
|
| 134 |
an update, or the inserted value if it was an isert
|
| 135 |
* @return
|
| 136 |
* The karma amount
|
| 137 |
*
|
| 138 |
*/
|
| 139 |
function user_karma_offensive_flag_user_karma_vote_cast($op, $v, $value) {
|
| 140 |
|
| 141 |
user_karma_msg("In 'user_karma_peerreview_user_karma_vote_cast'");
|
| 142 |
|
| 143 |
// If the module is disabled, then just do nothing.
|
| 144 |
// Note that this could be done a little earlier, but it's just
|
| 145 |
// more convenient to do it here
|
| 146 |
if ( ! variable_get('user_karma_peerreview_activate', FALSE)) {
|
| 147 |
user_karma_msg("This karma plugin is not active. Not recalculating.");
|
| 148 |
return NULL;
|
| 149 |
}
|
| 150 |
|
| 151 |
// Load the node
|
| 152 |
|
| 153 |
if (
|
| 154 |
in_array($v[0]['content_type'], array('node') ) && //explode(',', variable_get('user_karma_peerreview_content_types', 'node') ) ) &&
|
| 155 |
in_array($v[0]['value_type'], array('points') ) //explode(',', variable_get('user_karma_peerreview_value_types', 'points') ) )
|
| 156 |
) {
|
| 157 |
user_karma_msg("IN the voting section");
|
| 158 |
$o = node_load($v[0]['content_id']);
|
| 159 |
$recipient_uid = $o->uid;
|
| 160 |
if ($recipient_uid) {
|
| 161 |
user_karma_msg("Recalculating karma for $recipient_uid");
|
| 162 |
user_karma_calculate_karma($recipient_uid);
|
| 163 |
}
|
| 164 |
}
|
| 165 |
else {
|
| 166 |
user_karma_msg("Content type $v[0]['content_type'] discarded");
|
| 167 |
}
|
| 168 |
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
*
|
| 173 |
* This function adds specific permission for this module
|
| 174 |
*
|
| 175 |
* @return
|
| 176 |
*/
|
| 177 |
function user_karma_offensive_flag_perm() {
|
| 178 |
return array('mark offensive');
|
| 179 |
}
|