forked from Wunderbyte-GmbH/moodle-mod_booking
/
locallib.php
executable file
·1437 lines (1195 loc) · 56.3 KB
/
locallib.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
require_once($CFG->dirroot . '/user/selector/lib.php');
/**
* Standard base class for mod_booking
*
* Module was originally programmed for 1.9 but further adjustments should be made with
* new Moodle 2.X coding style using this base class.
*
* @package mod_booking
* @copyright 2013 David Bogner {@link http://www.edulabs.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class booking {
/** @var id booking id */
public $id = 0;
/** @var context the context of the course module for this booking instance (or just the course if we are
creating a new one) */
protected $context = null;
/** @var stdClass the course this booking instance belongs to */
public $course = null;
/** @var stdClass the course module for this assign instance */
public $cm = null;
/** @var array of user objects who have capability to book. object contains only id */
public $canbookusers = array();
/** @var array users who are members of the current users group */
public $groupmembers = array();
/** @var booking booking object from booking instance settings */
public $booking;
/**
* Constructor for the booking class
*
* @param mixed $context context|null the course module context (or the course context if the coursemodule has not been created yet)
* @param mixed $coursemodule the current course module if it was already loaded - otherwise this class will load one from the context as required
* @param mixed $course the current course if it was already loaded - otherwise this class will load one from the context as required
*/
public function __construct($cmid) {
global $DB, $USER;
$this->cm = get_coursemodule_from_id('booking', $cmid, 0, false, MUST_EXIST);
$this->id = $this->cm->instance;
$this->context = context_module::instance($this->cm->id);
$this->course = $DB->get_record('course', array('id' => $this->cm->course), 'id, fullname, shortname, groupmode, groupmodeforce, visible', MUST_EXIST);
$this->booking = $DB->get_record("booking", array("id" => $this->id));
// if the course has groups and I do not have the capability to see all groups, show only users of my groups
if ($this->course->groupmode !== 0 && !has_capability('moodle/site:accessallgroups', $this->context)) {
$this->groupmembers = $this::booking_get_groupmembers($this->course->id);
}
}
public function apply_tags() {
$tags = new booking_tags($this->cm);
$this->booking = $tags->bookingReplace($this->booking);
}
/**
* get all the user ids who are allowed to book
* capability mod/booking:choose
* available in $htis->canbookusers
*/
public function get_canbook_userids() {
//TODO check if course has guest access if not get all enrolled users and check with has_capability if user has right to book
//$this->canbookusers = get_users_by_capability($this->context, 'mod/booking:choose', 'u.id', 'u.lastname ASC, u.firstname ASC', '', '', '', '', true, true);
$this->canbookusers = get_enrolled_users($this->context, 'mod/booking:choose', null, 'u.id');
}
/**
* get all group members of $USER (of all groups $USER belongs to)
*
* @param int $courseid
* @return array: all members of all groups $USER belongs to
*/
public static function booking_get_groupmembers($courseid) {
global $USER, $DB;
$groupmembers = array();
$usergroups = groups_get_all_groups($courseid, $USER->id);
if (!empty($usergroups)) {
$groupsparam = implode(',', array_keys($usergroups));
$groupmembers = $DB->get_records_sql("SELECT u.id
FROM {user} u, {groups_members} gm
WHERE u.id = gm.userid AND gm.groupid IN (?)
ORDER BY lastname ASC", array($groupsparam));
}
return $groupmembers;
}
}
/**
* Managing a single booking option
* @package mod-booking
* @copyright 2014 David Bogner
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class booking_option extends booking {
/** @var array of the users booked for this option key userid */
public $bookedusers = array();
/** @var array of booked users visible to the current user (group members) */
public $bookedvisibleusers = array();
/** @var array of users that can be subscribed to that booking option if groups enabled, only members of groups user has access to are shown */
public $potentialusers = array();
public $optionid = null;
/** @var booking option confix object */
public $option = null;
/** @var number of answers */
public $numberofanswers = null;
/** @var array of users filters */
public $filters = array();
/** @var array of users objects - filtered */
public $users = array();
public $usersOnList = array();
public $usersOnWaitingList = array();
// Pagination
public $page = 0;
public $perpage = 0;
public $canBookToOtherBooking = 0;
/**
* Creates basic booking option
*
* @param int $id cm id
* @param int $optionid
* @param object $option option object
*/
public function __construct($id, $optionid, $filters = array(), $page = 0, $perpage = 0) {
global $DB;
parent::__construct($id);
$this->optionid = $optionid;
$this->update_booked_users();
$this->option = $DB->get_record('booking_options', array('id' => $optionid), '*', 'MUST_EXIST');
$this->filters = $filters;
$this->page = $page;
$this->perpage = $perpage;
$this->get_users();
$this->calculateHowManyCanBookToOther();
}
public function calculateHowManyCanBookToOther() {
global $DB;
if (isset($this->option->conectedoption) && $this->option->conectedoption > 0) {
$alredyBooked = 0;
$result = $DB->get_records_sql('SELECT answers.userid FROM {booking_answers} AS answers INNER JOIN {booking_answers} AS parent on parent.userid = answers.userid WHERE answers.optionid = ? AND parent.optionid = ?', array($this->optionid, $this->option->conectedoption));
$alredyBooked = count($result);
$keys = array();
foreach ($result as $value) {
$keys[] = $value->userid;
}
foreach ($this->usersOnWaitingList as $user) {
if (in_array($user->userid, $keys)) {
$user->bookedToOtherBooking = 1;
} else {
$user->bookedToOtherBooking = 0;
}
}
foreach ($this->usersOnList as $user) {
if (in_array($user->userid, $keys)) {
$user->usersOnList = 1;
} else {
$user->usersOnList = 0;
}
}
$this->canBookToOtherBooking = (int) $this->option->howmanyusers - (int) $alredyBooked;
} else {
$this->canBookToOtherBooking = 0;
}
}
public function apply_tags() {
parent::apply_tags();
$tags = new booking_tags($this->cm);
$this->option = $tags->optionReplace($this->option);
}
public function get_url_params() {
$bu = new booking_utils();
$params = $bu->generate_params($this->booking, $this->option);
$this->option->pollurl = $bu->get_body($params, 'pollurl', $params, TRUE);
$this->option->pollurlteachers = $bu->get_body($params, 'pollurlteachers', $params, TRUE);
}
// Get all users with filters
private function get_users() {
global $DB;
$params = array();
$options = "{booking_answers}.optionid = :optionid";
$params['optionid'] = $this->optionid;
if (isset($this->filters['searchFinished']) && strlen($this->filters['searchFinished']) > 0) {
$options .= " AND {booking_answers}.completed = :completed";
$params['completed'] = $this->filters['searchFinished'];
}
if (isset($this->filters['searchDate']) && $this->filters['searchDate'] == 1) {
$options .= " AND FROM_UNIXTIME({booking_answers}.timecreated, '%Y') = :searchdateyear AND FROM_UNIXTIME({booking_answers}.timecreated, '%m') = :searchdatemonth AND FROM_UNIXTIME({booking_answers}.timecreated, '%d') = :searchdateday";
$params['searchdateyear'] = $this->filters['searchDateYear'];
$params['searchdatemonth'] = $this->filters['searchDateMonth'];
$params['searchdateday'] = $this->filters['searchDateDay'];
}
if (isset($this->filters['searchName']) && strlen($this->filters['searchName']) > 0) {
$options .= " AND {user}.firstname LIKE :searchname";
$params['searchname'] = '%' . $this->filters['searchName'] . '%';
}
if (isset($this->filters['searchSurname']) && strlen($this->filters['searchSurname']) > 0) {
$options .= " AND {user}.lastname LIKE :searchsurname";
$params['searchsurname'] = '%' . $this->filters['searchSurname'] . '%';
}
$mainuserfields = explode(',', user_picture::fields());
foreach ($mainuserfields as $key => $value) {
$mainuserfields[$key] = '{user}.' . $value;
}
$mainuserfields = implode(', ', $mainuserfields);
$this->users = $DB->get_records_sql('SELECT {booking_answers}.id AS aid, {booking_answers}.bookingid, {booking_answers}.userid, {booking_answers}.optionid, {booking_answers}.timemodified, {booking_answers}.completed, {booking_answers}.timecreated, {booking_answers}.waitinglist, ' . $mainuserfields . ', CONCAT({user}.firstname, \' \', {user}.lastname) AS fullname FROM {booking_answers} LEFT JOIN {user} ON {booking_answers}.userid = {user}.id WHERE ' . $options . ' ORDER BY {booking_answers}.optionid, {booking_answers}.timemodified DESC', $params, $this->perpage * $this->page, $this->perpage);
foreach ($this->users as $user) {
if ($user->waitinglist == 1) {
$this->usersOnWaitingList[] = $user;
} else {
$this->usersOnList[] = $user;
}
}
}
// Get all users...filtered!
public function get_all_users() {
global $DB;
$params = array();
$options = "{booking_answers}.optionid = :optionid";
$params['optionid'] = $this->optionid;
if (isset($this->filters['searchFinished']) && strlen($this->filters['searchFinished']) > 0) {
$options .= " AND {booking_answers}.completed = :completed";
$params['completed'] = $this->filters['searchFinished'];
}
if (isset($this->filters['searchDate']) && $this->filters['searchDate'] == 1) {
$options .= " AND FROM_UNIXTIME({booking_answers}.timecreated, '%Y') = :searchdateyear AND FROM_UNIXTIME({booking_answers}.timecreated, '%m') = :searchdatemonth AND FROM_UNIXTIME({booking_answers}.timecreated, '%d') = :searchdateday";
$params['searchdateyear'] = $this->filters['searchDateYear'];
$params['searchdatemonth'] = $this->filters['searchDateMonth'];
$params['searchdateday'] = $this->filters['searchDateDay'];
}
if (isset($this->filters['searchName']) && strlen($this->filters['searchName']) > 0) {
$options .= " AND {user}.firstname LIKE :searchname";
$params['searchname'] = '%' . $this->filters['searchName'] . '%';
}
if (isset($this->filters['searchSurname']) && strlen($this->filters['searchSurname']) > 0) {
$options .= " AND {user}.lastname LIKE :searchsurname";
$params['searchsurname'] = '%' . $this->filters['searchSurname'] . '%';
}
$mainuserfields = user_picture::fields('{user}', NULL);
return $DB->get_records_sql('SELECT {booking_answers}.id AS aid, {booking_answers}.bookingid, {booking_answers}.userid, {booking_answers}.optionid, {booking_answers}.timemodified, {booking_answers}.completed, {booking_answers}.timecreated, {booking_answers}.waitinglist, ' . $mainuserfields . ' FROM {booking_answers} LEFT JOIN {user} ON {booking_answers}.userid = {user}.id WHERE ' . $options . ' ORDER BY {booking_answers}.optionid, {booking_answers}.timemodified ASC', $params);
}
// Count, how man users...for pagination.
public function count_users() {
global $DB;
$params = array();
$options = "{booking_answers}.optionid = :optionid";
$params['optionid'] = $this->optionid;
if (isset($this->filters['searchFinished']) && strlen($this->filters['searchFinished']) > 0) {
$options .= " AND {booking_answers}.completed = :completed";
$params['completed'] = $this->filters['searchFinished'];
}
if (isset($this->filters['searchDate']) && $this->filters['searchDate'] == 1) {
$options .= " AND FROM_UNIXTIME({booking_answers}.timecreated, '%Y') = :searchdateyear AND FROM_UNIXTIME({booking_answers}.timecreated, '%m') = :searchdatemonth AND FROM_UNIXTIME({booking_answers}.timecreated, '%d') = :searchdateday";
$params['searchdateyear'] = $this->filters['searchDateYear'];
$params['searchdatemonth'] = $this->filters['searchDateMonth'];
$params['searchdateday'] = $this->filters['searchDateDay'];
}
if (isset($this->filters['searchName']) && strlen($this->filters['searchName']) > 0) {
$options .= " AND {user}.firstname LIKE :searchname";
$params['searchname'] = '%' . $this->filters['searchName'] . '%';
}
if (isset($this->filters['searchSurname']) && strlen($this->filters['searchSurname']) > 0) {
$options .= " AND {user}.lastname LIKE :searchsurname";
$params['searchsurname'] = '%' . $this->filters['searchSurname'] . '%';
}
$count = $DB->get_record_sql('SELECT COUNT(*) AS count FROM {booking_answers} LEFT JOIN {user} ON {booking_answers}.userid = {user}.id WHERE ' . $options . ' ORDER BY {booking_answers}.optionid, {booking_answers}.timemodified ASC', $params);
return (int) $count->count;
}
/**
* Updates canbookusers and bookedusers does not check the status (booked or waitinglist)
* Just gets the registered booking from database
* Calculates the potential users (bookers able to book, but not yet booked)
*/
public function update_booked_users() {
global $DB;
if (empty($this->canbookusers)) {
$this->get_canbook_userids();
}
$mainuserfields = user_picture::fields('u', null);
$sql = "SELECT $mainuserfields, ba.id AS answerid, ba.optionid, ba.bookingid
FROM {booking_answers} ba, {user} u
WHERE ba.userid = u.id AND
u.deleted = 0 AND
ba.bookingid = ? AND
ba.optionid = ?
ORDER BY ba.timemodified ASC";
$params = array($this->id, $this->optionid);
/** it is possible that the cap mod/booking:choose has been revoked after the user has booked
* Therefore do not count them as booked users.
*/
$allanswers = $DB->get_records_sql($sql, $params);
$this->bookedusers = array_intersect_key($allanswers, $this->canbookusers);
// TODO offer users with according caps to delete excluded users from booking option
//$excludedusers = array_diff_key($allanswers, $this->canbookusers);
$this->numberofanswers = count($this->bookedusers);
if (!empty($this->groupmembers) && !(has_capability('moodle/site:accessallgroups', $this->context))) {
$this->bookedvisibleusers = array_intersect_key($this->bookedusers, $this->groupmembers);
$canbookgroupmembers = array_intersect_key($this->canbookusers, $this->groupmembers);
$this->potentialusers = array_diff_key($canbookgroupmembers, $this->bookedusers);
} else if (has_capability('moodle/site:accessallgroups', $this->context)) {
$this->bookedvisibleusers = $this->bookedusers;
$this->potentialusers = array_diff_key($this->canbookusers, $this->bookedusers);
}
$this->sort_answers();
}
/**
* add booked/waitinglist info to each userobject of users
*/
public function sort_answers() {
if (!empty($this->bookedusers) && null != $this->option) {
foreach ($this->bookedusers as $rank => $userobject) {
$userobject->bookingcmid = $this->cm->id;
if (!$this->option->limitanswers) {
$userobject->booked = 'booked';
}
// rank starts at 0 so add + 1 to corespond to max answer settings
if ($this->option->maxanswers < ($rank + 1) && $rank + 1 <= ($this->option->maxanswers + $this->option->maxoverbooking)) {
$userobject->booked = 'waitinglist';
} else if ($rank + 1 <= $this->option->maxanswers) {
$userobject->booked = 'booked';
}
}
}
}
/**
* Mass delete all responses
* @param $users Array of users
* @return void
*/
public function delete_responses($users = array()) {
global $DB;
if (!is_array($users) || empty($users)) {
return false;
}
foreach ($users as $userid) {
$this->user_delete_response($userid);
}
return TRUE;
}
/**
* Deletes a single booking of a user if user cancels the booking, sends mail to bookingmanager. If there is a limit
* book other user ans send mail to him.
* @param $userid
* @return true if booking was deleted successfully, otherwise false
*/
public function user_delete_response($userid) {
global $USER, $DB;
if (!$DB->delete_records('booking_answers', array('userid' => $userid, 'optionid' => $this->optionid))) {
return false;
}
if ($userid == $USER->id) {
$user = $USER;
} else {
$user = $DB->get_record('user', array('id' => $userid));
}
/** log deletion of user * */
$event = \mod_booking\event\booking_cancelled::create(array(
'objectid' => $this->optionid,
'context' => context_module::instance($this->cm->id),
'relateduserid' => $user->id,
'other' => array('userid' => $user->id)
));
$event->trigger();
booking_check_unenrol_user($this->option, $this->booking, $user->id);
$params = booking_generate_email_params($this->booking, $this->option, $user, $this->cm->id);
$messagetext = get_string('deletedbookingmessage', 'booking', $params);
if ($userid == $USER->id) {
// I canceled the booking
$deletedbookingusermessage = booking_get_email_body($this->booking, 'userleave', 'userleavebookedmessage', $params);
$subject = get_string('userleavebookedsubject', 'booking', $params);
} else {
// Booking manager canceled the booking
$deletedbookingusermessage = booking_get_email_body($this->booking, 'deletedtext', 'deletedbookingmessage', $params);
$subject = get_string('deletedbookingsubject', 'booking', $params);
}
$bookingmanager = $DB->get_record('user', array('username' => $this->booking->bookingmanager));
$eventdata = new stdClass();
if ($this->booking->sendmail) {
// Generate ical attachment to go with the message.
$attachname = '';
$ical = new booking_ical($this->booking, $this->option, $user, $bookingmanager);
if ($attachment = $ical->get_attachment(true)) {
$attachname = $ical->get_name();
}
$messagehtml = text_to_html($deletedbookingusermessage, false, false, true);
if (isset($this->booking->sendmailtobooker) && $this->booking->sendmailtobooker) {
$eventdata->userto = $USER;
} else {
$eventdata->userto = $user;
}
$eventdata->userfrom = $bookingmanager;
$eventdata->subject = $subject;
$eventdata->messagetext = $deletedbookingusermessage;
$eventdata->messagehtml = $messagehtml;
$eventdata->attachment = $attachment;
$eventdata->attachname = $attachname;
booking_booking_deleted($eventdata);
if ($this->booking->copymail) {
$eventdata->userto = $bookingmanager;
booking_booking_deleted($eventdata);
}
}
if ($this->option->limitanswers) {
$maxplacesavailable = $this->option->maxanswers + $this->option->maxoverbooking;
$bookedUsers = $DB->count_records("booking_answers", array('optionid' => $this->optionid, 'waitinglist' => 0));
$waitingUsers = $DB->count_records("booking_answers", array('optionid' => $this->optionid, 'waitinglist' => 1));
$allUsersCount = $bookedUsers + $waitingUsers;
if ($waitingUsers > 0 && $this->option->maxanswers > $bookedUsers) {
$newUser = $DB->get_record_sql('SELECT * FROM {booking_answers} WHERE optionid = ? AND waitinglist = 1 ORDER BY timemodified ASC', array($this->optionid), IGNORE_MULTIPLE);
$newUser->waitinglist = 0;
$DB->update_record("booking_answers", $newUser);
booking_check_enrol_user($this->option, $this->booking, $newUser->userid);
if ($this->booking->sendmail == 1 || $this->booking->copymail) {
$newbookeduser = $DB->get_record('user', array('id' => $newUser->userid));
$params = booking_generate_email_params($this->booking, $this->option, $newbookeduser, $this->cm->id);
$messagetextnewuser = booking_get_email_body($this->booking, 'statuschangetext', 'statuschangebookedmessage', $params);
$messagehtml = text_to_html($messagetextnewuser, false, false, true);
// Generate ical attachment to go with the message.
$attachname = '';
$ical = new booking_ical($this->booking, $this->option, $newbookeduser, $bookingmanager);
if ($attachment = $ical->get_attachment()) {
$attachname = $ical->get_name();
}
$eventdata->userto = $newbookeduser;
$eventdata->userfrom = $bookingmanager;
$eventdata->subject = get_string('statuschangebookedsubject', 'booking', $params);
$eventdata->messagetext = $messagetextnewuser;
$eventdata->messagehtml = $messagehtml;
$eventdata->attachment = $attachment;
$eventdata->attachname = $attachname;
if ($this->booking->sendmail == 1) {
booking_booking_deleted($eventdata);
}
if ($this->booking->copymail) {
$eventdata->userto = $bookingmanager;
booking_booking_deleted($eventdata);
}
}
}
}
return true;
}
/**
* "Sync" users on waiting list, based on edited option - if has limit or not.
*/
public function sync_waiting_list() {
global $DB;
if ($this->option->limitanswers) {
$nBooking = $DB->get_records_sql('SELECT * FROM {booking_answers} WHERE optionid = ? ORDER BY timemodified ASC', array($this->optionid), 0, $this->option->maxanswers);
foreach ($nBooking as $value) {
$value->waitinglist = 0;
$DB->update_record("booking_answers", $value);
}
$nOverBooking = $DB->get_records_sql('SELECT * FROM {booking_answers} WHERE optionid = ? ORDER BY timemodified ASC', array($this->optionid), $this->option->maxanswers, $this->option->maxoverbooking);
foreach ($nOverBooking as $value) {
$value->waitinglist = 1;
$DB->update_record("booking_answers", $value);
}
$nOver = $DB->get_records_sql('SELECT * FROM {booking_answers} WHERE optionid = ? ORDER BY timemodified ASC', array($this->optionid), $this->option->maxoverbooking + $this->option->maxanswers);
foreach ($nOver as $value) {
$DB->delete_records('booking_answers', array('id' => $value->id));
}
} else {
$DB->execute("UPDATE {booking_answers} SET waitinglist = 0 WHERE optionid = :optionid", array('optionid' => $this->optionid));
}
}
/**
* Saves the booking for the user
* @return boolean true if booking was possible, false if meanwhile the booking got full
*/
public function user_submit_response($user) {
global $DB;
if (null == $this->option) {
return false;
}
$waitingList = $this->check_if_limit();
if ($waitingList === FALSE) {
return FALSE;
}
$underlimit = ($this->booking->maxperuser == 0);
$underlimit = $underlimit || (booking_get_user_booking_count($this, $user, NULL) < $this->booking->maxperuser);
if (!$underlimit) {
return FALSE;
}
if (!($currentanswerid = $DB->get_field('booking_answers', 'id', array('userid' => $user->id, 'optionid' => $this->optionid)))) {
$newanswer = new stdClass();
$newanswer->bookingid = $this->id;
$newanswer->userid = $user->id;
$newanswer->optionid = $this->optionid;
$newanswer->timemodified = time();
$newanswer->timecreated = time();
$newanswer->waitinglist = $waitingList;
if (!$DB->insert_record("booking_answers", $newanswer)) {
error("Could not register your booking because of a database error");
}
//TODO replace
booking_check_enrol_user($this->option, $this->booking, $user->id);
}
$event = \mod_booking\event\bookingoption_booked::create(array(
'objectid' => $this->optionid,
'context' => context_module::instance($this->cm->id),
'relateduserid' => $user->id,
'other' => array('userid' => $user->id)
));
$event->trigger();
if ($this->booking->sendmail) {
$eventdata = new stdClass();
$eventdata->user = $user;
$eventdata->booking = $this->booking;
// TODO the next line is for backward compatibility only, delete when finished refurbishing the module ;-)
$eventdata->booking->option[$this->optionid] = $this->option;
$eventdata->optionid = $this->optionid;
$eventdata->cmid = $this->cm->id;
//TODO replace
booking_send_confirm_message($eventdata);
}
return true;
}
/**
* Check if user can enrol
* @return mixed false on full, or if can enrol or 1 for waiting list.
*/
private function check_if_limit() {
global $DB;
if ($this->option->limitanswers) {
$maxplacesavailable = $this->option->maxanswers + $this->option->maxoverbooking;
$bookedUsers = $DB->count_records("booking_answers", array('optionid' => $this->optionid, 'waitinglist' => 0));
$waitingUsers = $DB->count_records("booking_answers", array('optionid' => $this->optionid, 'waitinglist' => 1));
$allUsersCount = $bookedUsers + $waitingUsers;
if ($maxplacesavailable > $allUsersCount) {
if ($this->option->maxanswers > $bookedUsers) {
return 0;
} else {
return 1;
}
} else {
return FALSE;
}
} else {
return 0;
}
}
}
/**
* Manage the view of all booking options
* General methods for all options
* @param cmid int coursemodule id
*/
class booking_options extends booking {
/** @var array of users booked and on waitinglist $allbookedusers[optionid][sortnumber]->userobject
* no user data is stored in the object only id and booking option related data such as
* bookingvisible = true/false, booked = booked/waitinglist, optionid and bookingid */
public $allbookedusers = array();
/** @var array key: optionid numberofbookingsperoption */
public $numberofbookingsperoption;
/** @var array: config objects of options id as key */
public $options = array();
/** @var boolean verify booked users against canbook users yes/no */
protected $checkcanbookusers = true;
private $action = "showonlyactive";
/** @var array of users filters */
public $filters = array();
// Pagination
public $page = 0;
public $perpage = 0;
public $sort = ' ORDER BY bo.coursestarttime ASC';
public function __construct($cmid, $checkcanbookusers = true, $urlParams = array('searchText' => '', 'searchLocation' => '', 'searchInstitution' => '', 'searchName' => '', 'searchSurname' => ''), $page = 0, $perpage = 0) {
parent::__construct($cmid);
$this->checkcanbookusers = $checkcanbookusers;
$this->filters = $urlParams;
$this->page = $page;
$this->perpage = $perpage;
if (isset($this->filters['sort']) && $this->filters['sort'] === 1) {
$this->sort = ' ORDER BY bo.coursestarttime DESC';
}
$this->fill_options();
$this->get_options_data();
// call only when needed TODO
$this->set_booked_visible_users();
$this->add_additional_info();
}
public function get_url_params() {
$bu = new booking_utils();
$params = $bu->generate_params($this->booking);
$this->booking->pollurl = $bu->get_body($this->booking, 'pollurl', $params);
$this->booking->pollurlteachers = $bu->get_body($this->booking, 'pollurlteachers', $params);
}
private function q_params() {
global $USER, $DB;
$args = array();
$conditions = " bo.bookingid = :bookingid ";
$args['bookingid'] = $this->id;
if (!empty($this->filters['searchText'])) {
$tags = $DB->get_records_sql('SELECT * FROM {booking_tags} WHERE text LIKE :text', array('text' => '%' . $this->filters['searchText'] . '%'));
if (!empty($tags)) {
$conditions .= " AND (bo.text LIKE :text ";
$args['text'] = '%' . $this->filters['searchText'] . '%';
foreach ($tags as $tag) {
$conditions .= " OR bo.text LIKE :tag{$tag->id} ";
$args["tag{$tag->id}"] = '%[' . $tag->tag . ']%';
}
$conditions .= " ) ";
} else {
$conditions .= " AND bo.text LIKE :text ";
$args['text'] = '%' . $this->filters['searchText'] . '%';
}
}
if (!empty($this->filters['searchLocation'])) {
$conditions .= " AND bo.location LIKE :location ";
$args['location'] = '%' . $this->filters['searchLocation'] . '%';
}
if (!empty($this->filters['searchInstitution'])) {
$conditions .= " AND bo.institution LIKE :institution ";
$args['institution'] = '%' . $this->filters['searchInstitution'] . '%';
}
if (!empty($this->filters['coursestarttime'])) {
$conditions .= ' AND (coursestarttime = 0 OR coursestarttime > :coursestarttime)';
$args['coursestarttime'] = $this->filters['coursestarttime'];
}
if (!empty($this->filters['searchName'])) {
$conditions .= " AND (u.firstname LIKE :searchname OR ut.firstname LIKE :searchnamet) ";
$args['searchname'] = '%' . $this->filters['searchName'] . '%';
$args['searchnamet'] = '%' . $this->filters['searchName'] . '%';
}
if (!empty($this->filters['searchSurname'])) {
$conditions .= " AND (u.lastname LIKE :searchsurname OR ut.lastname LIKE :searchsurnamet) ";
$args['searchsurname'] = '%' . $this->filters['searchSurname'] . '%';
$args['searchsurnamet'] = '%' . $this->filters['searchSurname'] . '%';
}
if (isset($this->filters['whichview'])) {
switch ($this->filters['whichview']) {
case 'mybooking':
$conditions .= " AND ba.userid = " . $USER->id . " ";
break;
case 'showall':
break;
case 'showonlyone':
$conditions .= " AND bo.id = :optionid ";
$args['optionid'] = $this->filters['optionid'];
break;
case 'showactive':
$conditions .= " AND (bo.courseendtime > " . time() . " OR bo.courseendtime = 0) ";
break;
default:
break;
}
}
$sql = " FROM {booking_options} AS bo LEFT JOIN {booking_teachers} AS bt ON bt.optionid = bo.id LEFT JOIN {user} AS ut ON bt.userid = ut.id LEFT JOIN {booking_answers} AS ba ON bo.id = ba.optionid LEFT JOIN {user} AS u ON ba.userid = u.id WHERE {$conditions} {$this->sort}";
return array('sql' => $sql, 'args' => $args);
}
private function fill_options() {
global $DB;
$options = $this->q_params();
$this->options = $DB->get_records_sql('SELECT DISTINCT bo.* ' . $options['sql'], $options['args'], $this->perpage * $this->page, $this->perpage);
}
public function apply_tags() {
parent::apply_tags();
$tags = new booking_tags($this->cm);
foreach ($this->options as $key => $value) {
$this->options[$key] = $tags->optionReplace($this->options[$key]);
}
}
// Count, how man options...for pagination.
public function count() {
global $DB;
$options = $this->q_params();
$count = $DB->get_record_sql('SELECT COUNT(DISTINCT bo.id) AS count ' . $options['sql'], $options['args']);
return (int) $count->count;
}
// Add additional info to options (status, availspaces, taken, ...)
private function add_additional_info() {
global $DB;
$answers = $DB->get_records('booking_answers', array('bookingid' => $this->id), 'id');
$allresponses = array();
$mainuserfields = user_picture::fields('u', NULL);
$allresponses = get_users_by_capability($this->context, 'mod/booking:choose', $mainuserfields . ', u.id', 'u.lastname ASC, u.firstname ASC', '', '', '', '', true, true);
foreach ($this->options as $option) {
$count = $DB->get_record_sql('SELECT COUNT(*) AS count FROM {booking_answers} WHERE optionid = :optionid', array('optionid' => $option->id));
$option->count = (int) $count->count;
if (!$option->coursestarttime == 0) {
$option->coursestarttimetext = userdate($option->coursestarttime, get_string('strftimedatetime'));
} else {
$option->coursestarttimetext = get_string("starttimenotset", 'booking');
}
if (!$option->courseendtime == 0) {
$option->courseendtimetext = userdate($option->courseendtime, get_string('strftimedatetime'), '', false);
} else {
$option->courseendtimetext = get_string("endtimenotset", 'booking');
}
// we have to change $taken is different from booking_show_results
$answerstocount = array();
if ($answers) {
foreach ($answers as $answer) {
if ($answer->optionid == $option->id && isset($allresponses[$answer->userid])) {
$answerstocount[] = $answer;
}
}
}
$taken = count($answerstocount);
$totalavailable = $option->maxanswers + $option->maxoverbooking;
if (!$option->limitanswers) {
$option->status = "available";
$option->taken = $taken;
$option->availspaces = "unlimited";
} else {
if ($taken < $option->maxanswers) {
$option->status = "available";
$option->availspaces = $option->maxanswers - $taken;
$option->taken = $taken;
$option->availwaitspaces = $option->maxoverbooking;
} elseif ($taken >= $option->maxanswers && $taken < $totalavailable) {
$option->status = "waitspaceavailable";
$option->availspaces = 0;
$option->taken = $option->maxanswers;
$option->availwaitspaces = $option->maxoverbooking - ($taken - $option->maxanswers);
} elseif ($taken >= $totalavailable) {
$option->status = "full";
$option->availspaces = 0;
$option->taken = $option->maxanswers;
$option->availwaitspaces = 0;
}
}
if (time() > $option->bookingclosingtime and $option->bookingclosingtime != 0) {
$option->status = "closed";
}
if ($option->bookingclosingtime) {
$option->bookingclosingtime = userdate($option->bookingclosingtime, get_string('strftimedate'), '', false);
} else {
$option->bookingclosingtime = false;
}
}
}
/**
* Gives a list of booked users sorted in an array by booking option
* former get_spreadsheet_data
* @return void
*/
public function get_options_data() {
global $DB;
$context = $this->context;
/// bookinglist $bookinglist[optionid][sortnumber] = userobject;
$bookinglist = array();
$optionids = array();
$totalbookings = array();
///TODO from 2.6 on use get_all_user_name_fields() instead of user_picture
$mainuserfields = user_picture::fields('u', null);
$sql = "SELECT ba.id as answerid, $mainuserfields, ba.optionid, ba.bookingid, ba.userid, ba.timemodified, ba.completed, ba.timecreated, ba.waitinglist
FROM {booking_answers} ba
JOIN {user} u
ON ba.userid = u.id
WHERE u.deleted = 0
AND ba.bookingid = ?
ORDER BY ba.optionid, ba.timemodified ASC";
$rawresponses = $DB->get_records_sql($sql, array($this->id));
if ($rawresponses) {
if ($this->checkcanbookusers) {
if (empty($this->canbookusers)) {
$this->get_canbook_userids();
}
foreach ($rawresponses as $answerid => $userobject) {
$sortedusers[$userobject->id] = $userobject;
}
$validresponses = array_intersect_key($sortedusers, $this->canbookusers);
} else {
$validresponses = $rawresponses;
}
foreach ($validresponses as $response) {
if (isset($this->options[$response->optionid])) {
$bookinglist[$response->optionid][] = $response;
$optionids[$response->optionid] = $response->optionid;
}
}
foreach ($optionids as $optionid) {
$totalbookings[$optionid] = count($bookinglist[$optionid]);
}
}
$this->allbookedusers = $bookinglist;
$this->sort_bookings();
$this->numberofbookingsperoption = $totalbookings;
}
/**
* sorts booking options in booked users and waitinglist users
* adds the status to userobject
*/
public function sort_bookings() {
if (!empty($this->allbookedusers) && !empty($this->options)) {
foreach ($this->options as $option) {
if (!empty($this->allbookedusers[$option->id])) {
foreach ($this->allbookedusers[$option->id] as $rank => $userobject) {
$statusinfo = new stdClass();
$statusinfo->bookingcmid = $this->cm->id;
if (!$option->limitanswers) {
$statusinfo->booked = 'booked';
$userobject->status[$option->id] = $statusinfo;
} else {
if ($userobject->waitinglist) {
$statusinfo->booked = 'waitinglist';
$userobject->status[$option->id] = $statusinfo;
} else {
$statusinfo->booked = 'booked';
$userobject->status[$option->id] = $statusinfo;
}
}
}
}
}
}
}
/**
* Returns all bookings of $USER with status
* @return array of [bookingid][optionid] = userobjects:
*/
public function get_my_bookings() {
global $USER;
$mybookings = array();
if (!empty($this->allbookedusers) && !empty($this->options)) {
foreach ($this->options as $optionid => $option) {
if (!empty($this->allbookedusers[$option->id])) {
foreach ($this->allbookedusers[$option->id] as $userobject) {
if ($userobject->id == $USER->id) {
$userobject->status[$option->id]->coursename = $this->course->fullname;
$userobject->status[$option->id]->courseid = $this->course->id;
$userobject->status[$option->id]->bookingtitle = $this->booking->name;
$userobject->status[$option->id]->bookingoptiontitle = $this->options[$option->id]->text;
$mybookings[$optionid] = $userobject;
}
}
}
}
}
return $mybookings;
}
public static function booking_set_visiblefalse(&$item1, $key) {
$item1->bookingvisible = false;
}
/**
* sets $user->bookingvisible to true or false dependant on group
* member status and access all group capability
*/
public function set_booked_visible_users() {
if (!empty($this->allbookedusers)) {
if ($this->course->groupmode == 0 || has_capability('moodle/site:accessallgroups', $this->context)) {
foreach ($this->allbookedusers as $optionid => $optionusers) {