[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 28 defined('MOODLE_INTERNAL') || die; 29 30 require_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 */ 41 class 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 } 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. 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), 150 151 ), 'Options', VALUE_DEFAULT, array()) 152 ) 153 ); 154 } 155 156 /** 157 * Get Calendar events 158 * 159 * @param array $events A list of events 160 * @param 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. 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(); 173 174 // Let us findout courses that we can return events from. 175 if (!$hassystemcap) { 176 $courses = enrol_get_my_courses('id'); 177 $courses = array_keys($courses); 178 foreach ($params['events']['courseids'] as $id) { 179 try { 180 $context = context_course::instance($id); 181 self::validate_context($context); 182 $funcparam['courses'][] = $id; 183 } catch (Exception $e) { 184 $warnings[] = array( 185 'item' => 'course', 186 'itemid' => $id, 187 'warningcode' => 'nopermissions', 188 'message' => 'No access rights in course context '.$e->getMessage().$e->getTraceAsString() 189 ); 190 } 191 } 192 } else { 193 $courses = $params['events']['courseids']; 194 $funcparam['courses'] = $courses; 195 } 196 197 // Let us findout groups that we can return events from. 198 if (!$hassystemcap) { 199 $groups = groups_get_my_groups(); 200 $groups = array_keys($groups); 201 foreach ($params['events']['groupids'] as $id) { 202 if (in_array($id, $groups)) { 203 $funcparam['groups'][] = $id; 204 } else { 205 $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to access this group'); 206 } 207 } 208 } else { 209 $groups = $params['events']['groupids']; 210 $funcparam['groups'] = $groups; 211 } 212 213 // Do we need user events? 214 if (!empty($params['options']['userevents'])) { 215 $funcparam['users'] = array($USER->id); 216 } else { 217 $funcparam['users'] = false; 218 } 219 220 // Do we need site events? 221 if (!empty($params['options']['siteevents'])) { 222 $funcparam['courses'][] = $SITE->id; 223 } 224 225 // We treat 0 and null as no end. 226 if (empty($params['options']['timeend'])) { 227 $params['options']['timeend'] = PHP_INT_MAX; 228 } 229 230 // Event list does not check visibility and permissions, we'll check that later. 231 $eventlist = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'], 232 $funcparam['courses'], true, $params['options']['ignorehidden']); 233 234 // WS expects arrays. 235 $events = array(); 236 237 // We need to get events asked for eventids. 238 if ($eventsbyid = calendar_get_events_by_id($params['events']['eventids'])) { 239 $eventlist += $eventsbyid; 240 } 241 242 foreach ($eventlist as $eventid => $eventobj) { 243 $event = (array) $eventobj; 244 // Description formatting. 245 $calendareventobj = new calendar_event($event); 246 list($event['description'], $event['format']) = $calendareventobj->format_external_text(); 247 248 if ($hassystemcap) { 249 // User can see everything, no further check is needed. 250 $events[$eventid] = $event; 251 } else if (!empty($eventobj->modulename)) { 252 $cm = get_coursemodule_from_instance($eventobj->modulename, $eventobj->instance); 253 if (\core_availability\info_module::is_user_visible($cm, 0, false)) { 254 $events[$eventid] = $event; 255 } 256 } else { 257 // Can the user actually see this event? 258 $eventobj = calendar_event::load($eventobj); 259 if (($eventobj->courseid == $SITE->id) || 260 (!empty($eventobj->groupid) && in_array($eventobj->groupid, $groups)) || 261 (!empty($eventobj->courseid) && in_array($eventobj->courseid, $courses)) || 262 ($USER->id == $eventobj->userid) || 263 (calendar_edit_event_allowed($eventid))) { 264 $events[$eventid] = $event; 265 } else { 266 $warnings[] = array('item' => $eventid, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to view this event'); 267 } 268 } 269 } 270 return array('events' => $events, 'warnings' => $warnings); 271 } 272 273 /** 274 * Returns description of method result value 275 * 276 * @return external_description 277 * @since Moodle 2.5 278 */ 279 public static function get_calendar_events_returns() { 280 return new external_single_structure(array( 281 'events' => new external_multiple_structure( new external_single_structure( 282 array( 283 'id' => new external_value(PARAM_INT, 'event id'), 284 'name' => new external_value(PARAM_TEXT, 'event name'), 285 'description' => new external_value(PARAM_RAW, 'Description', VALUE_OPTIONAL, null, NULL_ALLOWED), 286 'format' => new external_format_value('description'), 287 'courseid' => new external_value(PARAM_INT, 'course id'), 288 'groupid' => new external_value(PARAM_INT, 'group id'), 289 'userid' => new external_value(PARAM_INT, 'user id'), 290 'repeatid' => new external_value(PARAM_INT, 'repeat id'), 291 'modulename' => new external_value(PARAM_TEXT, 'module name', VALUE_OPTIONAL, null, NULL_ALLOWED), 292 'instance' => new external_value(PARAM_INT, 'instance id'), 293 'eventtype' => new external_value(PARAM_TEXT, 'Event type'), 294 'timestart' => new external_value(PARAM_INT, 'timestart'), 295 'timeduration' => new external_value(PARAM_INT, 'time duration'), 296 'visible' => new external_value(PARAM_INT, 'visible'), 297 'uuid' => new external_value(PARAM_TEXT, 'unique id of ical events', VALUE_OPTIONAL, null, NULL_NOT_ALLOWED), 298 'sequence' => new external_value(PARAM_INT, 'sequence'), 299 'timemodified' => new external_value(PARAM_INT, 'time modified'), 300 'subscriptionid' => new external_value(PARAM_INT, 'Subscription id', VALUE_OPTIONAL, null, NULL_ALLOWED), 301 ), 'event') 302 ), 303 'warnings' => new external_warnings() 304 ) 305 ); 306 } 307 308 /** 309 * Returns description of method parameters. 310 * 311 * @return external_function_parameters. 312 * @since Moodle 2.5 313 */ 314 public static function create_calendar_events_parameters() { 315 // Userid is always current user, so no need to get it from client. 316 // Module based calendar events are not allowed here. Hence no need of instance and modulename. 317 // subscription id and uuid is not allowed as this is not an ical api. 318 return new external_function_parameters( 319 array('events' => new external_multiple_structure( 320 new external_single_structure( 321 array( 322 'name' => new external_value(PARAM_TEXT, 'event name', VALUE_REQUIRED, '', NULL_NOT_ALLOWED), 323 'description' => new external_value(PARAM_RAW, 'Description', VALUE_DEFAULT, null, NULL_ALLOWED), 324 'format' => new external_format_value('description', VALUE_DEFAULT), 325 'courseid' => new external_value(PARAM_INT, 'course id', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED), 326 'groupid' => new external_value(PARAM_INT, 'group id', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED), 327 'repeats' => new external_value(PARAM_INT, 'number of repeats', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED), 328 'eventtype' => new external_value(PARAM_TEXT, 'Event type', VALUE_DEFAULT, 'user', NULL_NOT_ALLOWED), 329 'timestart' => new external_value(PARAM_INT, 'timestart', VALUE_DEFAULT, time(), NULL_NOT_ALLOWED), 330 'timeduration' => new external_value(PARAM_INT, 'time duration', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED), 331 'visible' => new external_value(PARAM_INT, 'visible', VALUE_DEFAULT, 1, NULL_NOT_ALLOWED), 332 'sequence' => new external_value(PARAM_INT, 'sequence', VALUE_DEFAULT, 1, NULL_NOT_ALLOWED), 333 ), 'event') 334 ) 335 ) 336 ); 337 } 338 339 /** 340 * Delete Calendar events. 341 * 342 * @param array $events A list of events to create. 343 * @return array array of events created. 344 * @since Moodle 2.5 345 * @throws moodle_exception if user doesnt have the permission to create events. 346 */ 347 public static function create_calendar_events($events) { 348 global $CFG, $DB, $USER; 349 require_once($CFG->dirroot."/calendar/lib.php"); 350 351 // Parameter validation. 352 $params = self::validate_parameters(self::create_calendar_events_parameters(), array('events' => $events)); 353 354 $transaction = $DB->start_delegated_transaction(); 355 $return = array(); 356 $warnings = array(); 357 358 foreach ($params['events'] as $event) { 359 360 // Let us set some defaults. 361 $event['userid'] = $USER->id; 362 $event['modulename'] = ''; 363 $event['instance'] = 0; 364 $event['subscriptionid'] = null; 365 $event['uuid']= ''; 366 $event['format'] = external_validate_format($event['format']); 367 if ($event['repeats'] > 0) { 368 $event['repeat'] = 1; 369 } else { 370 $event['repeat'] = 0; 371 } 372 373 $eventobj = new calendar_event($event); 374 375 // Let's check if the user is allowed to delete an event. 376 if (!calendar_add_event_allowed($eventobj)) { 377 $warnings [] = array('item' => $event['name'], 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to create this event'); 378 continue; 379 } 380 // Let's create the event. 381 $var = $eventobj->create($event); 382 $var = (array)$var->properties(); 383 if ($event['repeat']) { 384 $children = $DB->get_records('event', array('repeatid' => $var['id'])); 385 foreach ($children as $child) { 386 $return[] = (array) $child; 387 } 388 } else { 389 $return[] = $var; 390 } 391 } 392 393 // Everything done smoothly, let's commit. 394 $transaction->allow_commit(); 395 return array('events' => $return, 'warnings' => $warnings); 396 } 397 398 /** 399 * Returns description of method result value. 400 * 401 * @return external_description. 402 * @since Moodle 2.5 403 */ 404 public static function create_calendar_events_returns() { 405 return new external_single_structure( 406 array( 407 'events' => new external_multiple_structure( new external_single_structure( 408 array( 409 'id' => new external_value(PARAM_INT, 'event id'), 410 'name' => new external_value(PARAM_TEXT, 'event name'), 411 'description' => new external_value(PARAM_RAW, 'Description', VALUE_OPTIONAL), 412 'format' => new external_format_value('description'), 413 'courseid' => new external_value(PARAM_INT, 'course id'), 414 'groupid' => new external_value(PARAM_INT, 'group id'), 415 'userid' => new external_value(PARAM_INT, 'user id'), 416 'repeatid' => new external_value(PARAM_INT, 'repeat id', VALUE_OPTIONAL), 417 'modulename' => new external_value(PARAM_TEXT, 'module name', VALUE_OPTIONAL), 418 'instance' => new external_value(PARAM_INT, 'instance id'), 419 'eventtype' => new external_value(PARAM_TEXT, 'Event type'), 420 'timestart' => new external_value(PARAM_INT, 'timestart'), 421 'timeduration' => new external_value(PARAM_INT, 'time duration'), 422 'visible' => new external_value(PARAM_INT, 'visible'), 423 'uuid' => new external_value(PARAM_TEXT, 'unique id of ical events', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED), 424 'sequence' => new external_value(PARAM_INT, 'sequence'), 425 'timemodified' => new external_value(PARAM_INT, 'time modified'), 426 'subscriptionid' => new external_value(PARAM_INT, 'Subscription id', VALUE_OPTIONAL), 427 ), 'event') 428 ), 429 'warnings' => new external_warnings() 430 ) 431 ); 432 } 433 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |