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",
146 VALUE_DEFAULT
, time(), 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 * @package 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 $courses = enrol_get_my_courses();
177 $courses = array_keys($courses);
178 foreach ($params['events']['courseids'] as $id) {
179 if (in_array($id, $courses)) {
180 $funcparam['courses'][] = $id;
182 $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to access this course');
186 $courses = $params['events']['courseids'];
187 $funcparam['courses'] = $courses;
190 // Let us findout groups that we can return events from.
191 if (!$hassystemcap) {
192 $groups = groups_get_my_groups();
193 $groups = array_keys($groups);
194 foreach ($params['events']['groupids'] as $id) {
195 if (in_array($id, $groups)) {
196 $funcparam['groups'][] = $id;
198 $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to access this group');
202 $groups = $params['events']['groupids'];
203 $funcparam['groups'] = $groups;
206 // Do we need user events?
207 if (!empty($params['options']['userevents'])) {
208 $funcparam['users'] = array($USER->id
);
210 $funcparam['users'] = false;
213 // Do we need site events?
214 if (!empty($params['options']['siteevents'])) {
215 $funcparam['courses'][] = $SITE->id
;
218 $eventlist = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'],
219 $funcparam['courses'], true, $params['options']['ignorehidden']);
220 // WS expects arrays.
222 foreach ($eventlist as $id => $event) {
223 $events[$id] = (array) $event;
226 // We need to get events asked for eventids.
227 $eventsbyid = calendar_get_events_by_id($params['events']['eventids']);
228 foreach ($eventsbyid as $eventid => $eventobj) {
229 $event = (array) $eventobj;
230 if (isset($events[$eventid])) {
234 // User can see everything, no further check is needed.
235 $events[$eventid] = $event;
236 } else if (!empty($eventobj->modulename
)) {
237 $cm = get_coursemodule_from_instance($eventobj->modulename
, $eventobj->instance
);
238 if (groups_course_module_visible($cm)) {
239 $events[$eventid] = $event;
242 // Can the user actually see this event?
243 $eventobj = calendar_event
::load($eventobj);
244 if (($eventobj->courseid
== $SITE->id
) ||
245 (!empty($eventobj->groupid
) && in_array($eventobj->groupid
, $groups)) ||
246 (!empty($eventobj->courseid
) && in_array($eventobj->courseid
, $courses)) ||
247 ($USER->id
== $eventobj->userid
) ||
248 (calendar_edit_event_allowed($eventid))) {
249 $events[$eventid] = $event;
251 $warnings[] = array('item' => $eventid, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to view this event');
255 return array('events' => $events, 'warnings' => $warnings);
259 * Returns description of method result value
261 * @return external_description
264 public static function get_calendar_events_returns() {
265 return new external_single_structure(array(
266 'events' => new external_multiple_structure( new external_single_structure(
268 'id' => new external_value(PARAM_INT
, 'event id'),
269 'name' => new external_value(PARAM_TEXT
, 'event name'),
270 'description' => new external_value(PARAM_RAW
, 'Description', VALUE_OPTIONAL
, null, NULL_ALLOWED
),
271 'format' => new external_format_value('description'),
272 'courseid' => new external_value(PARAM_INT
, 'course id'),
273 'groupid' => new external_value(PARAM_INT
, 'group id'),
274 'userid' => new external_value(PARAM_INT
, 'user id'),
275 'repeatid' => new external_value(PARAM_INT
, 'repeat id'),
276 'modulename' => new external_value(PARAM_TEXT
, 'module name', VALUE_OPTIONAL
, null, NULL_ALLOWED
),
277 'instance' => new external_value(PARAM_INT
, 'instance id'),
278 'eventtype' => new external_value(PARAM_TEXT
, 'Event type'),
279 'timestart' => new external_value(PARAM_INT
, 'timestart'),
280 'timeduration' => new external_value(PARAM_INT
, 'time duration'),
281 'visible' => new external_value(PARAM_INT
, 'visible'),
282 'uuid' => new external_value(PARAM_TEXT
, 'unique id of ical events', VALUE_OPTIONAL
, null, NULL_NOT_ALLOWED
),
283 'sequence' => new external_value(PARAM_INT
, 'sequence'),
284 'timemodified' => new external_value(PARAM_INT
, 'time modified'),
285 'subscriptionid' => new external_value(PARAM_INT
, 'Subscription id', VALUE_OPTIONAL
, null, NULL_ALLOWED
),
288 'warnings' => new external_warnings()
294 * Returns description of method parameters.
296 * @return external_function_parameters.
299 public static function create_calendar_events_parameters() {
300 // Userid is always current user, so no need to get it from client.
301 // Module based calendar events are not allowed here. Hence no need of instance and modulename.
302 // subscription id and uuid is not allowed as this is not an ical api.
303 return new external_function_parameters(
304 array('events' => new external_multiple_structure(
305 new external_single_structure(
307 'name' => new external_value(PARAM_TEXT
, 'event name', VALUE_REQUIRED
, '', NULL_NOT_ALLOWED
),
308 'description' => new external_value(PARAM_RAW
, 'Description', VALUE_DEFAULT
, null, NULL_ALLOWED
),
309 'format' => new external_format_value('description', VALUE_DEFAULT
),
310 'courseid' => new external_value(PARAM_INT
, 'course id', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
311 'groupid' => new external_value(PARAM_INT
, 'group id', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
312 'repeats' => new external_value(PARAM_INT
, 'number of repeats', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
313 'eventtype' => new external_value(PARAM_TEXT
, 'Event type', VALUE_DEFAULT
, 'user', NULL_NOT_ALLOWED
),
314 'timestart' => new external_value(PARAM_INT
, 'timestart', VALUE_DEFAULT
, time(), NULL_NOT_ALLOWED
),
315 'timeduration' => new external_value(PARAM_INT
, 'time duration', VALUE_DEFAULT
, 0, NULL_NOT_ALLOWED
),
316 'visible' => new external_value(PARAM_INT
, 'visible', VALUE_DEFAULT
, 1, NULL_NOT_ALLOWED
),
317 'sequence' => new external_value(PARAM_INT
, 'sequence', VALUE_DEFAULT
, 1, NULL_NOT_ALLOWED
),
325 * Delete Calendar events.
327 * @param array $events A list of events to create.
328 * @return array array of events created.
330 * @throws moodle_exception if user doesnt have the permission to create events.
332 public static function create_calendar_events($events) {
333 global $CFG, $DB, $USER;
334 require_once($CFG->dirroot
."/calendar/lib.php");
336 // Parameter validation.
337 $params = self
::validate_parameters(self
::create_calendar_events_parameters(), array('events' => $events));
339 $transaction = $DB->start_delegated_transaction();
343 foreach ($params['events'] as $event) {
345 // Let us set some defaults.
346 $event['userid'] = $USER->id
;
347 $event['modulename'] = '';
348 $event['instance'] = 0;
349 $event['subscriptionid'] = null;
351 $event['format'] = external_validate_format($event['format']);
352 if ($event['repeats'] > 0) {
353 $event['repeat'] = 1;
355 $event['repeat'] = 0;
358 $eventobj = new calendar_event($event);
360 // Let's check if the user is allowed to delete an event.
361 if (!calendar_add_event_allowed($eventobj)) {
362 $warnings [] = array('item' => $event['name'], 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to create this event');
365 // Let's create the event.
366 $var = $eventobj->create($event);
367 $var = (array)$var->properties();
368 if ($event['repeat']) {
369 $children = $DB->get_records('event', array('repeatid' => $var['id']));
370 foreach ($children as $child) {
371 $return[] = (array) $child;
378 // Everything done smoothly, let's commit.
379 $transaction->allow_commit();
380 return array('events' => $return, 'warnings' => $warnings);
384 * Returns description of method result value.
386 * @return external_description.
389 public static function create_calendar_events_returns() {
390 return new external_single_structure(
392 'events' => new external_multiple_structure( new external_single_structure(
394 'id' => new external_value(PARAM_INT
, 'event id'),
395 'name' => new external_value(PARAM_TEXT
, 'event name'),
396 'description' => new external_value(PARAM_RAW
, 'Description', VALUE_OPTIONAL
),
397 'format' => new external_format_value('description'),
398 'courseid' => new external_value(PARAM_INT
, 'course id'),
399 'groupid' => new external_value(PARAM_INT
, 'group id'),
400 'userid' => new external_value(PARAM_INT
, 'user id'),
401 'repeatid' => new external_value(PARAM_INT
, 'repeat id', VALUE_OPTIONAL
),
402 'modulename' => new external_value(PARAM_TEXT
, 'module name', VALUE_OPTIONAL
),
403 'instance' => new external_value(PARAM_INT
, 'instance id'),
404 'eventtype' => new external_value(PARAM_TEXT
, 'Event type'),
405 'timestart' => new external_value(PARAM_INT
, 'timestart'),
406 'timeduration' => new external_value(PARAM_INT
, 'time duration'),
407 'visible' => new external_value(PARAM_INT
, 'visible'),
408 'uuid' => new external_value(PARAM_TEXT
, 'unique id of ical events', VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
409 'sequence' => new external_value(PARAM_INT
, 'sequence'),
410 'timemodified' => new external_value(PARAM_INT
, 'time modified'),
411 'subscriptionid' => new external_value(PARAM_INT
, 'Subscription id', VALUE_OPTIONAL
),
414 'warnings' => new external_warnings()