MDL-37100 webservices: Define core_calendar_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
158 * Note that, this functions silently disregards all couseids, eventids etc passed that are not with proper caps
159 *
160 *
161 * @param array $events A list of events
162 * @package array $options various options
163 * @return array Array of event details
164 * @since Moodle 2.5
165 */
166 public static function get_calendar_events($events = array(), $options = array()) {
167 global $SITE, $DB, $USER, $CFG;
168 require_once($CFG->dirroot."/calendar/lib.php");
169
170 // Parameter validation.
171 $params = self::validate_parameters(self:: get_calendar_events_parameters(), array('events' => $events, 'options' => $options));
172 $funcparam = array();
173 $hassystemcap = has_capability('moodle/calendar:manageentries', context_system::instance());
174
175 // Let us findout courses that we can return events from.
176 if (!$hassystemcap) {
177 $courses = enrol_get_my_courses();
178 $courses = array_keys($courses);
179 $funcparam['courses'] = array_intersect($params['events']['courseids'], $courses);
180 } else {
181 $courses = $params['events']['courseids'];
182 $funcparam['courses'] = $courses;
183 }
184
185 // Let us findout groups that we can return events from.
186 if (!$hassystemcap) {
187 $groups = groups_get_my_groups();
188 $groups = array_keys($groups);
189 $funcparam['groups'] = array_intersect($params['events']['groupids'], $groups);
190 } else {
191 $groups = $params['events']['groupids'];
192 $funcparam['groups'] = $groups;
193 }
194
195
196
197 // Do we need user events?
198 if (!empty($params['options']['userevents'])) {
199 $funcparam['users'] = array($USER->id);
200 } else {
201 $funcparam['users'] = false;
202 }
203
204 // Do we need site events?
205 if (!empty($params['options']['siteevents'])) {
206 $funcparam['courses'][] = $SITE->id;
207 }
208
7c30224a 209 $eventlist = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'],
475896bd 210 $funcparam['courses'], true, $params['options']['ignorehidden']);
7c30224a
AA
211 // WS expects arrays.
212 $events = array();
213 foreach ($eventlist as $id => $event) {
214 $events[$id] = (array) $event;
215 }
475896bd
AA
216
217 // We need to get events asked for eventids.
218 $eventsbyid = calendar_get_events_by_id($params['events']['eventids']);
7c30224a
AA
219 foreach ($eventsbyid as $eventid => $eventobj) {
220 $event = (array) $eventobj;
221 if (isset($events[$eventid])) {
222 continue;
223 }
475896bd
AA
224 if ($hassystemcap) {
225 // User can see everything, no further check is needed.
7c30224a
AA
226 $events[$eventid] = $event;
227 } else if (!empty($eventobj->modulename)) {
228 $cm = get_coursemodule_from_instance($eventobj->modulename, $eventobj->instance);
229 if (groups_course_module_visible($cm)) {
230 $events[$eventid] = $event;
475896bd
AA
231 }
232 } else {
233 // Can the user actually see this event?
7c30224a
AA
234 $eventobj = calendar_event::load($eventobj);
235 if (($eventobj->courseid == $SITE->id) ||
236 (!empty($eventobj->groupid) && in_array($eventobj->groupid, $groups)) ||
237 (!empty($eventobj->courseid) && in_array($eventobj->courseid, $courses)) ||
238 ($USER->id == $eventobj->userid) ||
475896bd 239 (calendar_edit_event_allowed($eventid))) {
7c30224a 240 $events[$eventid] = $event;
475896bd
AA
241 }
242 }
243 }
244
245 return $events;
246 }
247
248 /**
249 * Returns description of method result value
250 *
251 * @return external_description
252 * @since Moodle 2.5
253 */
254 public static function get_calendar_events_returns() {
255 return new external_multiple_structure(
256 new external_single_structure(
257 array(
258 'id' => new external_value(PARAM_INT, 'event id'),
259 'name' => new external_value(PARAM_TEXT, 'event name'),
260 'description' => new external_value(PARAM_RAW, 'Description', VALUE_DEFAULT, null, NULL_ALLOWED),
261 'format' => new external_format_value('description'),
262 'courseid' => new external_value(PARAM_INT, 'course id'),
263 'groupid' => new external_value(PARAM_INT, 'group id'),
264 'userid' => new external_value(PARAM_INT, 'user id'),
265 'repeatid' => new external_value(PARAM_INT, 'repeat id'),
266 'modulename' => new external_value(PARAM_TEXT, 'module name', VALUE_DEFAULT, null, NULL_ALLOWED),
267 'instance' => new external_value(PARAM_INT, 'instance id'),
268 'eventtype' => new external_value(PARAM_TEXT, 'Event type'),
269 'timestart' => new external_value(PARAM_INT, 'timestart'),
270 'timeduration' => new external_value(PARAM_INT, 'time duration'),
271 'visible' => new external_value(PARAM_INT, 'visible'),
272 'uuid' => new external_value(PARAM_TEXT, 'unique id of ical events', VALUE_DEFAULT, null, NULL_NOT_ALLOWED),
273 'sequence' => new external_value(PARAM_INT, 'sequence'),
274 'timemodified' => new external_value(PARAM_INT, 'time modified'),
275 'subscriptionid' => new external_value(PARAM_INT, 'Subscription id', VALUE_DEFAULT, null, NULL_ALLOWED),
276 ), 'event'
277 )
278 );
279 }
01bea07d 280}