2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * External calendar API
21 * @package core_calendar
23 * @copyright 2012 Ankit Agarwal
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die;
30 require_once("$CFG->libdir/externallib.php");
32 use \core_calendar\local\api
as local_api
;
33 use \core_calendar\local\event\container
as event_container
;
34 use \core_calendar\local\event\forms\create
as create_event_form
;
35 use \core_calendar\local\event\forms\update
as update_event_form
;
36 use \core_calendar\local\event\mappers\create_update_form_mapper
;
37 use \core_calendar\external\event_exporter
;
38 use \core_calendar\external\events_exporter
;
39 use \core_calendar\external\events_grouped_by_course_exporter
;
40 use \core_calendar\external\events_related_objects_cache
;
43 * Calendar external functions
45 * @package core_calendar
47 * @copyright 2012 Ankit Agarwal
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 class core_calendar_external
extends external_api
{
55 * Returns description of method parameters
57 * @return external_function_parameters
60 public static function delete_calendar_events_parameters() {
61 return new external_function_parameters(
62 array('events' => new external_multiple_structure(
63 new external_single_structure(
65 'eventid' => new external_value(PARAM_INT
, 'Event ID', VALUE_REQUIRED
, '', NULL_NOT_ALLOWED
),
66 'repeat' => new external_value(PARAM_BOOL
, 'Delete comeplete series if repeated event')
67 ), 'List of events to delete'
75 * Delete Calendar events
77 * @param array $eventids A list of event ids with repeat flag to delete
81 public static function delete_calendar_events($events) {
83 require_once($CFG->dirroot
."/calendar/lib.php");
85 // Parameter validation.
86 $params = self
::validate_parameters(self
:: delete_calendar_events_parameters(), array('events' => $events));
88 $transaction = $DB->start_delegated_transaction();
90 foreach ($params['events'] as $event) {
91 $eventobj = calendar_event
::load($event['eventid']);
93 // Let's check if the user is allowed to delete an event.
94 if (!calendar_delete_event_allowed($eventobj)) {
95 throw new moodle_exception('nopermissions', 'error', '', get_string('deleteevent', 'calendar'));
97 // Time to do the magic.
98 $eventobj->delete($event['repeat']);
101 // Everything done smoothly, let's commit.
102 $transaction->allow_commit();
108 * Returns description of method result value
110 * @return external_description
113 public static function delete_calendar_events_returns() {
118 * Returns description of method parameters
120 * @return external_function_parameters
123 public static function get_calendar_events_parameters() {
124 return new external_function_parameters(
125 array('events' => new external_single_structure(
127 'eventids' => new external_multiple_structure(
128 new external_value(PARAM_INT
, 'event ids')
129 , 'List of event ids',
130 VALUE_DEFAULT
, array(), NULL_ALLOWED
132 'courseids' => new external_multiple_structure(
133 new external_value(PARAM_INT
, 'course ids')
134 , 'List of course ids for which events will be returned',
135 VALUE_DEFAULT
, array(), NULL_ALLOWED
137 'groupids' => new external_multiple_structure(
138 new external_value(PARAM_INT
, 'group ids')
139 , 'List of group ids for which events should be returned',
140 VALUE_DEFAULT
, array(), NULL_ALLOWED
142 ), 'Event details', VALUE_DEFAULT
, array()),
143 'options' => new external_single_structure(
145 'userevents' => new external_value(PARAM_BOOL
,
146 "Set to true to return current user's user events",
147 VALUE_DEFAULT
, true, NULL_ALLOWED
),
148 'siteevents' => new external_value(PARAM_BOOL
,
149 "Set to true to return global events",
150 VALUE_DEFAULT
, true, NULL_ALLOWED
),
151 'timestart' => new external_value(PARAM_INT
,
152 "Time from which events should be returned",
153 VALUE_DEFAULT
, 0, NULL_ALLOWED
),
154 'timeend' => new external_value(PARAM_INT
,
155 "Time to which the events should be returned. We treat 0 and null as no end",
156 VALUE_DEFAULT
, 0, NULL_ALLOWED
),
157 'ignorehidden' => new external_value(PARAM_BOOL
,
158 "Ignore hidden events or not",
159 VALUE_DEFAULT
, true, NULL_ALLOWED
),
161 ), 'Options', VALUE_DEFAULT
, array())
167 * Get Calendar events
169 * @param array $events A list of events
170 * @param array $options various options
171 * @return array Array of event details
174 public static function get_calendar_events($events = array(), $options = array()) {
175 global $SITE, $DB, $USER, $CFG;
176 require_once($CFG->dirroot
."/calendar/lib.php");
178 // Parameter validation.
179 $params = self
::validate_parameters(self
::get_calendar_events_parameters(), array('events' => $events, 'options' => $options));
180 $funcparam = array('courses' => array(), 'groups' => array());
181 $hassystemcap = has_capability('moodle/calendar:manageentries', context_system
::instance());
184 // Let us findout courses that we can return events from.
185 if (!$hassystemcap) {
186 $courses = enrol_get_my_courses('id');
187 $courses = array_keys($courses);
188 foreach ($params['events']['courseids'] as $id) {
190 $context = context_course
::instance($id);
191 self
::validate_context($context);
192 $funcparam['courses'][] = $id;
193 } catch (Exception
$e) {
197 'warningcode' => 'nopermissions',
198 'message' => 'No access rights in course context '.$e->getMessage().$e->getTraceAsString()
203 $courses = $params['events']['courseids'];
204 $funcparam['courses'] = $courses;
207 // Let us findout groups that we can return events from.
208 if (!$hassystemcap) {
209 $groups = groups_get_my_groups();
210 $groups = array_keys($groups);
211 foreach ($params['events']['groupids'] as $id) {
212 if (in_array($id, $groups)) {
213 $funcparam['groups'][] = $id;
215 $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to access this group');
219 $groups = $params['events']['groupids'];
220 $funcparam['groups'] = $groups;
223 // Do we need user events?
224 if (!empty($params['options']['userevents'])) {
225 $funcparam['users'] = array($USER->id
);
227 $funcparam['users'] = false;
230 // Do we need site events?
231 if (!empty($params['options']['siteevents'])) {
232 $funcparam['courses'][] = $SITE->id
;
235 // We treat 0 and null as no end.
236 if (empty($params['options']['timeend'])) {
237 $params['options']['timeend'] = PHP_INT_MAX
;
240 // Event list does not check visibility and permissions, we'll check that later.
241 $eventlist = calendar_get_legacy_events($params['options']['timestart'], $params['options']['timeend'],
242 $funcparam['users'], $funcparam['groups'], $funcparam['courses'], true, $params['options']['ignorehidden']);
244 // WS expects arrays.
247 // We need to get events asked for eventids.
248 if ($eventsbyid = calendar_get_events_by_id($params['events']['eventids'])) {
249 $eventlist +
= $eventsbyid;
252 foreach ($eventlist as $eventid => $eventobj) {
253 $event = (array) $eventobj;
254 // Description formatting.
255 $calendareventobj = new calendar_event($event);
256 list($event['description'], $event['format']) = $calendareventobj->format_external_text();
259 // User can see everything, no further check is needed.
260 $events[$eventid] = $event;
261 } else if (!empty($eventobj->modulename
)) {
262 $courseid = $eventobj->courseid
;
264 if (!$calendareventobj->context ||
!($context = $calendareventobj->context
->get_course_context(false))) {
267 $courseid = $context->instanceid
;
269 $instances = get_fast_modinfo($courseid)->get_instances_of($eventobj->modulename
);
270 if (!empty($instances[$eventobj->instance
]->uservisible
)) {
271 $events[$eventid] = $event;
274 // Can the user actually see this event?
275 $eventobj = calendar_event
::load($eventobj);
276 if (($eventobj->courseid
== $SITE->id
) ||
277 (!empty($eventobj->groupid
) && in_array($eventobj->groupid
, $groups)) ||
278 (!empty($eventobj->courseid
) && in_array($eventobj->courseid
, $courses)) ||
279 ($USER->id
== $eventobj->userid
) ||
280 (calendar_edit_event_allowed($eventobj))) {
281 $events[$eventid] = $event;
283 $warnings[] = array('item' => $eventid, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to view this event');
287 return array('events' => $events, 'warnings' => $warnings);
291 * Returns description of method result value
293 * @return external_description
296 public static function get_calendar_events_returns() {
297 return new external_single_structure(array(
298 'events' => new external_multiple_structure( new external_single_structure(
300 'id' => new external_value(PARAM_INT
, 'event id'),
301 'name' => new external_value(PARAM_TEXT
, 'event name'),
302 'description' => new external_value(PARAM_RAW
, 'Description', VALUE_OPTIONAL
, null, NULL_ALLOWED
),
303 'format' => new external_format_value('description'),
304 'courseid' => new external_value(PARAM_INT
, 'course id'),
305 'groupid' => new external_value(PARAM_INT
, 'group id'),
306 'userid' => new external_value(PARAM_INT
, 'user id'),
307 'repeatid' => new external_value(PARAM_INT
, 'repeat id'),
308 'modulename' => new external_value(PARAM_TEXT
, 'module name', VALUE_OPTIONAL
, null, NULL_ALLOWED
),
309 'instance' => new external_value(PARAM_INT
, 'instance id'),
310 'eventtype' => new external_value(PARAM_TEXT
, 'Event type'),
311 'timestart' => new external_value(PARAM_INT
, 'timestart'),
312 'timeduration' => new external_value(PARAM_INT
, 'time duration'),
313 'visible' => new external_value(PARAM_INT
, 'visible'),
314 'uuid' => new external_value(PARAM_TEXT
, 'unique id of ical events', VALUE_OPTIONAL
, null, NULL_NOT_ALLOWED
),
315 'sequence' => new external_value(PARAM_INT
, 'sequence'),
316 'timemodified' => new external_value(PARAM_INT
, 'time modified'),
317 'subscriptionid' => new external_value(PARAM_INT
, 'Subscription id', VALUE_OPTIONAL
, null, NULL_ALLOWED
),
320 'warnings' => new external_warnings()
326 * Returns description of method parameters.
329 * @return external_function_parameters
331 public static function get_calendar_action_events_by_timesort_parameters() {
332 return new external_function_parameters(
334 'timesortfrom' => new external_value(PARAM_INT
, 'Time sort from', VALUE_DEFAULT
, 0),
335 'timesortto' => new external_value(PARAM_INT
, 'Time sort to', VALUE_DEFAULT
, null),
336 'aftereventid' => new external_value(PARAM_INT
, 'The last seen event id', VALUE_DEFAULT
, 0),
337 'limitnum' => new external_value(PARAM_INT
, 'Limit number', VALUE_DEFAULT
, 20)
343 * Get calendar action events based on the timesort value.
346 * @param null|int $timesortfrom Events after this time (inclusive)
347 * @param null|int $timesortto Events before this time (inclusive)
348 * @param null|int $aftereventid Get events with ids greater than this one
349 * @param int $limitnum Limit the number of results to this value
352 public static function get_calendar_action_events_by_timesort($timesortfrom = 0, $timesortto = null,
353 $aftereventid = 0, $limitnum = 20) {
354 global $CFG, $PAGE, $USER;
356 require_once($CFG->dirroot
. '/calendar/lib.php');
359 $params = self
::validate_parameters(
360 self
::get_calendar_action_events_by_timesort_parameters(),
362 'timesortfrom' => $timesortfrom,
363 'timesortto' => $timesortto,
364 'aftereventid' => $aftereventid,
365 'limitnum' => $limitnum,
368 $context = \context_user
::instance($USER->id
);
369 self
::validate_context($context);
371 if (empty($params['aftereventid'])) {
372 $params['aftereventid'] = null;
375 $renderer = $PAGE->get_renderer('core_calendar');
376 $events = local_api
::get_action_events_by_timesort(
377 $params['timesortfrom'],
378 $params['timesortto'],
379 $params['aftereventid'],
383 $exportercache = new events_related_objects_cache($events);
384 $exporter = new events_exporter($events, ['cache' => $exportercache]);
386 return $exporter->export($renderer);
390 * Returns description of method result value.
393 * @return external_description
395 public static function get_calendar_action_events_by_timesort_returns() {
396 return events_exporter
::get_read_structure();
400 * Returns description of method parameters.
402 * @return external_function_parameters
404 public static function get_calendar_action_events_by_course_parameters() {
405 return new external_function_parameters(
407 'courseid' => new external_value(PARAM_INT
, 'Course id'),
408 'timesortfrom' => new external_value(PARAM_INT
, 'Time sort from', VALUE_DEFAULT
, null),
409 'timesortto' => new external_value(PARAM_INT
, 'Time sort to', VALUE_DEFAULT
, null),
410 'aftereventid' => new external_value(PARAM_INT
, 'The last seen event id', VALUE_DEFAULT
, 0),
411 'limitnum' => new external_value(PARAM_INT
, 'Limit number', VALUE_DEFAULT
, 20)
417 * Get calendar action events for the given course.
420 * @param int $courseid Only events in this course
421 * @param null|int $timesortfrom Events after this time (inclusive)
422 * @param null|int $timesortto Events before this time (inclusive)
423 * @param null|int $aftereventid Get events with ids greater than this one
424 * @param int $limitnum Limit the number of results to this value
427 public static function get_calendar_action_events_by_course(
428 $courseid, $timesortfrom = null, $timesortto = null, $aftereventid = 0, $limitnum = 20) {
430 global $CFG, $PAGE, $USER;
432 require_once($CFG->dirroot
. '/calendar/lib.php');
435 $params = self
::validate_parameters(
436 self
::get_calendar_action_events_by_course_parameters(),
438 'courseid' => $courseid,
439 'timesortfrom' => $timesortfrom,
440 'timesortto' => $timesortto,
441 'aftereventid' => $aftereventid,
442 'limitnum' => $limitnum,
445 $context = \context_user
::instance($USER->id
);
446 self
::validate_context($context);
448 if (empty($params['aftereventid'])) {
449 $params['aftereventid'] = null;
452 $courses = enrol_get_my_courses('*', 'visible DESC,sortorder ASC', 0, [$courseid]);
453 $courses = array_values($courses);
455 if (empty($courses)) {
459 $course = $courses[0];
460 $renderer = $PAGE->get_renderer('core_calendar');
461 $events = local_api
::get_action_events_by_course(
463 $params['timesortfrom'],
464 $params['timesortto'],
465 $params['aftereventid'],
469 $exportercache = new events_related_objects_cache($events, $courses);
470 $exporter = new events_exporter($events, ['cache' => $exportercache]);
472 return $exporter->export($renderer);
476 * Returns description of method result value.
478 * @return external_description
480 public static function get_calendar_action_events_by_course_returns() {
481 return events_exporter
::get_read_structure();
485 * Returns description of method parameters.
487 * @return external_function_parameters
489 public static function get_calendar_action_events_by_courses_parameters() {
490 return new external_function_parameters(
492 'courseids' => new external_multiple_structure(
493 new external_value(PARAM_INT
, 'Course id')
495 'timesortfrom' => new external_value(PARAM_INT
, 'Time sort from', VALUE_DEFAULT
, null),
496 'timesortto' => new external_value(PARAM_INT
, 'Time sort to', VALUE_DEFAULT
, null),
497 'limitnum' => new external_value(PARAM_INT
, 'Limit number', VALUE_DEFAULT
, 10)
503 * Get calendar action events for a given list of courses.
506 * @param array $courseids Only include events for these courses
507 * @param null|int $timesortfrom Events after this time (inclusive)
508 * @param null|int $timesortto Events before this time (inclusive)
509 * @param int $limitnum Limit the number of results per course to this value
512 public static function get_calendar_action_events_by_courses(
513 array $courseids, $timesortfrom = null, $timesortto = null, $limitnum = 10) {
515 global $CFG, $PAGE, $USER;
517 require_once($CFG->dirroot
. '/calendar/lib.php');
520 $params = self
::validate_parameters(
521 self
::get_calendar_action_events_by_courses_parameters(),
523 'courseids' => $courseids,
524 'timesortfrom' => $timesortfrom,
525 'timesortto' => $timesortto,
526 'limitnum' => $limitnum,
529 $context = \context_user
::instance($USER->id
);
530 self
::validate_context($context);
532 if (empty($params['courseids'])) {
533 return ['groupedbycourse' => []];
536 $renderer = $PAGE->get_renderer('core_calendar');
537 $courses = enrol_get_my_courses('*', 'visible DESC,sortorder ASC', 0, $params['courseids']);
538 $courses = array_values($courses);
540 if (empty($courses)) {
541 return ['groupedbycourse' => []];
544 $events = local_api
::get_action_events_by_courses(
546 $params['timesortfrom'],
547 $params['timesortto'],
551 if (empty($events)) {
552 return ['groupedbycourse' => []];
555 $exportercache = new events_related_objects_cache($events, $courses);
556 $exporter = new events_grouped_by_course_exporter($events, ['cache' => $exportercache]);
558 return $exporter->export($renderer);
562 * Returns description of method result value.
564 * @return external_description
566 public static function get_calendar_action_events_by_courses_returns() {
567 return events_grouped_by_course_exporter
::get_read_structure();
571 * Returns description of method parameters.
573 * @return external_function_parameters.
576 public static function create_calendar_events_parameters() {
577 // Userid is always current user, so no need to get it from client.
578 // Module based calendar events are not allowed here. Hence no need of instance and modulename.
579 // subscription id and uuid is not allowed as this is not an ical api.
580 return new external_function_parameters(
581 array('events' => new external_multiple_structure(
582 new external_single_structure(
584 'name' => new external_value(PARAM_TEXT
, 'event name', VALUE_REQUIRED
, '', NULL_NOT_ALLOWED
),
585 'description' => new external_value(PARAM_RAW
, 'Description', VALUE_DEFAULT
, null, NULL_ALLOWED
),
586 'format' => new external_format_value('description', VALUE_DEFAULT
),
587 'courseid' => new external_value(PARAM_INT
, 'course id', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
588 'groupid' => new external_value(PARAM_INT
, 'group id', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
589 'repeats' => new external_value(PARAM_INT
, 'number of repeats', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
590 'eventtype' => new external_value(PARAM_TEXT
, 'Event type', VALUE_DEFAULT
, 'user', NULL_NOT_ALLOWED
),
591 'timestart' => new external_value(PARAM_INT
, 'timestart', VALUE_DEFAULT
, time(), NULL_NOT_ALLOWED
),
592 'timeduration' => new external_value(PARAM_INT
, 'time duration', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
593 'visible' => new external_value(PARAM_INT
, 'visible', VALUE_DEFAULT
, 1, NULL_NOT_ALLOWED
),
594 'sequence' => new external_value(PARAM_INT
, 'sequence', VALUE_DEFAULT
, 1, NULL_NOT_ALLOWED
),
602 * Delete Calendar events.
604 * @param array $events A list of events to create.
605 * @return array array of events created.
607 * @throws moodle_exception if user doesnt have the permission to create events.
609 public static function create_calendar_events($events) {
610 global $CFG, $DB, $USER;
611 require_once($CFG->dirroot
."/calendar/lib.php");
613 // Parameter validation.
614 $params = self
::validate_parameters(self
::create_calendar_events_parameters(), array('events' => $events));
616 $transaction = $DB->start_delegated_transaction();
620 foreach ($params['events'] as $event) {
622 // Let us set some defaults.
623 $event['userid'] = $USER->id
;
624 $event['modulename'] = '';
625 $event['instance'] = 0;
626 $event['subscriptionid'] = null;
628 $event['format'] = external_validate_format($event['format']);
629 if ($event['repeats'] > 0) {
630 $event['repeat'] = 1;
632 $event['repeat'] = 0;
635 $eventobj = new calendar_event($event);
637 // Let's check if the user is allowed to delete an event.
638 if (!calendar_add_event_allowed($eventobj)) {
639 $warnings [] = array('item' => $event['name'], 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to create this event');
642 // Let's create the event.
643 $var = $eventobj->create($event);
644 $var = (array)$var->properties();
645 if ($event['repeat']) {
646 $children = $DB->get_records('event', array('repeatid' => $var['id']));
647 foreach ($children as $child) {
648 $return[] = (array) $child;
655 // Everything done smoothly, let's commit.
656 $transaction->allow_commit();
657 return array('events' => $return, 'warnings' => $warnings);
661 * Returns description of method result value.
663 * @return external_description.
666 public static function create_calendar_events_returns() {
667 return new external_single_structure(
669 'events' => new external_multiple_structure( new external_single_structure(
671 'id' => new external_value(PARAM_INT
, 'event id'),
672 'name' => new external_value(PARAM_TEXT
, 'event name'),
673 'description' => new external_value(PARAM_RAW
, 'Description', VALUE_OPTIONAL
),
674 'format' => new external_format_value('description'),
675 'courseid' => new external_value(PARAM_INT
, 'course id'),
676 'groupid' => new external_value(PARAM_INT
, 'group id'),
677 'userid' => new external_value(PARAM_INT
, 'user id'),
678 'repeatid' => new external_value(PARAM_INT
, 'repeat id', VALUE_OPTIONAL
),
679 'modulename' => new external_value(PARAM_TEXT
, 'module name', VALUE_OPTIONAL
),
680 'instance' => new external_value(PARAM_INT
, 'instance id'),
681 'eventtype' => new external_value(PARAM_TEXT
, 'Event type'),
682 'timestart' => new external_value(PARAM_INT
, 'timestart'),
683 'timeduration' => new external_value(PARAM_INT
, 'time duration'),
684 'visible' => new external_value(PARAM_INT
, 'visible'),
685 'uuid' => new external_value(PARAM_TEXT
, 'unique id of ical events', VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
686 'sequence' => new external_value(PARAM_INT
, 'sequence'),
687 'timemodified' => new external_value(PARAM_INT
, 'time modified'),
688 'subscriptionid' => new external_value(PARAM_INT
, 'Subscription id', VALUE_OPTIONAL
),
691 'warnings' => new external_warnings()
697 * Returns description of method parameters.
699 * @return external_function_parameters
701 public static function get_calendar_event_by_id_parameters() {
702 return new external_function_parameters(
704 'eventid' => new external_value(PARAM_INT
, 'The event id to be retrieved'),
710 * Get calendar event by id.
712 * @param int $eventid The calendar event id to be retrieved.
713 * @return array Array of event details
715 public static function get_calendar_event_by_id($eventid) {
716 global $CFG, $PAGE, $USER;
717 require_once($CFG->dirroot
."/calendar/lib.php");
719 $params = self
::validate_parameters(self
::get_calendar_event_by_id_parameters(), ['eventid' => $eventid]);
720 $context = \context_user
::instance($USER->id
);
722 self
::validate_context($context);
725 $legacyevent = calendar_event
::load($eventid);
726 $legacyevent->count_repeats();
728 $eventmapper = event_container
::get_event_mapper();
729 $event = $eventmapper->from_legacy_event_to_event($legacyevent);
731 $cache = new events_related_objects_cache([$event]);
733 'context' => $cache->get_context($event),
734 'course' => $cache->get_course($event),
737 $exporter = new event_exporter($event, $relatedobjects);
738 $renderer = $PAGE->get_renderer('core_calendar');
740 return array('event' => $exporter->export($renderer), 'warnings' => $warnings);
744 * Returns description of method result value
746 * @return external_description
748 public static function get_calendar_event_by_id_returns() {
749 $eventstructure = event_exporter
::get_read_structure();
751 return new external_single_structure(array(
752 'event' => $eventstructure,
753 'warnings' => new external_warnings()
759 * Returns description of method parameters.
761 * @return external_function_parameters.
763 public static function submit_create_update_form_parameters() {
764 return new external_function_parameters(
766 'formdata' => new external_value(PARAM_RAW
, 'The data from the event form'),
772 * Handles the event form submission.
774 * @param string $formdata The event form data in a URI encoded param string
775 * @return array The created or modified event
776 * @throws moodle_exception
778 public static function submit_create_update_form($formdata) {
779 global $CFG, $USER, $PAGE;
780 require_once($CFG->dirroot
."/calendar/lib.php");
781 require_once($CFG->libdir
."/filelib.php");
783 // Parameter validation.
784 $params = self
::validate_parameters(self
::submit_create_update_form_parameters(), ['formdata' => $formdata]);
785 $context = \context_user
::instance($USER->id
);
788 self
::validate_context($context);
789 parse_str($params['formdata'], $data);
791 if (!empty($data['id'])) {
792 $eventid = clean_param($data['id'], PARAM_INT
);
793 $legacyevent = calendar_event
::load($eventid);
794 $legacyevent->count_repeats();
795 $formoptions = ['event' => $legacyevent];
796 $mform = new update_event_form(null, $formoptions, 'post', '', null, true, $data);
799 $mform = new create_event_form(null, null, 'post', '', null, true, $data);
802 if ($validateddata = $mform->get_data()) {
803 $formmapper = new create_update_form_mapper();
804 $properties = $formmapper->from_data_to_event_properties($validateddata);
806 if (is_null($legacyevent)) {
807 $legacyevent = new \
calendar_event($properties);
808 // Need to do this in order to initialise the description
809 // property which then triggers the update function below
810 // to set the appropriate default properties on the event.
811 $properties = $legacyevent->properties(true);
814 if (!calendar_edit_event_allowed($legacyevent, true)) {
815 print_error('nopermissiontoupdatecalendar');
818 $legacyevent->update($properties);
819 $eventcontext = $legacyevent->context
;
821 file_remove_editor_orphaned_files($validateddata->description
);
823 // Take any files added to the description draft file area and
824 // convert them into the proper event description file area. Also
825 // parse the description text and replace the URLs to the draft files
826 // with the @@PLUGIN_FILE@@ placeholder to be persisted in the DB.
827 $description = file_save_draft_area_files(
828 $validateddata->description
['itemid'],
833 create_event_form
::build_editor_options($eventcontext),
834 $validateddata->description
['text']
837 // If draft files were found then we need to save the new
838 // description value.
839 if ($description != $validateddata->description
['text']) {
840 $properties->id
= $legacyevent->id
;
841 $properties->description
= $description;
842 $legacyevent->update($properties);
845 $eventmapper = event_container
::get_event_mapper();
846 $event = $eventmapper->from_legacy_event_to_event($legacyevent);
847 $cache = new events_related_objects_cache([$event]);
849 'context' => $cache->get_context($event),
850 'course' => $cache->get_course($event),
852 $exporter = new event_exporter($event, $relatedobjects);
853 $renderer = $PAGE->get_renderer('core_calendar');
855 return [ 'event' => $exporter->export($renderer) ];
857 return [ 'validationerror' => true ];
862 * Returns description of method result value.
864 * @return external_description.
866 public static function submit_create_update_form_returns() {
867 $eventstructure = event_exporter
::get_read_structure();
868 $eventstructure->required
= VALUE_OPTIONAL
;
870 return new external_single_structure(
872 'event' => $eventstructure,
873 'validationerror' => new external_value(PARAM_BOOL
, 'Invalid form data', VALUE_DEFAULT
, false),
879 * Get data for the monthly calendar view.
881 * @param int $year The year to be shown
882 * @param int $month The month to be shown
883 * @param int $courseid The course to be included
884 * @param int $categoryid The category to be included
885 * @param bool $includenavigation Whether to include navigation
888 public static function get_calendar_monthly_view($year, $month, $courseid, $categoryid, $includenavigation) {
889 global $CFG, $DB, $USER, $PAGE;
890 require_once($CFG->dirroot
."/calendar/lib.php");
892 // Parameter validation.
893 $params = self
::validate_parameters(self
::get_calendar_monthly_view_parameters(), [
896 'courseid' => $courseid,
897 'categoryid' => $categoryid,
898 'includenavigation' => $includenavigation,
901 $context = \context_user
::instance($USER->id
);
902 self
::validate_context($context);
903 $PAGE->set_url('/calendar/');
905 $type = \core_calendar\type_factory
::get_calendar_instance();
907 $time = $type->convert_to_timestamp($params['year'], $params['month'], 1);
908 $calendar = \calendar_information
::create($time, $params['courseid'], $params['categoryid']);
909 self
::validate_context($calendar->context
);
911 list($data, $template) = calendar_get_view($calendar, 'month', $params['includenavigation']);
917 * Returns description of method parameters.
919 * @return external_function_parameters
921 public static function get_calendar_monthly_view_parameters() {
922 return new external_function_parameters(
924 'year' => new external_value(PARAM_INT
, 'Month to be viewed', VALUE_REQUIRED
),
925 'month' => new external_value(PARAM_INT
, 'Year to be viewed', VALUE_REQUIRED
),
926 'courseid' => new external_value(PARAM_INT
, 'Course being viewed', VALUE_DEFAULT
, SITEID
, NULL_ALLOWED
),
927 'categoryid' => new external_value(PARAM_INT
, 'Category being viewed', VALUE_DEFAULT
, null, NULL_ALLOWED
),
928 'includenavigation' => new external_value(
930 'Whether to show course navigation',
940 * Returns description of method result value.
942 * @return external_description
944 public static function get_calendar_monthly_view_returns() {
945 return \core_calendar\external\month_exporter
::get_read_structure();
949 * Get data for the daily calendar view.
951 * @param int $year The year to be shown
952 * @param int $month The month to be shown
953 * @param int $day The day to be shown
954 * @param int $courseid The course to be included
957 public static function get_calendar_day_view($year, $month, $day, $courseid, $categoryid) {
958 global $CFG, $DB, $USER, $PAGE;
959 require_once($CFG->dirroot
."/calendar/lib.php");
961 // Parameter validation.
962 $params = self
::validate_parameters(self
::get_calendar_day_view_parameters(), [
966 'courseid' => $courseid,
967 'categoryid' => $categoryid,
970 $context = \context_user
::instance($USER->id
);
971 self
::validate_context($context);
973 $type = \core_calendar\type_factory
::get_calendar_instance();
975 $time = $type->convert_to_timestamp($params['year'], $params['month'], $params['day']);
976 $calendar = \calendar_information
::create($time, $params['courseid'], $params['categoryid']);
977 self
::validate_context($calendar->context
);
979 list($data, $template) = calendar_get_view($calendar, 'day', $params['includenavigation']);
985 * Returns description of method parameters.
987 * @return external_function_parameters
989 public static function get_calendar_day_view_parameters() {
990 return new external_function_parameters(
992 'year' => new external_value(PARAM_INT
, 'Year to be viewed', VALUE_REQUIRED
),
993 'month' => new external_value(PARAM_INT
, 'Month to be viewed', VALUE_REQUIRED
),
994 'day' => new external_value(PARAM_INT
, 'Day to be viewed', VALUE_REQUIRED
),
995 'courseid' => new external_value(PARAM_INT
, 'Course being viewed', VALUE_DEFAULT
, SITEID
, NULL_ALLOWED
),
996 'categoryid' => new external_value(PARAM_INT
, 'Category being viewed', VALUE_DEFAULT
, null, NULL_ALLOWED
),
1002 * Returns description of method result value.
1004 * @return external_description
1006 public static function get_calendar_day_view_returns() {
1007 return \core_calendar\external\calendar_day_exporter
::get_read_structure();
1012 * Returns description of method parameters.
1014 * @return external_function_parameters
1016 public static function update_event_start_day_parameters() {
1017 return new external_function_parameters(
1019 'eventid' => new external_value(PARAM_INT
, 'Id of event to be updated', VALUE_REQUIRED
),
1020 'daytimestamp' => new external_value(PARAM_INT
, 'Timestamp for the new start day', VALUE_REQUIRED
),
1026 * Change the start day for the given calendar event to the day that
1027 * corresponds with the provided timestamp.
1029 * The timestamp only needs to be anytime within the desired day as only
1030 * the date data is extracted from it.
1032 * The event's original time of day is maintained, only the date is shifted.
1034 * @param int $eventid Id of event to be updated
1035 * @param int $daytimestamp Timestamp for the new start day
1038 public static function update_event_start_day($eventid, $daytimestamp) {
1039 global $USER, $PAGE;
1041 // Parameter validation.
1042 $params = self
::validate_parameters(self
::update_event_start_day_parameters(), [
1043 'eventid' => $eventid,
1044 'daytimestamp' => $daytimestamp,
1047 $vault = event_container
::get_event_vault();
1048 $mapper = event_container
::get_event_mapper();
1049 $event = $vault->get_event_by_id($eventid);
1052 throw new \
moodle_exception('Unable to find event with id ' . $eventid);
1055 $legacyevent = $mapper->from_event_to_legacy_event($event);
1057 if (!calendar_edit_event_allowed($legacyevent, true)) {
1058 print_error('nopermissiontoupdatecalendar');
1061 self
::validate_context($legacyevent->context
);
1063 $newdate = usergetdate($daytimestamp);
1064 $startdatestring = implode('-', [$newdate['year'], $newdate['mon'], $newdate['mday']]);
1065 $startdate = new DateTimeImmutable($startdatestring);
1066 $event = local_api
::update_event_start_day($event, $startdate);
1067 $cache = new events_related_objects_cache([$event]);
1069 'context' => $cache->get_context($event),
1070 'course' => $cache->get_course($event),
1072 $exporter = new event_exporter($event, $relatedobjects);
1073 $renderer = $PAGE->get_renderer('core_calendar');
1075 return array('event' => $exporter->export($renderer));
1079 * Returns description of method result value.
1081 * @return external_description
1083 public static function update_event_start_day_returns() {
1084 return new external_single_structure(
1086 'event' => event_exporter
::get_read_structure()
1092 * Get data for the monthly calendar view.
1094 * @param int $courseid The course to be included
1095 * @param int $categoryid The category to be included
1098 public static function get_calendar_upcoming_view($courseid, $categoryid) {
1099 global $CFG, $DB, $USER, $PAGE;
1100 require_once($CFG->dirroot
."/calendar/lib.php");
1102 // Parameter validation.
1103 $params = self
::validate_parameters(self
::get_calendar_upcoming_view_parameters(), [
1104 'courseid' => $courseid,
1105 'categoryid' => $categoryid,
1108 $context = \context_user
::instance($USER->id
);
1109 self
::validate_context($context);
1110 $PAGE->set_url('/calendar/');
1112 $calendar = \calendar_information
::create(time(), $params['courseid'], $params['categoryid']);
1113 self
::validate_context($calendar->context
);
1115 list($data, $template) = calendar_get_view($calendar, 'upcoming');
1121 * Returns description of method parameters.
1123 * @return external_function_parameters
1125 public static function get_calendar_upcoming_view_parameters() {
1126 return new external_function_parameters(
1128 'courseid' => new external_value(PARAM_INT
, 'Course being viewed', VALUE_DEFAULT
, SITEID
, NULL_ALLOWED
),
1129 'categoryid' => new external_value(PARAM_INT
, 'Category being viewed', VALUE_DEFAULT
, null, NULL_ALLOWED
),
1135 * Returns description of method result value.
1137 * @return external_description
1139 public static function get_calendar_upcoming_view_returns() {
1140 return \core_calendar\external\calendar_upcoming_exporter
::get_read_structure();