/[drupal]/contributions/modules/nodevote/nodevote.module
ViewVC logotype

Contents of /contributions/modules/nodevote/nodevote.module

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


Revision 1.32 - (show annotations) (download) (as text)
Wed Oct 15 16:52:54 2008 UTC (13 months, 1 week ago) by kbahey
Branch: MAIN
CVS Tags: HEAD
Changes since 1.31: +9 -4 lines
File MIME type: text/x-php
SA-2008-064
1 <?php
2
3 //$Id: nodevote.module,v 1.31.2.1 2008/10/15 16:52:12 kbahey Exp $
4
5 // Copyright 2005 Khalid Baheyeldin http://2bits.com
6
7 define('NODEVOTE_MIN_SCORE', 1);
8 define('NODEVOTE_MAX_SCORE', 10);
9 define('NODEVOTE_VOTE_DEFAULT', 0);
10 define('NODEVOTE_TYPE', 'nodevote_type_');
11 define('NODEVOTE_PERM_VIEW', 'view nodevote');
12 define('NODEVOTE_PERM_USE', 'use nodevote');
13 define('NODEVOTE_VOTE_OWN_NODE', 'nodevote_vote_own_node');
14 define('NODEVOTE_CHANGE_VOTE', 'nodevote_change_vote');
15 define('NODEVOTE_RESULT_VOTED', 'nodevote_result_voted');
16 define('NODEVOTE_RESULT_DISPLAY_PAGE', 'nodevote_result_display_page');
17 define('NODEVOTE_RESULT_DISPLAY_TEASER', 'nodevote_result_display_teaser');
18 define('NODEVOTE_DAILY_THRESHOLD', 'nodevote_daily_threshold');
19 define('NODEVOTE_BLOCK_COUNT', 'nodevote_block_count');
20 define('NODEVOTE_HIGHEST_BLOCK_THRESHOLD', 'nodevote_highest_block_threshold');
21 define('NODEVOTE_USERPOINTS', 'nodevote_userpoints');
22
23 function nodevote_help($path, $arg) {
24 $output = '';
25
26 switch ($path) {
27 case 'admin/settings/nodevote':
28 $output = t('This module provides the facility to vote on certain node types using several criteria.');
29 break;
30 }
31 return $output;
32 }
33
34 function nodevote_menu() {
35 $items = array();
36
37 $items['nodevote/%node/add'] = array(
38 'page callback' => 'nodevote_page',
39 'page arguments' => array(1),
40 'title' => t('Nodevote add'),
41 'access callback' => 'user_access',
42 'access arguments' => array(NODEVOTE_PERM_USE),
43 'type' => MENU_CALLBACK
44 );
45
46 $items['nodevote/%node'] = array(
47 'page callback' => 'nodevote_page',
48 'page arguments' => array(1),
49 'title' => t('Nodevote view'),
50 'access callback' => 'user_access',
51 'access arguments' => array(NODEVOTE_PERM_VIEW),
52 'type' => MENU_CALLBACK
53 );
54
55 $items['admin/settings/nodevote'] = array(
56 'title' => t('Nodevote'),
57 'description' => t('Change settings for voting on nodes.'),
58 'page callback' => 'drupal_get_form',
59 'page arguments' => array('nodevote_admin_settings'),
60 'access callback' => 'user_access',
61 'access arguments' => array('administer site configuration'),
62 'type' => MENU_NORMAL_ITEM, // optional
63 );
64
65 return $items;
66 }
67
68 function nodevote_perm() {
69 return array (NODEVOTE_PERM_VIEW, NODEVOTE_PERM_USE);
70 }
71
72 function nodevote_admin_settings() {
73 $set = 'types';
74 $form[$set] = array(
75 '#type' => 'fieldset',
76 '#title' => t('Enable voting for these node types:'),
77 );
78
79 foreach(node_get_types() as $type => $name) {
80 $form[$set][NODEVOTE_TYPE . $type] = array(
81 '#type' => 'checkbox',
82 '#title' => $name->name,
83 '#return_value' => 1,
84 '#default_value' => variable_get(NODEVOTE_TYPE . $type, '0'),
85 );
86 }
87
88 $set = 'setting';
89 $form[$set] = array(
90 '#type' => 'fieldset',
91 '#title' => t('Settings'),
92 );
93
94 $_display = array(
95 0 => t('Text'),
96 1 => t('Stars'),
97 2 => t('Both')
98 );
99 $form[$set][NODEVOTE_RESULT_DISPLAY_PAGE]= array(
100 '#type' => 'radios',
101 '#title' => t('Vote result display (page view)'),
102 '#default_value' => variable_get(NODEVOTE_RESULT_DISPLAY_PAGE, 0),
103 '#options' => $_display,
104 '#description' => t('Select which format the results of the vote will be displayed in when in node view.'));
105
106 $_display = array(
107 0 => t('Text'),
108 1 => t('Stars'),
109 2 => t('Both'),
110 3 => t('None'));
111 $form[$set][NODEVOTE_RESULT_DISPLAY_TEASER]= array(
112 '#type' => 'radios',
113 '#title' => t('Vote result display (teaser view)'),
114 '#default_value' => variable_get(NODEVOTE_RESULT_DISPLAY_TEASER, 0),
115 '#options' => $_display,
116 '#description' => t('Select which format the results of the vote will be displayed in when in teaser view.'));
117
118 $set='advance';
119 $form[$set] = array(
120 '#type' => 'fieldset',
121 '#title' => t('Advanced Settings'),
122 );
123 $form[$set][NODEVOTE_RESULT_VOTED] = array(
124 '#type' => 'checkbox',
125 '#title' => t('Show vote results only if user has voted on node.'),
126 '#return_value' => 1,
127 '#default_value' => variable_get(NODEVOTE_RESULT_VOTED, 0),
128 );
129 $form[$set][NODEVOTE_CHANGE_VOTE] = array(
130 '#type' => 'checkbox',
131 '#title' => t('Allow user to vote again.'),
132 '#return_value' => 1,
133 '#default_value' => variable_get(NODEVOTE_CHANGE_VOTE, 0),
134 );
135 $form[$set][NODEVOTE_VOTE_OWN_NODE] = array(
136 '#type' => 'checkbox',
137 '#title' => t('Allow user to vote on his own node.'),
138 '#return_value' => 1,
139 '#default_value' => variable_get(NODEVOTE_VOTE_OWN_NODE, 0),
140 );
141 $form[$set][NODEVOTE_DAILY_THRESHOLD] = array(
142 '#type' => 'select',
143 '#title' => t('Daily threshold'),
144 '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 100, 250)),
145 '#default_value' => variable_get(NODEVOTE_DAILY_THRESHOLD, 10),
146 '#description' => t('The maximum number of votes a user can cast in a 24 hour period.'),
147 );
148 $form[$set][NODEVOTE_BLOCK_COUNT] = array(
149 '#type' => 'select',
150 '#title' => t('Node per block'),
151 '#options' => drupal_map_assoc(array(3, 5, 7, 10, 15, 20, 25)),
152 '#default_value' => variable_get(NODEVOTE_BLOCK_COUNT, 3),
153 '#description' => t('Number of nodes to show in blocks.'),
154 );
155 $form[$set][NODEVOTE_HIGHEST_BLOCK_THRESHOLD] = array(
156 '#type' => 'select',
157 '#title' => t('Highest block vote threshold'),
158 '#options' => drupal_map_assoc(array(3, 5, 7, 10, 15, 20, 25)),
159 '#default_value' => variable_get(NODEVOTE_HIGHEST_BLOCK_THRESHOLD, 3),
160 '#description' => t('Nodes must have the given number of votes before they show in the highest block.'),
161 );
162
163 return system_settings_form($form);
164 }
165
166 /**
167 * Check if the current voter has cast his maximum daily votes.
168 * The user is allowed to vote if he did not exceed the threshold.
169 *
170 * @param $uid
171 * The the ID of the voting user.
172 * @param $threshold
173 * The maximum number votes allowed (per user) in a 24-hour period.
174 * @return
175 * True if the user did not exceed the threshold. False otherwise.
176 */
177 function _nodevote_within_threshold($uid, $threshold) {
178 $number = db_result(db_query("SELECT count(uid) FROM {nodevote} WHERE uid = '%s' AND timestamp > '%d'", $uid, time() - 86400));
179 return ($number < $threshold ? TRUE : FALSE);
180 }
181
182 function nodevote_link($type, $node, $teaser = FALSE) {
183 if ($type == 'node' && $teaser) {
184 if (_nodevote_is_votable($node)) {
185 $links['nodevote_link'] = array(
186 'title' => t('Vote on it'),
187 'href' => "node/$node->nid"
188 );
189 }
190 }
191 return $links;
192 }
193
194 function nodevote_user($op) {
195 global $user;
196
197 switch($op) {
198 case 'delete':
199 db_query('DELETE FROM {nodevote} WHERE uid = %d', $user->uid);
200 break;
201 }
202 }
203
204 function nodevote_block($op = 'list', $delta = 0, $edit = array()) {
205 $num = variable_get(NODEVOTE_BLOCK_COUNT, 3);
206 $threshold = variable_get(NODEVOTE_HIGHEST_BLOCK_THRESHOLD, 3);
207
208 $block_title = array();
209 $block_title[] = t('Most voted for nodes');
210 $block_title[] = t('Highest rated nodes');
211 $block_title[] = t('Most voting users');
212
213 switch ($op) {
214 case 'list':
215 $blocks[0]['info'] = $block_title[0];
216 $blocks[1]['info'] = $block_title[1];
217 $blocks[2]['info'] = $block_title[2];
218 return $blocks;
219
220 case 'view':
221 if (user_access(NODEVOTE_PERM_VIEW)) {
222 switch ($delta) {
223 case 0:
224 $title = $block_title[$delta];
225 $sql = 'SELECT n.nid, n.title, COUNT(*) AS num_votes
226 FROM {node} n, {nodevote} nv
227 WHERE n.nid = nv.nid
228 GROUP BY n.nid
229 ORDER BY num_votes DESC';
230 $content = node_title_list(db_query_range($sql, 0, $num));
231 break;
232
233 case 1:
234 $title = $block_title[$delta];
235 $sql = 'SELECT n.nid, n.title, AVG(vote) AS av_avg
236 FROM {node} n, {nodevote} nv
237 WHERE n.nid = nv.nid
238 GROUP BY n.nid
239 HAVING COUNT(*) >= %d
240 ORDER BY av_avg DESC';
241 $content = node_title_list(db_query_range($sql, $threshold, 0, $num));
242 break;
243
244 case 2:
245 $title = $block_title[$delta];
246 $result = db_query_range('SELECT nv.uid, u.name, COUNT(*) AS num_votes
247 FROM {nodevote} nv, {users} u
248 WHERE u.uid = nv.uid
249 GROUP BY nv.uid
250 ORDER BY num_votes DESC', 0, $num);
251 while ($account = db_fetch_object($result)) {
252 $items[] = $account;
253 }
254 $content = theme('user_list', $items);
255 break;
256 }
257
258 $block['subject'] = $title;
259 $block['content'] = $content;
260
261 return $block;
262 }
263 }
264 }
265
266 function nodevote_page($node) {
267 global $user;
268 $edit = $_POST;
269
270 $op = arg(2);
271
272 switch($op) {
273 case 'add':
274 $vote = $edit['vote'];
275 if (_nodevote_validate_vote($vote)) {
276 if (_nodevote_user_voted($user->uid, $node->nid)){
277 $result = db_query("UPDATE {nodevote} SET vote = %d WHERE uid = %d AND nid = %d", $vote, $user->uid , $node->nid);
278 }
279 else {
280 // check to see if the user has voted too many times today
281 if(_nodevote_within_threshold($user->uid, variable_get(NODEVOTE_DAILY_THRESHOLD, 10))) {
282 // log the vote
283 $result = db_query("INSERT INTO {nodevote} VALUES (%d, %d, %d, %d)", $user->uid, $node->nid, $vote, time());
284 // do the userpoints dance
285 nodevote_do_userpoints();
286 }
287 else {
288 // if they've voted too many times today, show a message
289 $output = t("You can only vote %number times in 24 hours. Please try again later.", array('%number' => variable_get(NODEVOTE_DAILY_THRESHOLD, 10)));
290 drupal_set_message($output, 'error');
291 }
292 $o = l(t('Back to node'), 'node/' . $node->nid);
293 }
294 drupal_goto("node/" . $node->nid);
295 }
296 else {
297 drupal_set_message(t('You must select a valid vote score.'), 'error');
298 }
299 $o = l(t('Back to node'), 'node/' . $node->nid);
300 break;
301
302 case 'view':
303 default:
304 if (_nodevote_vote_is_visible($node)) {
305 $vote_data = _nodevote_get_vote_data($node->nid);
306 $o = theme('nodevote_display_vote', $vote_data);
307 }
308 break;
309 }
310
311 print theme('page', $o);
312 }
313
314 function nodevote_nodeapi(&$node, $op, $teaser, $page) {
315 global $user;
316 switch($op) {
317 case 'load':
318 if (variable_get(NODEVOTE_TYPE . $node->type, '0')) {
319 if (_nodevote_vote_is_visible($node)) {
320 $extra['nodevote']->vote_display = 1;
321 }
322 if (_nodevote_is_votable($node)) {
323 $extra['nodevote']->vote_do = 1;
324 }
325 }
326 return $extra;
327
328 case 'view':
329 if (variable_get(NODEVOTE_TYPE . $node->type, 0)) {
330 if ($node->nodevote->vote_display) {
331 $vote_data = _nodevote_get_vote_data($node->nid, $page);
332 $node->content['nodevote_display'] = array(
333 '#value' => theme('nodevote_display_vote', $vote_data),
334 '#weight' => 10,
335 );
336 }
337 }
338
339 if ($page) {
340 if ($node->nodevote->vote_do) {
341 $vote = theme('nodevote_do_vote', $node);
342 $node->content['nodevote'] = array(
343 '#value' => $vote,
344 );
345 }
346 }
347 break;
348
349 case 'alter':
350 if(!$page) {
351 if (variable_get(NODEVOTE_RESULT_DISPLAY_TEASER, 0) != 3) {
352 $data = _nodevote_get_vote_data($node->nid, $page);
353 $node->content['nodevote_result'] = array(
354 '#value' => theme('nodevote_display_vote', $vote_data),
355 '#weight' => 10,
356 );
357 }
358 }
359 break;
360
361 case 'delete':
362 db_query('DELETE FROM {nodevote} WHERE nid = %d', $node->nid);
363 break;
364 }
365 }
366
367 function _nodevote_user_voted($uid, $nid) {
368 if (db_result(db_query('SELECT count(nid) FROM {nodevote} WHERE uid = %d AND nid = %d', $uid, $nid))) {
369 return TRUE;
370 }
371 else {
372 return FALSE;
373 }
374 }
375
376 function _nodevote_get_vote_data($nid, $page = TRUE) {
377 $result = db_fetch_array(db_query('SELECT COUNT(*) AS votes, AVG(vote) AS score FROM {nodevote} WHERE nid = %d', $nid));
378
379 $score = $result['score'];
380 $votes = $result['votes'];
381
382 if ($page) {
383 $vote_display = variable_get(NODEVOTE_RESULT_DISPLAY_PAGE, 0);
384 }
385 else {
386 $vote_display = variable_get(NODEVOTE_RESULT_DISPLAY_TEASER, 0);
387 }
388
389 return array('score' => $score, 'votes' => $votes, 'vote_display' => $vote_display);
390 }
391
392 function theme_nodevote_display_vote($vote_data) {
393 $score = $vote_data['score'];
394 $votes = $vote_data['votes'];
395 $vote_display = $vote_data['vote_display'];
396
397 switch($vote_display) {
398 case 1:
399 $result = _nodevote_display_stars($votes, $score);
400 break;
401
402 case 2:
403 $result = _nodevote_display_stars($votes, $score);
404 $result .= _nodevote_display_number($votes, $score);
405 break;
406
407 case 0:
408 default:
409 $result = _nodevote_display_number($votes, $score);
410 break;
411 }
412
413 $output .= '<div id="nodevote result">';
414 $output .= '<h3>' . t('Vote Result') . '</h3>';
415 $output .= $result;
416 $output .= '</div>';
417
418 return $output;
419 }
420
421 function _nodevote_display_number($votes, $score) {
422 return '<br/>' . t('Score: @score, Votes: @votes', array('@votes' => $votes, '@score' => number_format($score, 1)));
423 }
424
425 function _nodevote_display_stars($votes, $score) {
426 global $base_path;
427 for ($i = 1; $i< NODEVOTE_MAX_SCORE+1; $i++) {
428 if ($score >= $i) {
429 $val = 'on';
430 $alt = '+';
431 }
432 else {
433 $val = 'off';
434 $alt = '-';
435 }
436 $stars .='<img src="' . $base_path . drupal_get_path('module', 'nodevote') . '/star_' . $val . '.png" alt="' . $alt . '" />';
437 };
438
439 return $stars;
440 }
441
442 function _nodevote_is_votable($node) {
443 global $user;
444
445 $ret = FALSE;
446
447 // Is user logged in?
448 if ($user->uid) {
449 // Is node type votable?
450 if (variable_get(NODEVOTE_TYPE . $node->type, 0)) {
451 // Does the user has permission to vote?
452 if (user_access(NODEVOTE_PERM_USE)) {
453 // Is the node posted by someone else?
454 if (($node->uid != $user->uid) || (variable_get(NODEVOTE_VOTE_OWN_NODE, 0))) {
455 // Did the user already vote on this node?
456 if ((!_nodevote_user_voted($user->uid, $node->nid)) || (variable_get(NODEVOTE_CHANGE_VOTE, 0))){
457 $ret = TRUE;
458 }
459 }
460 }
461 }
462 }
463 return $ret;
464 }
465
466 function _nodevote_vote_is_visible($node) {
467 global $user;
468 $ret = FALSE;
469 if (user_access(NODEVOTE_PERM_VIEW)) {
470 if (!variable_get(NODEVOTE_RESULT_VOTED, 0)) {
471 $ret = TRUE;
472 }
473 else {
474 if (_nodevote_user_voted($user->uid, $node->nid)) {
475 $ret = TRUE;
476 }
477 }
478 }
479 return $ret;
480 }
481
482 /**
483 * Implementation of hook_theme().
484 */
485 function nodevote_theme() {
486 return array(
487 'nodevote_display_vote' => array(
488 'arguments' => array('vote_data' => NULL),
489 ),
490 'nodevote_do_vote' => array(
491 'arguments' => array('node' => NULL),
492 ),
493 );
494 }
495
496 function theme_nodevote_do_vote($node) {
497 return drupal_get_form('nodevote_rate_form', $node);
498 }
499
500 function nodevote_rate_form($form_state, $node) {
501 global $user;
502 $score = array();
503 $r = 0;
504 $score[] = t('Select');
505 if (_nodevote_user_voted($user->uid, $node->nid)){
506 $r = db_result(db_query("SELECT vote FROM {nodevote} WHERE uid = %d AND nid = %d" , $user->uid ,$node->nid));
507 $score[] =$r;
508 }
509
510 for($j=NODEVOTE_MIN_SCORE; $j<NODEVOTE_MAX_SCORE+1; $j++) {
511 $score[$j] = $j;
512 }
513
514 $set='rate';
515 $form[$set] = array(
516 '#type' => 'fieldset',
517 '#title' => t('Votes'),
518 '#value' => t('<br>Please rate this node. 1 = worst score, 10 = best score'),
519 '#prefix' => '<div id="nodevote vote">',
520 '#suffix' => '</div>',
521 );
522
523 $form[$set]['vote'] = array(
524 '#type' => 'select',
525 '#title' => t('Score'),
526 '#default_value' => $score[$r],
527 '#options' => $score,
528 );
529 $form[$set]['button'] = array(
530 '#type' => 'submit',
531 '#value' => t('Vote'),
532 );
533
534 $form['#method'] = 'post';
535 $form['#action'] = url('nodevote/' . $node->nid . '/add');
536 $form['#attributes'] = array('class' => 'nodevote-form');
537
538 return $form;
539 }
540
541 function _nodevote_validate_vote($vote) {
542 if (!is_numeric($vote)) {
543 return FALSE;
544 }
545
546 if ($vote >= NODEVOTE_MIN_SCORE && $vote <= NODEVOTE_MAX_SCORE) {
547 return TRUE;
548 }
549
550 return FALSE;
551 }
552
553 function nodevote_userpoints($op, $points = 0, $uid = 0, $event = '') {
554 switch($op) {
555 case 'setting':
556 $group = 'nodevote';
557 $form[$group] = array(
558 '#type' => 'fieldset',
559 '#collapsible' => TRUE,
560 '#collapsed' => TRUE,
561 '#title' => t('!Points for nodevote', userpoints_translation()),
562 );
563
564 $form[$group][NODEVOTE_USERPOINTS] = array(
565 '#type' => 'textfield',
566 '#title' => t('!Points for voting using nodevote', userpoints_translation()),
567 '#default_value' => variable_get(NODEVOTE_USERPOINTS, 0),
568 '#size' => 5,
569 '#maxlength' => 5,
570 );
571 return $form;
572 }
573 }
574
575 function nodevote_do_userpoints() {
576 global $user;
577
578 if (module_exists('userpoints')) {
579 $points = variable_get(NODEVOTE_USERPOINTS, 0);
580 userpoints_userpointsapi('points', $points, $user->uid, 'nodevote');
581 }
582 }
583

  ViewVC Help
Powered by ViewVC 1.1.2