-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAwardsSubs.php
1464 lines (1337 loc) · 38.1 KB
/
AwardsSubs.php
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
/**
* @package Awards Modification
* @license Mozilla Public License version 1.1 http://www.mozilla.org/MPL/1.1/.
*
* @version 3.0.1
*
* Original Software by: Juan "JayBachatero" Hernandez
* Copyright (c) 2006-2009: YodaOfDarkness (Fustrate)
* Copyright (c) 2010: Jason "JBlaze" Clemons
*
*/
if (!defined('SMF'))
{
die('Hacking attempt...');
}
/**
* Loads an award by ID and places the values in to context
*
* @param int $id
*/
function AwardsLoadAward($id = -1)
{
global $smcFunc, $modSettings, $scripturl;
// Load single award
$request = $smcFunc['db_query']('', '
SELECT
id_award, award_name, description, id_category, time_added, filename, minifile,
award_trigger, award_type, award_location, award_requestable, award_assignable
FROM {db_prefix}awards
WHERE id_award = {int:id}
LIMIT 1',
array(
'id' => $id
)
);
$row = $smcFunc['db_fetch_assoc']($request);
// Check if that award actually exists
if ($row['id_award'] != $id)
{
fatal_lang_error('awards_error_no_award');
}
$award = array(
'id' => $row['id_award'],
'award_name' => $row['award_name'],
'description' => $row['description'],
'category' => $row['id_category'],
'time' => timeformat($row['time_added']),
'trigger' => $row['award_trigger'],
'type' => $row['award_type'],
'location' => $row['award_location'],
'requestable' => $row['award_requestable'],
'assignable' => $row['award_assignable'],
'filename' => $row['filename'],
'minifile' => $row['minifile'],
'img' => dirname($scripturl) . '/' . (empty($modSettings['awards_dir']) ? '' : $modSettings['awards_dir'] . '/') . $row['filename'],
'small' => dirname($scripturl) . '/' . (empty($modSettings['awards_dir']) ? '' : $modSettings['awards_dir'] . '/') . $row['minifile'],
);
// Free results
$smcFunc['db_free_result']($request);
return $award;
}
/**
* Helper function to load the number of awards in a given category
*
* - Used by createlist in awardsmain
*
* @param int $cat
*/
function AwardsCountCategoryAwards($cat)
{
global $smcFunc;
// Count the number of items in the database for create index
$request = $smcFunc['db_query']('', '
SELECT
COUNT(id_award)
FROM {db_prefix}awards
WHERE id_category = {int:cat}',
array(
'cat' => $cat,
)
);
list($countAwards) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
return $countAwards;
}
/**
* Helper function to load the awards in a given category
*
* - Used by createlist in awardsmain
*
* @param int $start
* @param int $items_per_page
* @param string $sort
* @param int $cat
*/
function AwardsLoadCategoryAwards($start, $items_per_page, $sort, $cat)
{
global $context, $scripturl, $modSettings, $smcFunc;
// Select the awards and their categories.
$request = $smcFunc['db_query']('', '
SELECT
a.id_category, a.id_award, a.award_name, a.description, a.time_added, a.filename, a.minifile, a.award_type,
a.award_requestable, a.award_assignable, a.award_trigger,
c.category_name
FROM {db_prefix}awards AS a
LEFT JOIN {db_prefix}awards_categories AS c ON (c.id_category = a.id_category)
WHERE a.id_category = {int:cat}
ORDER BY {raw:sort}
LIMIT {int:start}, {int:end}',
array(
'start' => $start,
'end' => $items_per_page,
'sort' => $sort,
'cat' => $cat,
)
);
$categories = array();
// Loop through the results.
while ($row = $smcFunc['db_fetch_assoc']($request))
{
// load up the award details
$categories[] = array(
'id' => $row['id_award'],
'award_name' => $row['award_name'],
'award_type' => $row['award_type'],
'description' => parse_bbc($row['description']),
'time' => timeformat($row['time_added']),
'requestable' => $row['award_requestable'],
'assignable' => $row['award_assignable'],
'filename' => $row['filename'],
'minifile' => $row['minifile'],
'trigger' => $row['award_trigger'],
'img' => dirname($scripturl) . '/' . (empty($modSettings['awards_dir']) ? '' : $modSettings['awards_dir'] . '/') . $row['filename'],
'small' => dirname($scripturl) . '/' . (empty($modSettings['awards_dir']) ? '' : $modSettings['awards_dir'] . '/') . $row['minifile'],
'edit' => ((allowedTo('manage_awards')) ? $scripturl . '?action=admin;area=awards;sa=modify;a_id=' . $row['id_award'] : ''),
'delete' => ((allowedTo('manage_awards')) ? $scripturl . '?action=admin;area=awards;sa=delete;a_id=' . $row['id_award'] . ';' . $context['session_var'] . '=' . $context['session_id'] : ''),
'assign' => ((allowedTo('manage_awards') || !empty($row['award_assignable'])) ? $scripturl . '?action=admin;area=awards;sa=assign;step=1;a_id=' . $row['id_award'] : ''),
'view_assigned' => $scripturl . '?action=admin;area=awards;sa=viewassigned;a_id=' . $row['id_award'],
);
}
$smcFunc['db_free_result']($request);
return $categories;
}
/**
* Determines the number of individual or group awards a member has received
*
* @param int $memID
*/
function AwardsCountMembersAwards($memID)
{
global $smcFunc, $cur_profile;
// Count the number of items in the database for create index
$request = $smcFunc['db_query']('', '
SELECT
id_award, id_group, active
FROM {db_prefix}awards_members
WHERE (id_member = {int:mem}' . (!empty($cur_profile['groups']) ? '
OR (id_member < 0 AND id_group IN ({array_int:groups}))' : '') . ')
AND active = {int:active}',
array(
'mem' => $memID,
'groups' => !empty($cur_profile['groups']) ? array_map('\intval', $cur_profile['groups']) : '',
'active' => 1
)
);
// load/count them this way as they may have been assigned an award individually or via group
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$awards[$row['id_award']] = $row['id_award'];
}
$smcFunc['db_free_result']($request);
return !empty($awards) ? count($awards) : 0;
}
/**
* Loads in the awards/categories details for a members set of awards
*
* @param int $start
* @param int $end
* @param int $memID
*/
function AwardsLoadMembersAwards($start, $end, $memID)
{
global $smcFunc, $cur_profile, $scripturl, $settings, $modSettings, $txt;
// Load the individual and group awards
$request = $smcFunc['db_query']('', '
SELECT
aw.id_award, aw.award_name, aw.description, aw.filename, aw.minifile,
am.id_member, am.date_received, am.favorite, am.id_group,
c.category_name, c.id_category
FROM {db_prefix}awards AS aw
LEFT JOIN {db_prefix}awards_members AS am ON (am.id_award = aw.id_award)
LEFT JOIN {db_prefix}awards_categories AS c ON (c.id_category = aw.id_category)
WHERE (am.id_member = {int:member}' . (!empty($cur_profile['groups']) ? '
OR (am.id_member < 0 AND am.id_group IN({array_int:groups}))' : '') . ')
AND am.active = {int:active}
ORDER BY am.favorite DESC, c.category_name DESC, aw.award_name DESC
LIMIT {int:start}, {int:end}',
array(
'start' => $start,
'end' => $end,
'member' => $memID,
'groups' => !empty($cur_profile['groups']) ? array_map('\intval', $cur_profile['groups']) : '',
'active' => 1
)
);
$categories = array();
// Fetch the award info just once
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (!isset($categories[$row['id_category']]['name']))
{
$categories[$row['id_category']] = array(
'name' => $row['category_name'],
'view' => $scripturl . '?action=admin;area=awards;sa=viewcategory;in=' . $row['id_category'],
'awards' => array(),
);
}
$categories[$row['id_category']]['awards'][$row['id_award']] = array(
'id' => $row['id_award'],
'award_name' => $row['award_name'],
'description' => parse_bbc($row['description']),
'more' => $scripturl . '?action=profile;area=membersAwards;a_id=' . $row['id_award'],
'favorite' => array(
'fav' => $row['favorite'],
'href' => $scripturl . '?action=profile;area=showAwards;in=' . $row['id_award'] . ';makeFavorite=' . ($row['favorite'] == 1 ? '0' : '1') . (isset($_REQUEST['u']) ? ';u=' . $_REQUEST['u'] : ''),
'img' => '<img src="' . $settings['images_url'] . '/awards/' . ($row['favorite'] == 1 ? 'delete' : 'add') . '.png" alt="' . $txt['awards_favorite2'] . '" title="' . ($row['favorite'] == 1 ? $txt['awards_remove_favorite'] : $txt['awards_make_favorite']) . '" />',
'allowed' => empty($row['id_group']),
),
'filename' => $row['filename'],
'time' => sscanf($row['date_received'], '%d-%d-%d'),
'img' => dirname($scripturl) . '/' . $modSettings['awards_dir'] . '/' . $row['filename'],
'small' => dirname($scripturl) . '/' . $modSettings['awards_dir'] . '/' . $row['minifile'],
);
}
$smcFunc['db_free_result']($request);
return $categories;
}
/**
* Loads all non-auto assignable awards for use in the template
*
* - Returns an array of awards/details for each one allowable
*/
function AwardsLoadAssignableAwards()
{
global $smcFunc;
// Select the awards (NON-auto) that can be assigned by this member
$request = $smcFunc['db_query']('', '
SELECT
id_award, award_name, filename, minifile, description, award_assignable
FROM {db_prefix}awards
WHERE award_type <= {int:type}' . ((allowedTo('manage_awards')) ? '' : ' AND award_assignable = {int:assign}') . '
ORDER BY award_name ASC',
array(
'type' => 1,
'assign' => 1,
)
);
$awards = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$awards[$row['id_award']] = array(
'award_name' => $row['award_name'],
'filename' => $row['filename'],
'minifile' => $row['minifile'],
'description' => $row['description'],
'assignable' => $row['award_assignable']
);
}
$smcFunc['db_free_result']($request);
return $awards;
}
/**
* Loads all member requestable awards that have active requests against them
*
* - Finds members that have requested these awards for approval display
*/
function AwardsLoadRequestedAwards()
{
global $scripturl, $modSettings, $settings, $smcFunc;
// Select all the requestable awards, so we have the award specifics
$request = $smcFunc['db_query']('', '
SELECT
a.id_award, a.award_name, a.filename, a.minifile, a.description
FROM {db_prefix}awards as a
LEFT JOIN {db_prefix}awards_members as am ON (a.id_award = am.id_award)
WHERE a.award_type <= {int:type}
AND a.award_requestable = {int:requestable}
AND am.active = {int:active}
ORDER BY award_name ASC',
array(
'type' => 1,
'requestable' => 1,
'active' => 0,
)
);
$awards = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$awards[$row['id_award']] = array(
'id' => $row['id_award'],
'award_name' => $row['award_name'],
'filename' => $row['filename'],
'minifile' => $row['minifile'],
'description' => parse_bbc($row['description']),
'img' => dirname($scripturl) . '/' . (empty($modSettings['awards_dir']) ? '' : $modSettings['awards_dir'] . '/') . $row['filename'],
'small' => dirname($scripturl) . '/' . (empty($modSettings['awards_dir']) ? '' : $modSettings['awards_dir'] . '/') . $row['minifile'],
'members' => array(),
);
}
$smcFunc['db_free_result']($request);
// Now get just the members awaiting approval
$request = $smcFunc['db_query']('', '
SELECT
mem.real_name, mem.id_member,
am.id_award, am.comments
FROM {db_prefix}awards_members AS am
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = am.id_member)
WHERE am.active = {int:active}',
array(
'active' => 0
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$awards[$row['id_award']]['members'][$row['id_member']] = array(
'id' => $row['id_member'],
'name' => $row['real_name'],
'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
'pm' => '<a href="' . $scripturl . '?action=pm;sa=send;u=' . $row['id_member'] . '"><img src="' . $settings['images_url'] . '/icons/pm_read.gif" alt="" /></a>',
'comments' => $row['comments'],
);
}
$smcFunc['db_free_result']($request);
return $awards;
}
/**
* Adds a new award to the system, returns the id
*
* @param string $award_name
* @param string $description
* @param int $time_added
* @param int $category
* @param int $award_type
* @param int $trigger
* @param int $award_location
* @param int $award_requestable
* @param int $award_assignable
*/
function AwardsAddAward($award_name, $description, $time_added, $category, $award_type, $trigger, $award_location, $award_requestable, $award_assignable)
{
global $smcFunc;
// Add in a new award
$smcFunc['db_insert']('replace', '{db_prefix}awards',
array(
'award_name' => 'string',
'description' => 'string',
'time_added' => 'int',
'filename' => 'string',
'minifile' => 'string',
'id_category' => 'int',
'award_type' => 'int',
'award_trigger' => 'int',
'award_location' => 'int',
'award_requestable' => 'int',
'award_assignable' => 'int'
),
array(
$award_name,
$description,
$time_added,
'',
'',
$category,
$award_type,
$trigger,
$award_location,
$award_requestable,
$award_assignable
),
array('id_award')
);
// Return the id_award for this new award
return $smcFunc['db_insert_id']('{db_prefix}awards', 'id_award');
}
/**
* Updates an award that already exists in the system
*
* @param int $id
* @param string $award_name
* @param string $description
* @param int $category
* @param int $award_type
* @param int $trigger
* @param int $award_location
* @param int $award_requestable
* @param int $award_assignable
*/
function AwardsUpdateAward($id, $award_name, $description, $category, $award_type, $trigger, $award_location, $award_requestable, $award_assignable)
{
global $smcFunc;
// Make the updates to the award
return $smcFunc['db_query']('', '
UPDATE {db_prefix}awards
SET
award_name = {string:awardname},
description = {string:award_desc},
id_category = {int:category},
award_type = {int:awardtype},
award_trigger = {int:trigger},
award_location = {int:awardlocation},
award_requestable = {int:awardrequestable},
award_assignable = {int:awardassignable}
WHERE id_award = {int:id_award}',
array(
'awardname' => $award_name,
'award_desc' => $description,
'id_award' => $id,
'category' => $category,
'awardtype' => $award_type,
'trigger' => $trigger,
'awardlocation' => $award_location,
'awardrequestable' => $award_requestable,
'awardassignable' => $award_assignable,
)
);
}
/**
* Deletes an award by id
*
* @param int $id
*/
function AwardsDeleteAward($id)
{
global $smcFunc;
// Delete the award
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}awards
WHERE id_award = {int:award}
LIMIT 1',
array(
'award' => $id
)
);
}
/**
* Load the list of groups that this member can see
*
* - Counts the number of members in each group (including post count based ones)
* - Loads all groups, normal, moderator, postcount, hidden, etc.
* - Returns the array of values
*/
function AwardsLoadGroups()
{
global $context, $user_info, $smcFunc;
// Get started
$groups = array();
$context['can_moderate'] = allowedTo('manage_membergroups');
$group_ids_pc = array();
$group_ids = array();
// Find all the groups
$request = $smcFunc['db_query']('', '
SELECT
mg.id_group, mg.group_name, mg.group_type, mg.hidden,
IFNULL(gm.id_member, 0) AS can_moderate,
CASE WHEN min_posts != {int:min_posts} THEN 1 ELSE 0 END AS is_post_group
FROM {db_prefix}membergroups AS mg
LEFT JOIN {db_prefix}group_moderators AS gm ON (gm.id_group = mg.id_group AND gm.id_member = {int:current_member})
WHERE mg.id_group != {int:mod_group}' . (allowedTo('admin_forum') ? '' : '
AND mg.group_type != {int:is_protected}') . '
ORDER BY group_name',
array(
'current_member' => $user_info['id'],
'min_posts' => -1,
'mod_group' => 3,
'is_protected' => 1,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
// If this group is hidden then it can only exist if the user can moderate it!
if ($row['hidden'] && !$row['can_moderate'] && !allowedTo('manage_membergroups'))
{
continue;
}
$groups[$row['id_group']] = array(
'id' => $row['id_group'],
'name' => $row['group_name'],
'type' => $row['group_type'],
'is_post_group' => $row['is_post_group'],
'member_count' => 0,
);
$context['can_moderate'] |= $row['can_moderate'];
// Keep track of the groups we can see as normal or post count
if (!empty($row['is_post_group']))
{
$group_ids_pc[] = $row['id_group'];
}
else
{
$group_ids[] = $row['id_group'];
}
}
$smcFunc['db_free_result']($request);
// Now count up the number of members in each of the normal groups
if (!empty($group_ids))
{
$query = $smcFunc['db_query']('', '
SELECT
id_group, COUNT(*) AS num_members
FROM {db_prefix}members
WHERE id_group IN ({array_int:group_list})
GROUP BY id_group',
array(
'group_list' => $group_ids,
)
);
while ($row = $smcFunc['db_fetch_assoc']($query))
{
$groups[$row['id_group']]['member_count'] += $row['num_members'];
}
$smcFunc['db_free_result']($query);
// Only do additional groups if we can moderate...
if ($context['can_moderate'])
{
$query = $smcFunc['db_query']('', '
SELECT
mg.id_group, COUNT(*) AS num_members
FROM {db_prefix}membergroups AS mg
INNER JOIN {db_prefix}members AS mem ON (mem.additional_groups != {string:blank_screen}
AND mem.id_group != mg.id_group
AND FIND_IN_SET(mg.id_group, mem.additional_groups) != 0)
WHERE mg.id_group IN ({array_int:group_list})
GROUP BY mg.id_group',
array(
'group_list' => $group_ids,
'blank_screen' => '',
)
);
while ($row = $smcFunc['db_fetch_assoc']($query))
{
$groups[$row['id_group']]['member_count'] += $row['num_members'];
}
$smcFunc['db_free_result']($query);
}
}
// Now on to the post count groups
if (!empty($group_ids_pc))
{
$query = $smcFunc['db_query']('', '
SELECT
id_post_group AS id_group, COUNT(*) AS num_members
FROM {db_prefix}members
WHERE id_post_group IN ({array_int:group_list})
GROUP BY id_post_group',
array(
'group_list' => $group_ids_pc,
)
);
while ($row = $smcFunc['db_fetch_assoc']($query))
{
$groups[$row['id_group']]['member_count'] += $row['num_members'];
}
$smcFunc['db_free_result']($query);
}
return $groups;
}
/**
* For a given set of groups, gets all members in those groups
*/
function AwardsLoadGroupMembers()
{
global $smcFunc;
$members = array();
// Stop any monkey business
$allowed_groups = $_SESSION['allowed_groups'];
$_POST['who'] = array_intersect_key($_POST['who'], $allowed_groups);
$postsave = $_POST['who'];
unset($_SESSION['allowed_groups']);
// Did they select the moderator group
if (!empty($_POST['who']) && in_array(3, $_POST['who']))
{
$request = $smcFunc['db_query']('', '
SELECT
DISTINCT mem.id_member, mem.real_name
FROM ({db_prefix}members AS mem, {db_prefix}moderators AS mods)
WHERE mem.id_member = mods.id_member
ORDER BY mem.real_name ASC',
array()
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$members[$row['id_member']] = $row['real_name'];
}
$smcFunc['db_free_result']($request);
unset($_POST['who'][3]);
}
// How about regular members, they are people too, well most of them :P
if (!empty($_POST['who']) && in_array(0, $_POST['who']))
{
$request = $smcFunc['db_query']('', '
SELECT
mem.id_member, mem.real_name
FROM {db_prefix}members AS mem
WHERE mem.id_group = {int:id_group}
ORDER BY mem.real_name ASC',
array(
'id_group' => 0,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$members[$row['id_member']] = $row['real_name'];
}
$smcFunc['db_free_result']($request);
unset($_POST['who'][0]);
}
// Anyone else ?
if (!empty($_POST['who']))
{
// Select the members.
$request = $smcFunc['db_query']('', '
SELECT
id_member, real_name
FROM ({db_prefix}members AS mem, {db_prefix}membergroups AS mg)
WHERE (mg.id_group = mem.id_group OR FIND_IN_SET(mg.id_group, mem.additional_groups) OR mg.id_group = mem.id_post_group)
AND mg.id_group IN ({array_int:who})
ORDER BY real_name ASC',
array(
'who' => $_POST['who'],
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$members[$row['id_member']] = $row['real_name'];
}
$smcFunc['db_free_result']($request);
}
// Put it back so the template remains loaded (checked)
$_POST['who'] = $postsave;
return $members;
}
/**
* Callback for createlist
*
* - List all members and groups who have recived an award
*
* @param int $start
* @param int $items_per_page
* @param string $sort
* @param int $id
*/
function AwardsLoadMembers($start, $items_per_page, $sort, $id)
{
global $txt, $smcFunc;
// All the members with this award
$request = $smcFunc['db_query']('', '
SELECT
m.real_name, m.member_name,
a.id_member, a.date_received, a.id_group, a.uniq_id,
g.group_name
FROM {db_prefix}awards_members AS a
LEFT JOIN {db_prefix}members AS m ON (m.id_member = a.id_member)
LEFT JOIN {db_prefix}membergroups AS g ON (a.id_group = g.id_group)
WHERE a.id_award = {int:award}
AND a.active = {int:active}
ORDER BY {raw:sort}
LIMIT {int:start}, {int:per_page}',
array(
'award' => $id,
'active' => 1,
'sort' => $sort,
'start' => $start,
'per_page' => $items_per_page,
)
);
$members = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
// Group award?
if ($row['id_member'] < 0)
{
$row['member_name'] = $row['group_name'];
$row['real_name'] = $txt['awards_assign_membergroup'];
}
$members[] = $row;
}
$smcFunc['db_free_result']($request);
return $members;
}
/**
* Callback for createlist
*
* - Used to get the total number of members/groups who have received a specific award
*
* @param type $id
*/
function AwardsLoadMembersCount($id)
{
global $smcFunc;
// Count the number of items in the database for create index
$request = $smcFunc['db_query']('', '
SELECT
COUNT(*)
FROM {db_prefix}awards_members
WHERE id_award = {int:award}
AND active = {int:active}',
array(
'award' => $id,
'active' => 1
)
);
list ($num_members) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
return $num_members;
}
/**
* Places a member request for an award in to the system queue
*
* @param int $id
* @param int $date
* @param string $comments
* @param bool $flush
*/
function AwardsMakeRequest($id, $date, $comments, $flush = true)
{
global $smcFunc, $user_info, $modSettings;
$smcFunc['db_insert']('replace', '
{db_prefix}awards_members',
array(
'id_award' => 'int',
'id_member' => 'int',
'id_group' => 'int',
'date_received' => 'string',
'favorite' => 'int',
'award_type' => 'int',
'active' => 'int',
'comments' => 'string'
),
array(
$id,
$user_info['id'],
0,
$date,
0,
1,
0,
$comments
),
array('id_member', 'id_award')
);
// Update the cache as well
if ($flush)
{
// Get the number of unapproved requests so the awards team knows about it.
$request = $smcFunc['db_query']('', '
SELECT
COUNT(*)
FROM {db_prefix}awards_members
WHERE active = {int:active}',
array(
'active' => 0
)
);
list($modSettings['awards_request']) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
updateSettings(array(
'awards_request' => $modSettings['awards_request'],
));
}
}
/**
* Approve or deny a request by a member for a requestable award
*
* @param int[] $awards
* @param bool $approve
*/
function AwardsApproveDenyRequests($awards, $approve = true)
{
global $smcFunc;
// Accept the request
if ($approve)
{
// Now for the database.
foreach ($awards as $id_award => $member)
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}awards_members
SET active = {int:active}
WHERE id_award = {int:id_award}
AND id_member IN ({array_int:members})',
array(
'active' => 1,
'id_award' => $id_award,
'members' => $member,
)
);
}
}
// Or Deny
else
{
// Now for the database.
foreach ($awards as $id_award => $member)
{
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}awards_members
WHERE id_award = {int:id_award}
AND id_member IN ({array_int:members})',
array(
'id_award' => $id_award,
'members' => $member,
)
);
}
}
}
/**
* Loads all categories in the system
*
* - Returns array of categories with key of name and value of id
*
* @param string $sort order to return the categories
* @param bool $multi if true will return an array of arrays
*/
function AwardsLoadCategories($sort = 'DESC', $multi = false)
{
global $smcFunc;
// Load all the categories.
$request = $smcFunc['db_query']('', '
SELECT
id_category, category_name
FROM {db_prefix}awards_categories
ORDER BY category_name {raw:sort}',
array(
'sort' => $sort,
)
);
$categories = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
// return the data as key names or arrays
if ($multi)
{
$categories[] = array(
'id' => $row['id_category'],
'name' => $row['category_name'],
);
}
else
{
$categories[$row['category_name']] = $row['id_category'];
}
}
$smcFunc['db_free_result']($request);
return $categories;
}
/**
* Loads in the details of a category
*
* @param int $id
*/
function AwardsLoadCategory($id)
{
global $smcFunc;
// Load single category for editing.
$request = $smcFunc['db_query']('', '
SELECT *
FROM {db_prefix}awards_categories
WHERE id_category = {int:id}
LIMIT 1',
array(
'id' => $id
)
);
$row = $smcFunc['db_fetch_assoc']($request);
// Check if that category exists
if (empty($row['id_category']))
{
fatal_lang_error('awards_error_no_category');
}
$category = array(
'id' => $row['id_category'],
'name' => $row['category_name'],
);
$smcFunc['db_free_result']($request);
return $category;
}
/**
* Loads all the categories in the system
*
* - Returns an array of categories and links
*/
function AwardsLoadAllCategories()
{
global $scripturl, $smcFunc, $context;
// Load all the categories.
$request = $smcFunc['db_query']('', '
SELECT *
FROM {db_prefix}awards_categories'
);
$categories = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$categories[$row['id_category']] = array(
'id' => $row['id_category'],
'name' => $row['category_name'],
'view' => $scripturl . '?action=admin;area=awards;sa=viewcategory;a_id=' . $row['id_category'] . ';' . $context['session_var'] . '=' . $context['session_id'],
'edit' => $scripturl . '?action=admin;area=awards;sa=editcategory;a_id=' . $row['id_category'] . ';' . $context['session_var'] . '=' . $context['session_id'],
'delete' => $scripturl . '?action=admin;area=awards;sa=deletecategory;a_id=' . $row['id_category'] . ';' . $context['session_var'] . '=' . $context['session_id'],
);
}
$smcFunc['db_free_result']($request);
return $categories;
}
/**
* Returns the number of awards in each category
*
* @param int|null $id
*/