From 475896bdbc454bd4a07f20b9da147ad659db86f6 Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Thu, 13 Dec 2012 15:57:46 +0800 Subject: [PATCH] MDL-37100 webservices: Implement core_calendar_get_calendar_events() --- calendar/externallib.php | 171 +++++++++++++++++++++++++++++++++++++++ calendar/lib.php | 19 +++++ lib/grouplib.php | 20 ++++- 3 files changed, 209 insertions(+), 1 deletion(-) diff --git a/calendar/externallib.php b/calendar/externallib.php index 320d81c0958..d4967e1875d 100644 --- a/calendar/externallib.php +++ b/calendar/externallib.php @@ -103,4 +103,175 @@ class core_calendar_external extends external_api { public static function delete_calendar_events_returns() { return null; } + + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 2.5 + */ + public static function get_calendar_events_parameters() { + return new external_function_parameters( + array('events' => new external_single_structure( + array( + 'eventids' => new external_multiple_structure( + new external_value(PARAM_INT, 'event ids') + , 'List of event ids', + VALUE_DEFAULT, array(), NULL_ALLOWED + ), + 'courseids' => new external_multiple_structure( + new external_value(PARAM_INT, 'course ids') + , 'List of course ids for which events will be returned', + VALUE_DEFAULT, array(), NULL_ALLOWED + ), + 'groupids' => new external_multiple_structure( + new external_value(PARAM_INT, 'group ids') + , 'List of group ids for which events should be returned', + VALUE_DEFAULT, array(), NULL_ALLOWED + ) + ), 'Event details', VALUE_DEFAULT, array()), + 'options' => new external_single_structure( + array( + 'userevents' => new external_value(PARAM_BOOL, + "Set to true to return current user's user events", + VALUE_DEFAULT, true, NULL_ALLOWED), + 'siteevents' => new external_value(PARAM_BOOL, + "Set to true to return global events", + VALUE_DEFAULT, true, NULL_ALLOWED), + 'timestart' => new external_value(PARAM_INT, + "Time from which events should be returned", + VALUE_DEFAULT, 0, NULL_ALLOWED), + 'timeend' => new external_value(PARAM_INT, + "Time to which the events should be returned", + VALUE_DEFAULT, time(), NULL_ALLOWED), + 'ignorehidden' => new external_value(PARAM_BOOL, + "Ignore hidden events or not", + VALUE_DEFAULT, true, NULL_ALLOWED), + + ), 'Options', VALUE_DEFAULT, array()) + ) + ); + } + + /** + * Get Calendar events + * Note that, this functions silently disregards all couseids, eventids etc passed that are not with proper caps + * + * + * @param array $events A list of events + * @package array $options various options + * @return array Array of event details + * @since Moodle 2.5 + */ + public static function get_calendar_events($events = array(), $options = array()) { + global $SITE, $DB, $USER, $CFG; + require_once($CFG->dirroot."/calendar/lib.php"); + + // Parameter validation. + $params = self::validate_parameters(self:: get_calendar_events_parameters(), array('events' => $events, 'options' => $options)); + $funcparam = array(); + $hassystemcap = has_capability('moodle/calendar:manageentries', context_system::instance()); + + // Let us findout courses that we can return events from. + if (!$hassystemcap) { + $courses = enrol_get_my_courses(); + $courses = array_keys($courses); + $funcparam['courses'] = array_intersect($params['events']['courseids'], $courses); + } else { + $courses = $params['events']['courseids']; + $funcparam['courses'] = $courses; + } + + // Let us findout groups that we can return events from. + if (!$hassystemcap) { + $groups = groups_get_my_groups(); + $groups = array_keys($groups); + $funcparam['groups'] = array_intersect($params['events']['groupids'], $groups); + } else { + $groups = $params['events']['groupids']; + $funcparam['groups'] = $groups; + } + + + + // Do we need user events? + if (!empty($params['options']['userevents'])) { + $funcparam['users'] = array($USER->id); + } else { + $funcparam['users'] = false; + } + + // Do we need site events? + if (!empty($params['options']['siteevents'])) { + $funcparam['courses'][] = $SITE->id; + } + + $events = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'], + $funcparam['courses'], true, $params['options']['ignorehidden']); + + // We need to get events asked for eventids. + $eventsbyid = calendar_get_events_by_id($params['events']['eventids']); + foreach ($eventsbyid as $eventid => $event) { + if ($hassystemcap) { + // User can see everything, no further check is needed. + if (!in_array($event, $events)) { + $events[] = (array)$event; + } + } else if (!empty($event->modulename)) { + $cm = get_coursemodule_from_instance($event->modulename, $event->instance); + if (!groups_course_module_visible($cm)) { + unset($eventsbyid[$eventid]); + } else if (!in_array($event, $events)) { + $events[] = (array)$event; + } + } else { + // Can the user actually see this event? + $eventobj = calendar_event::load($eventid); + if (!in_array($event, $events) || + ($hassystemcap) || + ($event->courseid == $SITE->id) || + (!empty($event->groupid) && in_array($event->groupid, $groups)) || + (!empty($event->courseid) && in_array($event->courseid, $courses)) || + ($USER->id == $event->userid) || + (calendar_edit_event_allowed($eventid))) { + $events[] = (array)$event; + } + } + } + + return $events; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 2.5 + */ + public static function get_calendar_events_returns() { + return new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'event id'), + 'name' => new external_value(PARAM_TEXT, 'event name'), + 'description' => new external_value(PARAM_RAW, 'Description', VALUE_DEFAULT, null, NULL_ALLOWED), + 'format' => new external_format_value('description'), + 'courseid' => new external_value(PARAM_INT, 'course id'), + 'groupid' => new external_value(PARAM_INT, 'group id'), + 'userid' => new external_value(PARAM_INT, 'user id'), + 'repeatid' => new external_value(PARAM_INT, 'repeat id'), + 'modulename' => new external_value(PARAM_TEXT, 'module name', VALUE_DEFAULT, null, NULL_ALLOWED), + 'instance' => new external_value(PARAM_INT, 'instance id'), + 'eventtype' => new external_value(PARAM_TEXT, 'Event type'), + 'timestart' => new external_value(PARAM_INT, 'timestart'), + 'timeduration' => new external_value(PARAM_INT, 'time duration'), + 'visible' => new external_value(PARAM_INT, 'visible'), + 'uuid' => new external_value(PARAM_TEXT, 'unique id of ical events', VALUE_DEFAULT, null, NULL_NOT_ALLOWED), + 'sequence' => new external_value(PARAM_INT, 'sequence'), + 'timemodified' => new external_value(PARAM_INT, 'time modified'), + 'subscriptionid' => new external_value(PARAM_INT, 'Subscription id', VALUE_DEFAULT, null, NULL_ALLOWED), + ), 'event' + ) + ); + } } diff --git a/calendar/lib.php b/calendar/lib.php index dc56f91fd63..bfff273866d 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -737,6 +737,25 @@ function calendar_get_events($tstart, $tend, $users, $groups, $courses, $withdur return $events; } +/** Get calendar events by id + * + * @since Moodle 2.5 + * @param array $eventids list of event ids + * @return array Array of event entries, empty array if nothing found + */ + +function calendar_get_events_by_id($eventids) { + global $DB; + + if (!is_array($eventids) || empty($eventids)) { + return array(); + } + list($wheresql, $params) = $DB->get_in_or_equal($eventids); + $wheresql = "id $wheresql"; + + return $DB->get_records_select('event', $wheresql, $params); +} + /** * Get control options for Calendar * diff --git a/lib/grouplib.php b/lib/grouplib.php index 5378cd236d6..3b3808289e5 100644 --- a/lib/grouplib.php +++ b/lib/grouplib.php @@ -263,6 +263,24 @@ function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.* ORDER BY name ASC", $params); } + +/** + * Gets array of all groups in current user. + * + * @since Moodle 2.5 + * @category group + * @return array Returns an array of the group objects. + */ +function groups_get_my_groups() { + global $DB, $USER; + return $DB->get_records_sql("SELECT * + FROM {groups_members} gm + JOIN {groups} g + ON g.id = gm.groupid + WHERE gm.userid = ? + ORDER BY name ASC", array($USER->id)); +} + /** * Returns info about user's groups in course. * @@ -912,4 +930,4 @@ function groups_get_course_data($courseid, cache $cache = null) { $data = groups_cache_groupdata($courseid, $cache); } return $data; -} \ No newline at end of file +}