Skip to content
Browse files

Merge branch 'MDL-37100-master' of git://github.com/ankitagarwal/moodle

Conflicts:
	version.php
  • Loading branch information...
2 parents bc5165c + 38c52ef commit 46112c0277c3c68c2e3cda752108d3b1f1183819 @nebgor nebgor committed Jan 23, 2013
Showing with 350 additions and 11 deletions.
  1. +186 −0 calendar/externallib.php
  2. +19 −0 calendar/lib.php
  3. +115 −9 calendar/tests/externallib_tests.php
  4. +10 −0 lib/db/services.php
  5. +19 −1 lib/grouplib.php
  6. +1 −1 version.php
View
186 calendar/externallib.php
@@ -103,4 +103,190 @@ public static function delete_calendar_events($events) {
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
+ *
+ * @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('courses' => array(), 'groups' => array());
+ $hassystemcap = has_capability('moodle/calendar:manageentries', context_system::instance());
+ $warnings = array();
+
+ // Let us findout courses that we can return events from.
+ if (!$hassystemcap) {
+ $courses = enrol_get_my_courses();
+ $courses = array_keys($courses);
+ foreach ($params['events']['courseids'] as $id) {
+ if (in_array($id, $courses)) {
+ $funcparam['courses'][] = $id;
+ } else {
+ $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you donot have permissions to access this course');
+ }
+ }
+ } 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);
+ foreach ($params['events']['groupids'] as $id) {
+ if (in_array($id, $groups)) {
+ $funcparam['groups'][] = $id;
+ } else {
+ $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you donot have permissions to access this group');
+ }
+ }
+ } 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;
+ }
+
+ $eventlist = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'],
+ $funcparam['courses'], true, $params['options']['ignorehidden']);
+ // WS expects arrays.
+ $events = array();
+ foreach ($eventlist as $id => $event) {
+ $events[$id] = (array) $event;
+ }
+
+ // We need to get events asked for eventids.
+ $eventsbyid = calendar_get_events_by_id($params['events']['eventids']);
+ foreach ($eventsbyid as $eventid => $eventobj) {
+ $event = (array) $eventobj;
+ if (isset($events[$eventid])) {
+ continue;
+ }
+ if ($hassystemcap) {
+ // User can see everything, no further check is needed.
+ $events[$eventid] = $event;
+ } else if (!empty($eventobj->modulename)) {
+ $cm = get_coursemodule_from_instance($eventobj->modulename, $eventobj->instance);
+ if (groups_course_module_visible($cm)) {
+ $events[$eventid] = $event;
+ }
+ } else {
+ // Can the user actually see this event?
+ $eventobj = calendar_event::load($eventobj);
+ if (($eventobj->courseid == $SITE->id) ||
+ (!empty($eventobj->groupid) && in_array($eventobj->groupid, $groups)) ||
+ (!empty($eventobj->courseid) && in_array($eventobj->courseid, $courses)) ||
+ ($USER->id == $eventobj->userid) ||
+ (calendar_edit_event_allowed($eventid))) {
+ $events[$eventid] = $event;
+ } else {
+ $warnings[] = array('item' => $eventid, 'warningcode' => 'nopermissions', 'message' => 'you donot have permissions to view this event');
+ }
+ }
+ }
+ return array('events' => $events, 'warnings' => $warnings);
+ }
+
+ /**
+ * Returns description of method result value
+ *
+ * @return external_description
+ * @since Moodle 2.5
+ */
+ public static function get_calendar_events_returns() {
+ return new external_single_structure(array(
+ 'events' => 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_OPTIONAL, 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_OPTIONAL, 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_OPTIONAL, 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_OPTIONAL, null, NULL_ALLOWED),
+ ), 'event')
+ ),
+ 'warnings' => new external_warnings()
+ )
+ );
+ }
}
View
19 calendar/lib.php
@@ -771,6 +771,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
*
View
124 calendar/tests/externallib_tests.php
@@ -66,9 +66,9 @@ public static function create_calendar_event($name, $userid = 0, $type = 'user',
require_once("$CFG->dirroot/calendar/lib.php");
if (!empty($prop)) {
- if (is_array($prop)) {
- $prop = (object)$prop;
- }
+ if (is_array($prop)) {
+ $prop = (object)$prop;
+ }
} else {
$prop = new stdClass();
}
@@ -91,11 +91,11 @@ public static function create_calendar_event($name, $userid = 0, $type = 'user',
$prop->repeat = 1;
}
if (empty($prop->userid)) {
- if (!empty($userid)) {
+ if (!empty($userid)) {
$prop->userid = $userid;
- } else {
- return false;
- }
+ } else {
+ return false;
+ }
}
if (empty($prop->courseid)) {
$prop->courseid = $SITE->id;
@@ -127,7 +127,7 @@ public function test_create_calendar_events () {
}
/**
- * Test delete_courses
+ * Test delete_calendar_events
*/
public function test_delete_calendar_events() {
global $DB, $USER;
@@ -254,4 +254,110 @@ public function test_delete_calendar_events() {
core_calendar_external::delete_calendar_events($events);
}
-}
+ /**
+ * Test get_calendar_events
+ */
+ public function test_get_calendar_events() {
+ global $DB, $USER;
+
+ $this->resetAfterTest(true);
+ $this->setAdminUser();
+
+ // Create a few stuff to test with.
+ $user = $this->getDataGenerator()->create_user();
+ $course = $this->getDataGenerator()->create_course();
+ $record = new stdClass();
+ $record->courseid = $course->id;
+ $group = $this->getDataGenerator()->create_group($record);
+
+ $beforecount = $DB->count_records('event');
+
+ // Let's create a few events.
+ $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
+ $record = new stdClass();
+ $record->courseid = $course->id;
+ $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
+ $userevent = $this->create_calendar_event('user', $USER->id);
+ $record = new stdClass();
+ $record->courseid = $course->id;
+ $record->groupid = $group->id;
+ $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
+
+ $paramevents = array ('eventids' => array($siteevent->id), 'courseids' => array($course->id), 'groupids' => array($group->id));
+ $options = array ('siteevents' => true, 'userevents' => true);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+
+ // Check to see if we got all events.
+ $this->assertEquals(4, count($events['events']));
+ $this->assertEquals(0, count($events['warnings']));
+ $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7*WEEKSECS);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+ $this->assertEquals(5, count($events['events']));
+ $this->assertEquals(0, count($events['warnings']));
+
+ // Let's play around with caps.
+ $this->setUser($user);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+ $this->assertEquals(2, count($events['events'])); // site, user.
+ $this->assertEquals(2, count($events['warnings'])); // course, group.
+
+ $role = $DB->get_record('role', array('shortname' => 'student'));
+ $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+ $this->assertEquals(4, count($events['events'])); // site, user, both course events.
+ $this->assertEquals(1, count($events['warnings'])); // group.
+
+ $options = array ('siteevents' => true, 'userevents' => true);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+ $this->assertEquals(3, count($events['events'])); // site, user, one course event.
+ $this->assertEquals(1, count($events['warnings'])); // group.
+
+ groups_add_member($group, $user);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+ $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
+ $this->assertEquals(0, count($events['warnings']));
+
+ $paramevents = array ('courseids' => array($course->id), 'groupids' => array($group->id));
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
+ $this->assertEquals(0, count($events['warnings']));
+
+ $paramevents = array ('groupids' => array($group->id, 23));
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+ $this->assertEquals(3, count($events['events'])); // site, user, group.
+ $this->assertEquals(1, count($events['warnings']));
+
+ $paramevents = array ('courseids' => array(23));
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+ $this->assertEquals(2, count($events['events'])); // site, user.
+ $this->assertEquals(1, count($events['warnings']));
+
+ $paramevents = array ();
+ $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+ $this->assertEquals(0, count($events['events'])); // nothing returned.
+ $this->assertEquals(0, count($events['warnings']));
+
+ $paramevents = array ('eventids' => array($siteevent->id, $groupevent->id));
+ $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+ $this->assertEquals(2, count($events['events'])); // site, group.
+ $this->assertEquals(0, count($events['warnings']));
+
+ $paramevents = array ('eventids' => array($siteevent->id));
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
+ $this->assertEquals(1, count($events['events'])); // site.
+ $this->assertEquals(0, count($events['warnings']));
+ }
+}
View
10 lib/db/services.php
@@ -710,6 +710,16 @@
'capabilities'=> 'moodle/calendar:manageentries', 'moodle/calendar:manageownentries', 'moodle/calendar:managegroupentries'
),
+
+ 'core_calendar_get_calendar_events' => array(
+ 'classname' => 'core_calendar_external',
+ 'methodname' => 'get_calendar_events',
+ 'description' => 'Get calendar events',
+ 'classpath' => 'calendar/externallib.php',
+ 'type' => 'read',
+ 'capabilities'=> 'moodle/calendar:manageentries', 'moodle/calendar:manageownentries', 'moodle/calendar:managegroupentries'
+ ),
+
);
$services = array(
View
20 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;
-}
+}
View
2 version.php
@@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();
-$version = 2013011800.02; // YYYYMMDD = weekly release date of this DEV branch
+$version = 2013011800.03; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

0 comments on commit 46112c0

Please sign in to comment.
Something went wrong with that request. Please try again.