-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserpoints.module
More file actions
1782 lines (1633 loc) · 59.1 KB
/
userpoints.module
File metadata and controls
1782 lines (1633 loc) · 59.1 KB
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
// Copyright 2005-2007 Khalid Baheyeldin http://2bits.com
define('USERPOINTS_PERM_VIEW', 'view userpoints');
//The permission(PERM_USE) was removed from use as per #158490 it'll remain
//out until code exists to use the permission (jredding 12/26/2007)
define('USERPOINTS_PERM_USE', 'use userpoints');
define('USERPOINTS_PERM_ADMIN', 'admin userpoints');
define('USERPOINTS_TRANS_UCPOINTS', 'userpoints_trans_ucpoints');
define('USERPOINTS_TRANS_LCPOINTS', 'userpoints_trans_lcpoints');
define('USERPOINTS_TRANS_LCPOINT', 'userpoints_trans_lcpoint');
define('USERPOINTS_TRANS_UNCAT', 'userpoints_trans_uncat');
define('USERPOINTS_STATUS', 'userpoints_status');
define('USERPOINTS_POINTS_MODERATION', 'userpoints_points_moderation');
define('USERPOINTS_TXN_STATUS_APPROVED', 0);
define('USERPOINTS_TXN_STATUS_PENDING', 1);
define('USERPOINTS_TXN_STATUS_DECLINED', 2);
define('USERPOINTS_EXPIRY_DESCRIPTION', 'userpoints_expiry_description');
define('USERPOINTS_EXPIREON_DATE', 'userpoints_expireon_date');
define('USERPOINTS_EXPIREAFTER_DATE', 'userpoints_expireafter_date');
define('USERPOINTS_DISPLAY_MESSAGE', 'userpoints_display_message');
define('USERPOINTS_REPORT_USERCOUNT', 'userpoints_report_usercount');
define('USERPOINTS_REPORT_LIMIT', 'userpoints_report_limit');
define('USERPOINTS_REPORT_DISPLAYZERO', 'userpoints_report_displayzero');
define('USERPOINTS_CATEGORY_NAME', 'Userpoints');
define('USERPOINTS_CATEGORY_DEFAULT_VID', 'userpoints_category_default_vid');
define('USERPOINTS_CATEGORY_DEFAULT_TID', 'userpoints_category_default_tid');
/**
* Purpose: Returns an array of common translation placeholders
*/
function userpoints_translation() {
static $trans;
if (!isset($trans)) {
$trans = array(
'!Points' => variable_get(USERPOINTS_TRANS_UCPOINTS, 'Points'),
'!points' => variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'),
'!point' => variable_get(USERPOINTS_TRANS_LCPOINT, 'point'),
'!Uncategorized' => variable_get(USERPOINTS_TRANS_UNCAT, 'Uncategorized'),
);
}
return $trans;
}
/*
* Purpose: Returns an array of possible transaction statuses
*/
function userpoints_txn_status() {
return array(
USERPOINTS_TXN_STATUS_APPROVED => t('Approved'),
USERPOINTS_TXN_STATUS_PENDING => t('Pending'),
USERPOINTS_TXN_STATUS_DECLINED => t('Declined'),
);
}
/**
* Implementation of hook_help().
*/
function userpoints_help($section) {
switch ($section) {
case 'admin/settings/userpoints':
$output = t('Configure userpoints moderation and branding translation', userpoints_translation());
break;
case 'admin/help#userpoints':
$output = t('Users earn !points as they post nodes, comments, and vote on nodes', userpoints_translation());
}
return $output;
}
/**
* Implementation of hook_menu().
*/
function userpoints_menu() {
$items = array();
$items['admin/settings/userpoints'] = array(
'title' => '!Points settings',
'title arguments' => userpoints_translation(),
'page callback' => 'drupal_get_form',
'page arguments' => array('userpoints_admin_settings'),
'access arguments' => array(USERPOINTS_PERM_ADMIN),
'type' => MENU_NORMAL_ITEM
);
$items['admin/user/userpoints'] = array(
'title' => '!Points',
'title arguments' => userpoints_translation(),
'description' => 'Manage !points',
'page callback' => 'userpoints_admin_points',
'access arguments' => array(USERPOINTS_PERM_ADMIN)
);
$items['admin/user/userpoints/list'] = array(
'title' => 'List',
'title arguments' => userpoints_translation(),
'description' => 'List users by points',
'access arguments' => array(USERPOINTS_PERM_VIEW),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -2,
);
$items['admin/user/userpoints/moderate'] = array(
'title' => 'Moderation',
'title arguments' => userpoints_translation(),
'description' => 'Review points in moderation',
'page callback' => 'userpoints_admin_points',
'access arguments' => array(USERPOINTS_PERM_ADMIN),
'type' => MENU_LOCAL_TASK,
'weight' => -1,
);
$items['admin/user/userpoints/add'] = array(
'title' => 'Add',
'description' => 'Admin add/delete userpoints',
'page callback' => 'drupal_get_form',
'page arguments' => array('userpoints_admin_txn'),
'access arguments' => array(USERPOINTS_PERM_ADMIN),
'type' => MENU_LOCAL_TASK,
'weight' => 0,
);
$items['admin/user/userpoints/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('userpoints_admin_txn'),
'access arguments' => array(USERPOINTS_PERM_ADMIN),
'type' => MENU_CALLBACK
);
$items['admin/user/userpoints/approve'] = array(
'title' => 'Approve Userpoints',
'page callback' => 'userpoints_admin_approve',
'access arguments' => array(USERPOINTS_PERM_ADMIN),
'type' => MENU_CALLBACK
);
$items['admin/user/userpoints/decline'] = array(
'title' => 'Approve Userpoints',
'page callback' => 'userpoints_admin_approve',
'access arguments' => array(USERPOINTS_PERM_ADMIN),
'type' => MENU_CALLBACK
);
$items['userpoints'] = array(
'title' => 'Users by !points',
'title arguments' => userpoints_translation(),
'page callback' => 'userpoints_list_users',
'access' => array(USERPOINTS_PERM_VIEW),
'type' => MENU_NORMAL_ITEM,
);
$items['myuserpoints'] = array(
'title' => 'My !points',
'title arguments' => userpoints_translation(),
'page callback' => 'userpoints_my_userpoints',
'access callback' => 'userpoints_access_my_points',
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/**
* Checks if user can access their points - used via hook_menu().
*
* @return true if user has permissions to view userpoints and if the user is logged in
*/
function userpoints_access_my_points() {
global $user;
return (user_access(USERPOINTS_PERM_VIEW) && user_is_logged_in());
}
/**
* Implementation of hook_perm().
*/
function userpoints_perm() {
return array(USERPOINTS_PERM_VIEW, USERPOINTS_PERM_ADMIN);
}
/**
* Implementation of hook_theme().
*/
function userpoints_theme() {
$themes = array();
$themes['userpoints_list_users'] = array(
'arguments' => array(
'header',
'rows',
'tid',
'pager_limit',
),
);
$themes['userpoints_list_users_header'] = array(
);
$themes['userpoints_list_users_row'] = array(
'arguments' => array(
'row'
),
);
$themes['userpoints_my_userpoints'] = array(
'arguments' => array(
'args',
'header',
'rows',
),
);
return $themes;
}
/**
* menu callback for settings form.
*/
function userpoints_admin_settings() {
$form = array();
$group = 'status';
$form[$group] = array(
'#type' => 'fieldset',
'#title' => t('Moderation'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => -1,
);
$form[$group][USERPOINTS_POINTS_MODERATION] = array(
'#type' => 'radios',
'#title' => t('Transaction status'),
'#default_value' => variable_get(USERPOINTS_POINTS_MODERATION, 0),
'#options' => array(t('Approved'), t('Moderated')),
'#description' => t('Select whether all !points should be approved automatically, or moderated, and require admin approval', userpoints_translation()),
);
$group = 'renaming';
$form[$group] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Points branding'),
);
$form[$group][USERPOINTS_TRANS_UCPOINTS] = array(
'#type' => 'textfield',
'#title' => t('Word to use in the interface for the upper case plural word !Points', userpoints_translation()),
'#default_value' => variable_get(USERPOINTS_TRANS_UCPOINTS, 'Points'),
'#size' => 20,
'#maxlength' => 20,
);
$form[$group][USERPOINTS_TRANS_LCPOINTS] = array(
'#type' => 'textfield',
'#title' => t('Word to use in the interface for the lower case plural word !points', userpoints_translation()),
'#default_value' => variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'),
'#size' => 20,
'#maxlength' => 20,
);
$form[$group][USERPOINTS_TRANS_LCPOINT] = array(
'#type' => 'textfield',
'#title' => t('Word to use in the interface for the lower case singular word !point', userpoints_translation()),
'#default_value' => variable_get(USERPOINTS_TRANS_LCPOINT, 'point'),
'#size' => 20,
'#maxlength' => 20,
);
$form[$group][USERPOINTS_TRANS_UNCAT] = array(
'#type' => 'textfield',
'#title' => t('Word to use for the uncategorized category'),
'#default_value' => variable_get(USERPOINTS_TRANS_UNCAT, 'Uncategorized'),
'#size' => 20,
'#maxlength' => 20,
);
$group = "Points expiration";
$form[$group] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('!Points expiration', userpoints_translation()),
'#description' => t('These settings affect new points only, they are not retroactive!
Point expiration depends upon cron.
'),
);
$form[$group][USERPOINTS_EXPIREAFTER_DATE] = array(
'#type' => 'select',
'#title' => t('Expire !points after', userpoints_translation()),
'#description' => t('Once points have been obtained by the user
they will expire according to this setting'),
'#options' => expiry_dates(),
'#default_value' => variable_get(USERPOINTS_EXPIREAFTER_DATE, NULL),
);
/**
* If the expiration date is earlier than today
* new points will last forever. Although this may be desirable
* it could also be an oversight so we'll display a message
* to the administrator
*
*/
$warning="";
if (userpoints_date_to_timestamp(variable_get(USERPOINTS_EXPIREON_DATE, array('day' => 1, 'month' => 1, 'year' => 1900))) < time()) {
$warning = '<br /><strong>'. t('This setting will not take affect, date must be in the future') .'</strong>';
}
$form[$group][USERPOINTS_EXPIREON_DATE] = array(
'#type' => 'date',
'#title' => t('Expire !points on this date', userpoints_translation()),
'#description' => t('Once points have been obtained by the user they will
last until this date. This setting overrides the
"Expire after setting" above'. $warning),
'#default_value' => variable_get(USERPOINTS_EXPIREON_DATE, array('day' => 1, 'month' => 1, 'year' => 1980)),
);
$form[$group][USERPOINTS_EXPIRY_DESCRIPTION] = array(
'#type' => 'textarea',
'#title' => t('Expiration entry description'),
'#description' => t('A negating expiration entry is made to expire
points leaving the original entry intact
(e.g. original points + expiration points = 0).
When the expiration entry is made this description will
be placed on the entry. This is useful so the users will
know what happened to their point balance. In crafting
your message you can use the following variables.
<br /> !points = The name used in branding
above (also use !Points and !point)
<br />!operation = Original operation that granted the points
<br/> !description = Original description for the point
<br /> !txn_id Original transaction ID
<br /> !date = Date of the original entry'),
'#default_value' => variable_get(USERPOINTS_EXPIRY_DESCRIPTION, ''),
);
$group = "misc";
$form[$group] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Messages'),
'#description' => t('Control the behavior of messages users see. '),
);
$form[$group][USERPOINTS_DISPLAY_MESSAGE] = array(
'#type' => 'radios',
'#title' => t('Display message'),
'#default_value' => variable_get(USERPOINTS_DISPLAY_MESSAGE, 1),
'#options' => array(0 => t('No'), 1 => t('Yes')),
'#description' => t('Determines if a message should be displayed whenever !points are awarded/substracted ', userpoints_translation()),
);
$group = "reports";
$form[$group] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Report Settings'),
'#description' => t(''),
);
$form[$group][USERPOINTS_REPORT_LIMIT] = array(
'#type' => 'select',
'#title' => t('Transactions per page'),
'#default_value' => variable_get(USERPOINTS_REPORT_LIMIT, 10),
'#options' => array(10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 100 => 100),
'#description' => t('Limits the number of transactions displayed per page'),
);
$form[$group][USERPOINTS_REPORT_DISPLAYZERO] = array(
'#type' => 'radios',
'#title' => t('Display zero !point users?', userpoints_translation()),
'#default_value' => variable_get(USERPOINTS_REPORT_DISPLAYZERO, 1),
'#options' => array(t('No'), t('Yes')),
'#description' => t('If set to "No" users with zero !points will not be displayed in the reports ', userpoints_translation()),
);
$form[$group][USERPOINTS_REPORT_USERCOUNT] = array(
'#type' => 'select',
'#title' => t('Users per page'),
'#default_value' => variable_get(USERPOINTS_REPORT_USERCOUNT, 30),
'#options' => array(10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 100 => 100),
'#description' => t('When listing !points by user limit how many users are displayed on a single page', userpoints_translation()),
);
/* Categories will only appear if the taxonomy module is enabled as
* the module is required for this functionality but not necessarily
* a requirement for the module.
*/
if (module_exists('taxonomy')) {
$group = 'category';
$form[$group] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('!Points Categorization', userpoints_translation()),
);
$form[$group][USERPOINTS_CATEGORY_DEFAULT_TID] = array(
'#type' => 'select',
'#title' => t('Default Category'),
'#default_value' => variable_get(USERPOINTS_CATEGORY_DEFAULT_TID, NULL),
'#options' => userpoints_get_categories(),
'#description' => t('By default all points are assigned to this category. You can modify what categories are available by modifying the <a href="!url">Userpoints taxonomy</a>',
array('!url' => url('admin/content/taxonomy/'. variable_get(USERPOINTS_CATEGORY_DEFAULT_VID, '')))),
);
}
$form['setting'] = module_invoke_all('userpoints', 'setting');
return system_settings_form($form);
}
/**
* @param uid: user id of the user to get or lose the points
*
* @return number of current points in that user's account
*/
function userpoints_get_current_points($uid = NULL, $tid = NULL) {
if (!$uid) {
global $user;
$uid = $user->uid;
}
if (!$tid) {
$tid = userpoints_get_default_tid();
}
elseif ($tid == 'all') {
return (int)db_result(db_query('SELECT SUM(points) FROM {userpoints} WHERE uid = %d', $uid));
}
return (int)db_result(db_query('SELECT points FROM {userpoints} WHERE uid = %d AND tid = %d', $uid, $tid));
}
/**
* @param uid: user id of the user to get or lose the points
*
* @return number of max points in that user's account
*/
function userpoints_get_max_points($uid = NULL, $tid = NULL) {
if (!$uid) {
global $user;
$uid = $user->uid;
}
if (!$tid) {
$tid = userpoints_get_default_tid();
}
elseif ($tid == 'all') {
return (int)db_result(db_query('SELECT SUM(max_points) FROM {userpoints} WHERE uid = %d', $uid));
}
return (int)db_result(db_query('SELECT max_points FROM {userpoints} WHERE uid = %d AND tid = %d', $uid, $tid));
}
/**
* @param $params(array) or (int)
* if (int) assumed to be points for current user
* Accepts an array of keyed variables and parameters
* 'points' => # of points (int) (required)
* 'moderate' => TRUE/FALSE
* 'uid' => $user->uid
* 'operation' => 'published' 'moderated' etc.
* 'tid' => 'category ID'
* 'expirydate' => timestamp or 0, 0 = non-expiring; NULL = site default
* 'description' => 'description'
* 'reference' => reserved for module specific use
* 'display' => whether or not to display "points awarded" message
* 'txn_id' => Transaction ID of points, If present an UPDATE is performed
* 'entity_id' => ID of an entity in the Database. ex. $node->id or $user->uid
* 'entity_type' => string of the entity type. ex. 'node' or 'user' NOT 'node-content-custom'
*
* @return array with status and reason.
* 'status' => FALSE when no action is take, TRUE when points are credited or debited
* 'reason' => (string) error message to indicate reason for failure
*/
function userpoints_userpointsapi($params) {
//Test for the existence of parameters and set defaults if necessary
if (!isset($params['txn_id'])) {
//If a txn_id is passed in we'll do an UPDATE thus the std check don't apply
if (is_numeric($params)) {
$points = $params;
$params = array();
$params['points'] = $points;
unset($points);
}
if (!is_array($params)) {
//has to be an array to continue
return array(
'status' => false,
'reason' => 'Parameters did not properly form as an array,
this is an internal module error.
',
);
}
if (!isset($params['uid'])) {
global $user;
$params['uid'] = $user->uid;
}
if (!isset($params['operation'])) {
$params['operation'] = NULL;
}
if (!isset($params['description'])) {
$params['description'] = NULL;
}
if (!isset($params['reference'])) {
$params['reference'] = NULL;
}
if (!isset($params['display'])) {
$params['display'] = NULL;
}
if (!isset($params['moderate'])) {
//if not passed then site default is used
$params['status'] = variable_get(USERPOINTS_POINTS_MODERATION, 0);
}
else {
if ($params['moderate'] == true) {
$params['status'] = 1;
}
else {
$params['status'] = 0;
}
}
if (!isset($params['tid']) || !is_numeric($params['tid'])) {
//if not passed then site default is used
$params['tid'] = variable_get(USERPOINTS_CATEGORY_DEFAULT_TID, NULL);
}
if (!isset($params['entity_id'])) {
$params['entity_id'] = NULL;
}
if (!isset($params['entity_type'])) {
$params['entity_type'] = NULL;
}
// anonymous users do not get points, and there have to be points to process
if (($params['uid'] == 0 || $params['points'] == 0)) {
return array(
'status' => false,
'reason' => 'uid or points = 0. Anonymous users do not get points
and there must be points to process.
',
);
}
}
else {
//We have a txn_id so we can look up some user information
$sql = "SELECT uid from {userpoints_txn} WHERE txn_id = %d";
$params['uid'] = db_result(db_query($sql, $params['txn_id']));
} //if txn_id
// Load the user object that will be awarded the points
$account = user_load(array('uid' => $params['uid']));
// Call the _userpoints hook, and stop if one of them returns FALSE
$rc = module_invoke_all('userpoints', 'points before', $params['points'], $account->uid, $params['event']);
foreach ($rc as $key => $value) {
if ($value == FALSE) {
// Do not process the points
return array(
'status' => false,
'reason' => t('%key returned false from the hook_userpoints points before call', array('%key' => $key)),
);
}
}
if ($params['points'] < 0) {
$msg = t('lost');
}
elseif ($params['status'] == 2) {
//points have been declined
$msg = t('was declined');
}
else {
$msg = t('earned');
}
$ret = _userpoints_transaction($params);
if ($ret == false) {
return array(
'status' => false,
'reason' => 'transaction failed in _userpoints_transaction, this is an internal module error',
);
}
if ($params['status'] == 1) {
$mesg = (t('User %uname %op %pointsvalue !points, pending administrator approval.',
array_merge(userpoints_translation(), array(
'%uname' => $account->name,
'%op' => $msg,
'%pointsvalue' => abs($params['points']),
'%total' => userpoints_get_current_points($params['uid'], $params['tid'])
))));
}
else {
$mesg = (t('User %uname %op %pointsvalue !points! Total now is %total points.',
array_merge(userpoints_translation(), array(
'%uname' => $account->name,
'%op' => $msg,
'%pointsvalue' => abs($params['points']),
'%total' => userpoints_get_current_points($params['uid'], $params['tid'])
))));
} //if $params['status']
if ($mesg && ($params['display'] || ($params['display'] === null && variable_get(USERPOINTS_DISPLAY_MESSAGE, 1) == 1))) {
drupal_set_message($mesg);
}
// Call the _userpoints hook to allow modules to act after points are awarded
module_invoke_all('userpoints', 'points after', $params['points'], $account->uid, $params['operation']);
return array(
'status' => true,
);
}
/*
* Adds the points to the txn table
* PRIVATE FUNCTION use userpoints_userpointsapi!
*/
function _userpoints_transaction(&$params) {
//Check, again, for a properly formed array
if (!is_array($params)) {
return false;
}
if (!isset($params['txn_id'])) {
//If a txn_id is preset we UPDATE the record instead of adding one
//the standard checks don't apply
if (!is_numeric($params['points'])) {
return false;
}
if (!isset($params['uid'])) {
global $user;
$params['uid'] = $user->uid;
//there must be a UID, anonymous does not receive points
if (!$params['uid'] > 0) {
return false;
}
}
if (!isset($params['moderate'])) {
//if not passed then site default is used
$params['status'] = variable_get(USERPOINTS_POINTS_MODERATION, 0);
}
else {
if ($params['moderate'] == true) {
$params['status'] = 1;
}
else {
$params['status'] = 0;
}
}
if (!isset($params['operation'])) {
$params['operation'] = NULL;
}
if (!isset($params['description'])) {
$params['description'] = NULL;
}
if (!isset($params['reference'])) {
$params['reference'] = NULL;
}
if (!isset($params['tid']) || !is_numeric($params['tid'])) {
$params['tid'] = variable_get(USERPOINTS_CATEGORY_DEFAULT_TID, NULL);
}
elseif ($params['tid'] == 0) {
//tid with 0 are uncategorized and are set to NULL
//this is a backwards compatibilty issue
$params['tid'] = NULL;
}
if (!isset($params['expirydate'])) {
$params['expirydate'] = userpoints_get_default_expiry_date();
}
if (!isset($params['expired'])) {
$params['expired'] = NULL;
}
if (!isset($params['parent_txn_id'])) {
$params['parent_txn_id'] = NULL;
}
if (!isset($params['entity_id'])) {
$params['entity_id'] = NULL;
}
if (!isset($params['entity_type'])) {
$params['entity_type'] = NULL;
}
} // if txn_id
if (is_numeric($params['txn_id'])) {
//A transaction ID was passed in so we'll update the transaction
$result = db_query("SELECT txn_id, uid, approver_uid, points,
time_stamp, status, operation, description, reference, expirydate, expired,
parent_txn_id, tid, entity_id, entity_type
FROM {userpoints_txn}
WHERE txn_id = %d",
$params['txn_id']);
$txn = db_fetch_array($result);
foreach ($txn as $key => $value) {
if (isset($params[$key])) {
$arr[] = $key .' = \''. $params[$key] .'\'';
}
else {
$params[$key] = $value;
}
}
db_query('UPDATE {userpoints_txn} SET '. implode(', ', $arr) .'
WHERE txn_id = %d',
$params['txn_id']
);
_userpoints_update_cache($params);
}
else {
$ret = db_query("INSERT INTO {userpoints_txn}
(uid, points, time_stamp, status, operation, description,
reference, expirydate, expired, parent_txn_id, tid, entity_id, entity_type)
VALUES (%d, %d, %d, %d, '%s', '%s', '%s', %d, %d, %d, %d, %d, '%s')",
$params['uid'],
$params['points'],
time(),
$params['status'],
$params['operation'],
$params['description'],
$params['reference'],
$params['expirydate'],
$params['expired'],
$params['parent_txn_id'],
$params['tid'],
$params['entity_id'],
$params['entity_type']
);
if ($params['status'] != true && $ret != false) {
_userpoints_update_cache($params);
}
}
return TRUE;
} //function userpoints_transaction
/*
* Update the caching table
*/
function _userpoints_update_cache(&$params) {
if ( $params['status'] != 0 || $params['expired'] == 1) {
//Only update the cache for fully approved non-expired points
return false;
}
if (!isset($params['tid'])) {
$params['tid'] = NULL;
}
// Calculate the current points based upon the tid
$current_points = (int)$params['points'] + userpoints_get_current_points($params['uid'], $params['tid']);
//Grab the user's maximum points to preserve it
$max_points = db_result(db_query('SELECT max_points FROM {userpoints} WHERE uid = %d AND tid = %d',
$params['uid'], $params['tid']));
if ($params['points'] > 0) {
//points are greater than zero, update their max_points
$max_points = (int)$params['points'] + (int)$max_points;
}
// insert or update the userpoints caching table with the user's current points
if (_userpoints_user_exists($params['uid'], $params['tid'])) {
db_query("UPDATE {userpoints}
SET points = %d, max_points = %d, last_update = %d
WHERE uid = %d AND tid = %d",
$current_points,
$max_points,
time(),
$params['uid'],
$params['tid']
);
}
else {
$result = db_query("INSERT INTO {userpoints}
(uid, points, max_points, last_update, tid)
VALUES (%d, %d, %d, %d, %d )",
$params['uid'],
$current_points,
$max_points,
time(),
$params['tid']
);
}
unset($params);
}
/* Purpose: Determines the correct default expiration date
* Returns: timestamp
*/
function userpoints_get_default_expiry_date() {
$expirydate = userpoints_date_to_timestamp(variable_get(USERPOINTS_EXPIREON_DATE, NULL));
if ($expirydate < time()) {
$expirydate = variable_get(USERPOINTS_EXPIREAFTER_DATE, NULL);
if ($expirydate) {
$expirydate = time() + $expirydate;
}
}
return $expirydate;
} //userpoints_get_default_expiry_date
/*
* Purpose: Checks to ensure that a user exists corresponding to a category
* @param $uid User ID to check for existence of points for the user
* @param $tid taxonomy id of the category to limit to, if omitted
* if the use has points in any category the return is true
* Returns : true if user found, falase otherwise
*/
function _userpoints_user_exists($uid, $tid = NULL) {
if ( is_numeric($tid) ) {
return (int)db_result(
db_query('SELECT COUNT(uid)
FROM {userpoints}
WHERE uid = %d AND tid = %d ',
$uid, $tid));
}
else {
return (int)db_result(
db_query('SELECT COUNT(uid)
FROM {userpoints}
WHERE uid = %d',
$uid ));
}
}
function userpoints_user($op, &$edit, &$account, $category = '') {
switch ($op) {
case 'delete':
// The user is being deleted, delete all traces in userpoints and txn tables
db_query('DELETE FROM {userpoints} WHERE uid = %d', $account->uid);
db_query('DELETE FROM {userpoints_txn} WHERE uid = %d', $account->uid);
break;
case 'view':
// Get the points for the user
$points = userpoints_get_current_points($account->uid);
if (user_access(USERPOINTS_PERM_ADMIN)) {
$points = l($points, 'admin/user/userpoints/add/'. $account->uid, array('title' => t('Manage points')));
}
if (user_access(USERPOINTS_PERM_VIEW)) {
$disp_points[] = array(
'title' => t('User !points', userpoints_translation()),
'value' => $points,
);
return array(t('!Points', userpoints_translation()) => $disp_points);
}
break;
}
}
function userpoints_admin_manage() {
$tid = arg(4);
$cat_count = count(userpoints_get_categories());
$header = array(
array('data' => t('User'), 'field' => 'uid', 'sort' => 'desc'),
array('data' => t('Time stamp'), 'field' => 'time_stamp'),
array('data' => t('!Points', userpoints_translation()), 'field' => 'points'),
array('data' => t('Operation'), 'field' => 'operation'),
array('data' => t('Category'), 'field' => 'cat'),
array('data' => t('Operation')),
);
$sql = "SELECT p.txn_id, p.uid, p.time_stamp, p.points, p.operation, p.status,
p.entity_type, p.entity_id, t.name as cat
FROM {userpoints_txn} p
LEFT JOIN {term_data} t ON p.tid = t.tid
WHERE p.status = %d";
//Check for filtering
if (is_numeric($tid) && $tid == 0) {
$sql .= " AND (p.tid IS NULL OR p.tid = '')";
$cat = t('!Uncategorized', userpoints_translation());
}
elseif (is_numeric($tid)) {
$sql .= " AND p.tid = %d";
$cat = db_result(db_query("SELECT name from {term_data} WHERE tid = %d", $tid));
}
else {
$cat = t('All');
}
//Set the title of the page
drupal_set_title(t($cat) ." ". t("!points", userpoints_translation()));
$sql .= tablesort_sql($header);
$pager_limit = variable_get(USERPOINTS_REPORT_USERCOUNT, 30);
$result = pager_query($sql, $pager_limit, 0, NULL, USERPOINTS_TXN_STATUS_PENDING, $tid);
while ($data = db_fetch_object($result)) {
$user = user_load(array('uid' => $data->uid));
if (!$data->cat) {
$data->cat = t('!Uncategorized', userpoints_translation());
}
$operation = module_invoke_all('userpoints', 'entity_type', $data);
if (!$operation) {
switch ($data->entity_type) {
case 'node' :
$node = node_load($data->entity_id);
if ($node) {
$operation = l($data->operation, 'node/'. $node->nid, array('title' => $node->title));
}
else {
$operation = $data->operation;
}
break;
case 'comment' :
if (module_exists('comment')) {
//We have to load the comment to get the nid for the comment
$comment = _comment_load($data->entity_id);
if ($comment) {
$operation = l($data->operation, 'node/'. $comment->nid, array('title' => $comment->subject), NULL, 'comment-'. $comment->cid);
}
else {
$operation = $data->operation;
}
}
break;
default:
$operation = $data->operation;
}
}
$rows[] = array(
array('data' => theme('username', $user)),
array('data' => format_date($data->time_stamp, 'custom', 'Y-m-d H:i')),
array('data' => $data->points, 'align' => 'right'),
array('data' => $operation),
array('data' => $data->cat),
array('data' => l('approve', "admin/user/userpoints/approve/$data->txn_id") .
' '. l('decline', "admin/user/userpoints/decline/$data->txn_id") .
' '. l('edit', "admin/user/userpoints/edit/$data->txn_id")
),
);
}
if (!$rows) {
//no points in moderation
$rows[] = array(array('data' => t('No !points awaiting moderation', userpoints_translation()),
'colspan' => 6, 'align' => 'center')
); //$rows[]
}
if ($cat_count > 1) {
$output = drupal_get_form('userpoints_filter_cat_select', 'admin/user/userpoints/moderate/', arg(4));
$output .= theme('table', $header, $rows);
$output .= theme('pager', NULL, $pager_limit, 0);
}
else {
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, $pager_limit, 0);
}
return $output;
}
/*
* Purpose: Approves moderated points
*/
function userpoints_admin_approve() {
$operation = arg(3);
$txn_id = (int)arg(4);
return drupal_get_form('userpoints_confirm_approve', $operation, $txn_id);
}
function userpoints_confirm_approve($operation, $txn_id) {
$form = array(
'operation' => array(
'#type' => 'value',
'#value' => $operation,
),
'txn_id' => array(
'#type' => 'value',
'#value' => $txn_id,
),
);
return confirm_form(
$form,
t('Are you sure you want to @op txn @txn_id', array('@op' => $operation, '@txn_id' => $txn_id)),
'admin/user/userpoints/moderate'
);
}
function userpoints_confirm_approve_submit($form, &$form_state) {
global $user;
$form_values = $form_state['values'];
switch ($form_state['clicked_button']['#value']) {
case 'approve':
$status = USERPOINTS_TXN_STATUS_APPROVED;
break;
case 'decline':
$status = USERPOINTS_TXN_STATUS_DECLINED;
break;
default :
return false;
}
$params = array(
'txn_id' => $form_values['txn_id'],
'approver_uid' => $user->uid,
'status' => $status,
);
userpoints_userpointsapi($params);
$form_state['redirect'] = 'admin/user/userpoints/moderate';
}
function userpoints_admin_txn($form_state) {
global $user;
$mode = arg(3);
$timestamp = format_date(time(), 'custom', 'Y-m-d H:i O');
if ($mode == 'edit' && arg(4)) {
$txn_id = (int)arg(4);