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($eventid))) {
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 = ['eventid' => $eventid];
720 self
::validate_parameters(self
::get_calendar_event_by_id_parameters(), $params);
721 $context = \context_user
::instance($USER->id
);
723 self
::validate_context($context);
726 $legacyevent = calendar_event
::load($eventid);
727 $legacyevent->count_repeats();
729 $eventmapper = event_container
::get_event_mapper();
730 $event = $eventmapper->from_legacy_event_to_event($legacyevent);
732 $cache = new events_related_objects_cache([$event]);
734 'context' => $cache->get_context($event),
735 'course' => $cache->get_course($event),
738 $exporter = new event_exporter($event, $relatedobjects);
739 $renderer = $PAGE->get_renderer('core_calendar');
741 return array('event' => $exporter->export($renderer), 'warnings' => $warnings);
745 * Returns description of method result value
747 * @return external_description
749 public static function get_calendar_event_by_id_returns() {
750 $eventstructure = event_exporter
::get_read_structure();
752 return new external_single_structure(array(
753 'event' => $eventstructure,
754 'warnings' => new external_warnings()
760 * Returns description of method parameters.
762 * @return external_function_parameters.
764 public static function submit_create_update_form_parameters() {
765 return new external_function_parameters(
767 'formdata' => new external_value(PARAM_RAW
, 'The data from the event form'),
773 * Handles the event form submission.
775 * @param string $formdata The event form data in a URI encoded param string
776 * @return array The created or modified event
777 * @throws moodle_exception
779 public static function submit_create_update_form($formdata) {
780 global $CFG, $USER, $PAGE;
781 require_once($CFG->dirroot
."/calendar/lib.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);
820 $eventmapper = event_container
::get_event_mapper();
821 $event = $eventmapper->from_legacy_event_to_event($legacyevent);
822 $cache = new events_related_objects_cache([$event]);
824 'context' => $cache->get_context($event),
825 'course' => $cache->get_course($event),
827 $exporter = new event_exporter($event, $relatedobjects);
828 $renderer = $PAGE->get_renderer('core_calendar');
830 return [ 'event' => $exporter->export($renderer) ];
832 return [ 'validationerror' => true ];
837 * Returns description of method result value.
839 * @return external_description.
841 public static function submit_create_update_form_returns() {
842 $eventstructure = event_exporter
::get_read_structure();
843 $eventstructure->required
= VALUE_OPTIONAL
;
845 return new external_single_structure(
847 'event' => $eventstructure,
848 'validationerror' => new external_value(PARAM_BOOL
, 'Invalid form data', VALUE_DEFAULT
, false),
854 * Get data for the monthly calendar view.
856 * @param int $time The time to be shown
857 * @param int $courseid The course to be included
860 public static function get_calendar_monthly_view($time, $courseid) {
861 global $CFG, $DB, $USER, $PAGE;
862 require_once($CFG->dirroot
."/calendar/lib.php");
864 // Parameter validation.
865 $params = self
::validate_parameters(self
::get_calendar_monthly_view_parameters(), [
867 'courseid' => $courseid,
870 if ($courseid != SITEID
&& !empty($courseid)) {
871 // Course ID must be valid and existing.
872 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST
);
873 $courses = [$course->id
=> $course];
875 $course = get_site();
876 $courses = calendar_get_default_courses();
879 // TODO: Copy what we do in calendar/view.php.
880 $context = \context_user
::instance($USER->id
);
881 self
::validate_context($context);
883 $calendar = new calendar_information(0, 0, 0, $time);
884 $calendar->prepare_for_view($course, $courses);
886 list($data, $template) = calendar_get_view($calendar, 'month');
892 * Returns description of method parameters.
894 * @return external_function_parameters
896 public static function get_calendar_monthly_view_parameters() {
897 return new external_function_parameters(
899 'time' => new external_value(PARAM_INT
, 'Time to be viewed', VALUE_REQUIRED
, '', NULL_NOT_ALLOWED
),
900 'courseid' => new external_value(PARAM_INT
, 'Course being viewed', VALUE_DEFAULT
, SITEID
, NULL_ALLOWED
),
906 * Returns description of method result value.
908 * @return external_description
910 public static function get_calendar_monthly_view_returns() {
911 return \core_calendar\external\month_exporter
::get_read_structure();
915 * Returns description of method parameters.
917 * @return external_function_parameters
919 public static function update_event_start_day_parameters() {
920 return new external_function_parameters(
922 'eventid' => new external_value(PARAM_INT
, 'Id of event to be updated', VALUE_REQUIRED
),
923 'daytimestamp' => new external_value(PARAM_INT
, 'Timestamp for the new start day', VALUE_REQUIRED
),
929 * Change the start day for the given calendar event to the day that
930 * corresponds with the provided timestamp.
932 * The timestamp only needs to be anytime within the desired day as only
933 * the date data is extracted from it.
935 * The event's original time of day is maintained, only the date is shifted.
937 * @param int $eventid Id of event to be updated
938 * @param int $daytimestamp Timestamp for the new start day
941 public static function update_event_start_day($eventid, $daytimestamp) {
944 // Parameter validation.
945 $params = self
::validate_parameters(self
::update_event_start_day_parameters(), [
946 'eventid' => $eventid,
947 'daytimestamp' => $daytimestamp,
950 $vault = event_container
::get_event_vault();
951 $mapper = event_container
::get_event_mapper();
952 $event = $vault->get_event_by_id($eventid);
955 throw new \
moodle_exception('Unable to find event with id ' . $eventid);
958 $legacyevent = $mapper->from_event_to_legacy_event($event);
960 if (!calendar_edit_event_allowed($legacyevent, true)) {
961 print_error('nopermissiontoupdatecalendar');
964 self
::validate_context($legacyevent->context
);
966 $newdate = usergetdate($daytimestamp);
967 $startdatestring = implode('-', [$newdate['year'], $newdate['mon'], $newdate['mday']]);
968 $startdate = new DateTimeImmutable($startdatestring);
969 $event = local_api
::update_event_start_day($event, $startdate);
970 $cache = new events_related_objects_cache([$event]);
972 'context' => $cache->get_context($event),
973 'course' => $cache->get_course($event),
975 $exporter = new event_exporter($event, $relatedobjects);
976 $renderer = $PAGE->get_renderer('core_calendar');
978 return array('event' => $exporter->export($renderer));
982 * Returns description of method result value.
984 * @return external_description
986 public static function update_event_start_day_returns() {
987 return new external_single_structure(
989 'event' => event_exporter
::get_read_structure()