MDL-37100 webservices: Adding warnings and cleaning up get_calendar_events() api
[moodle.git] / calendar / externallib.php
CommitLineData
01bea07d
AA
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.
13//
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/>.
16
17
18/**
19 * External calendar API
20 *
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
26 */
27
28defined('MOODLE_INTERNAL') || die;
29
30require_once("$CFG->libdir/externallib.php");
31
32/**
33 * Calendar external functions
34 *
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
40 */
41class core_calendar_external extends external_api {
42
43
44 /**
45 * Returns description of method parameters
46 *
47 * @return external_function_parameters
48 * @since Moodle 2.5
49 */
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'
58 )
59 )
60 )
61 );
62 }
63
64 /**
65 * Delete Calendar events
66 *
67 * @param array $eventids A list of event ids with repeat flag to delete
68 * @return null
69 * @since Moodle 2.5
70 */
71 public static function delete_calendar_events($events) {
72 global $CFG, $DB;
73 require_once($CFG->dirroot."/calendar/lib.php");
74
75 // Parameter validation.
76 $params = self::validate_parameters(self:: delete_calendar_events_parameters(), array('events' => $events));
77
78 $transaction = $DB->start_delegated_transaction();
79
80 foreach ($params['events'] as $event) {
81 $eventobj = calendar_event::load($event['eventid']);
82
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");
86 }
87 // Time to do the magic.
88 $eventobj->delete($event['repeat']);
89 }
90
91 // Everything done smoothly, let's commit.
92 $transaction->allow_commit();
93
94 return null;
95 }
96
97 /**
98 * Returns description of method result value
99 *
100 * @return external_description
101 * @since Moodle 2.5
102 */
103 public static function delete_calendar_events_returns() {
104 return null;
105 }
475896bd
AA
106
107 /**
108 * Returns description of method parameters
109 *
110 * @return external_function_parameters
111 * @since Moodle 2.5
112 */
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
121 ),
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
126 ),
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
131 )
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),
150
151 ), 'Options', VALUE_DEFAULT, array())
152 )
153 );
154 }
155
156 /**
157 * Get Calendar events
475896bd
AA
158 *
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
163 */
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");
167
168 // Parameter validation.
e6a1f8c1
AA
169 $params = self::validate_parameters(self::get_calendar_events_parameters(), array('events' => $events, 'options' => $options));
170 $funcparam = array('courses' => array(), 'groups' => array());
475896bd 171 $hassystemcap = has_capability('moodle/calendar:manageentries', context_system::instance());
e6a1f8c1 172 $warnings = array();
475896bd
AA
173
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);
e6a1f8c1
AA
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 donot have permissions to access this course');
183 }
184 }
475896bd
AA
185 } else {
186 $courses = $params['events']['courseids'];
187 $funcparam['courses'] = $courses;
188 }
189
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);
e6a1f8c1
AA
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 donot have permissions to access this group');
199 }
200 }
475896bd
AA
201 } else {
202 $groups = $params['events']['groupids'];
203 $funcparam['groups'] = $groups;
204 }
205
475896bd
AA
206 // Do we need user events?
207 if (!empty($params['options']['userevents'])) {
208 $funcparam['users'] = array($USER->id);
209 } else {
210 $funcparam['users'] = false;
211 }
212
213 // Do we need site events?
214 if (!empty($params['options']['siteevents'])) {
215 $funcparam['courses'][] = $SITE->id;
216 }
217
7c30224a 218 $eventlist = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'],
475896bd 219 $funcparam['courses'], true, $params['options']['ignorehidden']);
7c30224a
AA
220 // WS expects arrays.
221 $events = array();
e6a1f8c1 222 foreach ($eventlist as $id => $event) {
7c30224a
AA
223 $events[$id] = (array) $event;
224 }
475896bd
AA
225
226 // We need to get events asked for eventids.
227 $eventsbyid = calendar_get_events_by_id($params['events']['eventids']);
7c30224a
AA
228 foreach ($eventsbyid as $eventid => $eventobj) {
229 $event = (array) $eventobj;
230 if (isset($events[$eventid])) {
231 continue;
232 }
475896bd
AA
233 if ($hassystemcap) {
234 // User can see everything, no further check is needed.
7c30224a
AA
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;
475896bd
AA
240 }
241 } else {
242 // Can the user actually see this event?
7c30224a 243 $eventobj = calendar_event::load($eventobj);
e6a1f8c1
AA
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))) {
7c30224a 249 $events[$eventid] = $event;
e6a1f8c1
AA
250 } else {
251 $warnings[] = array('item' => $eventid, 'warningcode' => 'nopermissions', 'message' => 'you donot have permissions to view this event');
475896bd
AA
252 }
253 }
254 }
e6a1f8c1 255 return array('events' => $events, 'warnings' => $warnings);
475896bd
AA
256 }
257
258 /**
259 * Returns description of method result value
260 *
261 * @return external_description
262 * @since Moodle 2.5
263 */
264 public static function get_calendar_events_returns() {
e6a1f8c1
AA
265 return new external_single_structure(array(
266 'events' => new external_multiple_structure( new external_single_structure(
475896bd
AA
267 array(
268 'id' => new external_value(PARAM_INT, 'event id'),
269 'name' => new external_value(PARAM_TEXT, 'event name'),
e6a1f8c1 270 'description' => new external_value(PARAM_RAW, 'Description', VALUE_OPTIONAL, null, NULL_ALLOWED),
475896bd
AA
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'),
e6a1f8c1 276 'modulename' => new external_value(PARAM_TEXT, 'module name', VALUE_OPTIONAL, null, NULL_ALLOWED),
475896bd
AA
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'),
e6a1f8c1 282 'uuid' => new external_value(PARAM_TEXT, 'unique id of ical events', VALUE_OPTIONAL, null, NULL_NOT_ALLOWED),
475896bd
AA
283 'sequence' => new external_value(PARAM_INT, 'sequence'),
284 'timemodified' => new external_value(PARAM_INT, 'time modified'),
e6a1f8c1
AA
285 'subscriptionid' => new external_value(PARAM_INT, 'Subscription id', VALUE_OPTIONAL, null, NULL_ALLOWED),
286 ), 'event')
287 ),
288 'warnings' => new external_warnings()
475896bd
AA
289 )
290 );
291 }
01bea07d 292}