-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate.module
1614 lines (1457 loc) · 51.5 KB
/
rate.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Rate module
*/
// Define display modes for the rate widget.
define('RATE_FULL', 1);
define('RATE_COMPACT', 2);
define('RATE_DISABLED', 3);
define('RATE_COMPACT_DISABLED', 5);
define('RATE_CLOSED', 4);
// Define display modes for node / comment bodies.
define('RATE_DISPLAY_DISABLE', 0);
define('RATE_DISPLAY_ABOVE_CONTENT', 1);
define('RATE_DISPLAY_BELOW_CONTENT', 2);
// Define modes for which rating to display
define('RATE_AVERAGE', 1);
define('RATE_USER', 2);
define('RATE_USER_OR_AVERAGE', 3);
// Define modes for behaviour with users w/o permission to vote.
define('RATE_NOPERM_REDIRECT_WITH_MESSAGE', 1);
define('RATE_NOPERM_REDIRECT_WITHOUT_MESSAGE', 2);
define('RATE_NOPERM_SHOW_DISABLED_WIDGET', 3);
define('RATE_NOPERM_HIDE_WIDGET', 4);
// Define constants for paths.
define('RATE_PATH_ADMIN_PAGE', 'admin/structure/rate');
define('RATE_PATH_ADMIN_PAGE_LIST', 'admin/structure/rate/list');
define('RATE_PATH_ADMIN_PAGE_ADD', 'admin/structure/rate/add');
define('RATE_PATH_ADMIN_PAGE_EDIT', 'admin/structure/rate/%/edit');
define('RATE_PATH_ADMIN_PAGE_DELETE', 'admin/structure/rate/%/delete');
define('RATE_PATH_ADMIN_PAGE_TEMPLATE', 'admin/structure/rate/%/template');
define('RATE_PATH_ADMIN_PAGE_SETTINGS', 'admin/structure/rate/settings');
define('RATE_PATH_AHAH', 'rate/vote/js');
define('RATE_PATH_RESULTS_PAGE', 'node/%node/rating');
define('RATE_PATH_LOGIN_PAGE', 'user/login-to-rate');
// Define constants for variable names.
define('RATE_VAR_WIDGETS', 'rate_widgets');
define('RATE_VAR_BOT_MINUTE_THRESHOLD', 'rate_bot_minute_threshold');
define('RATE_VAR_BOT_HOUR_THRESHOLD', 'rate_bot_hour_threshold');
define('RATE_VAR_BOT_RETROACTIVE', 'rate_bot_retroactive');
define('RATE_VAR_BOT_BOTSCOUT_KEY', 'rate_bot_botscout_key');
define('RATE_VAR_CRON_DELETE_LIMIT', 'rate_cron_delete_limit');
// Define constants for permission status.
define('RATE_PERMISSION_OK', 1);
define('RATE_PERMISSION_DISALLOWED_ROLE', 2);
define('RATE_PERMISSION_DISALLOWED_AUTHOR', 3);
/**
* Implements hook_perm().
*/
function rate_permission() {
return array(
'view rate results page' => array(
'title' => t('View rate results page'),
'description' => t('View rate results page'),
),
);
}
/**
* Implements hook_menu().
*/
function rate_menu() {
$menu = array();
$menu[RATE_PATH_ADMIN_PAGE] = array(
'title' => 'Rate widgets',
'description' => 'Manage rating widgets.',
'page callback' => 'rate_admin_page',
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'rate.admin.inc',
);
$menu[RATE_PATH_ADMIN_PAGE_LIST] = array(
'title' => 'List',
'page callback' => 'rate_admin_page',
'access arguments' => array('administer site configuration'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'rate.admin.inc',
'weight' => 0,
);
$menu[RATE_PATH_ADMIN_PAGE_ADD] = array(
'title' => 'Add widget',
'page callback' => 'backdrop_get_form',
'page arguments' => array('rate_widget_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
'file' => 'rate.admin.inc',
'weight' => 1,
);
$menu[RATE_PATH_ADMIN_PAGE_EDIT] = array(
'title' => 'Edit widget',
'page callback' => 'backdrop_get_form',
'page arguments' => array('rate_widget_form', 3),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
'file' => 'rate.admin.inc',
);
$menu[RATE_PATH_ADMIN_PAGE_DELETE] = array(
'title' => 'Delete widget',
'page callback' => 'backdrop_get_form',
'page arguments' => array('rate_widget_delete_form', 3),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
'file' => 'rate.admin.inc',
);
$menu[RATE_PATH_ADMIN_PAGE_TEMPLATE] = array(
'title' => 'Switch template',
'page callback' => 'backdrop_get_form',
'page arguments' => array('rate_choose_template_form', 3),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
'file' => 'rate.admin.inc',
);
$menu[RATE_PATH_AHAH] = array(
'page callback' => 'rate_vote_ahah',
'access callback' => 'rate_vote_ahah_access',
'type' => MENU_CALLBACK,
);
$menu[RATE_PATH_RESULTS_PAGE] = array(
'title' => 'Voting results',
'page callback' => 'rate_results_page',
'page arguments' => array(1),
'access callback' => 'rate_results_page_access',
'access arguments' => array('view rate results page', 1),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
'file' => 'rate.results.inc',
);
$menu[RATE_PATH_LOGIN_PAGE] = array(
'title' => 'Log in',
'page callback' => 'rate_login_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$menu[RATE_PATH_ADMIN_PAGE_SETTINGS] = array(
'title' => 'Settings',
'page callback' => 'backdrop_get_form',
'page arguments' => array('rate_settings_form'),
'access arguments' => array('administer site settings'),
'type' => MENU_LOCAL_TASK,
'file' => 'rate.admin.inc',
);
return $menu;
}
/**
* Implements hook_ctools_plugin_directory().
*
* It simply tells panels where to find the .inc files that define various
* args, contexts, content_types. In this case the subdirectories of
* ctools_plugin_example/panels are used.
*/
function rate_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/$plugin";
}
}
/**
* Get list of active widgets.
*
* @param string $content_type Node type
* @param string $type "node" or "comment"
* @return array
*/
function rate_get_active_widgets($content_type, $type, $view_mode = 'full') {
// TODO This variable was probably removed in Backdrop without replacement.
$widgets = config_get('rate.settings', RATE_VAR_WIDGETS, array());
$widgets = _rate_config_array_to_object($widgets);
$active = array();
foreach ($widgets as $widget_id => $widget) {
if ($content_type == 'node' && in_array($type, $widget->node_types)) {
isset($widget->teaser_display) or $widget->teaser_display = TRUE;
if ($view_mode == 'full' || $view_mode == 'embed' || $widget->teaser_display) {
$active[$widget_id] = $widget;
}
}
if ($content_type == 'comment' && in_array($type, $widget->comment_types)) {
$active[$widget_id] = $widget;
}
}
return $active;
}
/**
* Generate a widget.
*
* @param int $widget_id Widget id
* @param string $content_type "node" or "comment"
* @param int $content_id Node id (nid) or comment id (cid)
* @param bool $teaser
* @param bool $include_div
* @return array
*/
function rate_generate_widget($widget_id, $content_type, $content_id, $mode = RATE_FULL, $include_div = TRUE, $just_voted = FALSE, $displayed = NULL) {
global $user;
// Check input.
if (!is_numeric($widget_id) || empty($content_type) || !is_numeric($content_id)) {
return NULL;
}
// TODO This variable was probably removed in Backdrop without replacement.
$widgets = config_get('rate.settings', RATE_VAR_WIDGETS, array());
$widgets = _rate_config_array_to_object($widgets);
$widget = $widgets[$widget_id];
_rate_check_widget($widget);
// Determine if the user may vote.
$entities = entity_load($content_type, array($content_id));
$node = $entities[$content_id];
$permission_status = _rate_check_permissions($widget, $node);
// This option should be available, check for legacy.
isset($widget->noperm_behaviour) or $widget->noperm_behaviour = RATE_NOPERM_REDIRECT_WITH_MESSAGE;
isset($widget->displayed) or $widget->displayed = RATE_AVERAGE;
if ($permission_status != RATE_PERMISSION_OK && $widget->noperm_behaviour == RATE_NOPERM_HIDE_WIDGET) {
return NULL;
}
elseif ($permission_status == RATE_PERMISSION_DISALLOWED_ROLE && $widget->noperm_behaviour == RATE_NOPERM_SHOW_DISABLED_WIDGET) {
$mode = ($mode == RATE_COMPACT) ? RATE_COMPACT_DISABLED : RATE_DISABLED;
}
elseif ($permission_status == RATE_PERMISSION_DISALLOWED_AUTHOR) {
$mode = ($mode == RATE_COMPACT) ? RATE_COMPACT_DISABLED : RATE_DISABLED;
}
// Store the display mode in $widget, so it can be altered by hook_rate_widget.
$widget->mode = $mode;
// Let other modules alter the rate widget.
$context = array(
'content_type' => $content_type,
'content_id' => $content_id,
);
backdrop_alter('rate_widget', $widget, $context);
$div_id = "rate-$content_type-$content_id-$widget_id-$mode";
$theme_name = str_replace('-', '_', $widget->name);
$theme = array('rate_widget__' . $theme_name, 'rate_widget');
if (isset($widget->theme)) {
// Insert the general widget template suggestion at second position to go
// after the specific widget template and before the overall general
// template.
array_splice($theme, 1, 0, $widget->theme);
}
// Get voting results.
$results = rate_get_results($content_type, $content_id, $widget_id);
$results['empty'] = $results['count'] ? FALSE : TRUE;
// Handle interaction modes.
if (is_null($displayed)) {
$displayed = RATE_AVERAGE;
if ($just_voted) {
if ($widget->displayed_just_voted == RATE_USER) {
$displayed = RATE_USER;
}
}
else {
if (isset($results['user_vote']) && $widget->displayed == RATE_USER_OR_AVERAGE) {
$displayed = RATE_USER;
}
if ($widget->displayed == RATE_USER) {
$displayed = RATE_USER;
}
}
}
elseif ($displayed == RATE_USER_OR_AVERAGE && isset($results['user_vote'])) {
$displayed = RATE_USER;
}
elseif ($displayed == RATE_USER) {
$displayed = RATE_USER;
}
else {
$displayed = RATE_AVERAGE;
}
if ($displayed == RATE_USER) {
$results['rating'] = isset($results['user_vote']) ? $results['user_vote'] : 0;
if (!isset($results['user_vote'])) {
// We should display an empty rating in this case.
$results['empty'] = TRUE;
}
}
// Add generic javascript.
$dest = backdrop_get_destination();
backdrop_add_js(array(
'rate' => array(
'basePath' => url('rate/vote/js'),
'destination' => $dest['destination'],
),
), array('type' => 'setting'));
backdrop_add_js(backdrop_get_path('module', 'rate') . '/rate.js');
$links = array();
foreach ($widget->options as $option) {
// This name must be unique for all submit buttons across the page.
$id = "opt-$widget_id-$content_type-$content_id-{$option[0]}";
$token = rate_get_token($id);
if (isset($_GET['rate']) && $_GET['rate'] == $token) {
rate_save_vote($widget, $content_type, $content_id, $option[0]);
backdrop_goto($_GET['q'], array('query' => _rate_get_query()));
}
isset($widget->translate) or $widget->translate = TRUE;
$link_text = $widget->translate ? t($option[1]) : $option[1];
$link_href = url($_GET['q'], array('query' => _rate_get_query($token)));
if (isset($results['options']) && isset($results['options'][$option[0]])) {
$link_votes = $results['options'][$option[0]];
}
else {
$link_votes = NULL;
}
$disabled = FALSE;
if ($widget->mode == RATE_DISABLED || $widget->mode == RATE_COMPACT_DISABLED || $widget->mode == RATE_CLOSED) {
$disabled = TRUE;
}
$links[] = array(
'text' => $link_text,
'href' => $disabled ? NULL : $link_href,
'value' => $option[0],
'votes' => $link_votes,
);
}
if (isset($widget->css)) {
backdrop_add_css($widget->css);
}
if (isset($widget->js)) {
backdrop_add_js($widget->js);
}
if ($widget->mode == RATE_CLOSED && ($mode == RATE_COMPACT || $mode == RATE_DISABLED || $mode == RATE_COMPACT_DISABLED)) {
// The widget was closed by hook_rate_widget, but we want to display a compact or disabled
// widget. Set the mode back to compact / disabled in order to display a compact / disabled
// widget with disabled voting buttons.
$widget->mode = $mode == RATE_DISABLED ? RATE_DISABLED : RATE_COMPACT_DISABLED;
}
$display_options = array(
'description' => '',
'title' => check_plain($widget->title),
);
if (($widget->mode != RATE_COMPACT && $widget->mode != RATE_COMPACT_DISABLED) || $widget->description_in_compact) {
$display_options['description'] = check_plain($widget->description);
}
$widget_html = theme($theme, array('links' => $links, 'results' => $results, 'mode' => $widget->mode, 'just_voted' => $just_voted, 'content_type' => $content_type, 'content_id' => $content_id, 'display_options' => $display_options));
if ($include_div) {
$classes = array('rate-widget-' . $widget_id, 'rate-widget', 'clear-block');
$classes[] = $displayed == RATE_AVERAGE ? 'rate-average' : 'rate-user';
if (!empty($widget->template)) {
$classes[] = 'rate-widget-' . $widget->template;
}
// This token is required for the AHAH callback when using arbitrary values.
$id = "rate-$widget_id-$content_type-$content_id";
$token = _rate_get_token($id);
$classes[] = 'rate-' . $token;
return '<div class="' . implode(' ', $classes) . ' ' . $div_id . '" id="' . backdrop_html_id($div_id) . '">' .
$widget_html .
'</div>';
}
else {
// We do not want the div for AJAX callbacks, we would have 2 div's otherwise.
return $widget_html;
}
}
/**
* Get a query string used for links to the same page.
*
* @param string $token
* @return string
*/
function _rate_get_query($token = NULL) {
$args = $_GET;
if (isset($args['q'])) {
unset($args['q']);
}
if (isset($args['rate'])) {
unset($args['rate']);
}
if ($token) {
$args['rate'] = $token;
}
return $args;
}
/**
* Check if the current user has permission to vote on the given widget.
*
* @param object $widget
* @return bool
*/
function _rate_check_permissions($widget, $node = NULL) {
global $user;
// This option should be available, check for legacy.
isset($widget->roles) or $widget->roles = array();
isset($widget->allow_voting_by_author) or $widget->allow_voting_by_author = TRUE;
if (!$widget->allow_voting_by_author) {
// This option only affects authenticated users.
if ($node && $user->uid && $node->uid == $user->uid) {
return RATE_PERMISSION_DISALLOWED_AUTHOR;
}
}
$roles_selected = FALSE;
foreach ($widget->roles as $rid => $role) {
if ($role) {
$roles_selected = TRUE;
if (in_array($role, $user->roles)) {
return RATE_PERMISSION_OK;
}
}
}
if (!$roles_selected) {
// Allow voting for all roles if no roles are selected.
return RATE_PERMISSION_OK;
}
return RATE_PERMISSION_DISALLOWED_ROLE;
}
/**
* Get entity id for source translation.
*
* This function is safe for use with unsupported entity types.
*
* @param string $entity_type
* @param int $entity_id
* @return int
*/
function _rate_get_source_translation($entity_type, $entity_id) {
if ($entity_type == 'node' && module_exists('translation')) {
if (arg(0) == 'node' && arg(1) == $entity_id) {
// We are on the node page. Use node_load since the node is in static cache.
$entity_id = node_load($entity_id)->tnid;
}
else {
// We are not on the node page. Do not use node_load to prevent executing many useless queries.
$tnid = db_select('node', 'n')->fields('n', array('tnid'))->condition('n.nid', $entity_id)->execute()->fetchField();
if ($tnid) {
$entity_id = $tnid;
}
}
}
return $entity_id;
}
/**
* Get results for a voting widget.
*
* @param string $content_type "node" or "comment"
* @param int $content_id Node id (nid) or comment id (cid)
* @param int $widget_id Widget id
* @return array
*/
function rate_get_results($content_type, $content_id, $widget_id) {
global $user;
// TODO This variable was probably removed in Backdrop without replacement.
$widgets = config_get('rate.settings', RATE_VAR_WIDGETS, array());
$widgets = _rate_config_array_to_object($widgets);
$widget = $widgets[$widget_id];
$criteria = array(
'entity_type' => $content_type,
'entity_id' => $content_id,
'tag' => $widget->tag,
'value_type' => $widget->value_type,
);
// Check if we should use the source translation.
if ($widget->use_source_translation) {
$criteria['entity_id'] = _rate_get_source_translation($criteria['entity_type'], $criteria['entity_id']);
}
$results = votingapi_select_results($criteria);
$output = array('count' => 0);
if ($widget->value_type != 'option') {
// Set a default. Does not apply to options.
$output['rating'] = 0;
}
if ($widget->value_type == 'option') {
$output['options'] = array();
foreach ($widget->options as $option) {
$output['options'][$option[0]] = 0;
}
}
foreach ($results as $result) {
if ($widget->value_type == 'percent' && $result['function'] == 'average') {
$output['rating'] = $result['value'];
}
elseif ($widget->value_type == 'points' && $result['function'] == 'sum') {
$output['rating'] = $result['value'];
}
elseif ($result['function'] == 'count') {
$output['count'] = $result['value'];
}
elseif (preg_match('/^option\\-([\\-0-9]+)$/', $result['function'], $match)) {
$output['options'][$match[1]] = $result['value'];
$output['count'] += $result['value'];
}
}
// Check if this is a thumbs up / down voting, if so, calculate thumbs up / down percentages
if ($widget->value_type == 'points' && count($widget->options) == 2) {
// Votes down have a -1 value, votes up +1, $output['rating'] are the summed votes
$output['down'] = (int) ($output['count'] - $output['rating']) / 2;
$output['up'] = $output['count'] - $output['down'];
if ($output['count'] == 0) {
// If no one voted its 50-50
$output['up_percent'] = 50;
}
else {
$output['up_percent'] = (int) (($output['up'] * 100) / $output['count']);
}
$output['down_percent'] = 100 - $output['up_percent'];
}
$criteria += array(
'uid' => $user->uid,
'value_type' => $widget->value_type,
);
if (!$user->uid) {
$criteria += array('vote_source' => ip_address());
}
if ($user_vote = votingapi_select_votes($criteria)) {
// Check the anonymous window. We should not display this vote when its anonymous and casted too long ago.
// TODO This variable was probably removed in Backdrop without replacement.
$anonymous_window = config_get('rate.settings', 'votingapi_anonymous_window', 86400);
// Use a minimum of 5 seconds. Needed to get the anonymous vote directly after the user has voted.
if ($anonymous_window != -1) {
$anonymous_window = max(5, $anonymous_window);
}
if ($user->uid || $user_vote[0]['timestamp'] > REQUEST_TIME - $anonymous_window || $anonymous_window == -1) {
$output['user_vote'] = $user_vote[0]['value'];
if ($widget->value_type == 'option') {
foreach ($widget->options as $option) {
if ($option[0] == $user_vote[0]['value']) {
$output['user_vote'] = $option[1];
}
}
}
}
}
return $output;
}
/**
* Save a vote to the database.
*
* @param object $widget
* @param string $content_type
* @param int $content_id
* @param int $value
* @param bool $ahah
* @param bool $reset
*/
function rate_save_vote($widget, $content_type, $content_id, $value, $ahah = FALSE, $reset = FALSE) {
// Prevent votes from saved twice. This does not check for different widgets /
// content id's, but it's only possible to save a single vote per request for now,
// unless one uses the $reset flag with a TRUE value
static $saved = FALSE;
if ($saved && !$reset) {
return;
}
$saved = TRUE;
module_load_include('inc', 'rate', 'rate.bots');
if (rate_bots_is_blocked()) {
// TODO This variable was probably removed in Backdrop without replacement.
if (config_get('rate.settings', RATE_VAR_BOT_RETROACTIVE, TRUE)) {
$ip = ip_address();
if (!rate_bots_is_local($ip)) {
$queue = BackdropQueue::get('rate_delete_votes');
$queue->createQueue();
$queue->createItem($ip);
}
}
return;
}
// Check if we should use the source translation.
if ($widget->use_source_translation) {
$content_id = _rate_get_source_translation($content_type, $content_id);
}
// Determine if the user may vote.
$entities = entity_load($content_type, array($content_id));
$node = $entities[$content_id];
$permission_status = _rate_check_permissions($widget, $node);
// This option should be available, check for legacy.
isset($widget->noperm_behaviour) or $widget->noperm_behaviour = RATE_NOPERM_REDIRECT_WITH_MESSAGE;
if ($permission_status != RATE_PERMISSION_OK) {
switch ($widget->noperm_behaviour) {
case RATE_NOPERM_REDIRECT_WITH_MESSAGE:
// Redirects via AHAH have a destination param to prevent redirecting to the AJAX callback.
$destination = isset($_GET['destination']) ? $_GET['destination'] : $_GET['q'];
// Strip off protocol domain name to prevent long query strings in URL.
$destination = preg_replace('/http:\\/\\/[^\\/]+\\//', '', $destination);
$query = array('destination' => $destination);
if ($ahah) {
print url('user/login-to-rate', array('query' => $query, 'absolute' => TRUE));
module_invoke_all('exit') & exit;
}
else {
backdrop_goto('user/login-to-rate', array('query' => $query));
}
break;
case RATE_NOPERM_REDIRECT_WITHOUT_MESSAGE:
$destination = isset($_GET['destination']) ? $_GET['destination'] : $_GET['q'];
$query = array('destination' => $destination);
if ($ahah) {
print url('user', array('query' => $query, 'absolute' => TRUE));
module_invoke_all('exit') & exit;
}
else {
backdrop_goto('user', array('query' => $query));
}
break;
default:
return;
}
}
$votes = array(
'entity_type' => $content_type,
'entity_id' => $content_id,
'value_type' => $widget->value_type,
'value' => $value,
'tag' => $widget->tag,
);
$criteria = NULL;
// Call hook_rate_vote_alter().
$redirect = FALSE;
$save = TRUE;
$context = array(
'redirect' => &$redirect,
'save' => &$save,
'criteria' => &$criteria,
'widget' => $widget,
);
backdrop_alter('rate_vote', $votes, $context);
if ($save) {
votingapi_set_votes($votes, $criteria);
}
if ($redirect) {
preg_match('/^(.+?)(\\?(.*?))?(\\#(.*))?$/', $redirect, $parts);
$path = $parts[1];
if (isset($parts[3])) {
parse_str($parts[3], $query);
}
else {
$query = array();
}
$fragment = isset($parts[5]) ? $parts[5] : '';
if ($ahah) {
print url($path, array('absolute' => TRUE, 'query' => $query, 'fragment' => $fragment));
module_invoke_all('exit') & exit;
}
else {
backdrop_goto($path, $query, $fragment);
}
}
}
/*
Delete same up/down vote on second click
-----------------------------------------
Check to see if the vote already exists.
We also check to see if this feature is enabled.
If it exists and this feature is enabled then,
delete it and disable the saving of a new one.
*/
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function rate_rate_vote_alter($votes, &$context) {
// Make sure that this feature is enabled for the widget.
if (!$context['widget']->delete_vote_on_second_click) {
return;
}
$criteria = $context['criteria'];
// This might create problems for other modules doing this so soon.
// Which is why I don't pass in the $votes variable by reference,
// so that it doesn't get changed before other hooks get to run.
if (!empty($votes['entity_id'])) {
$votes = array($votes);
}
// Handle clearing out old votes if they exists.
if (empty($criteria)) {
// If the calling function didn't explicitly set criteria for vote deletion,
// build up the delete queries here.
foreach ($votes as $vote) {
$tmp = votingapi_current_user_identifier();
$tmp += $vote;
$tmp_v = votingapi_select_votes($tmp);
if (!empty($tmp_v)) {
votingapi_delete_votes($tmp_v);
$context['save'] = FALSE;
// We have to recalculate all the results for it.
votingapi_recalculate_results($vote['entity_type'], $vote['entity_id']);
}
}
}
elseif (is_array($criteria)) {
// The calling function passed in an explicit set of delete filters.
if (!empty($criteria['entity_id'])) {
$criteria = array($criteria);
}
foreach ($criteria as $c) {
$tmp_v = votingapi_select_votes($c);
if (!empty($tmp_v)) {
votingapi_delete_votes($tmp_v);
$context['save'] = FALSE;
// We have to recalculate all the results for it.
votingapi_recalculate_results($c['entity_type'], $c['entity_id']);
}
}
}
}
/**
* Implements hook_node_view().
*/
function rate_node_view($node, $view_mode, $langcode = '') {
if ($view_mode != 'rss') {
// Adding the form to the node view
$widgets = rate_get_active_widgets('node', $node->type, $view_mode);
foreach ($widgets as $widget_id => $widget) {
$widget_name = 'rate_' . $widget->name;
_rate_check_widget($widget);
$display_mode = $view_mode == 'teaser' ? $widget->teaser_display_mode : $widget->node_display_mode;
$widget_code = array(
'#weight' => $widget->node_display == RATE_DISPLAY_ABOVE_CONTENT ? -50 : 50,
'#markup' => rate_generate_widget($widget_id, 'node', $node->nid, $display_mode),
'#title' => check_plain($widget->title),
'#type' => 'item',
);
if ($widget->node_display == RATE_DISPLAY_DISABLE) {
$node->$widget_name = $widget_code;
}
else {
$node->content[$widget_name] = $widget_code;
}
}
}
}
/**
* Get the widget code to embed.
*
* @param object $node
* @param string $machine_name
* @param int $mode
*/
function rate_embed(&$node, $machine_name, $mode = RATE_FULL) {
// Adding the form to the node view
$widgets = rate_get_active_widgets('node', $node->type, 'full');
foreach ($widgets as $widget_id => $widget) {
if ($widget->name != $machine_name) {
continue;
}
return rate_generate_widget($widget_id, 'node', $node->nid, $mode);
}
return FALSE;
}
/**
* Access callback for AHAH callback.
*/
function rate_vote_ahah_access() {
$content_type = preg_replace('/[^\\w\\-]/', '', $_GET['content_type']);
$content_id = (int) $_GET['content_id'];
if ($content_type == 'node') {
// Check if the node exists and if the user has view access.
return ($node = node_load($content_id)) && node_access('view', $node);
}
if ($content_type == 'comment' && module_exists('comment')) {
// Check if the comment exists, is not hidden and if the user has view access on its node.
if (($comment = comment_load($content_id)) && ($node = node_load($comment->nid))) {
return $node->comment != COMMENT_NODE_HIDDEN && node_access('view', $node) && user_access('access comments');
}
}
// Rate only works on nodes and comments.
return FALSE;
}
/**
* Implements hook_comment_view_alter().
*/
function rate_comment_view_alter(&$comment) {
$node = $comment['#node'];
$widgets = rate_get_active_widgets('comment', $node->type);
foreach ($widgets as $widget_id => $widget) {
$widget_name = 'rate_' . $widget->name;
$widget_code = array(
'#weight' => $widget->comment_display == RATE_DISPLAY_ABOVE_CONTENT ? -50 : 50,
'#markup' => rate_generate_widget($widget_id, 'comment', $comment['#comment']->cid, $widget->comment_display_mode),
);
if ($widget->comment_display == RATE_DISPLAY_DISABLE) {
$comment['#' . $widget_name] = $widget_code;
}
else {
$comment[$widget_name] = $widget_code;
}
}
}
/**
* AHAH callback for the vote buttons.
*/
function rate_vote_ahah() {
$content_type = preg_replace('/[^\\w\\-]/', '', $_GET['content_type']);
$content_id = (int) $_GET['content_id'];
$widget_id = (int) $_GET['widget_id'];
$widget_mode = (int) $_GET['widget_mode'];
// TODO This variable was probably removed in Backdrop without replacement.
$widgets = config_get('rate.settings', RATE_VAR_WIDGETS, array());
$widgets = _rate_config_array_to_object($widgets);
$widget = $widgets[$widget_id];
// Process options.
foreach ($widget->options as $option) {
// This name must be unique for all submit buttons across the page, AHAH will fail otherwise.
$id = "opt-$widget_id-$content_type-$content_id-{$option[0]}";
$token = rate_get_token($id);
if (isset($_GET['token']) && $_GET['token'] == $token) {
rate_save_vote($widget, $content_type, $content_id, $option[0], TRUE);
}
}
// Process arbitrary value, used for sliders.
if (isset($_GET['value']) && $widget->value_type == 'percent') {
// Validate the token against the general widget token (found in the widget classes).
$id = "rate-$widget_id-$content_type-$content_id";
$token = _rate_get_token($id);
$value = (int) $_GET['value'];
if (isset($_GET['token']) && $_GET['token'] == $token && $value >= 0 && $value <= 100) {
rate_save_vote($widget, $content_type, $content_id, $value, TRUE);
}
}
print rate_generate_widget($widget_id, $content_type, $content_id, $widget_mode, TRUE, TRUE);
module_invoke_all('exit') & exit;
}
/**
* Get a template object by name.
*
* @param string $name
* @return object
*/
function _rate_get_template($name) {
$templates = array();
foreach (module_implements('rate_templates') as $module) {
if ($module_templates = module_invoke($module, 'rate_templates')) {
$templates = array_merge($module_templates, $templates);
}
}
return isset($templates[$name]) ? $templates[$name] : FALSE;
}
/**
* Implements hook_rate_templates().
*/
function rate_rate_templates() {
$templates = array();
$templates['thumbs_up'] = new stdClass();
$templates['thumbs_up']->value_type = 'points';
$templates['thumbs_up']->options = array(
array(1, 'up'),
);
$templates['thumbs_up']->theme = 'rate_template_thumbs_up';
$templates['thumbs_up']->css = backdrop_get_path('module', 'rate') . '/templates/thumbs-up/thumbs-up.css';
$templates['thumbs_up']->customizable = FALSE;
$templates['thumbs_up']->translate = TRUE;
$templates['thumbs_up']->template_title = t('Thumbs up');
$templates['thumbs_up_down'] = new stdClass();
$templates['thumbs_up_down']->value_type = 'points';
$templates['thumbs_up_down']->options = array(
array(1, 'up'),
array(-1, 'down'),
);
$templates['thumbs_up_down']->theme = 'rate_template_thumbs_up_down';
$templates['thumbs_up_down']->css = backdrop_get_path('module', 'rate') . '/templates/thumbs-up-down/thumbs-up-down.css';
$templates['thumbs_up_down']->customizable = FALSE;
$templates['thumbs_up_down']->translate = TRUE;
$templates['thumbs_up_down']->template_title = t('Thumbs up / down');
$templates['number_up_down'] = new stdClass();
$templates['number_up_down']->value_type = 'points';
$templates['number_up_down']->options = array(
array(1, '+1'),
array(-1, '-1'),
);
$templates['number_up_down']->theme = 'rate_template_number_up_down';
$templates['number_up_down']->css = backdrop_get_path('module', 'rate') . '/templates/number-up-down/number-up-down.css';
$templates['number_up_down']->customizable = FALSE;
$templates['number_up_down']->translate = TRUE;
$templates['number_up_down']->template_title = t('Number up / down');
$templates['fivestar'] = new stdClass();
$templates['fivestar']->value_type = 'percent';
$templates['fivestar']->options = array(
array(0, '1'),
array(25, '2'),
array(50, '3'),
array(75, '4'),
array(100, '5'),
);
$templates['fivestar']->theme = 'rate_template_fivestar';
$templates['fivestar']->css = backdrop_get_path('module', 'rate') . '/templates/fivestar/fivestar.css';
$templates['fivestar']->js = backdrop_get_path('module', 'rate') . '/templates/fivestar/fivestar.js';
$templates['fivestar']->customizable = TRUE;
$templates['fivestar']->translate = FALSE;
$templates['fivestar']->template_title = t('Fivestar');
$templates['emotion'] = new stdClass();
$templates['emotion']->value_type = 'option';
$templates['emotion']->options = array(
array(1, 'funny'),
array(2, 'mad'),
array(3, 'angry'),
);
$templates['emotion']->theme = 'rate_template_emotion';
$templates['emotion']->css = backdrop_get_path('module', 'rate') . '/templates/emotion/emotion.css';
$templates['emotion']->customizable = TRUE;
$templates['emotion']->translate = TRUE;
$templates['emotion']->template_title = t('Emotion');
$templates['yesno'] = new stdClass();
$templates['yesno']->value_type = 'option';
$templates['yesno']->options = array(
array(1, 'yes'),
array(2, 'no'),
);
$templates['yesno']->theme = 'rate_template_yesno';
$templates['yesno']->css = backdrop_get_path('module', 'rate') . '/templates/yesno/yesno.css';
$templates['yesno']->customizable = TRUE;
$templates['yesno']->translate = TRUE;
$templates['yesno']->template_title = t('Yes / no');
return $templates;