Merge branch 'MDL-42714-master' of git://github.com/sammarshallou/moodle
[moodle.git] / calendar / externallib.php
blobe47ddc8ed78b2094e25164e3a84a11607705b0b2
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
18 /**
19 * External calendar API
21 * @package core_calendar
22 * @category external
23 * @copyright 2012 Ankit Agarwal
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 * @since Moodle 2.5
28 defined('MOODLE_INTERNAL') || die;
30 require_once("$CFG->libdir/externallib.php");
32 /**
33 * Calendar external functions
35 * @package core_calendar
36 * @category external
37 * @copyright 2012 Ankit Agarwal
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 * @since Moodle 2.5
41 class core_calendar_external extends external_api {
44 /**
45 * Returns description of method parameters
47 * @return external_function_parameters
48 * @since Moodle 2.5
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(
54 array(
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'
64 /**
65 * Delete Calendar events
67 * @param array $eventids A list of event ids with repeat flag to delete
68 * @return null
69 * @since Moodle 2.5
71 public static function delete_calendar_events($events) {
72 global $CFG, $DB;
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();
94 return null;
97 /**
98 * Returns description of method result value
100 * @return external_description
101 * @since Moodle 2.5
103 public static function delete_calendar_events_returns() {
104 return null;
108 * Returns description of method parameters
110 * @return external_function_parameters
111 * @since Moodle 2.5
113 public static function get_calendar_events_parameters() {
114 return new external_function_parameters(
115 array('events' => new external_single_structure(
116 array(
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(
134 array(
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
162 * @since Moodle 2.5
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());
172 $warnings = array();
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;
181 } else {
182 $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to access this course');
185 } else {
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;
197 } else {
198 $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to access this group');
201 } else {
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);
209 } else {
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.
221 $events = array();
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])) {
231 continue;
233 if ($hassystemcap) {
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;
241 } else {
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;
250 } else {
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
262 * @since Moodle 2.5
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(
267 array(
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),
286 ), 'event')
288 'warnings' => new external_warnings()
294 * Returns description of method parameters.
296 * @return external_function_parameters.
297 * @since Moodle 2.5
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(
306 array(
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),
318 ), 'event')
325 * Delete Calendar events.
327 * @param array $events A list of events to create.
328 * @return array array of events created.
329 * @since Moodle 2.5
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();
340 $return = array();
341 $warnings = array();
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;
350 $event['uuid']= '';
351 $event['format'] = external_validate_format($event['format']);
352 if ($event['repeats'] > 0) {
353 $event['repeat'] = 1;
354 } else {
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');
363 continue;
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;
373 } else {
374 $return[] = $var;
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.
387 * @since Moodle 2.5
389 public static function create_calendar_events_returns() {
390 return new external_single_structure(
391 array(
392 'events' => new external_multiple_structure( new external_single_structure(
393 array(
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),
412 ), 'event')
414 'warnings' => new external_warnings()