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");
33 * Calendar external functions
35 * @package core_calendar
37 * @copyright 2012 Ankit Agarwal
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class core_calendar_external
extends external_api
{
45 * Returns description of method parameters
47 * @return external_function_parameters
50 public static function delete_calendar_events_parameters() {
51 return new external_function_parameters(
52 array('events' => new external_multiple_structure(
53 new external_single_structure(
55 'eventid' => new external_value(PARAM_INT
, 'Event ID', VALUE_REQUIRED
, '', NULL_NOT_ALLOWED
),
56 'repeat' => new external_value(PARAM_BOOL
, 'Delete comeplete series if repeated event')
57 ), 'List of events to delete'
65 * Delete Calendar events
67 * @param array $eventids A list of event ids with repeat flag to delete
71 public static function delete_calendar_events($events) {
73 require_once($CFG->dirroot
."/calendar/lib.php");
75 // Parameter validation.
76 $params = self
::validate_parameters(self
:: delete_calendar_events_parameters(), array('events' => $events));
78 $transaction = $DB->start_delegated_transaction();
80 foreach ($params['events'] as $event) {
81 $eventobj = calendar_event
::load($event['eventid']);
83 // Let's check if the user is allowed to delete an event.
84 if (!calendar_edit_event_allowed($eventobj)) {
85 throw new moodle_exception("nopermissions");
87 // Time to do the magic.
88 $eventobj->delete($event['repeat']);
91 // Everything done smoothly, let's commit.
92 $transaction->allow_commit();
98 * Returns description of method result value
100 * @return external_description
103 public static function delete_calendar_events_returns() {
108 * Returns description of method parameters
110 * @return external_function_parameters
113 public static function get_calendar_events_parameters() {
114 return new external_function_parameters(
115 array('events' => new external_single_structure(
117 'eventids' => new external_multiple_structure(
118 new external_value(PARAM_INT
, 'event ids')
119 , 'List of event ids',
120 VALUE_DEFAULT
, array(), NULL_ALLOWED
122 'courseids' => new external_multiple_structure(
123 new external_value(PARAM_INT
, 'course ids')
124 , 'List of course ids for which events will be returned',
125 VALUE_DEFAULT
, array(), NULL_ALLOWED
127 'groupids' => new external_multiple_structure(
128 new external_value(PARAM_INT
, 'group ids')
129 , 'List of group ids for which events should be returned',
130 VALUE_DEFAULT
, array(), NULL_ALLOWED
132 ), 'Event details', VALUE_DEFAULT
, array()),
133 'options' => new external_single_structure(
135 'userevents' => new external_value(PARAM_BOOL
,
136 "Set to true to return current user's user events",
137 VALUE_DEFAULT
, true, NULL_ALLOWED
),
138 'siteevents' => new external_value(PARAM_BOOL
,
139 "Set to true to return global events",
140 VALUE_DEFAULT
, true, NULL_ALLOWED
),
141 'timestart' => new external_value(PARAM_INT
,
142 "Time from which events should be returned",
143 VALUE_DEFAULT
, 0, NULL_ALLOWED
),
144 'timeend' => new external_value(PARAM_INT
,
145 "Time to which the events should be returned. We treat 0 and null as no end",
146 VALUE_DEFAULT
, 0, NULL_ALLOWED
),
147 'ignorehidden' => new external_value(PARAM_BOOL
,
148 "Ignore hidden events or not",
149 VALUE_DEFAULT
, true, NULL_ALLOWED
),
151 ), 'Options', VALUE_DEFAULT
, array())
157 * Get Calendar events
159 * @param array $events A list of events
160 * @param array $options various options
161 * @return array Array of event details
164 public static function get_calendar_events($events = array(), $options = array()) {
165 global $SITE, $DB, $USER, $CFG;
166 require_once($CFG->dirroot
."/calendar/lib.php");
168 // Parameter validation.
169 $params = self
::validate_parameters(self
::get_calendar_events_parameters(), array('events' => $events, 'options' => $options));
170 $funcparam = array('courses' => array(), 'groups' => array());
171 $hassystemcap = has_capability('moodle/calendar:manageentries', context_system
::instance());
174 // Let us findout courses that we can return events from.
175 if (!$hassystemcap) {
176 foreach ($params['events']['courseids'] as $id) {
178 $context = context_course
::instance($id);
179 self
::validate_context($context);
180 $funcparam['courses'][] = $id;
181 } catch (Exception
$e) {
185 'warningcode' => 'nopermissions',
186 'message' => 'No access rights in course context '.$e->getMessage().$e->getTraceAsString()
192 $courses = $params['events']['courseids'];
193 $funcparam['courses'] = $courses;
196 // Let us findout groups that we can return events from.
197 if (!$hassystemcap) {
198 $groups = groups_get_my_groups();
199 $groups = array_keys($groups);
200 foreach ($params['events']['groupids'] as $id) {
201 if (in_array($id, $groups)) {
202 $funcparam['groups'][] = $id;
204 $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to access this group');
208 $groups = $params['events']['groupids'];
209 $funcparam['groups'] = $groups;
212 // Do we need user events?
213 if (!empty($params['options']['userevents'])) {
214 $funcparam['users'] = array($USER->id
);
216 $funcparam['users'] = false;
219 // Do we need site events?
220 if (!empty($params['options']['siteevents'])) {
221 $funcparam['courses'][] = $SITE->id
;
224 // We treat 0 and null as no end.
225 if (empty($params['options']['timeend'])) {
226 $params['options']['timeend'] = PHP_INT_MAX
;
229 $eventlist = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'],
230 $funcparam['courses'], true, $params['options']['ignorehidden']);
231 // WS expects arrays.
233 foreach ($eventlist as $id => $event) {
234 $events[$id] = (array) $event;
237 // We need to get events asked for eventids.
238 $eventsbyid = calendar_get_events_by_id($params['events']['eventids']);
239 foreach ($eventsbyid as $eventid => $eventobj) {
240 $event = (array) $eventobj;
241 if (isset($events[$eventid])) {
245 // User can see everything, no further check is needed.
246 $events[$eventid] = $event;
247 } else if (!empty($eventobj->modulename
)) {
248 $cm = get_coursemodule_from_instance($eventobj->modulename
, $eventobj->instance
);
249 if (\core_availability\info_module
::is_user_visible($cm, 0, false)) {
250 $events[$eventid] = $event;
253 // Can the user actually see this event?
254 $eventobj = calendar_event
::load($eventobj);
255 if (($eventobj->courseid
== $SITE->id
) ||
256 (!empty($eventobj->groupid
) && in_array($eventobj->groupid
, $groups)) ||
257 (!empty($eventobj->courseid
) && in_array($eventobj->courseid
, $courses)) ||
258 ($USER->id
== $eventobj->userid
) ||
259 (calendar_edit_event_allowed($eventid))) {
260 $events[$eventid] = $event;
262 $warnings[] = array('item' => $eventid, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to view this event');
266 return array('events' => $events, 'warnings' => $warnings);
270 * Returns description of method result value
272 * @return external_description
275 public static function get_calendar_events_returns() {
276 return new external_single_structure(array(
277 'events' => new external_multiple_structure( new external_single_structure(
279 'id' => new external_value(PARAM_INT
, 'event id'),
280 'name' => new external_value(PARAM_TEXT
, 'event name'),
281 'description' => new external_value(PARAM_RAW
, 'Description', VALUE_OPTIONAL
, null, NULL_ALLOWED
),
282 'format' => new external_format_value('description'),
283 'courseid' => new external_value(PARAM_INT
, 'course id'),
284 'groupid' => new external_value(PARAM_INT
, 'group id'),
285 'userid' => new external_value(PARAM_INT
, 'user id'),
286 'repeatid' => new external_value(PARAM_INT
, 'repeat id'),
287 'modulename' => new external_value(PARAM_TEXT
, 'module name', VALUE_OPTIONAL
, null, NULL_ALLOWED
),
288 'instance' => new external_value(PARAM_INT
, 'instance id'),
289 'eventtype' => new external_value(PARAM_TEXT
, 'Event type'),
290 'timestart' => new external_value(PARAM_INT
, 'timestart'),
291 'timeduration' => new external_value(PARAM_INT
, 'time duration'),
292 'visible' => new external_value(PARAM_INT
, 'visible'),
293 'uuid' => new external_value(PARAM_TEXT
, 'unique id of ical events', VALUE_OPTIONAL
, null, NULL_NOT_ALLOWED
),
294 'sequence' => new external_value(PARAM_INT
, 'sequence'),
295 'timemodified' => new external_value(PARAM_INT
, 'time modified'),
296 'subscriptionid' => new external_value(PARAM_INT
, 'Subscription id', VALUE_OPTIONAL
, null, NULL_ALLOWED
),
299 'warnings' => new external_warnings()
305 * Returns description of method parameters.
307 * @return external_function_parameters.
310 public static function create_calendar_events_parameters() {
311 // Userid is always current user, so no need to get it from client.
312 // Module based calendar events are not allowed here. Hence no need of instance and modulename.
313 // subscription id and uuid is not allowed as this is not an ical api.
314 return new external_function_parameters(
315 array('events' => new external_multiple_structure(
316 new external_single_structure(
318 'name' => new external_value(PARAM_TEXT
, 'event name', VALUE_REQUIRED
, '', NULL_NOT_ALLOWED
),
319 'description' => new external_value(PARAM_RAW
, 'Description', VALUE_DEFAULT
, null, NULL_ALLOWED
),
320 'format' => new external_format_value('description', VALUE_DEFAULT
),
321 'courseid' => new external_value(PARAM_INT
, 'course id', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
322 'groupid' => new external_value(PARAM_INT
, 'group id', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
323 'repeats' => new external_value(PARAM_INT
, 'number of repeats', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
324 'eventtype' => new external_value(PARAM_TEXT
, 'Event type', VALUE_DEFAULT
, 'user', NULL_NOT_ALLOWED
),
325 'timestart' => new external_value(PARAM_INT
, 'timestart', VALUE_DEFAULT
, time(), NULL_NOT_ALLOWED
),
326 'timeduration' => new external_value(PARAM_INT
, 'time duration', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
327 'visible' => new external_value(PARAM_INT
, 'visible', VALUE_DEFAULT
, 1, NULL_NOT_ALLOWED
),
328 'sequence' => new external_value(PARAM_INT
, 'sequence', VALUE_DEFAULT
, 1, NULL_NOT_ALLOWED
),
336 * Delete Calendar events.
338 * @param array $events A list of events to create.
339 * @return array array of events created.
341 * @throws moodle_exception if user doesnt have the permission to create events.
343 public static function create_calendar_events($events) {
344 global $CFG, $DB, $USER;
345 require_once($CFG->dirroot
."/calendar/lib.php");
347 // Parameter validation.
348 $params = self
::validate_parameters(self
::create_calendar_events_parameters(), array('events' => $events));
350 $transaction = $DB->start_delegated_transaction();
354 foreach ($params['events'] as $event) {
356 // Let us set some defaults.
357 $event['userid'] = $USER->id
;
358 $event['modulename'] = '';
359 $event['instance'] = 0;
360 $event['subscriptionid'] = null;
362 $event['format'] = external_validate_format($event['format']);
363 if ($event['repeats'] > 0) {
364 $event['repeat'] = 1;
366 $event['repeat'] = 0;
369 $eventobj = new calendar_event($event);
371 // Let's check if the user is allowed to delete an event.
372 if (!calendar_add_event_allowed($eventobj)) {
373 $warnings [] = array('item' => $event['name'], 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to create this event');
376 // Let's create the event.
377 $var = $eventobj->create($event);
378 $var = (array)$var->properties();
379 if ($event['repeat']) {
380 $children = $DB->get_records('event', array('repeatid' => $var['id']));
381 foreach ($children as $child) {
382 $return[] = (array) $child;
389 // Everything done smoothly, let's commit.
390 $transaction->allow_commit();
391 return array('events' => $return, 'warnings' => $warnings);
395 * Returns description of method result value.
397 * @return external_description.
400 public static function create_calendar_events_returns() {
401 return new external_single_structure(
403 'events' => new external_multiple_structure( new external_single_structure(
405 'id' => new external_value(PARAM_INT
, 'event id'),
406 'name' => new external_value(PARAM_TEXT
, 'event name'),
407 'description' => new external_value(PARAM_RAW
, 'Description', VALUE_OPTIONAL
),
408 'format' => new external_format_value('description'),
409 'courseid' => new external_value(PARAM_INT
, 'course id'),
410 'groupid' => new external_value(PARAM_INT
, 'group id'),
411 'userid' => new external_value(PARAM_INT
, 'user id'),
412 'repeatid' => new external_value(PARAM_INT
, 'repeat id', VALUE_OPTIONAL
),
413 'modulename' => new external_value(PARAM_TEXT
, 'module name', VALUE_OPTIONAL
),
414 'instance' => new external_value(PARAM_INT
, 'instance id'),
415 'eventtype' => new external_value(PARAM_TEXT
, 'Event type'),
416 'timestart' => new external_value(PARAM_INT
, 'timestart'),
417 'timeduration' => new external_value(PARAM_INT
, 'time duration'),
418 'visible' => new external_value(PARAM_INT
, 'visible'),
419 'uuid' => new external_value(PARAM_TEXT
, 'unique id of ical events', VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
420 'sequence' => new external_value(PARAM_INT
, 'sequence'),
421 'timemodified' => new external_value(PARAM_INT
, 'time modified'),
422 'subscriptionid' => new external_value(PARAM_INT
, 'Subscription id', VALUE_OPTIONAL
),
425 'warnings' => new external_warnings()