forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib.php
1481 lines (1312 loc) · 61.2 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/>.
/**
* External calendar API
*
* @package core_calendar
* @category external
* @copyright 2012 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.5
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/calendar/lib.php');
use core_calendar\local\api as local_api;
use core_calendar\local\event\container as event_container;
use core_calendar\local\event\forms\create as create_event_form;
use core_calendar\local\event\forms\update as update_event_form;
use core_calendar\local\event\mappers\create_update_form_mapper;
use core_calendar\external\event_exporter;
use core_calendar\external\events_exporter;
use core_calendar\external\events_grouped_by_course_exporter;
use core_calendar\external\events_related_objects_cache;
use core_external\external_api;
use core_external\external_format_value;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use core_external\util as external_util;
/**
* Calendar external functions
*
* @package core_calendar
* @category external
* @copyright 2012 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.5
*/
class core_calendar_external extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function delete_calendar_events_parameters() {
return new external_function_parameters(
array('events' => new external_multiple_structure(
new external_single_structure(
array(
'eventid' => new external_value(PARAM_INT, 'Event ID', VALUE_REQUIRED, '', NULL_NOT_ALLOWED),
'repeat' => new external_value(PARAM_BOOL, 'Delete comeplete series if repeated event')
), 'List of events to delete'
)
)
)
);
}
/**
* Delete Calendar events
*
* @param array $eventids A list of event ids with repeat flag to delete
* @return null
* @since Moodle 2.5
*/
public static function delete_calendar_events($events) {
global $DB;
// Parameter validation.
$params = self::validate_parameters(self:: delete_calendar_events_parameters(), array('events' => $events));
$transaction = $DB->start_delegated_transaction();
foreach ($params['events'] as $event) {
$eventobj = calendar_event::load($event['eventid']);
// Let's check if the user is allowed to delete an event.
if (!calendar_delete_event_allowed($eventobj)) {
throw new moodle_exception('nopermissions', 'error', '', get_string('deleteevent', 'calendar'));
}
// Time to do the magic.
$eventobj->delete($event['repeat']);
}
// Everything done smoothly, let's commit.
$transaction->allow_commit();
return null;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 2.5
*/
public static function delete_calendar_events_returns() {
return null;
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function get_calendar_events_parameters() {
return new external_function_parameters(
array('events' => new external_single_structure(
array(
'eventids' => new external_multiple_structure(
new external_value(PARAM_INT, 'event ids')
, 'List of event ids',
VALUE_DEFAULT, array()),
'courseids' => new external_multiple_structure(
new external_value(PARAM_INT, 'course ids')
, 'List of course ids for which events will be returned',
VALUE_DEFAULT, array()),
'groupids' => new external_multiple_structure(
new external_value(PARAM_INT, 'group ids')
, 'List of group ids for which events should be returned',
VALUE_DEFAULT, array()),
'categoryids' => new external_multiple_structure(
new external_value(PARAM_INT, 'Category ids'),
'List of category ids for which events will be returned',
VALUE_DEFAULT, array()),
), 'Event details', VALUE_DEFAULT, array()),
'options' => new external_single_structure(
array(
'userevents' => new external_value(PARAM_BOOL,
"Set to true to return current user's user events",
VALUE_DEFAULT, true, NULL_ALLOWED),
'siteevents' => new external_value(PARAM_BOOL,
"Set to true to return site events",
VALUE_DEFAULT, true, NULL_ALLOWED),
'timestart' => new external_value(PARAM_INT,
"Time from which events should be returned",
VALUE_DEFAULT, 0, NULL_ALLOWED),
'timeend' => new external_value(PARAM_INT,
"Time to which the events should be returned. We treat 0 and null as no end",
VALUE_DEFAULT, 0, NULL_ALLOWED),
'ignorehidden' => new external_value(PARAM_BOOL,
"Ignore hidden events or not",
VALUE_DEFAULT, true, NULL_ALLOWED),
), 'Options', VALUE_DEFAULT, array())
)
);
}
/**
* Get Calendar events
*
* @param array $events A list of events
* @param array $options various options
* @return array Array of event details
* @since Moodle 2.5
*/
public static function get_calendar_events($events = array(), $options = array()) {
global $SITE, $DB, $USER;
// Parameter validation.
$params = self::validate_parameters(self::get_calendar_events_parameters(), array('events' => $events, 'options' => $options));
$funcparam = array('courses' => array(), 'groups' => array(), 'categories' => array());
$hassystemcap = has_capability('moodle/calendar:manageentries', context_system::instance());
$warnings = array();
$coursecategories = array();
// Let us find out courses and their categories that we can return events from.
if (!$hassystemcap) {
$courseobjs = enrol_get_my_courses();
$courses = array_keys($courseobjs);
$coursecategories = array_flip(array_map(function($course) {
return $course->category;
}, $courseobjs));
foreach ($params['events']['courseids'] as $id) {
try {
$context = context_course::instance($id);
self::validate_context($context);
$funcparam['courses'][] = $id;
} catch (Exception $e) {
$warnings[] = array(
'item' => 'course',
'itemid' => $id,
'warningcode' => 'nopermissions',
'message' => 'No access rights in course context '.$e->getMessage().$e->getTraceAsString()
);
}
}
} else {
$courses = $params['events']['courseids'];
$funcparam['courses'] = $courses;
if (!empty($courses)) {
list($wheresql, $sqlparams) = $DB->get_in_or_equal($courses);
$wheresql = "id $wheresql";
$coursecategories = array_flip(array_map(function($course) {
return $course->category;
}, $DB->get_records_select('course', $wheresql, $sqlparams, '', 'category')));
}
}
// Let us findout groups that we can return events from.
if (!$hassystemcap) {
$groups = groups_get_my_groups();
$groups = array_keys($groups);
foreach ($params['events']['groupids'] as $id) {
if (in_array($id, $groups)) {
$funcparam['groups'][] = $id;
} else {
$warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to access this group');
}
}
} else {
$groups = $params['events']['groupids'];
$funcparam['groups'] = $groups;
}
$categories = array();
if ($hassystemcap || !empty($courses)) {
// Use the category id as the key in the following array. That way we do not have to remove duplicates and
// have a faster lookup later.
$categories = [];
if (!empty($params['events']['categoryids'])) {
$catobjs = \core_course_category::get_many(
array_merge($params['events']['categoryids'], array_keys($coursecategories)));
foreach ($catobjs as $catobj) {
if (isset($coursecategories[$catobj->id]) ||
has_capability('moodle/category:manage', $catobj->get_context())) {
// If the user has access to a course in this category or can manage the category,
// then they can see all parent categories too.
$categories[$catobj->id] = true;
foreach ($catobj->get_parents() as $catid) {
$categories[$catid] = true;
}
}
}
$funcparam['categories'] = array_keys($categories);
} else {
// Fetch all categories where this user has any enrolment, and all categories that this user can manage.
$calcatcache = cache::make('core', 'calendar_categories');
// Do not use cache if the user has the system capability as $coursecategories might not represent the
// courses the user is enrolled in.
$categories = (!$hassystemcap) ? $calcatcache->get('site') : false;
if ($categories !== false) {
// The ids are stored in a list in the cache.
$funcparam['categories'] = $categories;
$categories = array_flip($categories);
} else {
$categories = [];
foreach (\core_course_category::get_all() as $category) {
if (isset($coursecategories[$category->id]) ||
has_capability('moodle/category:manage', $category->get_context(), $USER, false)) {
// If the user has access to a course in this category or can manage the category,
// then they can see all parent categories too.
$categories[$category->id] = true;
foreach ($category->get_parents() as $catid) {
$categories[$catid] = true;
}
}
}
$funcparam['categories'] = array_keys($categories);
if (!$hassystemcap) {
$calcatcache->set('site', $funcparam['categories']);
}
}
}
}
// Do we need user events?
if (!empty($params['options']['userevents'])) {
$funcparam['users'] = array($USER->id);
} else {
$funcparam['users'] = false;
}
// Do we need site events?
if (!empty($params['options']['siteevents'])) {
$funcparam['courses'][] = $SITE->id;
}
// We treat 0 and null as no end.
if (empty($params['options']['timeend'])) {
$params['options']['timeend'] = PHP_INT_MAX;
}
// Event list does not check visibility and permissions, we'll check that later.
$eventlist = calendar_get_legacy_events($params['options']['timestart'], $params['options']['timeend'],
$funcparam['users'], $funcparam['groups'], $funcparam['courses'], true,
$params['options']['ignorehidden'], $funcparam['categories']);
// WS expects arrays.
$events = array();
// We need to get events asked for eventids.
if ($eventsbyid = calendar_get_events_by_id($params['events']['eventids'])) {
$eventlist += $eventsbyid;
}
foreach ($eventlist as $eventid => $eventobj) {
$event = (array) $eventobj;
// Description formatting.
$calendareventobj = new calendar_event($event);
$event['name'] = $calendareventobj->format_external_name();
list($event['description'], $event['format']) = $calendareventobj->format_external_text();
if ($hassystemcap) {
// User can see everything, no further check is needed.
$events[$eventid] = $event;
} else if (!empty($eventobj->modulename)) {
$courseid = $eventobj->courseid;
if (!$courseid) {
if (!$calendareventobj->context || !($context = $calendareventobj->context->get_course_context(false))) {
continue;
}
$courseid = $context->instanceid;
}
$instances = get_fast_modinfo($courseid)->get_instances_of($eventobj->modulename);
if (!empty($instances[$eventobj->instance]->uservisible)) {
$events[$eventid] = $event;
}
} else {
// Can the user actually see this event?
$eventobj = calendar_event::load($eventobj);
if ((($eventobj->courseid == $SITE->id) && (empty($eventobj->categoryid))) ||
(!empty($eventobj->categoryid) && isset($categories[$eventobj->categoryid])) ||
(!empty($eventobj->groupid) && in_array($eventobj->groupid, $groups)) ||
(!empty($eventobj->courseid) && in_array($eventobj->courseid, $courses)) ||
($USER->id == $eventobj->userid) ||
(calendar_edit_event_allowed($eventobj))) {
$events[$eventid] = $event;
} else {
$warnings[] = array('item' => $eventid, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to view this event');
}
}
}
return array('events' => $events, 'warnings' => $warnings);
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 2.5
*/
public static function get_calendar_events_returns() {
return new external_single_structure(array(
'events' => new external_multiple_structure( new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'event id'),
'name' => new external_value(PARAM_RAW, 'event name'),
'description' => new external_value(PARAM_RAW, 'Description', VALUE_OPTIONAL, null, NULL_ALLOWED),
'format' => new external_format_value('description'),
'courseid' => new external_value(PARAM_INT, 'course id'),
'categoryid' => new external_value(PARAM_INT, 'Category id (only for category events).',
VALUE_OPTIONAL),
'groupid' => new external_value(PARAM_INT, 'group id'),
'userid' => new external_value(PARAM_INT, 'user id'),
'repeatid' => new external_value(PARAM_INT, 'repeat id'),
'modulename' => new external_value(PARAM_TEXT, 'module name', VALUE_OPTIONAL, null, NULL_ALLOWED),
'instance' => new external_value(PARAM_INT, 'instance id'),
'eventtype' => new external_value(PARAM_TEXT, 'Event type'),
'timestart' => new external_value(PARAM_INT, 'timestart'),
'timeduration' => new external_value(PARAM_INT, 'time duration'),
'visible' => new external_value(PARAM_INT, 'visible'),
'uuid' => new external_value(PARAM_TEXT, 'unique id of ical events', VALUE_OPTIONAL, null, NULL_NOT_ALLOWED),
'sequence' => new external_value(PARAM_INT, 'sequence'),
'timemodified' => new external_value(PARAM_INT, 'time modified'),
'subscriptionid' => new external_value(PARAM_INT, 'Subscription id', VALUE_OPTIONAL, null, NULL_ALLOWED),
), 'event')
),
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters.
*
* @since Moodle 3.3
* @return external_function_parameters
*/
public static function get_calendar_action_events_by_timesort_parameters() {
return new external_function_parameters(
array(
'timesortfrom' => new external_value(PARAM_INT, 'Time sort from', VALUE_DEFAULT, 0),
'timesortto' => new external_value(PARAM_INT, 'Time sort to', VALUE_DEFAULT, null),
'aftereventid' => new external_value(PARAM_INT, 'The last seen event id', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 20),
'limittononsuspendedevents' => new external_value(PARAM_BOOL,
'Limit the events to courses the user is not suspended in', VALUE_DEFAULT, false),
'userid' => new external_value(PARAM_INT, 'The user id', VALUE_DEFAULT, null),
'searchvalue' => new external_value(PARAM_RAW, 'The value a user wishes to search against', VALUE_DEFAULT, null)
)
);
}
/**
* Get calendar action events based on the timesort value.
*
* @since Moodle 3.3
* @param null|int $timesortfrom Events after this time (inclusive)
* @param null|int $timesortto Events before this time (inclusive)
* @param null|int $aftereventid Get events with ids greater than this one
* @param int $limitnum Limit the number of results to this value
* @param null|int $userid The user id
* @param string|null $searchvalue The value a user wishes to search against
* @return array
*/
public static function get_calendar_action_events_by_timesort($timesortfrom = 0, $timesortto = null,
$aftereventid = 0, $limitnum = 20, $limittononsuspendedevents = false,
$userid = null, ?string $searchvalue = null) {
global $PAGE, $USER;
$params = self::validate_parameters(
self::get_calendar_action_events_by_timesort_parameters(),
[
'timesortfrom' => $timesortfrom,
'timesortto' => $timesortto,
'aftereventid' => $aftereventid,
'limitnum' => $limitnum,
'limittononsuspendedevents' => $limittononsuspendedevents,
'userid' => $userid,
'searchvalue' => $searchvalue
]
);
if ($params['userid']) {
$user = \core_user::get_user($params['userid']);
} else {
$user = $USER;
}
$context = \context_user::instance($user->id);
self::validate_context($context);
if ($params['userid'] && $USER->id !== $params['userid'] && !has_capability('moodle/calendar:manageentries', $context)) {
throw new \required_capability_exception($context, 'moodle/calendar:manageentries', 'nopermission', '');
}
if (empty($params['aftereventid'])) {
$params['aftereventid'] = null;
}
$renderer = $PAGE->get_renderer('core_calendar');
$events = local_api::get_action_events_by_timesort(
$params['timesortfrom'],
$params['timesortto'],
$params['aftereventid'],
$params['limitnum'],
$params['limittononsuspendedevents'],
$user,
clean_param($params['searchvalue'], PARAM_TEXT)
);
$exportercache = new events_related_objects_cache($events);
$exporter = new events_exporter($events, ['cache' => $exportercache]);
return $exporter->export($renderer);
}
/**
* Returns description of method result value.
*
* @since Moodle 3.3
* @return \core_external\external_description
*/
public static function get_calendar_action_events_by_timesort_returns() {
return events_exporter::get_read_structure();
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function get_calendar_action_events_by_course_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'Course id'),
'timesortfrom' => new external_value(PARAM_INT, 'Time sort from', VALUE_DEFAULT, null),
'timesortto' => new external_value(PARAM_INT, 'Time sort to', VALUE_DEFAULT, null),
'aftereventid' => new external_value(PARAM_INT, 'The last seen event id', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 20),
'searchvalue' => new external_value(PARAM_RAW, 'The value a user wishes to search against', VALUE_DEFAULT, null)
)
);
}
/**
* Get calendar action events for the given course.
*
* @since Moodle 3.3
* @param int $courseid Only events in this course
* @param null|int $timesortfrom Events after this time (inclusive)
* @param null|int $timesortto Events before this time (inclusive)
* @param null|int $aftereventid Get events with ids greater than this one
* @param int $limitnum Limit the number of results to this value
* @param string|null $searchvalue The value a user wishes to search against
* @return array
*/
public static function get_calendar_action_events_by_course(
$courseid, $timesortfrom = null, $timesortto = null, $aftereventid = 0, $limitnum = 20, ?string $searchvalue = null) {
global $PAGE, $USER;
$user = null;
$params = self::validate_parameters(
self::get_calendar_action_events_by_course_parameters(),
[
'courseid' => $courseid,
'timesortfrom' => $timesortfrom,
'timesortto' => $timesortto,
'aftereventid' => $aftereventid,
'limitnum' => $limitnum,
'searchvalue' => $searchvalue
]
);
$context = \context_user::instance($USER->id);
self::validate_context($context);
if (empty($params['aftereventid'])) {
$params['aftereventid'] = null;
}
$courses = enrol_get_my_courses('*', null, 0, [$courseid]);
$courses = array_values($courses);
if (empty($courses)) {
return [];
}
$course = $courses[0];
$renderer = $PAGE->get_renderer('core_calendar');
$events = local_api::get_action_events_by_course(
$course,
$params['timesortfrom'],
$params['timesortto'],
$params['aftereventid'],
$params['limitnum'],
clean_param($params['searchvalue'], PARAM_TEXT)
);
$exportercache = new events_related_objects_cache($events, $courses);
$exporter = new events_exporter($events, ['cache' => $exportercache]);
return $exporter->export($renderer);
}
/**
* Returns description of method result value.
*
* @return \core_external\external_description
*/
public static function get_calendar_action_events_by_course_returns() {
return events_exporter::get_read_structure();
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function get_calendar_action_events_by_courses_parameters() {
return new external_function_parameters(
array(
'courseids' => new external_multiple_structure(
new external_value(PARAM_INT, 'Course id')
),
'timesortfrom' => new external_value(PARAM_INT, 'Time sort from', VALUE_DEFAULT, null),
'timesortto' => new external_value(PARAM_INT, 'Time sort to', VALUE_DEFAULT, null),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 10),
'searchvalue' => new external_value(PARAM_RAW, 'The value a user wishes to search against', VALUE_DEFAULT, null)
)
);
}
/**
* Get calendar action events for a given list of courses.
*
* @since Moodle 3.3
* @param array $courseids Only include events for these courses
* @param null|int $timesortfrom Events after this time (inclusive)
* @param null|int $timesortto Events before this time (inclusive)
* @param int $limitnum Limit the number of results per course to this value
* @param string|null $searchvalue The value a user wishes to search against
* @return array
*/
public static function get_calendar_action_events_by_courses(
array $courseids, $timesortfrom = null, $timesortto = null, $limitnum = 10, ?string $searchvalue = null) {
global $PAGE, $USER;
$user = null;
$params = self::validate_parameters(
self::get_calendar_action_events_by_courses_parameters(),
[
'courseids' => $courseids,
'timesortfrom' => $timesortfrom,
'timesortto' => $timesortto,
'limitnum' => $limitnum,
'searchvalue' => $searchvalue
]
);
$context = \context_user::instance($USER->id);
self::validate_context($context);
if (empty($params['courseids'])) {
return ['groupedbycourse' => []];
}
$renderer = $PAGE->get_renderer('core_calendar');
$courses = enrol_get_my_courses('*', null, 0, $params['courseids']);
$courses = array_values($courses);
if (empty($courses)) {
return ['groupedbycourse' => []];
}
$events = local_api::get_action_events_by_courses(
$courses,
$params['timesortfrom'],
$params['timesortto'],
$params['limitnum'],
clean_param($params['searchvalue'], PARAM_TEXT)
);
if (empty($events)) {
return ['groupedbycourse' => []];
}
$exportercache = new events_related_objects_cache($events, $courses);
$exporter = new events_grouped_by_course_exporter($events, ['cache' => $exportercache]);
return $exporter->export($renderer);
}
/**
* Returns description of method result value.
*
* @return \core_external\external_description
*/
public static function get_calendar_action_events_by_courses_returns() {
return events_grouped_by_course_exporter::get_read_structure();
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters.
* @since Moodle 2.5
*/
public static function create_calendar_events_parameters() {
// Userid is always current user, so no need to get it from client.
// Module based calendar events are not allowed here. Hence no need of instance and modulename.
// subscription id and uuid is not allowed as this is not an ical api.
return new external_function_parameters(
array('events' => new external_multiple_structure(
new external_single_structure(
array(
'name' => new external_value(PARAM_TEXT, 'event name', VALUE_REQUIRED, '', NULL_NOT_ALLOWED),
'description' => new external_value(PARAM_RAW, 'Description', VALUE_DEFAULT, null, NULL_ALLOWED),
'format' => new external_format_value('description', VALUE_DEFAULT),
'courseid' => new external_value(PARAM_INT, 'course id', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'groupid' => new external_value(PARAM_INT, 'group id', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'repeats' => new external_value(PARAM_INT, 'number of repeats', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'eventtype' => new external_value(PARAM_TEXT, 'Event type', VALUE_DEFAULT, 'user', NULL_NOT_ALLOWED),
'timestart' => new external_value(PARAM_INT, 'timestart', VALUE_DEFAULT, time(), NULL_NOT_ALLOWED),
'timeduration' => new external_value(PARAM_INT, 'time duration', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'visible' => new external_value(PARAM_INT, 'visible', VALUE_DEFAULT, 1, NULL_NOT_ALLOWED),
'sequence' => new external_value(PARAM_INT, 'sequence', VALUE_DEFAULT, 1, NULL_NOT_ALLOWED),
), 'event')
)
)
);
}
/**
* Create calendar events.
*
* @param array $events A list of events to create.
* @return array array of events created.
* @since Moodle 2.5
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function create_calendar_events($events) {
global $DB, $USER;
// Parameter validation.
$params = self::validate_parameters(self::create_calendar_events_parameters(), array('events' => $events));
$transaction = $DB->start_delegated_transaction();
$return = array();
$warnings = array();
foreach ($params['events'] as $event) {
// Let us set some defaults.
$event['userid'] = $USER->id;
$event['modulename'] = '';
$event['instance'] = 0;
$event['subscriptionid'] = null;
$event['uuid']= '';
$event['format'] = external_util::validate_format($event['format']);
if ($event['repeats'] > 0) {
$event['repeat'] = 1;
} else {
$event['repeat'] = 0;
}
$eventobj = new calendar_event($event);
// Let's check if the user is allowed to delete an event.
if (!calendar_add_event_allowed($eventobj)) {
$warnings [] = array('item' => $event['name'], 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to create this event');
continue;
}
// Let's create the event.
$var = $eventobj->create($event);
$var = (array)$var->properties();
if ($event['repeat']) {
$children = $DB->get_records('event', array('repeatid' => $var['id']));
foreach ($children as $child) {
$return[] = (array) $child;
}
} else {
$return[] = $var;
}
}
// Everything done smoothly, let's commit.
$transaction->allow_commit();
return array('events' => $return, 'warnings' => $warnings);
}
/**
* Returns description of method result value.
*
* @return \core_external\external_description.
* @since Moodle 2.5
*/
public static function create_calendar_events_returns() {
return new external_single_structure(
array(
'events' => new external_multiple_structure( new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'event id'),
'name' => new external_value(PARAM_RAW, 'event name'),
'description' => new external_value(PARAM_RAW, 'Description', VALUE_OPTIONAL),
'format' => new external_format_value('description'),
'courseid' => new external_value(PARAM_INT, 'course id'),
'groupid' => new external_value(PARAM_INT, 'group id'),
'userid' => new external_value(PARAM_INT, 'user id'),
'repeatid' => new external_value(PARAM_INT, 'repeat id', VALUE_OPTIONAL),
'modulename' => new external_value(PARAM_TEXT, 'module name', VALUE_OPTIONAL),
'instance' => new external_value(PARAM_INT, 'instance id'),
'eventtype' => new external_value(PARAM_TEXT, 'Event type'),
'timestart' => new external_value(PARAM_INT, 'timestart'),
'timeduration' => new external_value(PARAM_INT, 'time duration'),
'visible' => new external_value(PARAM_INT, 'visible'),
'uuid' => new external_value(PARAM_TEXT, 'unique id of ical events', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
'sequence' => new external_value(PARAM_INT, 'sequence'),
'timemodified' => new external_value(PARAM_INT, 'time modified'),
'subscriptionid' => new external_value(PARAM_INT, 'Subscription id', VALUE_OPTIONAL),
), 'event')
),
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function get_calendar_event_by_id_parameters() {
return new external_function_parameters(
array(
'eventid' => new external_value(PARAM_INT, 'The event id to be retrieved'),
)
);
}
/**
* Get calendar event by id.
*
* @param int $eventid The calendar event id to be retrieved.
* @return array Array of event details
*/
public static function get_calendar_event_by_id($eventid) {
global $PAGE, $USER;
$params = self::validate_parameters(self::get_calendar_event_by_id_parameters(), ['eventid' => $eventid]);
$context = \context_user::instance($USER->id);
self::validate_context($context);
$warnings = array();
$eventvault = event_container::get_event_vault();
if ($event = $eventvault->get_event_by_id($params['eventid'])) {
$mapper = event_container::get_event_mapper();
if (!calendar_view_event_allowed($mapper->from_event_to_legacy_event($event))) {
throw new moodle_exception('nopermissiontoviewcalendar', 'error');
}
}
if (!$event) {
// We can't return a warning in this case because the event is not optional.
// We don't know the context for the event and it's not worth loading it.
$syscontext = context_system::instance();
throw new \required_capability_exception($syscontext, 'moodle/course:view', 'nopermissions', 'error');
}
$cache = new events_related_objects_cache([$event]);
$relatedobjects = [
'context' => $cache->get_context($event),
'course' => $cache->get_course($event),
];
$exporter = new event_exporter($event, $relatedobjects);
$renderer = $PAGE->get_renderer('core_calendar');
return array('event' => $exporter->export($renderer), 'warnings' => $warnings);
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
*/
public static function get_calendar_event_by_id_returns() {
$eventstructure = event_exporter::get_read_structure();
return new external_single_structure(array(
'event' => $eventstructure,
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters.
*/
public static function submit_create_update_form_parameters() {
return new external_function_parameters(
[
'formdata' => new external_value(PARAM_RAW, 'The data from the event form'),
]
);
}
/**
* Handles the event form submission.
*
* @param string $formdata The event form data in a URI encoded param string
* @return array The created or modified event
* @throws moodle_exception
*/
public static function submit_create_update_form($formdata) {
global $USER, $PAGE, $CFG;
require_once($CFG->libdir."/filelib.php");
// Parameter validation.
$params = self::validate_parameters(self::submit_create_update_form_parameters(), ['formdata' => $formdata]);
$context = \context_user::instance($USER->id);
$data = [];
self::validate_context($context);
parse_str($params['formdata'], $data);
if (WS_SERVER) {
// Request via WS, ignore sesskey checks in form library.
$USER->ignoresesskey = true;
}
$eventtype = isset($data['eventtype']) ? $data['eventtype'] : null;
$coursekey = ($eventtype == 'group') ? 'groupcourseid' : 'courseid';
$courseid = (!empty($data[$coursekey])) ? $data[$coursekey] : null;
$editoroptions = \core_calendar\local\event\forms\create::build_editor_options($context);
$formoptions = ['editoroptions' => $editoroptions, 'courseid' => $courseid];
$formoptions['eventtypes'] = calendar_get_allowed_event_types($courseid);
if ($courseid) {
require_once($CFG->libdir . '/grouplib.php');
$groupcoursedata = groups_get_course_data($courseid);
if (!empty($groupcoursedata->groups)) {
$formoptions['groups'] = [];
foreach ($groupcoursedata->groups as $groupid => $groupdata) {
$formoptions['groups'][$groupid] = $groupdata->name;
}
}
}
if (!empty($data['id'])) {
$eventid = clean_param($data['id'], PARAM_INT);
$legacyevent = calendar_event::load($eventid);
$legacyevent->count_repeats();
$formoptions['event'] = $legacyevent;
$mform = new update_event_form(null, $formoptions, 'post', '', null, true, $data);
} else {
$legacyevent = null;
$mform = new create_event_form(null, $formoptions, 'post', '', null, true, $data);
}
if ($validateddata = $mform->get_data()) {
$formmapper = new create_update_form_mapper();
$properties = $formmapper->from_data_to_event_properties($validateddata);
if (is_null($legacyevent)) {
$legacyevent = new \calendar_event($properties);
// Need to do this in order to initialise the description
// property which then triggers the update function below
// to set the appropriate default properties on the event.
$properties = $legacyevent->properties(true);
}
if (!calendar_edit_event_allowed($legacyevent, true)) {
throw new \moodle_exception('nopermissiontoupdatecalendar');
}
$legacyevent->update($properties);
$eventcontext = $legacyevent->context;
file_remove_editor_orphaned_files($validateddata->description);
// Take any files added to the description draft file area and
// convert them into the proper event description file area. Also
// parse the description text and replace the URLs to the draft files
// with the @@PLUGIN_FILE@@ placeholder to be persisted in the DB.
$description = file_save_draft_area_files(
$validateddata->description['itemid'],
$eventcontext->id,
'calendar',
'event_description',
$legacyevent->id,
create_event_form::build_editor_options($eventcontext),
$validateddata->description['text']
);
// If draft files were found then we need to save the new
// description value.
if ($description != $validateddata->description['text']) {
$properties->id = $legacyevent->id;
$properties->description = $description;
$legacyevent->update($properties);
}
$eventmapper = event_container::get_event_mapper();
$event = $eventmapper->from_legacy_event_to_event($legacyevent);
$cache = new events_related_objects_cache([$event]);
$relatedobjects = [
'context' => $cache->get_context($event),
'course' => $cache->get_course($event),
];
$exporter = new event_exporter($event, $relatedobjects);
$renderer = $PAGE->get_renderer('core_calendar');
return [ 'event' => $exporter->export($renderer) ];
} else {
return [ 'validationerror' => true ];
}
}
/**
* Returns description of method result value.
*
* @return \core_external\external_description.
*/
public static function submit_create_update_form_returns() {
$eventstructure = event_exporter::get_read_structure();
$eventstructure->required = VALUE_OPTIONAL;
return new external_single_structure(
array(
'event' => $eventstructure,
'validationerror' => new external_value(PARAM_BOOL, 'Invalid form data', VALUE_DEFAULT, false),
)