/[drupal]/contributions/modules/PeerReview/user_karma_peerreview.module
ViewVC logotype

Contents of /contributions/modules/PeerReview/user_karma_peerreview.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Mon Jun 29 13:19:47 2009 UTC (4 months, 4 weeks ago) by ingo86
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +203 -0 lines
File MIME type: text/x-php
Added two buttons to add or remove the default data for the peer review system. The add button has now a function that set the roles automatically and add the permission for the mark offensive flag,
1 <?php
2 // $Id:
3
4 /**
5 * @file
6 */
7
8 /**
9 * Implementation of hook_help().
10 */
11 function user_karma_peerreview_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 the complete peer review platform based upon user karma');
16 }
17 }
18
19 /**
20 * Implementation of hook_menu.
21 */
22 function user_karma_peerreview_menu() {
23 $items = array();
24
25 $items['admin/settings/user_karma/plugins/peerreview'] = array(
26 'title' => t('Peer Review'),
27 'description' => t('Settings for the plugin that enables the complete peer review platform'),
28 'page callback' => 'drupal_get_form',
29 'page arguments' => array('user_karma_peerreview_admin_settings'),
30 'access arguments' => array('administer site configuration'),
31 'type' => MENU_LOCAL_TASK,
32 );
33
34 $items['user_karma_peerreview/add_defaults'] = array(
35 'title' => 'User karma peerreview - Add defaults data',
36 'page callback' => 'drupal_get_form',
37 'page arguments' => array('user_karma_peerreview_add_defaults_confirm'),
38 'access arguments' => array('administer site configuration'),
39 'type' => MENU_CALLBACK,
40 );
41
42 $items['user_karma_peerreview/remove_defaults'] = array(
43 'title' => 'User karma peerreview - Remove defaults data',
44 'page callback' => 'drupal_get_form',
45 'page arguments' => array('user_karma_peerreview_remove_defaults_confirm'),
46 'access arguments' => array('administer site configuration'),
47 'type' => MENU_CALLBACK,
48 );
49
50 // That's all!
51 return $items;
52 }
53
54 ##################################################################
55 # START OF SETTINGS FORM
56 ##################################################################
57
58
59 /**
60 * Alter the two configuration forms for user_karma_receive_vote_comment and
61 * user_karma_receive_vote_node
62 *
63 * @return
64 * The aletered config form
65 */
66 /*function user_karma_peerreview_form_alter(&$form, $form_state, $form_id) {
67 switch ($form_id) {
68 case 'user_karma_receive_vote_node_admin_settings':
69 unset($form['user_karma_receive_vote_node_karma_multiplier']);
70 break;
71 case 'user_karma_receive_vote_comment_admin_settings':
72 unset($form['user_karma_receive_vote_comment_karma_multiplier']);
73 break;
74 }
75 }*/
76
77
78 /**
79 * Return the configuration form for the peer review system.
80 *
81 * @return
82 * The config form
83 */
84 function user_karma_peerreview_admin_settings() {
85
86 $form['defaults'] = array(
87 '#type' => 'fieldset',
88 '#description' => t('Default data consists in a bunch off karma based roles and permission. Click <a href="http://www.drupal.org" target="_blank">here</a> for details.'),
89 '#collapsible' => FALSE,
90 '#collapsed' => FALSE,
91 );
92
93 $form['defaults']['user_karma_add_defaults'] = array(
94 '#weight' => -5,
95 '#type' => 'submit',
96 '#value' => t('Add default data for user karma peerreview'),
97 );
98
99 $form['defaults']['user_karma_remove_defaults'] = array(
100 '#weight' => -4,
101 '#type' => 'submit',
102 '#value' => t('Remove default data for user karma peerreview'),
103 );
104
105 $form['user_karma_peerreview_activate'] = array(
106 '#type' => 'checkbox',
107 '#title' => t('Activate this karma method'),
108 '#description' => t('This module will only really do anything when this box is ticket. If you decide to disable it, remember to do a karma mass recalculation.'),
109 '#default_value' => variable_get('user_karma_peerreview_activate', FALSE),
110 );
111
112 $form['gain_karma'] = array(
113 '#type' => 'fieldset',
114 '#title' => t('Gain Karma'),
115 '#collapsible' => TRUE,
116 '#collapsed' => FALSE,
117 );
118
119 $form['gain_karma']['user_karma_peerreview_receive_positive_post'] = array(
120 '#type' => 'textfield',
121 '#title' => t('How much karma will be assigned for each positive vote received on a post'),
122 '#size' => 4,
123 '#maxlength' => 4,
124 '#default_value' => variable_get('user_karma_peerreview_receive_positive_post', 10),
125 );
126
127 $form['lose_karma'] = array(
128 '#type' => 'fieldset',
129 '#title' => t('Lose Karma'),
130 '#collapsible' => TRUE,
131 '#collapsed' => FALSE,
132 );
133
134 $form['lose_karma']['user_karma_peerreview_receive_negative_post'] = array(
135 '#type' => 'textfield',
136 '#title' => t('How much karma will be assigned for each negative vote received on a post'),
137 '#size' => 4,
138 '#maxlength' => 4,
139 '#default_value' => variable_get('user_karma_peerreview_receive_negative_post', 2),
140 );
141
142 $form['lose_karma']['user_karma_peerreview_mark_negative_post'] = array(
143 '#type' => 'textfield',
144 '#title' => t('How much karma will be removed for each negative vote marked'),
145 '#size' => 4,
146 '#maxlength' => 4,
147 '#default_value' => variable_get('user_karma_peerreview_mark_negative_post', 1),
148 );
149
150 return system_settings_form($form);
151 }
152
153
154 /**
155 * Implementation of hook_validate().
156 * Used to validate user_karma_admin_settings() but here is used to change the
157 * flow in case the user click on the "Add defaults" or "Remove defaults" button
158 */
159 function user_karma_peerreview_admin_settings_validate($form, &$form_state) {
160
161 // This hook will sneakly intercept the roles recalculation request
162 // and will redirect accordingly to the roles mass-recalculation
163 // function
164 if ($form_state['values']['op'] == t('Add default data for user karma peerreview')) {
165 drupal_goto('user_karma_peerreview/add_defaults');
166 }
167
168 // This hook will sneakly intercept the roles recalculation request
169 // and will redirect accordingly to the roles mass-recalculation
170 // function
171 if ($form_state['values']['op'] == t('Remove default data for user karma peerreview')) {
172 drupal_goto('user_karma_peerreview/remove_defaults');
173 }
174 }
175
176 /**
177 * This is a simple confirmation step to make sure that the
178 * user really wants to go ahead
179 *
180 * @return
181 * The confirmation form
182 */
183 function user_karma_peerreview_add_defaults_confirm() {
184 return confirm_form(
185 array(),
186 t('Are you sure you want to add default data (roles and permissions) about the peerreview system?'),
187 'admin/settings/user_karma/plugins/peerreview',
188 t('Default data will be added to your database to enable the full peerreview system.'),
189 t('Add default data'), t('Cancel')
190 );
191 }
192
193 /**
194 * This is a simple confirmation step to make sure that the
195 * user really wants to go ahead
196 *
197 * @return
198 * The confirmation form
199 */
200 function user_karma_peerreview_remove_defaults_confirm() {
201 return confirm_form(
202 array(),
203 t('Are you sure you want to remove default data (roles and permissions) about the peerreview system?'),
204 'admin/settings/user_karma/plugins/peerreview',
205 t('Default data will be removed from your database to disable the preset peerreview system.'),
206 t('Remove default data'), t('Cancel')
207 );
208 }
209
210 /**
211 * At this point, the answer was confirmed. So, the default data will be added
212 *
213 * @param $form_id
214 * The form id
215 * @param $form
216 * The form
217 * @return
218 * A redirection to the module's configuration screen
219 */
220 function user_karma_peerreview_add_defaults_confirm_submit($form, &$form_state) {
221 if ($form_state['values']['confirm']) {
222 user_karma_peerreview_add_defaults();
223 drupal_set_message(t('The default data are installed!'));
224 //return 'admin/settings/user_karma/plugins/peerreview';
225 $form_state['redirect'] = 'admin/settings/user_karma/plugins/peerreview';
226 }
227 }
228
229 /**
230 * At this point, the answer was confirmed. So, the default data will be removed
231 *
232 * @param $form_id
233 * The form id
234 * @param $form
235 * The form
236 * @return
237 * A redirection to the module's configuration screen
238 */
239 function user_karma_peerreview_remove_defaults_confirm_submit($form, &$form_state) {
240 if ($form_state['values']['confirm']) {
241 user_karma_peerreview_remove_defaults();
242 drupal_set_message(t('The default data are installed!'));
243 //return 'admin/settings/user_karma/plugins/peerreview';
244 $form_state['redirect'] = 'admin/settings/user_karma/plugins/peerreview';
245 }
246 }
247
248 ##################################################################
249 # HOOK TO RETURN A KARMA VALUE
250 ##################################################################
251 /**
252 * Calculate karma on-the-fly calling every nodes created by the user.
253 *
254 * It takes from the database the multiplier for positive and negative votes to
255 * assign a different value for everyone.
256 *
257 * @return
258 * The value of the karma calculated
259 */
260 function user_karma_peerreview_user_karma_partial($uid) {
261
262 // If the module is not active, then just always return 0
263 if ( ! variable_get('user_karma_peerreview_activate', FALSE)) {
264 user_karma_msg("This karma plugin is not active. returning '0'");
265 return 0;
266 }
267
268
269 user_karma_msg("In 'user_karma_peerreview_user_karma_partial'");
270
271 // Get the number of days
272 //$days = variable_get('user_karma_receive_vote_node_point_duration', 90);
273
274 //user_karma_msg("$days");
275
276 $content_types_array = array('node');
277 $value_types_array = array('points');
278
279 $content_types_sql = db_placeholders($content_types_array,'text');
280 $value_types_sql = db_placeholders($value_types_array,'text');
281
282 // Calculating positive karma
283 $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");
284 user_karma_msg("Query: $query");
285 // Return the value, multiplied by the multiplier for positive votes!
286 $positive_karma = db_result(
287 db_query(
288 $query,
289 array_merge( $content_types_array, $value_types_array, array($uid) )
290 )
291 ) * variable_get('user_karma_peerreview_receive_positive_post', 1) ;
292 user_karma_msg("Returning positive karma: $positive_karma");
293
294 // Calculating negative karma
295 $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");
296 user_karma_msg("Query: $query");
297 // Return the value, multiplied by the multiplier for negative votes!
298 $negative_karma = db_result(
299 db_query(
300 $query,
301 array_merge( $content_types_array, $value_types_array, array($uid) )
302 )
303 ) * variable_get('user_karma_peerreview_receive_negative_post', 1) ;
304 user_karma_msg("Returning negative karma: $negative_karma");
305
306 // Calculating karma for the negative votes assigned
307 $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 vv.uid = %d ". user_karma_sql_duration($days, "n.created");
308 user_karma_msg("Query: $query");
309 // Return the value, multiplied by the multiplier for negative votes assigned!
310 $removed_for_negative_vote = db_result(
311 db_query(
312 $query,
313 array_merge( $content_types_array, $value_types_array, array($uid) )
314 )
315 ) * variable_get('user_karma_peerreview_mark_negative_post', 1) ;
316 user_karma_msg("Returning negative votes assigned karma: $removed_for_negative_vote");
317
318 $karma = $positive_karma + $negative_karma + $removed_for_negative_vote;
319
320 return $karma;
321
322 #$query = "SELECT SUM(vv.value) FROM {votingapi_vote} vv LEFT JOIN {comments} c ON vv.content_id = c.cid WHERE vv.content_type = 'comment' AND vv.value_type = 'points' AND c.uid = %d ". user_karma_sql_duration("c.timestamp");
323 #$karma_from_comments = db_result(db_query($query, $recipient_uid, time()));
324 }
325
326 ##################################################################
327 # HOOKS TO TRIGGER VOTE RECALCULATION IF CERTAIN EVENTS HAPPEN
328 ##################################################################
329
330 /**
331 * This function is the super-hook for hook_votingapi_update and _insert,
332 * since it's identical as far as karma is concerned. If a node is
333 * voted, this function will trigger the karma recalculation.
334 *
335 * @param $op
336 * Not really used (yet). It tells the function if it was an
337 * insert or an update
338 * @param $v
339 * The vote object
340 * @value
341 * The vote value. This holds the "new" value of the vote if it was
342 an update, or the inserted value if it was an isert
343 * @return
344 * The karma amount
345 *
346 */
347 function user_karma_peerreview_user_karma_vote_cast($op, $v, $value) {
348
349 user_karma_msg("In 'user_karma_peerreview_user_karma_vote_cast'");
350
351 // If the module is disabled, then just do nothing.
352 // Note that this could be done a little earlier, but it's just
353 // more convenient to do it here
354 if ( ! variable_get('user_karma_peerreview_activate', FALSE)) {
355 user_karma_msg("This karma plugin is not active. Not recalculating.");
356 return NULL;
357 }
358
359 // Load the node
360
361 if (
362 in_array($v[0]['content_type'], array('node') ) && //explode(',', variable_get('user_karma_peerreview_content_types', 'node') ) ) &&
363 in_array($v[0]['value_type'], array('points') ) //explode(',', variable_get('user_karma_peerreview_value_types', 'points') ) )
364 ) {
365 user_karma_msg("IN the voting section");
366 $o = node_load($v[0]['content_id']);
367 $recipient_uid = $o->uid;
368 if ($recipient_uid) {
369 user_karma_msg("Recalculating karma for $recipient_uid");
370 user_karma_calculate_karma($recipient_uid);
371 user_karma_msg("Recalculating karma for $v[0]['uid']");
372 user_karma_calculate_karma($v[0]['uid']);
373 }
374 }
375 else {
376 user_karma_msg("Content type $v[0]['content_type'] discarded");
377 }
378 }
379
380 ##################################################################
381 # FUNCTION THAT ADD/REMOVE THE DEFAULT DATA
382 ##################################################################
383
384 /**
385 * This is the function that add the default data inside the system.
386 * Default data are:
387 * - a present of karma based roles
388 * - a preset of permissions connected to them
389 *
390 * @return
391 * Nothing
392 */
393 function user_karma_peerreview_add_defaults() {
394
395 // Just in case. This could take a little while...
396 set_time_limit(300);
397
398 // Preset roles
399 $roles = array(
400 array ('user_karma_00', '0', '14', '0'),
401 array ('user_karma_01', '15', '49', '0'),
402 array ('user_karma_02', '50', '99', '0'),
403 array ('user_karma_03', '100', '199', '0'),
404 array ('user_karma_04', '200', '249', '0'),
405 array ('user_karma_05', '250', '499', '0'),
406 array ('user_karma_06', '500', '749', '0'),
407 array ('user_karma_07', '750', '1999', '0'),
408 array ('user_karma_08', '2000', '2999', '0'),
409 array ('user_karma_09', '3000', '4999', '0'),
410 array ('user_karma_10', '5000', '9999', '0'),
411 array ('user_karma_11', '10000', '', '0'),
412 );
413
414 foreach ($roles as $role) {
415 // Adding new roles inside role table
416 db_query("INSERT INTO {role} (name) VALUES ('%s')", $role[0]);
417 $rid = db_last_insert_id('role', 'rid');
418
419 // Create array with new roles eligible for user_karma
420 $eligible_roles[$rid] = $rid;
421
422 // Adding new variables with the karma values for each role
423 variable_set('user_karma_role_'.$rid.'_from', $role[1]);
424 variable_set('user_karma_role_'.$rid.'_to', $role[2]);
425 variable_set('user_karma_role_'.$rid.'_type', $role[3]);
426
427 // Adding enabled roles to flags module: permissions to "mark offensive" from 15 to n
428 if ($role[1] >= '15') {
429 if (!isset($offensive_roles)) {
430 $offensive_roles = $offensive_roles.$rid;
431 }
432 else {
433 $offensive_roles = $offensive_roles.','.$rid;
434 }
435 }
436 }
437 // Set new roles eligible for user_karma
438 variable_set('user_karma_eligible_roles', $eligible_roles);
439
440 // Saving offensive flag settings to the database
441 db_query("UPDATE {flags} SET roles = '%s' WHERE name = 'abuse_node'", $offensive_roles);
442 }
443
444 /**
445 * This is the function that removes default data from the system.
446 * Default data are:
447 * - a present of karma based roles
448 * - a preset of permissions connected to them
449 *
450 * @return
451 * Nothing
452 */
453 function user_karma_peerreview_remove_defaults() {}
454 ?>

  ViewVC Help
Powered by ViewVC 1.1.2