-
Notifications
You must be signed in to change notification settings - Fork 3
/
externallib.php
executable file
·1520 lines (1327 loc) · 56.5 KB
/
externallib.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/>.
/**
* Session Booking Plugin
*
* @package local_booking
* @author Mustafa Hajjar ([email protected])
* @copyright BAVirtual.co.uk © 2024
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/group/lib.php');
require_once($CFG->dirroot . '/local/booking/lib.php');
use core_external\external_api;
use core_external\external_value;
use core_external\external_warnings;
use core_external\external_single_structure;
use core_external\external_multiple_structure;
use core_external\external_function_parameters;
use local_booking\external\logentry_exporter;
use local_booking\local\logbook\form\create as update_logentry_form;
use local_booking\local\logbook\entities\logbook;
use local_booking\local\message\notification;
use local_booking\local\participant\entities\instructor;
use local_booking\local\participant\entities\participant;
use local_booking\local\participant\entities\student;
use local_booking\local\session\entities\booking;
use local_booking\local\slot\entities\slot;
use local_booking\local\subscriber\entities\subscriber;
use local_booking\output\views\booking_view;
use local_booking\output\views\calendar_view;
use local_booking\output\views\logentry_view;
/**
* Session Booking Plugin
*
* @package local_booking
* @author Mustafa Hajjar ([email protected])
* @copyright BAVirtual.co.uk © 2021
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_booking_external extends external_api {
// Availability slots table name for.
const DB_SLOTS = 'local_booking_slots';
/**
* Sets the course subscriber and context url
*
* @param string $url PAGE url
* @param int $courseid course id for context
*/
protected static function get_course_subscriber_context(string $url, int $courseid) {
global $PAGE, $COURSE;
$context = context_course::instance($courseid);
self::validate_context($context);
$PAGE->set_url($url);
$PAGE->set_context($context);
// define subscriber globally
if (empty($COURSE->subscriber)) {
$COURSE->subscriber = new subscriber($courseid);
}
return $COURSE->subscriber;
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function get_bookings_view_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'The course id', VALUE_DEFAULT),
'userid' => new external_value(PARAM_INT, 'The user id', VALUE_DEFAULT),
'filter' => new external_value(PARAM_RAW, 'The results filter', VALUE_DEFAULT),
)
);
}
/**
* Retrieve instructor's booking.
*
* @param int $courseid The course id for context.
* @param int $userid The user id for single user selection.
* @param string $filter The filter to show students, inactive (including graduates), suspended, and default to active.
* @return array Array of booking objects.
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function get_bookings_view(int $courseid, int $userid, string $filter) {
global $USER;
// Parameter validation.
$params = self::validate_parameters(self::get_bookings_view_parameters(), array(
'courseid' => $courseid,
'userid' => $userid,
'filter' => $filter,
)
);
// set the subscriber object
$subscriber = self::get_course_subscriber_context('/local/booking/', $courseid);
// data required get data
$data = [
'instructor' => $subscriber->get_instructor($USER->id),
'studentid' => $userid,
'action' => 'book',
'view' => 'sessions',
'sorttype' => '',
'filter' => $filter,
'page' => 0,
];
$bookingview = new booking_view($data, ['subscriber'=>$subscriber, 'context'=>$subscriber->get_context()]);
$bookings = $bookingview->get_student_progression(false);
return $bookings;
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function get_bookings_view_returns() {
return \local_booking\external\dashboard_bookings_exporter::get_read_structure();
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function get_instructor_bookings_view_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'The course id', VALUE_DEFAULT),
)
);
}
/**
* Retrieve instructor's booking.
*
* @param int $courseid The course id for context.
* @return array Array of instructor booking objects.
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function get_instructor_bookings_view(int $courseid) {
global $USER;
// Parameter validation.
$params = self::validate_parameters(self::get_bookings_view_parameters(), array(
'courseid' => $courseid,
)
);
// set the subscriber object
$subscriber = self::get_course_subscriber_context('/local/booking/', $courseid);
// data required get data
$data = [
'instructor' => $subscriber->get_instructor($USER->id),
];
$bookingview = new booking_view($data, ['subscriber'=>$subscriber, 'context'=>$subscriber->get_context()]);
$instuctorbookings = $bookingview->get_instructor_bookings(false);
return $instuctorbookings;
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function get_instructor_bookings_view_returns() {
return \local_booking\external\dashboard_mybookings_exporter::get_read_structure();
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function get_student_names_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'The course id', VALUE_DEFAULT),
'wildcard' => new external_value(PARAM_RAW, 'The search wildcard', VALUE_DEFAULT),
)
);
}
/**
* Retrieve student names for autocomplete.
*
* @param int $courseid The course id for context.
* @param string $wildcard The search wildcard.
* @return array Array of student names exported.
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function get_student_names(int $courseid, string $wildcard) {
// Parameter validation.
$params = self::validate_parameters(self::get_student_names_parameters(), array(
'courseid' => $courseid,
'wildcard' => $wildcard,
)
);
// set the subscriber object
$subscriber = self::get_course_subscriber_context('/local/booking/', $courseid);
// get student names data
$studentrecords = $subscriber->get_participant_names('active', true, 'student', $wildcard);
$students = array_map(function($userid, $fullname){return ['userid'=>$userid, 'fullname'=>$fullname];},array_keys($studentrecords), array_values($studentrecords));
return $students;
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function get_student_names_returns() {
return new external_multiple_structure(\local_booking\external\list_student_name_exporter::get_read_structure());
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function get_pilot_logbook_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'The course id in context', VALUE_DEFAULT),
'userid' => new external_value(PARAM_INT, 'The user id', VALUE_DEFAULT),
)
);
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function get_pilot_logbook_returns() {
return new external_multiple_structure(logentry_exporter::get_read_structure());
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function get_logentry_by_id_parameters() {
return new external_function_parameters(
array(
'logentryid' => new external_value(PARAM_INT, 'The logbook entry id', VALUE_DEFAULT),
'courseid' => new external_value(PARAM_INT, 'The course id in context', VALUE_DEFAULT),
'userid' => new external_value(PARAM_INT, 'The user id', VALUE_DEFAULT),
)
);
}
/**
* Retrieve a logbook entry by its id.
*
* @param int $logentryid The logbook entry id.
* @param int $courseid The course id in context.
* @param int $userid The user user id in context.
* @return array array of slots created.
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function get_logentry_by_id(int $logentryid, int $courseid, int $userid) {
global $USER;
// Parameter validation.
$params = self::validate_parameters(self::get_logentry_by_id_parameters(), array(
'logentryid' => $logentryid,
'courseid' => $courseid,
'userid' => $userid,
)
);
$warnings = array();
$logentry = (new logbook($courseid, $userid))->get_logentry($logentryid);
$subscriber = self::get_course_subscriber_context('/local/booking/logbook?courseid=' . $courseid, $courseid);
$data = array('subscriber'=>$subscriber, 'logentry' => $logentry, 'view' => 'summary', 'canedit' => $subscriber->get_instructor($USER->id)->is_instructor()) + $params;
$entry = new logentry_view($data, ['subscriber'=>$subscriber, 'context'=>$subscriber->get_context()]);
return array('logentry' => $entry->get_exported_data(), 'warnings' => $warnings);
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function get_logentry_by_id_returns() {
$logentrystructure = logentry_exporter::get_read_structure();
return new external_single_structure(array(
'logentry' => $logentrystructure,
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function get_pirep_parameters() {
return new external_function_parameters(
array(
'pirep' => new external_value(PARAM_TEXT, 'The PIREP id', VALUE_DEFAULT),
'courseid' => new external_value(PARAM_INT, 'The cousre id', VALUE_DEFAULT),
'userid' => new external_value(PARAM_INT, 'The user id', VALUE_DEFAULT),
'exerciseid' => new external_value(PARAM_INT, 'The exercise id', VALUE_DEFAULT),
)
);
}
/**
* Retrieve a logbook entry by its id.
*
* @param int $logentryid The logbook entry id.
* @param int $courseid The course id in context.
* @param int $userid The user id in context.
* @param int $exerciseid The exerciseid id in context.
* @return array array of slots created.
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function get_pirep($pirep, $courseid, $userid, $exerciseid) {
// Parameter validation.
$params = self::validate_parameters(self::get_pirep_parameters(), array(
'pirep' => $pirep,
'courseid' => $courseid,
'userid' => $userid,
)
);
// set the subscriber object
$subscriber = self::get_course_subscriber_context('/local/booking/', $courseid);
$result = true;
$warnings = array();
$errorcode = '';
// get PIREP integrated info
$logentry = (new logbook($courseid, $userid))->create_logentry();
$pireprec = $subscriber->get_external_data('pireps', 'pirepinfo', $pirep);
if (!empty($pireprec)) {
// get logentry data from the PIREP record
$logentry->read($pireprec);
// get pilot integrated info
if (subscriber::has_integration('external_data', 'pilots')) {
$pilotrec = $subscriber->get_external_data('pilots', 'pilotinfo', $logentry->get_pilotid());
$alternatename = $pilotrec['alternatename'];
if (core_user::get_user($userid, 'alternatename')->alternatename == $alternatename) {
// get engine type integrated data
if ($subscriber->has_integration('external_data', 'aircraft')) {
$enginetyperec = $subscriber->get_external_data('aircraft', 'aircraftinfo', $logentry->get_aircraft());
if (!empty($enginetyperec))
$logentry->set_enginetype($enginetyperec['engine_type'] == 'single' ? 'SE' : 'ME');
}
// export logentry data
$data = [
'subscriber'=> $subscriber,
'logentry' => $logentry,
'courseid' => $courseid,
'userid' => $userid,
'exerciseid'=> $exerciseid,
'view' => 'summary',
'nullable' => false
];
$entry = new logentry_view($data, ['subscriber'=>$subscriber, 'context'=>$subscriber->get_context()]);
$data = $entry->get_exported_data();
$warnings[] = [
'item' => $pirep,
'warningcode' => '0',
'message' => get_string('pirepfound' . (!empty($logentry->get_linkedpirep()) ? '' : 'notlinked'), 'local_booking')
];
} else {
$result = false;
$errorcode = 'errorp1pirepwrongpilot';
}
} else {
$result = false;
$errorcode = 'errorp1pirepnopilotintegration';
}
} else {
$result = false;
$errorcode = 'errorp1pirepnotfound';
}
if (!$result) {
// get empty logentry for returns structure
$data = $logentry->__toArray(false, false) + $params;
$data['canedit'] = $subscriber->get_instructor($userid)->is_instructor();
$data['visible'] = 1;
// set the warring message
$warnings[] = [
'item' => $pirep,
'warningcode' => $errorcode,
'message' => get_string($errorcode, 'local_booking')
];
}
return array('logentry' => $data, 'result' => $result, 'warnings' => $warnings);
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function get_pirep_returns() {
$logentrystructure = logentry_exporter::get_read_structure();
return new external_single_structure(array(
'logentry' => $logentrystructure,
'result' => new external_value(PARAM_BOOL, get_string('processingresult', 'local_booking')),
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function delete_logentry_parameters() {
return new external_function_parameters(
array(
'logentryid' => new external_value(PARAM_INT, 'The logbook entry id', VALUE_DEFAULT),
'userid' => new external_value(PARAM_INT, 'The user id', VALUE_DEFAULT),
'courseid' => new external_value(PARAM_INT, 'The course id in context', VALUE_DEFAULT),
'cascade' => new external_value(PARAM_BOOL, 'Whether to ignore cascade delete of linked logentry', VALUE_DEFAULT),
)
);
}
/**
* Delete a logbook entry.
*
* @param int $logentryid The logbook entry id.
* @param int $userid The user user id in context.
* @param int $courseid The course id in context.
* @param bool $cascade Whether to ignore cascading delete of linked logentry.
* @return array array of slots created.
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function delete_logentry($logentryid, $userid, $courseid, $cascade) {
global $PAGE;
// Parameter validation.
$params = self::validate_parameters(self::delete_logentry_parameters(), array(
'logentryid' => $logentryid,
'courseid' => $courseid,
'userid' => $userid,
'cascade' => $cascade,
)
);
$context = context_course::instance($courseid);
self::validate_context($context);
$PAGE->set_url('/local/booking/');
$logbook = new logbook($courseid, $userid);
if ($logbook->delete($logentryid, $cascade))
\core\notification::SUCCESS(get_string('logentrydeletesuccess', 'local_booking'));
else
\core\notification::ERROR(get_string('logentrydeletefailed', 'local_booking'));
return null;
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function delete_logentry_returns() {
return null;
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function get_weekly_view_parameters() {
return new external_function_parameters(
[
'year' => new external_value(PARAM_INT, 'Year to be viewed', VALUE_REQUIRED),
'week' => new external_value(PARAM_INT, 'Week to be viewed', VALUE_REQUIRED),
'time' => new external_value(PARAM_INT, 'Timestamp of the first day of the week to be viewed', VALUE_REQUIRED),
'courseid' => new external_value(PARAM_INT, 'Course being viewed', VALUE_DEFAULT, SITEID, NULL_ALLOWED),
'categoryid' => new external_value(PARAM_INT, 'Category being viewed', VALUE_DEFAULT, null, NULL_ALLOWED),
'action' => new external_value(PARAM_RAW, 'The action being performed view or book', VALUE_DEFAULT, 'view', NULL_ALLOWED),
'view' => new external_value(PARAM_RAW, 'The action being performed view or book', VALUE_DEFAULT, 'view', NULL_ALLOWED),
'studentid' => new external_value(PARAM_INT, 'The user id the slots belongs to', VALUE_DEFAULT, 0, NULL_ALLOWED),
'exerciseid' => new external_value(PARAM_INT, 'The exercise id the slots belongs to', VALUE_DEFAULT, 0, NULL_ALLOWED),
]
);
}
/**
* Get data for the weekly calendar view.
*
* @param int $year The year to be shown
* @param int $week The week to be shown
* @param int $time The timestamp of the first day in the week to be shown
* @param int $courseid The course to be included
* @param int $categoryid The category to be included
* @param string $action The action to be pefromed if in booking view
* @param string $view The view to be displayed if user or all
* @param int $studentid The student id the action is performed on
* @param int $exercise The exercise id the action is associated with
* @return array
*/
public static function get_weekly_view($year, $week, $time, $courseid, $categoryid, $action, $view, $userid, $exerciseid) {
// Parameter validation.
$params = self::validate_parameters(self::get_weekly_view_parameters(), [
'year' => $year,
'week' => $week,
'time' => $time,
'courseid' => $courseid,
'categoryid'=> $categoryid,
'action' => $action,
'view' => $view,
'studentid' => $userid,
'exerciseid'=> $exerciseid,
]);
// set the subscriber object
$subscriber = self::get_course_subscriber_context('/local/booking/', $courseid);
$calendar = \calendar_information::create($time, $params['courseid'], $params['categoryid']);
$data = [
'calendar' => $calendar,
'view' => $view,
'action' => $action,
'student' => $subscriber->get_participant($userid),
'exerciseid'=> $exerciseid == null ? 0 : $exerciseid,
];
$calendarview = new calendar_view($data, ['subscriber'=>$subscriber, 'context'=>$subscriber->get_context()]);
return $calendarview->get_exported_data();
}
/**
* Returns description of method result value.
*
* @return external_description
*/
public static function get_weekly_view_returns() {
return \local_booking\external\availability_week_exporter::get_read_structure();
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function save_booking_parameters() {
return new external_function_parameters(
array(
'bookedslot' => new external_single_structure(
array(
'starttime' => new external_value(PARAM_INT, 'booked slot start time', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'endtime' => new external_value(PARAM_INT, 'booked slot end time', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'year' => new external_value(PARAM_INT, 'booked slot year', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'week' => new external_value(PARAM_INT, 'booked slot week', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
), 'booking'),
'courseid' => new external_value(PARAM_INT, 'The course id', VALUE_DEFAULT),
'exerciseid' => new external_value(PARAM_INT, 'The exercise id', VALUE_DEFAULT),
'studentid' => new external_value(PARAM_INT, 'The student id', VALUE_DEFAULT),
)
);
}
/**
* Save booked slots. Delete existing ones for the user then update
* any existing slots if applicable with slot values
*
* @param {object} $bookedslot array containing booked slots.
* @param int $exerciseid The exercise the session is for.
* @param int $studentid The student id assocaited with the slot.
* @param int $refslotid The session slot associated.
* @return array array of slots created.
*/
public static function save_booking($slottobook, $courseid, $exerciseid, $studentid) {
global $USER;
// Parameter validation.
$params = self::validate_parameters(self::save_booking_parameters(), array(
'bookedslot' => $slottobook,
'courseid' => $courseid,
'exerciseid' => $exerciseid,
'studentid' => $studentid
)
);
// set the subscriber object
$subscriber = self::get_course_subscriber_context('/local/booking/', $courseid);
$warnings = array();
$instructorid = $USER->id;
// add a new tentatively booked slot for the student.
$sessiondata = [
'exercise' => $subscriber->get_exercise_name($exerciseid),
'instructor'=> student::get_fullname($instructorid),
'status' => ucwords(get_string('statustentative', 'local_booking')),
];
// add new booking and update slot
$studentslot = new slot(0,
$studentid,
$courseid,
$slottobook['starttime'],
$slottobook['endtime'],
$slottobook['year'],
$slottobook['week'],
get_string('statustentative', 'local_booking'),
get_string('bookinginfo', 'local_booking', $sessiondata)
);
$newbooking = new booking(0, $courseid, $studentid, $exerciseid, $studentslot,'', $instructorid);
$result = $newbooking->save();
if ($result) {
// remove restriciton override for the user
set_user_preference('local_booking_' .$courseid . '_availabilityoverride', false, $studentid);
// remove instructor from inactive group where applicable
$instructor = new instructor($subscriber, $instructorid);
if (!$instructor->is_active()) {
$instructor->activate();
}
// send emails to both student and instructor
$sessionstart = new DateTime('@' . $slottobook['starttime']);
$sessionend = new DateTime('@' . $slottobook['endtime']);
$message = new notification();
if ($message->send_booking_notification($studentid, $exerciseid, $sessionstart, $sessionend)) {
$message->send_instructor_confirmation($studentid, $exerciseid, $sessionstart, $sessionend);
}
$sessiondata['sessiondate'] = $sessionstart->format('D M j\, H:i');
$sessiondata['studentname'] = student::get_fullname($studentid);
\core\notification::SUCCESS(get_string('bookingsavesuccess', 'local_booking', $sessiondata));
} else {
\core\notification::WARNING(get_string('bookingsaveunable', 'local_booking'));
}
return array(
'result' => $result,
'warnings' => $warnings
);
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function save_booking_returns() {
return new external_single_structure(
array(
'result' => new external_value(PARAM_BOOL, get_string('processingresult', 'local_booking')),
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function cancel_booking_parameters() {
return new external_function_parameters(array(
'bookingid' => new external_value(PARAM_INT, 'The booking id', VALUE_DEFAULT),
'comment' => new external_value(PARAM_RAW, 'The instructor comment regarding the cancellation', VALUE_DEFAULT),
'noshow' => new external_value(PARAM_BOOL, 'Whether the cancellation is a no-show or instructor initiated', VALUE_DEFAULT),
)
);
}
/**
* Cancel an instructor's booking.
*
* @param int $bookingid
* @param string $comment
* @param bool $noshow
* @return bool $result
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function cancel_booking($bookingid, $comment, $noshow) {
global $COURSE;
// Parameter validation.
$params = self::validate_parameters(self::cancel_booking_parameters(), array(
'bookingid' => $bookingid,
'comment' => $comment,
'noshow' => $noshow,
)
);
$result = false;
$warnings = array();
$msg = '';
// get the booking to be cancelled
if (!empty($bookingid)) {
$booking = new booking($bookingid);
$booking->load();
$courseid = $booking->get_courseid() ?: $COURSE->id;
require_login($courseid, false);
// set the subscriber object
$subscriber = self::get_course_subscriber_context('/local/booking/', $courseid);
// cancel the booking
if ($result = $booking->cancel($noshow)) {
// suspend the student in the case of repetetive noshows
if ($noshow) {
$student = new student($subscriber, $booking->get_studentid());
if (count($student->get_noshow_bookings()) > 1) {
$student->suspend();
}
// send cancellation message
$message = new notification();
$result = $message->send_noshow_notification($booking, $subscriber->get_senior_instructors());
} else {
// enable restriction override if enabled to allow the student to repost slots sooner
if (intval($subscriber->overdueperiod) > 0) {
set_user_preference('local_booking_' . $courseid . '_availabilityoverride', true, $booking->get_studentid());
}
// send cancellation message to both instructor and student
$cancellationmessage = new notification();
$result = $cancellationmessage->send_session_cancellation($booking, $comment);
}
// confirmation Moodle notification to the instructor
$msg = get_string('bookingcanceledsuccess', 'local_booking', ['studentname'=>student::get_fullname($booking->get_studentid())]);
$msg .= $noshow ? ' ' . get_string('bookingcanceledsuccesswnoshow', 'local_booking') : '';
}
} else {
$msg = get_string('bookingcancelednotfound', 'local_booking');
}
if ($result) {
\core\notification::SUCCESS($msg);
} else {
\core\notification::WARNING($msg ?: get_string('bookingcanceledunable', 'local_booking'));
}
return array(
'result' => $result,
'warnings' => $warnings
);
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function cancel_booking_returns() {
return new external_single_structure(
array(
'result' => new external_value(PARAM_BOOL, get_string('processingresult', 'local_booking')),
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function is_conflicting_booking_parameters() {
return new external_function_parameters(
array(
'studentid' => new external_value(PARAM_INT, 'The student id the booking is for', VALUE_DEFAULT),
'bookedslot' => new external_single_structure(
array(
'starttime' => new external_value(PARAM_INT, 'booked slot start time', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'endtime' => new external_value(PARAM_INT, 'booked slot end time', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'year' => new external_value(PARAM_INT, 'booked slot year', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'week' => new external_value(PARAM_INT, 'booked slot week', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
), 'booking'),
)
);
}
/**
* Checks if the booking conflicts with another booking.
*
* @param {object} $bookedslot array containing booked slots.
* @return array array of slots created.
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function is_conflicting_booking($studentid, $slottobook) {
global $USER;
// Parameter validation.
$params = self::validate_parameters(self::save_booking_parameters(), array(
'studentid' => $studentid,
'bookedslot' => $slottobook,
)
);
$result = false;
$warnings = array();
$instructorid = $USER->id;
$conflictingbooking = booking::conflicts($instructorid, $studentid, $slottobook);
if (!empty($conflictingbooking)) {
// set the subscriber object
$subscriber = self::get_course_subscriber_context('/local/booking/', $conflictingbooking->courseid);
$result = true;
$warninginfo = [
'studentname' => participant::get_fullname($conflictingbooking->studentid),
'coursename' => $subscriber->get_shortname(),
'exercisename' => $subscriber->get_exercise_name($conflictingbooking->exerciseid),
'date' => (new \DateTime('@' . $conflictingbooking->starttime))->format('l M j \a\t H:i \z\u\l\u'),
];
$warnings[] = [
'warningcode' => 'errorconflictingbooking',
'message' => get_string('errorconflictingbooking', 'local_booking', $warninginfo)
];
}
return array(
'result' => $result,
'warnings' => $warnings
);
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function is_conflicting_booking_returns() {
return new external_single_structure(
array(
'result' => new external_value(PARAM_BOOL, get_string('processingresult', 'local_booking')),
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function update_user_preferences_parameters() {
return new external_function_parameters(array(
'preference' => new external_value(PARAM_RAW, 'The preference key', VALUE_DEFAULT),
'value' => new external_value(PARAM_RAW, 'The value of the preference', VALUE_DEFAULT),
'courseid' => new external_value(PARAM_INT, 'The course id', VALUE_DEFAULT),
'userid' => new external_value(PARAM_INT, 'The user id', VALUE_DEFAULT),
)
);
}
/**
* Set the user preferences
*
* @param string $preference The preference key of to be set.
* @param string $value The value of the preference to be set.
* @param int $courseid The course id.
* @param int $userid The user id.
* @return bool $result The result of the availability override operation.
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function update_user_preferences($preference, $value, $courseid, $userid) {
// Parameter validation.
$params = self::validate_parameters(self::update_user_preferences_parameters(), array(
'preference' => $preference,
'value' => $value,
'courseid' => $courseid,
'userid' => $userid,
)
);
$msgdata = ['preference' => $preference, 'value' => $value];
$warnings = array();
$result = set_user_preference('local_booking_' . $courseid . '_' . $preference, (gettype($value) == 'boolean' ? intval($value) : $value), $userid);
if (!$result) {
\core\notification::WARNING(get_string('bookingsetpreferencesunable', 'local_booking', $msgdata));
}
return array(
'result' => $result,
'warnings' => $warnings
);
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function update_user_preferences_returns() {
return new external_single_structure(
array(
'result' => new external_value(PARAM_BOOL, get_string('processingresult', 'local_booking')),
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function update_enrolement_status_parameters() {
return new external_function_parameters(array(
'status' => new external_value(PARAM_BOOL, 'The suspension status', VALUE_DEFAULT),
'courseid' => new external_value(PARAM_INT, 'The course id', VALUE_DEFAULT),