-
Notifications
You must be signed in to change notification settings - Fork 42
/
lib.php
executable file
·2363 lines (2116 loc) · 85.5 KB
/
lib.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
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Library of common module functions and constants.
*
* @package mod_booking
* @copyright 2023 Georg Maißer <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/filelib.php');
require_once($CFG->dirroot . '/group/lib.php');
require_once($CFG->dirroot . '/user/selector/lib.php');
require_once($CFG->dirroot . '/mod/booking/locallib.php');
require_once($CFG->dirroot . '/course/externallib.php');
use local_entities\entitiesrelation_handler;
use mod_booking\booking;
use mod_booking\booking_option;
use mod_booking\output\coursepage_shortinfo_and_button;
use mod_booking\singleton_service;
use mod_booking\teachers_handler;
use mod_booking\utils\wb_payment;
use mod_booking\booking_rules\rules_info;
// Default fields for bookingoptions in view.php and for download.
define('MOD_BOOKING_BOOKINGOPTION_DEFAULTFIELDS', "identifier,titleprefix,text,description,teacher,responsiblecontact," .
"showdates,dayofweektime,location,institution,course,minanswers,bookings,bookingopeningtime,bookingclosingtime");
// View params.
define('MOD_BOOKING_VIEW_PARAM_LIST', 0); // List view.
define('MOD_BOOKING_VIEW_PARAM_CARDS', 1); // Cards view.
define('MOD_BOOKING_VIEW_PARAM_LIST_IMG_LEFT', 2); // List view with image on the left.
define('MOD_BOOKING_VIEW_PARAM_LIST_IMG_RIGHT', 3); // List view with image on the right.
// Currently up to 9 different price categories can be set.
define('MOD_BOOKING_MAX_PRICE_CATEGORIES', 9);
// Time to confirm booking or cancellation in seconds.
define('MOD_BOOKING_TIME_TO_CONFIRM', 20);
// Define description parameters.
define('MOD_BOOKING_DESCRIPTION_WEBSITE', 1); // Shows link button with text "book now" and no link to TeamsMeeting etc.
define('MOD_BOOKING_DESCRIPTION_CALENDAR', 2); // Shows link button with text "go to bookingoption" and meeting links via link.php.
define('MOD_BOOKING_DESCRIPTION_ICAL', 3); // Shows link with text "go to bookingoption" and meeting links via link.php for iCal.
define('MOD_BOOKING_DESCRIPTION_MAIL', 4); // Shows link with text "go to bookingoption" and meeting links via link.php...
// ...for mail placeholder {bookingdetails}.
define('MOD_BOOKING_DESCRIPTION_OPTIONVIEW', 5); // Description for booking option preview page.
// Define message parameters.
define('MOD_BOOKING_MSGPARAM_CONFIRMATION', 1);
define('MOD_BOOKING_MSGPARAM_WAITINGLIST', 2);
define('MOD_BOOKING_MSGPARAM_REMINDER_PARTICIPANT', 3);
define('MOD_BOOKING_MSGPARAM_REMINDER_TEACHER', 4);
define('MOD_BOOKING_MSGPARAM_STATUS_CHANGED', 5);
define('MOD_BOOKING_MSGPARAM_CANCELLED_BY_PARTICIPANT', 6);
define('MOD_BOOKING_MSGPARAM_CANCELLED_BY_TEACHER_OR_SYSTEM', 7);
define('MOD_BOOKING_MSGPARAM_CHANGE_NOTIFICATION', 8);
define('MOD_BOOKING_MSGPARAM_POLLURL_PARTICIPANT', 9);
define('MOD_BOOKING_MSGPARAM_POLLURL_TEACHER', 10);
define('MOD_BOOKING_MSGPARAM_COMPLETED', 11);
define('MOD_BOOKING_MSGPARAM_SESSIONREMINDER', 12);
define('MOD_BOOKING_MSGPARAM_REPORTREMINDER', 13); // Reminder sent from report.php.
define('MOD_BOOKING_MSGPARAM_CUSTOM_MESSAGE', 14);
// Define booking status parameters.
define('MOD_BOOKING_STATUSPARAM_BOOKED', 0);
define('MOD_BOOKING_STATUSPARAM_WAITINGLIST', 1);
define('MOD_BOOKING_STATUSPARAM_RESERVED', 2);
define('MOD_BOOKING_STATUSPARAM_NOTIFYMELIST', 3); // Get message when place is open.
define('MOD_BOOKING_STATUSPARAM_NOTBOOKED', 4);
define('MOD_BOOKING_STATUSPARAM_DELETED', 5);
// Params to define behavior of booking_option::update.
define('MOD_BOOKING_UPDATE_OPTIONS_PARAM_DEFAULT', 1);
define('MOD_BOOKING_UPDATE_OPTIONS_PARAM_REDUCED', 2);
define('MOD_BOOKING_UPDATE_OPTIONS_PARAM_IMPORT', 3);
// Define message controller parameters.
define('MOD_BOOKING_MSGCONTRPARAM_SEND_NOW', 1);
define('MOD_BOOKING_MSGCONTRPARAM_QUEUE_ADHOC', 2);
define('MOD_BOOKING_MSGCONTRPARAM_DO_NOT_SEND', 3);
define('MOD_BOOKING_MSGCONTRPARAM_VIEW_CONFIRMATION', 4);
// Define booking availability condition ids.
define('MOD_BOOKING_BO_COND_CONFIRMCANCEL', 170);
define('MOD_BOOKING_BO_COND_ALREADYBOOKED', 150);
define('MOD_BOOKING_BO_COND_ALREADYRESERVED', 140);
define('MOD_BOOKING_BO_COND_ISCANCELLED', 130);
define('MOD_BOOKING_BO_COND_ISBOOKABLEINSTANCE', 125);
define('MOD_BOOKING_BO_COND_ISBOOKABLE', 120);
define('MOD_BOOKING_BO_COND_ONWAITINGLIST', 110);
define('MOD_BOOKING_BO_COND_CANCELMYSELF', 105);
define('MOD_BOOKING_BO_COND_BOOKONDETAIL', 104);
define('MOD_BOOKING_BO_COND_NOTIFYMELIST', 100);
define('MOD_BOOKING_BO_COND_FULLYBOOKED', 90);
define('MOD_BOOKING_BO_COND_MAX_NUMBER_OF_BOOKINGS', 80);
define('MOD_BOOKING_BO_COND_ISLOGGEDINPRICE', 75);
define('MOD_BOOKING_BO_COND_ISLOGGEDIN', 74);
define('MOD_BOOKING_BO_COND_OPTIONHASSTARTED', 70);
define('MOD_BOOKING_BO_COND_BOOKING_TIME', 60);
define('MOD_BOOKING_BO_COND_BOOKINGPOLICY', 50);
define('MOD_BOOKING_BO_COND_SUBBOOKINGBLOCKS', 45);
define('MOD_BOOKING_BO_COND_SUBBOOKING', 40);
define('MOD_BOOKING_BO_COND_CAMPAIGN_BLOCKBOOKING', 35);
// Careful with changing these JSON COND values! They are stored.
// If changed, DB Values need to be updated.
define('MOD_BOOKING_BO_COND_JSON_ALLOWEDTOBOOKININSTANCE', 18); // We might want to moove this up?
define('MOD_BOOKING_BO_COND_JSON_ENROLLEDINCOHORTS', 17);
define('MOD_BOOKING_BO_COND_JSON_CUSTOMFORM', 16);
define('MOD_BOOKING_BO_COND_JSON_ENROLLEDINCOURSE', 15);
define('MOD_BOOKING_BO_COND_JSON_SELECTUSERS', 14);
define('MOD_BOOKING_BO_COND_JSON_PREVIOUSLYBOOKED', 13);
define('MOD_BOOKING_BO_COND_JSON_CUSTOMUSERPROFILEFIELD', 12);
define('MOD_BOOKING_BO_COND_JSON_USERPROFILEFIELD', 11);
define('MOD_BOOKING_BO_COND_CAPBOOKINGCHOOSE', 4);
define('MOD_BOOKING_BO_COND_ASKFORCONFIRMATION', 0);
define('MOD_BOOKING_BO_COND_ELECTIVENOTBOOKABLE', -5);
define('MOD_BOOKING_BO_COND_ELECTIVEBOOKITBUTTON', -10);
define('MOD_BOOKING_BO_COND_CONFIRMBOOKWITHSUBSCRIPTION', -20);
define('MOD_BOOKING_BO_COND_BOOKWITHSUBSCRIPTION', -30);
define('MOD_BOOKING_BO_COND_CONFIRMBOOKWITHCREDITS', -40);
define('MOD_BOOKING_BO_COND_BOOKWITHCREDITS', -50);
define('MOD_BOOKING_BO_COND_NOSHOPPINGCART', -60);
define('MOD_BOOKING_BO_COND_PRICEISSET', -70);
define('MOD_BOOKING_BO_COND_CONFIRMBOOKIT', -80);
define('MOD_BOOKING_BO_COND_BOOKITBUTTON', -90); // This is only used to show the book it button.
define('MOD_BOOKING_BO_COND_CONFIRMATION', -100); // This is the last page after booking.
// Define conditions parameters.
define('MOD_BOOKING_CONDPARAM_ALL', 0);
define('MOD_BOOKING_CONDPARAM_HARDCODED_ONLY', 1);
define('MOD_BOOKING_CONDPARAM_JSON_ONLY', 2);
define('MOD_BOOKING_CONDPARAM_MFORM_ONLY', 3);
define('MOD_BOOKING_CONDPARAM_CANBEOVERRIDDEN', 4);
// Define status for booking & subbooking options.
define('MOD_BOOKING_UNVERIFIED', 0);
define('MOD_BOOKING_PENDING', 1);
define('MOD_BOOKING_VERIFIED', 2);
// Define common bookin settings.
define('MOD_BOOKING_PAGINATIONDEF', 25);
// Define campaign types.
define('MOD_BOOKING_CAMPAIGN_TYPE_CUSTOMFIELD', 0);
define('MOD_BOOKING_CAMPAIGN_TYPE_BLOCKBOOKING', 1);
// Categories for option fields.
define('MOD_BOOKING_OPTION_FIELD_NECESSARY', 1);
define('MOD_BOOKING_OPTION_FIELD_STANDARD', 2); // This field is part of standard.
define('MOD_BOOKING_OPTION_FIELD_EASY', 3); // This field is part of easy.
// Define IDs of Fields.
define('MOD_BOOKING_OPTION_FIELD_PREPARE_IMPORT', 1); // Has to be the first field class.
define('MOD_BOOKING_OPTION_FIELD_ID', 10);
define('MOD_BOOKING_OPTION_FIELD_JSON', 11);
define('MOD_BOOKING_OPTION_FIELD_DUPLICATION', 12); // Needed for duplication to work.
define('MOD_BOOKING_OPTION_FIELD_RETURNURL', 20);
define('MOD_BOOKING_OPTION_FIELD_FORMCONFIG', 25);
define('MOD_BOOKING_OPTION_FIELD_MOVEOPTION', 28);
define('MOD_BOOKING_OPTION_FIELD_TEMPLATE', 30);
define('MOD_BOOKING_OPTION_FIELD_TEXT', 40);
define('MOD_BOOKING_OPTION_FIELD_IDENTIFIER', 50);
define('MOD_BOOKING_OPTION_FIELD_TITLEPREFIX', 60);
define('MOD_BOOKING_OPTION_FIELD_EASY_TEXT', 61);
define('MOD_BOOKING_OPTION_FIELD_EASY_BOOKINGOPENINGTIME', 62);
define('MOD_BOOKING_OPTION_FIELD_EASY_BOOKINGCLOSINGTIME', 63);
define('MOD_BOOKING_OPTION_FIELD_EASY_AVAILABILITY_SELECTUSERS', 64);
define('MOD_BOOKING_OPTION_FIELD_EASY_AVAILABILITY_PREVIOUSLYBOOKED', 65);
define('MOD_BOOKING_OPTION_FIELD_DESCRIPTION', 70);
define('MOD_BOOKING_OPTION_FIELD_INVISIBLE', 80);
define('MOD_BOOKING_OPTION_FIELD_ANNOTATION', 90);
define('MOD_BOOKING_OPTION_FIELD_LOCATION', 100);
define('MOD_BOOKING_OPTION_FIELD_INSTITUTION', 110);
define('MOD_BOOKING_OPTION_FIELD_ADDRESS', 120);
define('MOD_BOOKING_OPTION_FIELD_OPTIONIMAGES', 130);
define('MOD_BOOKING_OPTION_FIELD_MAXANSWERS', 140);
define('MOD_BOOKING_OPTION_FIELD_MAXOVERBOOKING', 150);
define('MOD_BOOKING_OPTION_FIELD_MINANSWERS', 160);
define('MOD_BOOKING_OPTION_FIELD_POLLURL', 170);
define('MOD_BOOKING_OPTION_FIELD_COURSEID', 180); // Course to enrol to.
define('MOD_BOOKING_OPTION_FIELD_ENROLMENTSTATUS', 185);
define('MOD_BOOKING_OPTION_FIELD_ADDTOGROUP', 190);
define('MOD_BOOKING_OPTION_FIELD_DURATION', 195);
define('MOD_BOOKING_OPTION_FIELD_ENTITIES', 200);
define('MOD_BOOKING_OPTION_FIELD_SHOPPPINGCART', 205);
define('MOD_BOOKING_OPTION_FIELD_OPTIONDATES', 210);
define('MOD_BOOKING_OPTION_FIELD_COURSESTARTTIME', 220); // Replaced with optiondates class.
define('MOD_BOOKING_OPTION_FIELD_COURSEENDTIME', 230); // Replaced with optiondates class.
define('MOD_BOOKING_OPTION_FIELD_ADDTOCALENDAR', 240);
define('MOD_BOOKING_OPTION_FIELD_TEACHERS', 250);
define('MOD_BOOKING_OPTION_FIELD_RESPONSIBLECONTACT', 260);
define('MOD_BOOKING_OPTION_FIELD_PRICE', 270);
define('MOD_BOOKING_OPTION_FIELD_PRICEFORMULAADD', 280);
define('MOD_BOOKING_OPTION_FIELD_PRICEFORMULAMULTIPLY', 290);
define('MOD_BOOKING_OPTION_FIELD_PRICEFORMULAOFF', 300);
define('MOD_BOOKING_OPTION_FIELD_CREDITS', 310);
define('MOD_BOOKING_OPTION_FIELD_ELECTIVE', 320);
define('MOD_BOOKING_OPTION_FIELD_COSTUMFIELDS', 330);
define('MOD_BOOKING_OPTION_FIELD_AVAILABILITY', 340);
define('MOD_BOOKING_OPTION_FIELD_BOOKINGOPENINGTIME', 350);
define('MOD_BOOKING_OPTION_FIELD_BOOKINGCLOSINGTIME', 360);
define('MOD_BOOKING_OPTION_FIELD_SUBBOOKINGS', 370);
define('MOD_BOOKING_OPTION_FIELD_ACTIONS', 380);
define('MOD_BOOKING_OPTION_FIELD_ADVANCED', 390);
define('MOD_BOOKING_OPTION_FIELD_DISABLEBOOKINGUSERS', 400);
define('MOD_BOOKING_OPTION_FIELD_DISABLECANCEL', 410);
define('MOD_BOOKING_OPTION_FIELD_CANCELUNTIL', 420);
define('MOD_BOOKING_OPTION_FIELD_WAITFORCONFIRMATION', 425);
define('MOD_BOOKING_OPTION_FIELD_ATTACHMENT', 430);
define('MOD_BOOKING_OPTION_FIELD_NOTIFICATIONTEXT', 440);
define('MOD_BOOKING_OPTION_FIELD_REMOVEAFTERMINUTES', 450);
define('MOD_BOOKING_OPTION_FIELD_HOWMANYUSERS', 470);
define('MOD_BOOKING_OPTION_FIELD_BEFOREBOOKEDTEXT', 480);
define('MOD_BOOKING_OPTION_FIELD_BEFORECOMPLETEDTEXT', 490);
define('MOD_BOOKING_OPTION_FIELD_AFTERCOMPLETEDTEXT', 500);
define('MOD_BOOKING_OPTION_FIELD_RECURRINGOPTIONS', 510);
define('MOD_BOOKING_OPTION_FIELD_BOOKUSERS', 520);
define('MOD_BOOKING_OPTION_FIELD_TIMEMODIFIED', 530);
define('MOD_BOOKING_OPTION_FIELD_TEMPLATESAVE', 600);
define('MOD_BOOKING_OPTION_FIELD_EVENTSLIST', 700);
define('MOD_BOOKING_OPTION_FIELD_AFTERSUBMITACTION', 999);
// To define execution of field methods.
define('MOD_BOOKING_EXECUTION_NORMAL', 0);
define('MOD_BOOKING_EXECUTION_POSTSAVE', 1);
// Definition of Header sections in option form.
define('MOD_BOOKING_HEADER_GENERAL', 'general');
define('MOD_BOOKING_HEADER_DATES', 'datesheader');
define('MOD_BOOKING_HEADER_TEACHERS', 'bookingoptionteachers');
define('MOD_BOOKING_HEADER_RESPONSIBLECONTACT', 'responsiblecontactheader');
define('MOD_BOOKING_HEADER_ADVANCEDOPTIONS', 'advancedoptions');
define('MOD_BOOKING_HEADER_BOOKINGOPTIONTEXT', 'textdependingonstatus');
define('MOD_BOOKING_HEADER_RECURRINGOPTIONS', 'recurringheader');
define('MOD_BOOKING_HEADER_TEMPLATES', 'templateheader');
define('MOD_BOOKING_HEADER_PRICE', 'bookingoptionprice');
define('MOD_BOOKING_HEADER_ELECTIVE', 'electivesettings');
define('MOD_BOOKING_HEADER_ACTIONS', 'bookingactionsheader');
define('MOD_BOOKING_HEADER_AVAILABILITY', 'availabilityconditionsheader');
define('MOD_BOOKING_HEADER_SUBBOOKINGS', 'bookingsubbookingsheader');
define('MOD_BOOKING_HEADER_CUSTOMFIELDS', 'category_'); // There can be multiple headers, with custom names.
define('MOD_BOOKING_HEADER_TEMPLATESAVE', 'templateheader');
define('MOD_BOOKING_HEADER_COURSES', 'coursesheader');
define('MOD_BOOKING_MAX_CUSTOM_FIELDS', 3);
define('MOD_BOOKING_FORM_OPTIONDATEID', 'optiondateid_');
define('MOD_BOOKING_FORM_DAYSTONOTIFY', 'daystonotify_');
define('MOD_BOOKING_FORM_COURSESTARTTIME', 'coursestarttime_');
define('MOD_BOOKING_FORM_COURSEENDTIME', 'courseendtime_');
define('MOD_BOOKING_FORM_DELETEDATE', 'deletedate_');
// SQL Filter.
define('MOD_BOOKING_SQL_FILTER_INACTIVE', 0);
define('MOD_BOOKING_SQL_FILTER_ACTIVE_JSON_BO', 1);
define('MOD_BOOKING_SQL_FILTER_ACTIVE_BO_TIME', 2);
// Tracking of changes can be excluded for classes (fields).
// Implement this as setting if needed.
define('MOD_BOOKING_CLASSES_EXCLUDED_FROM_CHANGES_TRACKING', [
]);
/**
* Booking get coursemodule info.
*
* @param stdClass $cm
* @return cached_cm_info
*/
function booking_get_coursemodule_info($cm) {
$info = new cached_cm_info();
$booking = singleton_service::get_instance_of_booking_by_cmid($cm->id);
$booking->apply_tags();
if (!empty($booking->settings->name)) {
$info->name = $booking->settings->name;
}
// Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'.
if ($cm->completion == COMPLETION_TRACKING_AUTOMATIC) {
$info->customdata['customcompletionrules']['completionoptioncompleted'] = $booking->settings->enablecompletion;
}
return $info;
}
/**
* Serves the booking / option files.
*
* @package mod_booking
* @category files
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $filearea file area
* @param array $args extra arguments
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool false if file not found, does not return if found - justsend the file
*
* @throws coding_exception
* @throws moodle_exception
* @throws require_login_exception
*/
function booking_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
// Check the contextlevel is as expected - if your plugin is a block.
// We need context course if wee like to acces template files.
if (!in_array($context->contextlevel, [CONTEXT_MODULE, CONTEXT_COURSE])) {
return false;
}
// Make sure the filearea is one of those used by the plugin.
if (
$filearea !== 'myfilemanager'
&& $filearea !== 'bookingimages'
&& $filearea !== 'myfilemanageroption'
&& $filearea !== 'bookingoptionimage'
&& $filearea !== 'signinlogoheader'
&& $filearea !== 'signinlogofooter'
&& $filearea !== 'templatefile'
) {
return false;
}
// Make sure the user is logged in and has access to the module.
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
/* require_login($course, true, $cm); */
// Leave this line out if you set the itemid to null in make_pluginfile_url (set $itemid to 0 instead).
$itemid = array_shift($args); // The first item in the $args array.
// Use the itemid to retrieve any relevant data records and
// perform any security checks to see if the
// user really does have access to the file in question.
// Extract the filename / filepath from the $args array.
$filename = array_pop($args); // The last item in the $args array.
if (!$args) {
// Var $args is empty => the path is '/'.
$filepath = '/';
} else {
// Var $args contains elements of the filepath.
$filepath = '/' . implode('/', $args) . '/';
}
// Retrieve the file from the Files API.
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'mod_booking', $filearea, $itemid, $filepath, $filename);
if (!$file) {
return false; // The file does not exist.
}
// Send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering.
send_stored_file($file, null, 0, true, $options);
}
/**
* Booking user outline.
*
* @param object $course
* @param object $user
* @param object $mod
* @param object $booking
*
* @return stdClass|null
*
*/
function booking_user_outline($course, $user, $mod, $booking) {
global $DB;
if (
$answer = $DB->get_record(
'booking_answers',
['bookingid' => $booking->id, 'userid' => $user->id, 'waitinglist' => 0]
)
) {
$result = new stdClass();
$result->info = "'" . format_string(booking_get_option_text($booking, $answer->optionid)) .
"'";
$result->time = $answer->timemodified;
return $result;
}
return null;
}
/**
* Callback for the "Complete" report - prints the activity summary for the given user.
*
* @param stdClass $course
* @param stdClass $user
* @param stdClass $mod
* @param object $booking
*
* @throws coding_exception
* @throws dml_exception
*
* @return void
*/
function booking_user_complete($course, $user, $mod, $booking) {
global $DB;
if (
$answer = $DB->get_record(
'booking_answers',
["bookingid" => $booking->id, "userid" => $user->id]
)
) {
$result = new stdClass();
$result->info = "'" . format_string(booking_get_option_text($booking, $answer->optionid)) .
"'";
$result->time = $answer->timemodified;
echo get_string("answered", "booking") . ": $result->info. " .
get_string("updated", '', userdate($result->time));
} else {
print_string("notanswered", "booking");
}
}
/**
* Booking supports.
*
* @param bool $feature
*
* @return bool|null
*
*/
function booking_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return false;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return false;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_RATE:
return true;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_COMMENT:
return true;
default:
return null;
}
}
/**
* Running addtional permission check on plugin, for example, plugins may have switch to turn on/off comments option,
* this callback will affect UI display, not like pluginname_comment_validate only throw exceptions.
*
* @package mod_booking
* @category comment
*
* @param object $commentparam
*
* @return array
*
* @throws dml_exception
*/
function booking_comment_permissions($commentparam): array {
global $DB, $USER;
$odata = $DB->get_record('booking_options', ['id' => $commentparam->itemid]);
$bdata = $DB->get_record('booking', ['id' => $odata->bookingid]);
switch ($bdata->comments) {
case 0:
return ['post' => false, 'view' => false];
break;
case 1:
return ['post' => true, 'view' => true];
break;
case 2:
$udata = $DB->get_record(
'booking_answers',
['userid' => $USER->id, 'optionid' => $commentparam->itemid]
);
if ($udata) {
return ['post' => true, 'view' => true];
} else {
return ['post' => false, 'view' => true];
}
break;
case 3:
$udata = $DB->get_record(
'booking_answers',
['userid' => $USER->id, 'optionid' => $commentparam->itemid]
);
if ($udata && $udata->completed == 1) {
return ['post' => true, 'view' => true];
} else {
return ['post' => false, 'view' => true];
}
break;
}
return [];
}
/**
* Validate comment parameter before perform other comments actions.
*
* @param stdClass $commentparam { context => context the context object
* courseid => int course id
* cm => stdClass course module object
* commentarea => string comment area
* itemid => int itemid }
* @return bool
* @throws coding_exception
* @throws comment_exception
* @throws dml_exception
* @category comment
* @package mod_booking
*/
function booking_comment_validate(stdClass $commentparam): bool {
global $DB;
if ($commentparam->commentarea != 'booking_option') {
throw new comment_exception('invalidcommentarea');
}
if (!$record = $DB->get_record('booking_options', ['id' => $commentparam->itemid])) {
throw new comment_exception('invalidcommentitemid');
}
if ($record->id) {
$booking = $DB->get_record('booking', ['id' => $record->bookingid]);
}
if (!$booking) {
throw new comment_exception('invalidid', 'data');
}
if (!$course = $DB->get_record('course', ['id' => $booking->course])) {
throw new comment_exception('coursemisconf');
}
if (!$cm = get_coursemodule_from_instance('booking', $booking->id, $course->id)) {
throw new comment_exception('invalidcoursemodule');
}
$context = context_module::instance($cm->id);
// Validate context id.
if ($context->id != $commentparam->context->id) {
throw new comment_exception('invalidcontext');
}
return true;
}
/**
* Given an object containing all the necessary data this will create a new instance and return the id number of the new instance.
*
* @param object $booking
* @return number $bookingid
*/
function booking_add_instance($booking) {
global $DB, $CFG;
$booking->timemodified = time();
if (isset($booking->responsesfields) && is_array($booking->responsesfields) && count($booking->responsesfields) > 0) {
$booking->responsesfields = implode(',', $booking->responsesfields);
}
if (isset($booking->additionalfields) && count($booking->additionalfields) > 0) {
$booking->additionalfields = implode(',', $booking->additionalfields);
} else {
$booking->additionalfields = null;
}
if (isset($booking->categoryid) && count($booking->categoryid) > 0) {
$booking->categoryid = implode(',', $booking->categoryid);
} else {
$booking->categoryid = null;
}
if (isset($booking->templateid) && $booking->templateid > 0) {
$booking->templateid = $booking->templateid;
} else {
$booking->templateid = 0;
}
$booking->iselective = !empty($booking->iselective) ? $booking->iselective : 0;
if (empty($booking->timerestrict)) {
$booking->timeopen = $booking->timeclose = 0;
}
if (isset($booking->showviews) && is_array($booking->showviews) && count($booking->showviews) > 0) {
$booking->showviews = implode(',', $booking->showviews);
} else if (!isset($booking->showviews) || $booking->showviews === null) {
$booking->showviews = '';
}
if (isset($booking->reportfields) && is_array($booking->reportfields) && count($booking->reportfields) > 0) {
$booking->reportfields = implode(',', $booking->reportfields);
}
if (isset($booking->optionsfields) && is_array($booking->optionsfields) && count($booking->optionsfields) > 0) {
$booking->optionsfields = implode(',', $booking->optionsfields);
} else {
$booking->optionsfields = MOD_BOOKING_BOOKINGOPTION_DEFAULTFIELDS;
}
if (
isset($booking->optionsdownloadfields)
&& is_array($booking->optionsdownloadfields)
&& count($booking->optionsdownloadfields) > 0
) {
$booking->optionsdownloadfields = implode(',', $booking->optionsdownloadfields);
} else {
$booking->optionsdownloadfields = MOD_BOOKING_BOOKINGOPTION_DEFAULTFIELDS;
}
if (
isset($booking->signinsheetfields)
&& is_array($booking->signinsheetfields)
&& count($booking->signinsheetfields) > 0
) {
$booking->signinsheetfields = implode(',', $booking->signinsheetfields);
}
// Copy the text fields out.
$booking->bookedtext = $booking->bookedtext['text'] ?? $booking->bookedtext ?? null;
$booking->notificationtext = $booking->notificationtext['text'] ?? $booking->notificationtext ?? null;
$booking->waitingtext = $booking->waitingtext['text'] ?? $booking->waitingtext ?? null;
$booking->notifyemail = $booking->notifyemail['text'] ?? $booking->notifyemail ?? null;
$booking->notifyemailteachers = $booking->notifyemailteachers['text'] ?? $booking->notifyemailteachers ?? null;
$booking->statuschangetext = $booking->statuschangetext['text'] ?? $booking->statuschangetext ?? null;
$booking->deletedtext = $booking->deletedtext['text'] ?? $booking->deletedtext ?? null;
$booking->bookingchangedtext = $booking->bookingchangedtext['text'] ?? $booking->bookingchangedtext ?? null;
$booking->pollurltext = $booking->pollurltext['text'] ?? $booking->pollurltext ?? null;
$booking->pollurlteacherstext = $booking->pollurlteacherstext['text'] ?? $booking->pollurlteacherstext ?? null;
$booking->activitycompletiontext = $booking->activitycompletiontext['text'] ?? $booking->activitycompletiontext ?? null;
$booking->userleave = $booking->userleave['text'] ?? $booking->userleave ?? null;
$booking->beforebookedtext = $booking->beforebookedtext['text'] ?? null;
$booking->beforecompletedtext = $booking->beforecompletedtext['text'] ?? null;
$booking->aftercompletedtext = $booking->aftercompletedtext['text'] ?? null;
if (isset($booking->cancelrelativedate)) {
booking::add_data_to_json($booking, 'cancelrelativedate', $booking->cancelrelativedate);
}
if (isset($booking->allowupdatetimestamp)) {
booking::add_data_to_json($booking, 'allowupdatetimestamp', $booking->allowupdatetimestamp);
}
// If no policy was entered, we still have to check for HTML tags.
if (!isset($booking->bookingpolicy) || empty(strip_tags($booking->bookingpolicy))) {
$booking->bookingpolicy = '';
}
// Insert answer options from mod_form.
$booking->id = $DB->insert_record("booking", $booking);
$cmid = $booking->coursemodule;
$context = context_module::instance($cmid);
if ($draftitemid = file_get_submitted_draft_itemid('myfilemanager')) {
file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_booking',
'myfilemanager',
$booking->id,
['subdirs' => false, 'maxfiles' => 50]
);
}
if ($draftitemid = file_get_submitted_draft_itemid('bookingimages')) {
file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_booking',
'bookingimages',
$booking->id,
['subdirs' => false, 'maxfiles' => 500]
);
}
if ($draftitemid = file_get_submitted_draft_itemid('signinlogoheader')) {
file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_booking',
'signinlogoheader',
$booking->id,
['subdirs' => false, 'maxfiles' => 1]
);
}
if ($draftitemid = file_get_submitted_draft_itemid('signinlogofooter')) {
file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_booking',
'signinlogofooter',
$booking->id,
['subdirs' => false, 'maxfiles' => 1]
);
}
if (isset($booking->tags)) {
core_tag_tag::set_item_tags('mod_booking', 'booking', $booking->id, $context, $booking->tags);
}
if (!empty($booking->option)) {
foreach ($booking->option as $key => $value) {
$value = trim($value);
if (!empty($value)) {
$option = new stdClass();
$option->text = $value;
$option->bookingid = $booking->id;
if (isset($booking->limit[$key])) {
$option->maxanswers = $booking->limit[$key];
}
$option->timemodified = time();
$DB->insert_record("booking_options", $option);
}
}
}
booking_grade_item_update($booking);
// When adding an instance, we need to invalidate the cache for booking instances.
booking::purge_cache_for_booking_instance_by_cmid($cmid, false, false, false);
return $booking->id;
}
/**
* Given an object containing all the necessary data this will update an existing instance.
*
* @param stdClass|booking $booking
* @return bool
*/
function booking_update_instance($booking) {
global $DB, $CFG;
// We have to prepare the bookingclosingtimes as an $arrray, currently they are in $booking as $key (string).
$booking->id = $booking->instance;
$bookingid = $booking->id;
$bookingsettings = singleton_service::get_instance_of_booking_settings_by_bookingid($bookingid);
$booking->timemodified = time();
$cm = get_coursemodule_from_instance('booking', $booking->id);
$context = context_module::instance($cm->id);
if (isset($booking->showviews) && count($booking->showviews) > 0) {
$booking->showviews = implode(',', $booking->showviews);
} else {
$booking->showviews = '';
}
if (isset($booking->responsesfields) && is_array($booking->responsesfields) && count($booking->responsesfields) > 0) {
$booking->responsesfields = implode(',', $booking->responsesfields);
}
if (isset($booking->reportfields) && is_array($booking->reportfields) && count($booking->reportfields) > 0) {
$booking->reportfields = implode(',', $booking->reportfields);
}
if (isset($booking->signinsheetfields) && is_array($booking->signinsheetfields) && count($booking->signinsheetfields) > 0) {
$booking->signinsheetfields = implode(',', $booking->signinsheetfields);
}
if (isset($booking->templateid) && $booking->templateid > 0) {
$booking->templateid = $booking->templateid;
} else {
$booking->templateid = 0;
}
$booking->iselective = !empty($booking->iselective) ? $booking->iselective : 0;
if (isset($booking->optionsfields) && is_array($booking->optionsfields) && count($booking->optionsfields) > 0) {
$booking->optionsfields = implode(',', $booking->optionsfields);
} else {
$booking->optionsfields = MOD_BOOKING_BOOKINGOPTION_DEFAULTFIELDS;
}
if (
isset($booking->optionsdownloadfields)
&& is_array($booking->optionsdownloadfields)
&& count($booking->optionsdownloadfields) > 0
) {
$booking->optionsdownloadfields = implode(',', $booking->optionsdownloadfields);
} else {
$booking->optionsdownloadfields = MOD_BOOKING_BOOKINGOPTION_DEFAULTFIELDS;
}
if (isset($booking->categoryid) && count($booking->categoryid) > 0) {
$booking->categoryid = implode(',', $booking->categoryid);
} else {
$booking->categoryid = null;
}
if (empty($booking->assessed)) {
$booking->assessed = 0;
}
if (empty($booking->ratingtime) || empty($booking->assessed)) {
$booking->assesstimestart = 0;
$booking->assesstimefinish = 0;
}
$arr = [];
core_tag_tag::set_item_tags('mod_booking', 'booking', $booking->id, $context, $booking->tags);
if (!empty($booking->signinlogoheader)) {
file_save_draft_area_files(
$booking->signinlogoheader,
$context->id,
'mod_booking',
'signinlogoheader',
$booking->id,
['subdirs' => false, 'maxfiles' => 1]
);
}
if (!empty($booking->signinlogofooter)) {
file_save_draft_area_files(
$booking->signinlogofooter,
$context->id,
'mod_booking',
'signinlogofooter',
$booking->id,
['subdirs' => false, 'maxfiles' => 1]
);
}
if (!empty($booking->myfilemanager)) {
file_save_draft_area_files(
$booking->myfilemanager,
$context->id,
'mod_booking',
'myfilemanager',
$booking->id,
['subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 50]
);
}
if (!empty($booking->bookingimages)) {
file_save_draft_area_files(
$booking->bookingimages,
$context->id,
'mod_booking',
'bookingimages',
$booking->id,
['subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 500]
);
}
if (empty($booking->timerestrict)) {
$booking->timeopen = 0;
$booking->timeclose = 0;
}
// Copy the text fields out.
if (isset($booking->beforebookedtext['text'])) {
$booking->beforebookedtext = $booking->beforebookedtext['text'];
}
if (isset($booking->beforecompletedtext['text'])) {
$booking->beforecompletedtext = $booking->beforecompletedtext['text'];
}
if (isset($booking->aftercompletedtext['text'])) {
$booking->aftercompletedtext = $booking->aftercompletedtext['text'];
}
// If no policy was entered, we still have to check for HTML tags.
// NOTE: $booking->bookingpolicy is a string! So we never use ['text'] here!
if (!isset($booking->bookingpolicy) || empty(strip_tags($booking->bookingpolicy))) {
$booking->bookingpolicy = '';
}
$booking->bookedtext = $booking->bookedtext['text'] ?? $booking->bookedtext ?? null;
$booking->waitingtext = $booking->waitingtext['text'] ?? $booking->waitingtext ?? null;
$booking->notifyemail = $booking->notifyemail['text'] ?? $booking->notifyemail ?? null;
$booking->notifyemailteachers = $booking->notifyemailteachers['text'] ?? $booking->notifyemailteachers ?? null;
$booking->statuschangetext = $booking->statuschangetext['text'] ?? $booking->statuschangetext ?? null;
$booking->deletedtext = $booking->bookingchangedtext['text'] ?? $booking->bookingchangedtext ?? null;
$booking->bookingchangedtext = $booking->bookingchangedtext['text'] ?? $booking->bookingchangedtext ?? null;
$booking->pollurltext = $booking->pollurltext['text'] ?? $booking->pollurltext ?? null;
$booking->pollurlteacherstext = $booking->pollurlteacherstext['text'] ?? $booking->pollurlteacherstext ?? null;
$booking->activitycompletiontext = $booking->activitycompletiontext['text'] ?? $booking->activitycompletiontext ?? null;
$booking->userleave = $booking->userleave['text'] ?? $booking->userleave ?? null;
// Get JSON from bookingsettings.
$booking->json = $bookingsettings->json;
// We store the information if a booking option can be cancelled in the JSON.
// So this has to happen BEFORE JSON is saved!
if (empty($booking->disablecancel)) {
// This will store the correct JSON to $optionvalues->json.
booking::remove_key_from_json($booking, "disablecancel");
} else {
booking::add_data_to_json($booking, "disablecancel", 1);
}
// View param (list view or card view) is also stored in JSON.
if (empty($booking->viewparam)) {
// Save list view as default value.
booking::add_data_to_json($booking, "viewparam", MOD_BOOKING_VIEW_PARAM_LIST);
} else {
booking::add_data_to_json($booking, "viewparam", $booking->viewparam);
}
if (empty($booking->disablebooking)) {
// This will store the correct JSON to $optionvalues->json.
booking::remove_key_from_json($booking, "disablebooking");
} else {
booking::add_data_to_json($booking, "disablebooking", 1);
}
if (empty($booking->overwriteblockingwarnings)) {
// This will store the correct JSON to $optionvalues->json.
booking::remove_key_from_json($booking, "overwriteblockingwarnings");
} else {
booking::add_data_to_json($booking, "overwriteblockingwarnings", 1);
}
if (empty($booking->billboardtext)) {
// This will store the correct JSON to $optionvalues->json.
booking::remove_key_from_json($booking, "billboardtext");
} else {
booking::add_data_to_json($booking, "billboardtext", $booking->billboardtext);
}
if (empty($booking->cancelrelativedate)) {
// If date is not relative, delete given entries for relative dates.
$booking->allowupdatedays = "0";
booking::add_data_to_json($booking, "cancelrelativedate", $booking->cancelrelativedate);
booking::add_data_to_json($booking, "allowupdatetimestamp", $booking->allowupdatetimestamp);
} else {
booking::add_data_to_json($booking, "cancelrelativedate", $booking->cancelrelativedate);
booking::remove_key_from_json($booking, "allowupdatetimestamp");
}
// Update, delete or insert answers.
if (!empty($booking->option)) {
foreach ($booking->option as $key => $value) {
$value = trim($value);
$option = new stdClass();
$option->text = $value;
$option->bookingid = $booking->id;
if (isset($booking->limit[$key])) {
$option->maxanswers = $booking->limit[$key];
}
$option->timemodified = time();
if (isset($booking->optionid[$key]) && !empty($booking->optionid[$key])) { // Existing booking record.
$option->id = $booking->optionid[$key];
if (!empty($value)) {
$DB->update_record("booking_options", $option);
} else { // Empty old option - needs to be deleted.
$DB->delete_records("booking_options", ["id" => $option->id]);
}
} else {
if (!empty($value)) {
$DB->insert_record("booking_options", $option);
}
}
}
}
booking_grade_item_update($booking);
$oldrecord = $DB->get_record('booking', ['id' => $booking->id]);
$changes = booking::booking_instance_get_changes($oldrecord, $booking);
// We trigger the right event.
$event = \mod_booking\event\bookinginstance_updated::create([
'context' => $context,
'objectid' => $cm->id,
'other' => [
'changes' => $changes ?? '',
],
]);
$event->trigger();
// When updating an instance, we need to invalidate the cache for booking instances.