| 1 |
<?php
|
| 2 |
// $Id:
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_help().
|
| 10 |
*/
|
| 11 |
function user_karma_accepted_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 - enable a user to mark as accepted an answer giving a determined amount of karma to the author.');
|
| 16 |
}
|
| 17 |
}
|
| 18 |
|
| 19 |
##################################################################
|
| 20 |
# START OF SETTINGS FORM
|
| 21 |
##################################################################
|
| 22 |
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Add a new setting to the user_karma_peerreview settings panel.
|
| 26 |
*
|
| 27 |
* @return
|
| 28 |
* The altered config form
|
| 29 |
*/
|
| 30 |
function user_karma_accepted_flag_form_alter(&$form, $form_state, $form_id) {
|
| 31 |
switch ($form_id) {
|
| 32 |
case 'user_karma_peerreview_admin_settings':
|
| 33 |
$form['gain_karma']['user_karma_peerreview_receive_accepted_answer'] = array(
|
| 34 |
'#type' => 'textfield',
|
| 35 |
'#title' => t('How much karma will be assigned for an <em>ACCEPTED</em> answer'),
|
| 36 |
'#size' => 4,
|
| 37 |
'#maxlength' => 4,
|
| 38 |
'#default_value' => variable_get('user_karma_peerreview_receive_accepted_answer', 15),
|
| 39 |
);
|
| 40 |
$form['gain_karma']['user_karma_peerreview_mark_accepted_answer'] = array(
|
| 41 |
'#type' => 'textfield',
|
| 42 |
'#title' => t('How much karma will be assigned when you mark as <em>ACCEPTED</em> an answer to your questions'),
|
| 43 |
'#size' => 4,
|
| 44 |
'#maxlength' => 4,
|
| 45 |
'#default_value' => variable_get('user_karma_peerreview_mark_accepted_answer', 2),
|
| 46 |
);
|
| 47 |
break;
|
| 48 |
}
|
| 49 |
}
|
| 50 |
|
| 51 |
##################################################################
|
| 52 |
# HOOK TO INTERCEPT THE FLAGGING (ONLY THE ONE MARKED AS ACCEPTED)
|
| 53 |
##################################################################
|
| 54 |
/**
|
| 55 |
* Intercept the flag on a node to check inside this thread if
|
| 56 |
* someone is already marked as accepted.
|
| 57 |
* If so, substitute it with this one.
|
| 58 |
*
|
| 59 |
* @return
|
| 60 |
*
|
| 61 |
*/
|
| 62 |
function user_karma_accepted_flag_flag($event, $flag, $content_id, $account) {
|
| 63 |
if ($flag->name == "flag_accepted") {
|
| 64 |
|
| 65 |
// Take off the question_id
|
| 66 |
$query = "SELECT nid FROM {riat_relationships_descendants} WHERE did = %d";
|
| 67 |
$question_nid = db_result(db_query($query,$content_id));
|
| 68 |
|
| 69 |
// Take off every son
|
| 70 |
$query = "SELECT did FROM {riat_relationships_descendants} WHERE nid = %d";
|
| 71 |
$result = db_query($query,$question_nid);
|
| 72 |
while ($descendants_nid = db_fetch_object($result)) {
|
| 73 |
// Take off every flagged son except what i am flagging
|
| 74 |
if ($descendants_nid->did != $content_id) {
|
| 75 |
// Delete every flagged accepted son
|
| 76 |
if (db_result(db_query("SELECT COUNT(*) FROM {flag_content} WHERE content_id = %d", $descendants_nid->did))) {
|
| 77 |
db_query("DELETE FROM {flag_content} WHERE content_id = %d and fid = %d", $descendants_nid->did, $flag->fid);
|
| 78 |
db_query("DELETE FROM {flag_counts} WHERE content_id = %d and fid = %d", $descendants_nid->did, $flag->fid);
|
| 79 |
}
|
| 80 |
}
|
| 81 |
}
|
| 82 |
|
| 83 |
// Get the user id that has created the flagged answer to start the karma recalculation for him
|
| 84 |
$query = "SELECT u.uid FROM {users} u LEFT JOIN {node} n ON u.uid=n.uid WHERE n.nid = %d";
|
| 85 |
$user_id = db_result(db_query($query,$content_id));
|
| 86 |
user_karma_msg("Recalculating karma for $user_id");
|
| 87 |
user_karma_calculate_karma($user_id);
|
| 88 |
|
| 89 |
// Start karma recalculation for the question owner
|
| 90 |
user_karma_msg("Recalculating karma for $account->uid");
|
| 91 |
user_karma_calculate_karma($account->uid);
|
| 92 |
|
| 93 |
// Get the user id that has flagged the content, to recalculate his karma, if different from the above
|
| 94 |
//$query = "SELECT u.uid FROM {users} u LEFT JOIN {node} n ON u.uid=n.uid WHERE n.nid = %d";
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
##################################################################
|
| 99 |
# HOOK BASIC SETTINGS
|
| 100 |
##################################################################
|
| 101 |
/**
|
| 102 |
* Mark as accepted permitted only for the question creator.
|
| 103 |
*
|
| 104 |
* @return
|
| 105 |
*/
|
| 106 |
function user_karma_accepted_flag_link_alter(&$flag, $links) {
|
| 107 |
|
| 108 |
// Extract the uid that has the ability to mark as accepted
|
| 109 |
global $user;
|
| 110 |
$query = "SELECT n.uid FROM {riat_relationships_descendants} rrd LEFT JOIN {node} n ON rrd.nid=n.nid WHERE rrd.did = %d";
|
| 111 |
$question_creator = db_result(db_query($query,$links->nid));
|
| 112 |
|
| 113 |
// Unset flag for every uid that doesn't have the permission to flag
|
| 114 |
if ($user->uid != $question_creator) {
|
| 115 |
unset($flag['flag-flag_accepted']);
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
| 119 |
##################################################################
|
| 120 |
# HOOK TO RETURN A KARMA VALUE
|
| 121 |
##################################################################
|
| 122 |
/**
|
| 123 |
* Calculate karma on-the-fly calling every nodes created by the user.
|
| 124 |
*
|
| 125 |
* It takes from the database two multiplier:
|
| 126 |
* - multiplier to who post the answer
|
| 127 |
* - multiplier to who accept the answer
|
| 128 |
*
|
| 129 |
* Attention: to get the second multiplier working you need to disable
|
| 130 |
* global flag on answers!
|
| 131 |
*
|
| 132 |
* @return
|
| 133 |
* The value of the karma calculated
|
| 134 |
*/
|
| 135 |
function user_karma_accepted_flag_user_karma_partial($uid) {
|
| 136 |
|
| 137 |
// If the module is not active, then just always return 0
|
| 138 |
if ( ! variable_get('user_karma_peerreview_activate', FALSE)) {
|
| 139 |
user_karma_msg("This karma plugin is not active. returning '0'");
|
| 140 |
return 0;
|
| 141 |
}
|
| 142 |
|
| 143 |
user_karma_msg("In 'user_karma_accepted_flag_user_karma_partial'");
|
| 144 |
|
| 145 |
// Set the query to extract the number of accepted answer from a user (who is the owner of the flagged content?)
|
| 146 |
// Don't need to extract the node_type as this type of flags is working only on answers
|
| 147 |
$query = "SELECT COUNT(*) FROM {flags} f LEFT JOIN {flag_content} fc ON f.fid = fc.fid LEFT JOIN {node} n ON fc.content_id=n.nid WHERE f.name = 'flag_accepted' AND n.uid = %d";
|
| 148 |
user_karma_msg("Query for accepted answer received by user for his post: $query");
|
| 149 |
$result = db_result(db_query($query, $uid));
|
| 150 |
$karma_receive_accepted = $result * variable_get('user_karma_peerreview_receive_accepted_answer', 15) ;
|
| 151 |
user_karma_msg("Accepted answers karma: $karma_receive_accepted");
|
| 152 |
|
| 153 |
// Set the query to extract the number of questions made by user with accepted answer marked (who flagged the content?)
|
| 154 |
// It's a bonus for users that accept answers to his question
|
| 155 |
// Don't need to extract the node_type as this type of flags is working only on answers
|
| 156 |
$query = "SELECT COUNT(*) FROM {flags} f LEFT JOIN {flag_content} fc ON f.fid = fc.fid LEFT JOIN {node} n ON fc.content_id=n.nid WHERE f.name = 'flag_accepted' AND fc.uid = %d";
|
| 157 |
user_karma_msg("Query for accepted answer made by user for his question: $query");
|
| 158 |
$result = db_result(db_query($query, $uid));
|
| 159 |
$karma_mark_accepted = $result * variable_get('user_karma_peerreview_mark_accepted_answer', 2) ;
|
| 160 |
user_karma_msg("Accepted answers karma: $karma_mark_accepted");
|
| 161 |
|
| 162 |
$karma = $karma_mark_accepted + $karma_receive_accepted;
|
| 163 |
return $karma;
|
| 164 |
|
| 165 |
}
|
| 166 |
|
| 167 |
##################################################################
|
| 168 |
# HOOKS TO TRIGGER VOTE RECALCULATION IF CERTAIN EVENTS HAPPEN
|
| 169 |
##################################################################
|
| 170 |
|
| 171 |
/**
|
| 172 |
* This function is the super-hook for hook_votingapi_update and _insert,
|
| 173 |
* since it's identical as far as karma is concerned. If a node is
|
| 174 |
* voted, this function will trigger the karma recalculation.
|
| 175 |
*
|
| 176 |
* @param $op
|
| 177 |
* Not really used (yet). It tells the function if it was an
|
| 178 |
* insert or an update
|
| 179 |
* @param $v
|
| 180 |
* The vote object
|
| 181 |
* @value
|
| 182 |
* The vote value. This holds the "new" value of the vote if it was
|
| 183 |
an update, or the inserted value if it was an isert
|
| 184 |
* @return
|
| 185 |
* The karma amount
|
| 186 |
*
|
| 187 |
*/
|
| 188 |
function user_karma_accepted_flag_user_karma_vote_cast($op, $v, $value) {
|
| 189 |
|
| 190 |
user_karma_msg("In 'user_karma_peerreview_user_karma_vote_cast'");
|
| 191 |
|
| 192 |
// If the module is disabled, then just do nothing.
|
| 193 |
// Note that this could be done a little earlier, but it's just
|
| 194 |
// more convenient to do it here
|
| 195 |
if ( ! variable_get('user_karma_peerreview_activate', FALSE)) {
|
| 196 |
user_karma_msg("This karma plugin is not active. Not recalculating.");
|
| 197 |
return NULL;
|
| 198 |
}
|
| 199 |
|
| 200 |
// Load the node
|
| 201 |
|
| 202 |
if (
|
| 203 |
in_array($v[0]['content_type'], array('node') ) && //explode(',', variable_get('user_karma_peerreview_content_types', 'node') ) ) &&
|
| 204 |
in_array($v[0]['value_type'], array('points') ) //explode(',', variable_get('user_karma_peerreview_value_types', 'points') ) )
|
| 205 |
) {
|
| 206 |
user_karma_msg("IN the voting section");
|
| 207 |
$o = node_load($v[0]['content_id']);
|
| 208 |
$recipient_uid = $o->uid;
|
| 209 |
if ($recipient_uid) {
|
| 210 |
user_karma_msg("Recalculating karma for $recipient_uid");
|
| 211 |
user_karma_calculate_karma($recipient_uid);
|
| 212 |
}
|
| 213 |
}
|
| 214 |
else {
|
| 215 |
user_karma_msg("Content type $v[0]['content_type'] discarded");
|
| 216 |
}
|
| 217 |
|
| 218 |
}
|
| 219 |
?>
|