-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsus_lib.php
More file actions
2690 lines (2365 loc) · 88.5 KB
/
sus_lib.php
File metadata and controls
2690 lines (2365 loc) · 88.5 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
if (! verify_in_signup_sheets()) { die("not in signup_sheets"); }
////////////////////////////////////////////////////////////////////////////////////////
// constants
$SHEET_GROUP_FIELDS = 'sg.id AS sg_id
,sg.created_at AS sg_created_at
,sg.updated_at AS sg_updated_at
,sg.flag_deleted AS sg_flag_deleted
,sg.owner_user_id AS sg_owner_user_id
,sg.flag_is_default AS sg_flag_is_default
,sg.name AS sg_name
,sg.description AS sg_description
,sg.max_g_total_user_signups AS sg_max_g_total_user_signups
,sg.max_g_pending_user_signups AS sg_max_g_pending_user_signups';
$SHEET_FIELDS = 's.id AS s_id
,s.created_at AS s_created_at
,s.updated_at AS s_updated_at
,s.flag_deleted AS s_flag_deleted
,s.owner_user_id AS s_owner_user_id
,s.last_user_id AS s_last_user_id
,s.sus_sheetgroup_id AS s_sus_sheetgroup_id
,s.name AS s_name
,s.description AS s_description
,s.type AS s_type
,s.date_opens AS s_date_opens
,s.date_closes AS s_date_closes
,s.max_total_user_signups AS s_max_total_user_signups
,s.max_pending_user_signups AS s_max_pending_user_signups
,s.flag_alert_owner_change AS s_flag_alert_owner_change
,s.flag_alert_owner_signup AS s_flag_alert_owner_signup
,s.flag_alert_owner_imminent AS s_flag_alert_owner_imminent
,s.flag_alert_admin_change AS s_flag_alert_admin_change
,s.flag_alert_admin_signup AS s_flag_alert_admin_signup
,s.flag_alert_admin_imminent AS s_flag_alert_admin_imminent
,s.flag_private_signups AS s_flag_private_signups';
$OPENING_FIELDS = "o.id AS o_id
,o.created_at AS o_created_at
,o.updated_at AS o_updated_at
,o.flag_deleted AS o_flag_deleted
,o.last_user_id AS o_last_user_id
,o.sus_sheet_id AS o_sus_sheet_id
,o.opening_set_id AS o_opening_set_id
,o.name AS o_name
,o.description AS o_description
,o.max_signups AS o_max_signups
,o.admin_comment AS o_admin_comment
,o.begin_datetime AS o_begin_datetime
,o.end_datetime AS o_end_datetime
,o.end_datetime - o.begin_datetime AS o_dur_seconds
,FROM_UNIXTIME(o.begin_datetime,'%Y%m%d') AS o_dateymd
,FROM_UNIXTIME(o.begin_datetime,'%Y-%m-%d') AS o_date_y_m_d
,FROM_UNIXTIME(o.begin_datetime,'%l:%i %p') AS o_begin_time_h_m_p
,FROM_UNIXTIME(o.end_datetime,'%l:%i %p') AS o_end_time_h_m_p
,FROM_UNIXTIME(o.begin_datetime,'%k:%i') AS o_begin_time_h24_m
,FROM_UNIXTIME(o.end_datetime,'%k:%i') AS o_end_time_h24_m
,o.location AS o_location";
$SIGNUP_FIELDS = 'su.id AS su_id
,su.created_at AS su_created_at
,su.updated_at AS su_updated_at
,su.flag_deleted AS su_flag_deleted
,su.last_user_id AS su_last_user_id
,su.sus_opening_id AS su_sus_opening_id
,su.signup_user_id AS su_signup_user_id
,su.admin_comment AS su_admin_comment';
$ACCESS_FIELDS = 'ac.id AS a_id
,ac.created_at AS a_created_at
,ac.updated_at AS a_updated_at
,ac.last_user_id AS a_last_user_id
,ac.sheet_id AS a_sheet_id
,ac.type AS a_type
,ac.constraint_id AS a_constraint_id
,ac.constraint_data AS a_constraint_data
,ac.broadness AS a_broadness';
$USER_FIELDS = 'usr.id
,usr.auth
,usr.confirmed
,usr.policyagreed
,usr.deleted
,usr.mnethostid
,usr.username
,usr.password
,usr.idnumber
,usr.firstname
,usr.lastname
,usr.email
,usr.emailstop
,usr.icq
,usr.skype
,usr.yahoo
,usr.aim
,usr.msn
,usr.phone1
,usr.phone2
,usr.institution
,usr.department
,usr.address
,usr.city
,usr.country
,usr.lang
,usr.theme
,usr.timezone
,usr.firstaccess
,usr.lastaccess
,usr.lastlogin
,usr.currentlogin
,usr.lastip
,usr.secret
,usr.picture
,usr.url
,usr.description
,usr.mailformat
,usr.maildigest
,usr.maildisplay
,usr.htmleditor
,usr.ajax
,usr.autosubscribe
,usr.trackforums
,usr.timemodified
,usr.trustbitmask
,usr.imagealt
,usr.screenreader';
////////////////////////////////////////////////////////////////////////////////////////
# takes: a value
# returns: a version of that value ready to be used in a SQL statement (quoted as approp)
function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote if not integer
if (!is_numeric($value) || $value[0] == '0')
{
$value = preg_replace('/\\\\\'/',"''",mysql_real_escape_string($value));
$value = "'" . $value . "'";
}
return $value;
}
////////////////////////////////////////////////////////////////////////////////////////
# takes: none
# modifies the $USER object to include info about the user's sign-ups - the number of sign-ups total and future by group and sheet
# returns: none
function setUserSignupInfo()
{
global $USER,$CFG;
$sql = "
SELECT
su.id AS su_id
,o.id AS o_id
,o.begin_datetime AS o_begin_datetime
,s.id AS s_id
,sg.id AS sg_id
FROM
{$CFG->prefix}sus_signups AS su
JOIN {$CFG->prefix}sus_openings AS o ON o.id = su.sus_opening_id AND o.flag_deleted != 1
JOIN {$CFG->prefix}sus_sheets AS s ON s.id = o.sus_sheet_id AND s.flag_deleted != 1
JOIN {$CFG->prefix}sus_sheetgroups AS sg ON sg.id = s.sus_sheetgroup_id AND sg.flag_deleted != 1
WHERE
su.signup_user_id = {$USER->id}
AND su.flag_deleted != 1";
$sinfo = sus_get_records_sql($sql);
if (! $sinfo)
{
return;
}
$USER->signups = array('total' => array('groups'=>array(),'sheets'=>array()),
'future' => array('groups'=>array(),'sheets'=>array()));
$now = mktime();
foreach ($sinfo as $si)
{
if (isset($USER->signups['total']['groups'][$si->sg_id]))
{
$USER->signups['total']['groups'][$si->sg_id]++;
} else
{
$USER->signups['total']['groups'][$si->sg_id] = 1;
}
if (isset($USER->signups['total']['sheets'][$si->s_id]))
{
$USER->signups['total']['sheets'][$si->s_id]++;
} else
{
$USER->signups['total']['sheets'][$si->s_id] = 1;
}
if ($si->o_begin_datetime > $now)
{
if (isset($USER->signups['future']['groups'][$si->sg_id]))
{
$USER->signups['future']['groups'][$si->sg_id]++;
} else
{
$USER->signups['future']['groups'][$si->sg_id] = 1;
}
if (isset($USER->signups['future']['sheets'][$si->s_id]))
{
$USER->signups['future']['sheets'][$si->s_id]++;
} else
{
$USER->signups['future']['sheets'][$si->s_id] = 1;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
# takes: an optional group id (pass in 0 to skip), and an optional sheet id
# returns: an array of sheet info objects (sheet group (de-nomralized) and sheet (outer joined)) for the current user
function getOwnedSheetsAndAssociatedInfo($group_id=0,$sheet_id=0)
{
# get current user id
global $USER, $CFG, $SHEET_GROUP_FIELDS,$SHEET_FIELDS;
# get a list of all the user's sheets, and the group each one is in
$sql = "
SELECT
$SHEET_GROUP_FIELDS
,$SHEET_FIELDS
FROM
{$CFG->prefix}sus_sheetgroups AS sg
LEFT OUTER JOIN {$CFG->prefix}sus_sheets AS s
ON s.sus_sheetgroup_id = sg.id
AND s.owner_user_id = sg.owner_user_id
AND s.flag_deleted != 1"
.($sheet_id?"\n AND s.id = $sheet_id":'')."
WHERE sg.flag_deleted != 1
AND sg.owner_user_id = $USER->id"
.($group_id?" AND sg.id=$group_id":'')
."
ORDER BY
sg_name
,sg_id
,s_name
,s_created_at
";
debug(4,"\n\nsql is $sql\n\n");
return sus_get_records_sql($sql);
}
////////////////////////////////////////////////////////////////////////////////////////
# takes: an optional group id (pass in 0 to skip), and an optional sheet id
# returns: an array of sheet info objects (sheet group (de-nomralized) and sheet (outer joined)) for the current user has admin access and does NOT own
function getAdminSheetsAndAssociatedInfo($group_id=0,$sheet_id=0)
{
# get current user id
global $USER, $CFG, $SHEET_GROUP_FIELDS,$SHEET_FIELDS;
# get a list of all the user's sheets, and the group each one is in
$sql = "
SELECT
$SHEET_GROUP_FIELDS
,$SHEET_FIELDS
,usr.firstname AS usr_firstname
,usr.lastname AS usr_lastname
FROM
{$CFG->prefix}sus_sheetgroups AS sg
LEFT OUTER JOIN {$CFG->prefix}sus_sheets AS s
ON s.sus_sheetgroup_id = sg.id
AND s.owner_user_id = sg.owner_user_id
AND s.flag_deleted != 1
".($sheet_id?" AND s.id = $sheet_id":'')."
JOIN {$CFG->prefix}user AS usr ON usr.id = s.owner_user_id
JOIN {$CFG->prefix}sus_access AS a ON a.sheet_id = s.id
AND a.type = 'adminbyuser'
AND a.constraint_data = '{$USER->username}'
WHERE sg.flag_deleted != 1
AND s.owner_user_id != $USER->id
".($group_id?" AND sg.id=$group_id":'');
debug(4,"\n\nsql is $sql\n\n");
return sus_get_records_sql($sql);
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: an optional flag for whether access data should be included in the results
// returns: an array of sheet objects on which the current user can sign up
function getSignupAccessibleSheets($includeAccessRecords=true,$for_user_id=0,$for_sheet_id=0,$for_access_id=0)
{
global $USER, $CFG, $SHEET_FIELDS, $ACCESS_FIELDS;
if ($for_user_id<1)
{
$for_user_id = $USER->id;
}
// if filtering on the access id, need to get it in the query
if ($for_access_id > 0)
{
$includeAccessRecords=true;
}
$sql ="
SELECT
$SHEET_FIELDS".
($includeAccessRecords?"
,$ACCESS_FIELDS":'')."
FROM
{$CFG->prefix}sus_sheets AS s
JOIN (
SELECT DISTINCT
a.sheet_id".
($includeAccessRecords?'
,a.id AS id
,a.created_at AS created_at
,a.updated_at AS updated_at
,a.last_user_id AS last_user_id
,a.type AS type
,a.constraint_id AS constraint_id
,a.constraint_data AS constraint_data
,a.broadness AS broadness':'')."
FROM
{$CFG->prefix}sus_access AS a
WHERE
(a.type='byhasaccount')
OR
(a.type='byuser'
AND (a.constraint_data = '{$USER->username}' OR a.constraint_id=$for_user_id))
OR
(a.type='byrole'
AND a.constraint_data = 'teacher'
AND $for_user_id IN (
SELECT DISTINCT
usr.id
FROM
{$CFG->prefix}role_assignments AS r_asg
JOIN {$CFG->prefix}role AS role ON role.id = r_asg.roleid
JOIN {$CFG->prefix}user AS usr ON usr.id = r_asg.userid
JOIN {$CFG->prefix}context AS ctx ON ctx.id = r_asg.contextid
WHERE
role.name in ('editingteacher','teacher')
AND ctx.contextlevel = ". CONTEXT_COURSE."
AND usr.id = $for_user_id))
OR
(a.type='byrole'
AND a.constraint_data = 'student'
AND $for_user_id IN (
SELECT DISTINCT
usr.id
FROM
{$CFG->prefix}role_assignments AS r_asg
JOIN {$CFG->prefix}role AS role ON role.id = r_asg.roleid
JOIN {$CFG->prefix}user AS usr ON usr.id = r_asg.userid
JOIN {$CFG->prefix}context AS ctx ON ctx.id = r_asg.contextid
WHERE
role.name in ('student','auditor_student')
AND ctx.contextlevel = ". CONTEXT_COURSE."
AND usr.id = $for_user_id))
OR
(a.type='bycourse'
AND a.constraint_id IN (
SELECT DISTINCT
ctx.instanceid
FROM
{$CFG->prefix}role_assignments AS r_asg
JOIN {$CFG->prefix}role AS role ON role.id = r_asg.roleid
JOIN {$CFG->prefix}user AS usr ON usr.id = r_asg.userid
JOIN {$CFG->prefix}context AS ctx ON ctx.id = r_asg.contextid
WHERE
role.shortname in ('student','auditor','editingta','noneditingta','teacher','editingteacher')
AND ctx.contextlevel = ". CONTEXT_COURSE."
AND usr.id = $for_user_id))
OR
(a.type='bydept'
AND a.constraint_data IN (
SELECT DISTINCT
SUBSTRING(SUBSTRING_INDEX(crs.idnumber,'-',2),5)
FROM
{$CFG->prefix}role_assignments AS r_asg
JOIN {$CFG->prefix}role AS role ON role.id = r_asg.roleid
JOIN {$CFG->prefix}user AS usr ON usr.id = r_asg.userid
JOIN {$CFG->prefix}context AS ctx ON ctx.id = r_asg.contextid
JOIN {$CFG->prefix}course AS crs ON crs.id = ctx.instanceid
WHERE
role.name in ('student','auditor_student')
AND ctx.contextlevel = ". CONTEXT_COURSE."
AND (crs.idnumber LIKE '___-____-%' OR crs.idnumber LIKE '___-___-%')
AND usr.id = $for_user_id))
OR
(a.type='byinstr'
AND a.constraint_id IN (
SELECT DISTINCT
usr.id
FROM
{$CFG->prefix}role_assignments AS r_asg
JOIN {$CFG->prefix}role AS role ON role.id = r_asg.roleid
JOIN {$CFG->prefix}user AS usr ON usr.id = r_asg.userid
JOIN {$CFG->prefix}context AS ctx ON ctx.id = r_asg.contextid
WHERE
role.name in ('editingteacher','teacher')
AND ctx.contextlevel = ". CONTEXT_COURSE."
AND ctx.instanceid IN (
SELECT DISTINCT
ctx.instanceid
FROM
{$CFG->prefix}role_assignments AS r_asg
JOIN {$CFG->prefix}role AS role ON role.id = r_asg.roleid
JOIN {$CFG->prefix}user AS usr ON usr.id = r_asg.userid
JOIN {$CFG->prefix}context AS ctx ON ctx.id = r_asg.contextid
WHERE
role.name in ('student','auditor_student')
AND ctx.contextlevel = ". CONTEXT_COURSE."
AND usr.id = $for_user_id
)))
".
###############################
# NOTE: this is Williams specific, and is how access by grad year is
# handled in our system (i.e. via Williams specific tables) - I've
# left this code in a comment in case you want to implement a similar
# thing on your own system.
###############################
# "UNION
# SELECT DISTINCT
# a.sheet_id".
#($includeAccessRecords?'
# ,a.id AS id
# ,a.created_at AS created_at
# ,a.updated_at AS updated_at
# ,a.last_user_id AS last_user_id
# ,a.type AS type
# ,a.constraint_id AS constraint_id
# ,a.constraint_data AS constraint_data
# ,a.broadness AS broadness':'')."
# FROM
# {$CFG->prefix}sus_access AS a
# JOIN wms_card_ps_users AS wcpu ON wcpu.login_id = '{$USER->username}' AND wcpu.wms_class=a.constraint_data
# WHERE
# a.type='bygradyear'".
###############################
") AS ac ON s.id = ac.sheet_id
WHERE
s.flag_deleted != 1
AND s.date_closes > ".(mktime()-86400).
//(($for_sheet_id>0 || $for_access_id>0)?"WHERE ":'').
(($for_access_id > 0)?"\n AND ac.id = $for_access_id":'').
//(($for_sheet_id>0 && $for_access_id>0)?" AND ":'').
(($for_sheet_id > 0)?"\n AND s.id = $for_sheet_id":'')."
ORDER BY
s.name".
($includeAccessRecords?'
,ac.broadness DESC':'');
//echo "\n<pre>\nsql is $sql</pre>\n\n";
debug(5,"\n<pre>\nsql is $sql</pre>\n\n");
return sus_get_records_sql($sql);
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: an optional sheet group id
// returns: an array of sheet group objects belonging to the current user
function getSheetGroup($group_id=0)
{
global $USER, $CFG, $COURSE, $SHEET_GROUP_FIELDS;
$sql = "
SELECT
$SHEET_GROUP_FIELDS
FROM
{$CFG->prefix}sus_sheetgroups AS sg
WHERE sg.flag_deleted != 1
AND sg.owner_user_id = $USER->id
".($group_id?" AND sg.id=$group_id":'');
return sus_get_records_sql($sql);
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: a sheet group id, and an assoc array of info about the sheet group,
// with any or all of (deleted, owner_user_id, flag_is_default, name, description,
// max_total_user_signups, max_pending_user_signups)
// does: updates the sheet group of that id with the provided info
// returns: true on success, false on failure
function updateSheetGroup($sheet_group_id, $group_info)
{
global $CFG;
$sql = "UPDATE {$CFG->prefix}sus_sheetgroups\n SET updated_at=".time();
foreach ($group_info as $field=>$value)
{
$sql .= "\n ,$field=".quote_smart($value);
}
$sql .= "\n WHERE id=$sheet_group_id";
return execute_sql($sql, false); // second param turns off direct-to-screen feedback
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: a sheet group id, and an assoc array of info about the sheet group,
// with (name, description, max_total_user_signups, max_pending_user_signups)
// does: sheet group of that id with the provided info
// returns: the ID of the newly added sheet group, or 0 on failure
function addSheetGroup($group_info)
{
global $USER,$CFG;
$group_info['created_at'] = time();
$group_info['updated_at'] = time();
$group_info['flag_deleted'] = 0;
$group_info['owner_user_id'] = $USER->id;
return insert_record('sus_sheetgroups',(object)$group_info);
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: a sheet group id
// does: marks that sheet group as deleted
// returns: true on success, false on failure
function deleteSheetGroup($group_id)
{
$the_sheets = getOwnedSheetsAndAssociatedInfo($group_id);
//print_r($the_sheets);
foreach ($the_sheets as $sheet_plus) {
if ($sheet_plus->s_id) { deleteSheet($sheet_plus->s_id); }
}
updateSheetGroup($group_id, array('flag_deleted' => 1));
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: and optional prefix to use with the field names
// returns: an array containing a single object, which is a sparse (sheet info only) SheetsAndAssociatedInfo item for a new sheet group
function newSheetGroup($prefix='')
{
global $USER;
$newgroup = array(
$prefix.'flag_deleted' => 0,
$prefix.'owner_user_id' => $USER->id,
$prefix.'name' => 'New Sheet Group',
$prefix.'description' => 'description of new sheet group',
$prefix.'max_g_total_user_signups' => -1,
$prefix.'max_g_pending_user_signups' => -1
);
return array((object)$newgroup);
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: as sheet group id and optional prefix to use with the field names
// returns: an array containing a single object, which is a sparse (sheet info only) SheetsAndAssociatedInfo item for a new sheet group
function newSheet($group_id,$prefix='')
{
global $USER;
$newsheet = array(
$prefix.'created_at' => time(),
$prefix.'updated_at' => time(),
$prefix.'flag_deleted' => 0,
$prefix.'owner_user_id' => $USER->id,
$prefix.'last_user_id' => $USER->id,
$prefix.'sus_sheetgroup_id' => $group_id,
$prefix.'name' => 'New Sheet',
$prefix.'description' => 'description and/or instructions for new sheet',
$prefix.'type' => 'TIME',
$prefix.'date_opens' => time(),
$prefix.'date_closes' => time(),
$prefix.'max_total_user_signups' => -1,
$prefix.'max_pending_user_signups' => -1,
$prefix.'flag_alert_owner_change' => 1,
$prefix.'flag_alert_owner_signup' => 1,
$prefix.'flag_alert_owner_imminent' => 1,
$prefix.'flag_alert_admin_change' => 1,
$prefix.'flag_alert_admin_signup' => 1,
$prefix.'flag_alert_admin_imminent' => 1,
$prefix.'flag_private_signups' => 1
);
$groups = getSheetGroup($group_id);
$g = $groups[0];
$newsheet['group'] = $g;
// foreach ((array)$g as $p => $v)
// {
// $newsheet[$p] = $v;
// }
return array((object)$newsheet);
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: an assoc array of info about the sheet group,
// with (name, description, max_total_user_signups, max_pending_user_signups)
// does: adds a record to the DB
// returns: the ID of the newly added sheet, or 0 on failure
function addSheet($sheet_info)
{
global $USER,$CFG;
$sheet_info['created_at'] = time();
$sheet_info['updated_at'] = time();
$sheet_info['flag_deleted'] = 0;
$sheet_info['owner_user_id'] = $USER->id;
$sheet_info['last_user_id'] = $USER->id;
return insert_record('sus_sheets',(object)$sheet_info,true);
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: a sheet id
// does: marks that sheet as deleted
// returns: true on success, false on failure
function deleteSheet($sheet_id)
{
updateSheet($sheet_id, array('flag_deleted' => 1));
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: a sheet id, and an assoc array of info about the sheet,
// with any or all of (deleted, owner_user_id, last_user_id, sus_sheet_group_id, name,
// description, type, date_opens, date_closes, max_total_user_signups,
// max_pending_user_signups, flag_alert_owner_change, flag_alert_owner_signup,
// flag_alert_owner_imminent, flag_alert_admin_change, flag_alert_admin_signup,
// flag_alert_admin_imminent)
// does: updates the sheet of that id with the provided info
// returns: true on success, false on failure
function updateSheet($sheet_id, $sheet_info)
{
global $CFG;
$sql = "UPDATE {$CFG->prefix}sus_sheets\n SET updated_at=".time();
foreach ($sheet_info as $field=>$value)
{
$sql .= "\n ,$field=".quote_smart($value);
}
$sql .= "\n WHERE id=$sheet_id";
if (execute_sql($sql, false)) // second param turns off direct-to-screen feedback
{
// base sheet info updated, now handle the acceess and admin control?
// OR that's handled entirely by ajax from the sheet edit page?
return true;
debug_r(5,$_REQUEST);
} else
{
return false;
}
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: a sheet id
// returns: a single object, which is an opening item for a new sheet opening
function newOpening($sheet_id)
{
global $USER;
$newopening = array(
'created_at' => time(),
'updated_at' => time(),
'flag_deleted' => 0,
'last_user_id' => $USER->id,
'sus_sheet_id' => $sheet_id,
'opening_set_id' => '',
'name' => '',
'description' => '',
'max_signups' => '',
'admin_comment' => '',
'begin_datetime' => '',
'end_datetime' => '',
'location' => ''
);
return (object)$newopening;
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: an opening object (as returned from newOpening)
// does: adds that opening to the DB
// returns: the ID of the newly added opening, or <= 0 on failure
// 0 = generic failure
// -1 = timing conflict
function addOpening($opening)
{
global $CFG;
// first check that there's no timing conflict
$sql = "
SELECT
o.id AS o_id
FROM
{$CFG->prefix}sus_openings AS o
WHERE
o.sus_sheet_id = {$opening->sus_sheet_id}
AND o.flag_deleted != 1
AND
(
o.begin_datetime BETWEEN {$opening->begin_datetime} AND {$opening->end_datetime}-1
OR o.end_datetime BETWEEN {$opening->begin_datetime}+1 AND {$opening->end_datetime}
OR {$opening->begin_datetime} BETWEEN o.begin_datetime AND o.end_datetime-1
OR {$opening->end_datetime} BETWEEN o.begin_datetime+1 AND o.end_datetime
)
";
// OR o.begin_datetime = {$opening->begin_datetime}
// OR o.end_datetime = {$opening->end_datetime}
//echo "\n<pre>\nsql is $sql</pre>\n\n";
debug(5,"\n<pre>\nsql is $sql</pre>\n\n");
log_debug(5,"\n<pre>\nsql is $sql</pre>\n\n");
$res = sus_get_records_sql($sql);
if ((count($res) > 0) && ($res[0] != ''))
{
log_debug(1,"timing conflict with at least ".$res[0]->o_id);
return -1;
}
return insert_record('sus_openings',$opening,true);
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: an id, and an assoc array of info about the opening
// with any or all of (deleted, name, description, max_signups,
// admin_comment,location,begin_datetime,end_datetime)
// does: updates the opening of that id with the provided info
// returns: true on success, false on failure
function updateOpening($opening_id, $opening_info)
{
global $CFG,$USER;
$sql = "UPDATE {$CFG->prefix}sus_openings\n SET updated_at=".time().",last_user_id=".$USER->id;
foreach ($opening_info as $field=>$value)
{
$sql .= "\n ,$field=".quote_smart($value);
}
$sql .= "\n WHERE id=$opening_id";
debug(5,$sql);
if (execute_sql($sql, false)) // second param turns off direct-to-screen feedback
{
return true;
} else
{
log_debug(1,"opening update failed on:\n$sql\nwith ".mysql_err());
return false;
}
}
////////////////////////////////////////////////////////////////////////////////////////
# takes: a sheet id, an optional opening_id, an optional time value (i.e. a full datetime
# value, as returned from time() or mktime()), and an optional flag
# for retrieving sheet info as well as opening info
# returns: an array of opening objects for that sheet. If an opening
# id is given, returns only info for that opening. If the date is
# given, gets only the openings on that date, otherwise all openings
# for that sheet. If $flag_get_sheet_info is set, also gets
# denormalized sheet data.
function getOpenings($sheet_id,$opening_id=0,$datetime=0,$flag_get_sheet_info=false)
{
global $CFG,$OPENING_FIELDS,$SHEET_FIELDS;
$grouper = $OPENING_FIELDS .($flag_get_sheet_info?"
,$SHEET_FIELDS":'');
// strip the AS s_foo from the grouping clause fields
$grouper = preg_replace("/\\s+AS \\w+(\n|$)/","\n",$grouper);
debug(5,"grouper is $grouper");
$sql = "
SELECT
$OPENING_FIELDS
,COUNT(su.id) AS o_num_signups".
($flag_get_sheet_info?"
,$SHEET_FIELDS":'')."
FROM
{$CFG->prefix}sus_openings AS o
JOIN {$CFG->prefix}sus_sheets AS s ON s.id=o.sus_sheet_id
LEFT OUTER JOIN {$CFG->prefix}sus_signups AS su ON su.sus_opening_id = o.id
WHERE s.flag_deleted != 1
AND s.id = $sheet_id
AND o.flag_deleted != 1
AND IFNULL(su.flag_deleted,0) != 1
".($datetime?" AND FROM_UNIXTIME(o.begin_datetime,'%Y%m%d')= FROM_UNIXTIME($datetime,'%Y%m%d')\n":'')
.($opening_id?" AND o.id= $opening_id\n":'').
"
GROUP BY
$grouper
ORDER BY
o.begin_datetime";
// error("\n\nsql is $sql\n\n");
debug(5,"\n\nsql is $sql\n\n");
return sus_get_records_sql($sql);
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: an opening id
// does: marks that opening as deleted
// returns: true on success, false otherwise
function removeOpening($opening_id){
global $CFG;
$sql = "UPDATE {$CFG->prefix}sus_openings\n SET updated_at=".time().",flag_deleted=1\n WHERE id=$opening_id";
$res = execute_sql($sql, false); // second param turns off direct-to-screen feedback
$sql = "UPDATE {$CFG->prefix}sus_signups\n SET updated_at=".time().",flag_deleted=1\n WHERE sus_opening_id=$opening_id";
return ($res && execute_sql($sql, false));
}
////////////////////////////////////////////////////////////////////////////////////////
// TODO - add flags for including opening and sheet info, and implement that of course
// takes: a signup id
// returns: the signup object for that it
function getSignup($signup_id)
{
global $CFG,$SIGNUP_FIELDS;
$sql="
SELECT
$SIGNUP_FIELDS
FROM
{$CFG->prefix}sus_signups AS su
WHERE
su.id=$signup_id";
$res = sus_get_records_sql($sql);
if (! $res) { return false; }
return $res[0];
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: an opening id, a user id or name
// returns: a single object, which is a blank access item for a the given sheet
function newSignup($opening_id,$user_id_or_name)
{
global $USER;
if (! preg_match('/^\\d+$/',$user_id_or_name))
{
global $CFG;
$sql = "SELECT u.id AS uid FROM {$CFG->prefix}user AS u WHERE u.username = '$user_id_or_name'";
$uids = sus_get_records_sql($sql);
if (! $uids)
{
return false;
}
$user_id_or_name = $uids[0]->uid;
}
$newsignup = array(
'created_at' => time(),
'updated_at' => time(),
'flag_deleted' => 0,
'last_user_id' => $USER->id,
'sus_opening_id' => $opening_id,
'signup_user_id' => $user_id_or_name,
'admin_comment' => ''
);
return (object)$newsignup;
}
/////////////////////////////////////////////////////////////////////////////////////////
// takes: a signup object (as returned from newSignup)
// does: adds that signup to the DB
// returns: the ID of the newly added signup, or 0 on failure
function addSignup($signup)
{
return insert_record('sus_signups',$signup,true);
}
/////////////////////////////////////////////////////////////////////////////////////////
// takes: a signup id
// does: marks that signup as deleted
// returns: true on success, false otherwise
function removeSignup($signup_id)
{
global $CFG;
$sql = "UPDATE {$CFG->prefix}sus_signups\n SET updated_at=".time().",flag_deleted=1\n WHERE id=$signup_id";
return execute_sql($sql, false); // second param turns off direct-to-screen feedback
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: a signup id, and an assoc array of info about the signup
// with any or all of (flag_deleted,admin_comment)
// does: updates the signup of that id with the provided info
// returns: true on success, false on failure
function updateSignup($su_id, $su_info)
{
global $CFG,$USER;
$cond = '';
$sql = "UPDATE {$CFG->prefix}sus_signups\n SET updated_at=".time().",last_user_id=".$USER->id;
foreach ($su_info as $field=>$value)
{
$sql .= "\n ,$field=".quote_smart($value);
$cond .= " AND $field != ".quote_smart($value);
}
$sql .= "\n WHERE id=$su_id $cond";
return execute_sql($sql, false); // second param turns off direct-to-screen feedback
}
////////////////////////////////////////////////////////////////////////////////////////
// takes: a user ID, an optional from date in YYYYMMDD format, an optional to date in YYYYMMDD format
// returns: an array of signup data for that user (i.e. sign-ups of that user)
// if from date is provided, all returned are on or after the from date
// if to date is provided, all returned are on or before the to date
function getSignupsBySignee($user_id,$from_ymd='',$to_ymd='')
{
global $CFG;
// convert dates to unix timestamps for faster queries
if ($from_ymd)
{
$from_ymd = mktime(0,1,1,substr($from_ymd,4,2),substr($from_ymd,6,2),substr($from_ymd,0,4));
}
if ($to_ymd)
{
$to_ymd = mktime(0,1,1,substr($to_ymd,4,2),substr($to_ymd,6,2),substr($to_ymd,0,4));
}
$sql = "
SELECT
su.id AS su_id
,o.id AS o_id
,o.sus_sheet_id AS o_sus_sheet_id
,o.name AS o_name
,o.description AS o_description
,o.max_signups AS o_max_signups
,COUNT(su2.id) AS o_num_signups
,o.location AS o_location
,o.begin_datetime AS o_begin_datetime
,o.end_datetime AS o_end_datetime
,o.end_datetime - o.begin_datetime AS o_dur_seconds
,FROM_UNIXTIME(o.begin_datetime,'%Y%m%d') AS o_dateymd
,FROM_UNIXTIME(o.begin_datetime,'%Y-%m-%d') AS o_date_y_m_d
,FROM_UNIXTIME(o.begin_datetime,'%l:%i %p') AS o_begin_time_h_m_p
,FROM_UNIXTIME(o.end_datetime,'%l:%i %p') AS o_end_time_h_m_p
,FROM_UNIXTIME(o.begin_datetime,'%k:%i') AS o_begin_time_h24_m
,FROM_UNIXTIME(o.end_datetime,'%k:%i') AS o_end_time_h24_m
,s.id AS s_id
,s.name AS s_name
,s.description AS s_description
,s.max_total_user_signups AS s_max_total_user_signups
,s.max_pending_user_signups AS s_max_pending_user_signups
,sg.id AS sg_id
,sg.name AS sg_name
,sg.description AS sg_description
,sg.max_g_total_user_signups AS sg_max_g_total_user_signups
,sg.max_g_pending_user_signups AS sg_max_g_pending_user_signups
FROM
{$CFG->prefix}sus_signups AS su
JOIN {$CFG->prefix}sus_openings AS o ON o.id = su.sus_opening_id
JOIN {$CFG->prefix}sus_signups AS su2 ON su2.sus_opening_id = o.id AND su2.flag_deleted != 1
JOIN {$CFG->prefix}sus_sheets AS s ON s.id = o.sus_sheet_id
JOIN {$CFG->prefix}sus_sheetgroups AS sg ON sg.id = s.sus_sheetgroup_id
WHERE