Skip to content

Commit

Permalink
MDL-60813 calendar: Fix core_calendar_get_calendar_events for categories
Browse files Browse the repository at this point in the history
Users couldn’t see category events when filtering by categories in the
core_calendar_get_calendar_events WS.
  • Loading branch information
jleyva committed Nov 16, 2017
1 parent 0164f50 commit 44ae083
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 33 deletions.
1 change: 1 addition & 0 deletions calendar/classes/local/event/mappers/event_mapper.php
Expand Up @@ -119,6 +119,7 @@ public function from_event_to_assoc_array(event_interface $event) {
'description' => $event->get_description()->get_value(),
'format' => $event->get_description()->get_format(),
'courseid' => $event->get_course() ? $event->get_course()->get('id') : null,
'categoryid' => $event->get_category() ? $event->get_category()->get('id') : null,
'groupid' => $event->get_group() ? $event->get_group()->get('id') : null,
'userid' => $event->get_user() ? $event->get_user()->get('id') : null,
'repeatid' => $event->get_repeats()->get_id(),
Expand Down
54 changes: 25 additions & 29 deletions calendar/externallib.php
Expand Up @@ -226,25 +226,36 @@ public static function get_calendar_events($events = array(), $options = array()
}

$categories = array();
if (empty($params['events']['categoryids']) && !empty($courses)) {
list($wheresql, $sqlparams) = $DB->get_in_or_equal($courses);
$wheresql = "id $wheresql";
$courseswithcategory = $DB->get_records_select('course', $wheresql, $sqlparams);
if ($hassystemcap || !empty($courses)) {

// Grab the list of course categories for the requested course list.
$coursecategories = array();
foreach ($courseswithcategory as $course) {
if (empty($course->visible)) {
if (!has_capability('moodle/course:viewhidden', context_course::instance($course->id))) {
continue;
if (!empty($courses)) {
list($wheresql, $sqlparams) = $DB->get_in_or_equal($courses);
$wheresql = "id $wheresql";
$courseswithcategory = $DB->get_records_select('course', $wheresql, $sqlparams);

// Grab the list of course categories for the requested course list.
foreach ($courseswithcategory as $course) {
if (empty($course->visible)) {
if (!has_capability('moodle/course:viewhidden', context_course::instance($course->id))) {
continue;
}
}
$category = \coursecat::get($course->category);
// Fetch parent categories.
$coursecategories = array_merge($coursecategories, [$category->id], $category->get_parents());
}
$category = \coursecat::get($course->category);
$coursecategories[] = $category;
}

foreach (\coursecat::get_all() as $category) {
if (has_capability('moodle/category:manage', $category->get_context(), $USER, false)) {
// Skip categories not requested.
if (!empty($params['events']['categoryids'])) {
if (!in_array($category->id, $params['events']['categoryids'])) {
continue;
}
}

if (has_capability('moodle/category:manage', $category->get_context())) {
// If a user can manage a category, then they can see all child categories. as well as all parent categories.
$categories[] = $category->id;

Expand All @@ -254,29 +265,14 @@ public static function get_calendar_events($events = array(), $options = array()
}
}
$categories = array_merge($categories, $category->get_parents());
} else if (isset($coursecategories[$category->id])) {
} else if (in_array($category->id, $coursecategories)) {

// The user has access to a course in this category.
// Fetch all of the parents too.
$categories = array_merge($categories, [$category->id], $category->get_parents());
$categories[] = $category->id;
}
}
} else {
// Build the category list.
// This includes the current category.
foreach ($params['events']['categoryids'] as $categoryid) {
$category = \coursecat::get($categoryid);
$categories = [$category->id];
// All of its descendants.
foreach (\coursecat::get_all() as $cat) {
if (array_search($categoryid, $cat->get_parents()) !== false) {
$categories[] = $cat->id;
}
}

// And all of its parents.
$categories = array_merge($categories, $category->get_parents());
}
}

$funcparam['categories'] = array_unique($categories);
Expand Down
1 change: 1 addition & 0 deletions calendar/tests/container_test.php
Expand Up @@ -533,6 +533,7 @@ protected function create_event($properties = []) {
$record->timesort = 0;
$record->type = 1;
$record->courseid = 0;
$record->categoryid = 0;

foreach ($properties as $name => $value) {
$record->$name = $value;
Expand Down
1 change: 1 addition & 0 deletions calendar/tests/event_factory_test.php
Expand Up @@ -465,6 +465,7 @@ protected function create_event($properties = []) {
$record->timesort = 0;
$record->type = 1;
$record->courseid = 0;
$record->categoryid = 0;

foreach ($properties as $name => $value) {
$record->$name = $value;
Expand Down
1 change: 1 addition & 0 deletions calendar/tests/event_mapper_test.php
Expand Up @@ -147,6 +147,7 @@ protected function create_event($properties = []) {
$record->timesort = 0;
$record->type = 1;
$record->courseid = 0;
$record->categoryid = 0;

foreach ($properties as $name => $value) {
$record->$name = $value;
Expand Down
35 changes: 31 additions & 4 deletions calendar/tests/externallib_test.php
Expand Up @@ -485,31 +485,58 @@ public function test_get_calendar_events() {
// Create some category events.
$this->setAdminUser();
$record = new stdClass();
$record->courseid = 0;
$record->categoryid = $category->id;
$this->create_calendar_event('category a', $USER->id, 'category', 0, time(), $record);
$record->timestart = time() - DAYSECS;
$catevent1 = $this->create_calendar_event('category a', $USER->id, 'category', 0, time(), $record);

$record = new stdClass();
$record->courseid = 0;
$record->categoryid = $category2->id;
$this->create_calendar_event('category b', $USER->id, 'category', 0, time(), $record);
$record->timestart = time() + DAYSECS;
$catevent2 = $this->create_calendar_event('category b', $USER->id, 'category', 0, time(), $record);

// Now as student, make sure we get the events of the courses I am enrolled.
$this->setUser($user2);
$paramevents = array('categoryids' => array($category2b->id));
$options = array('timeend' => time() + 7 * WEEKSECS);
$options = array('timeend' => time() + 7 * WEEKSECS, 'userevents' => false, 'siteevents' => false);
$events = core_calendar_external::get_calendar_events($paramevents, $options);
$events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);

// Should be just one, since there's just one category event of the course I am enrolled (course3 - cat2b).
$this->assertEquals(1, count($events['events']));
$this->assertEquals($catevent2->id, $events['events'][0]['id']);
$this->assertEquals(0, count($events['warnings']));

// Now get category events but by course (there aren't course events in the course).
$paramevents = array('courseids' => array($course3->id));
$options = array('timeend' => time() + 7 * WEEKSECS, 'userevents' => false, 'siteevents' => false);
$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']));
$this->assertEquals($catevent2->id, $events['events'][0]['id']);
$this->assertEquals(0, count($events['warnings']));

// Empty events in one where I'm not enrolled and one parent category
// (parent of a category where this is a course where the user is enrolled).
$paramevents = array('categoryids' => array($category2->id, $category->id));
$options = array('timeend' => time() + 7 * WEEKSECS, 'userevents' => false, 'siteevents' => false);
$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']));
$this->assertEquals($catevent2->id, $events['events'][0]['id']);
$this->assertEquals(0, count($events['warnings']));

// Admin can see all category events.
$this->setAdminUser();
$paramevents = array('categoryids' => array($category->id, $category2->id, $category2b->id));
$options = array('timeend' => time() + 7 * WEEKSECS);
$options = array('timeend' => time() + 7 * WEEKSECS, 'userevents' => false, 'siteevents' => false);
$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']));
$this->assertEquals(0, count($events['warnings']));
$this->assertEquals($catevent1->id, $events['events'][0]['id']);
$this->assertEquals($catevent2->id, $events['events'][1]['id']);
}

/**
Expand Down
1 change: 1 addition & 0 deletions calendar/tests/helpers.php
Expand Up @@ -56,6 +56,7 @@ function create_event($properties) {
$record->timesort = 0;
$record->type = CALENDAR_EVENT_TYPE_STANDARD;
$record->courseid = 0;
$record->categoryid = 0;

foreach ($properties as $name => $value) {
$record->$name = $value;
Expand Down
1 change: 1 addition & 0 deletions calendar/tests/repeat_event_collection_test.php
Expand Up @@ -136,6 +136,7 @@ protected function create_event($properties = []) {
$record->timesort = 0;
$record->type = 1;
$record->courseid = 0;
$record->categoryid = 0;

foreach ($properties as $name => $value) {
$record->$name = $value;
Expand Down
1 change: 1 addition & 0 deletions lib/testing/generator/data_generator.php
Expand Up @@ -1124,6 +1124,7 @@ public function create_event($data = []) {
$record->timesort = 0;
$record->eventtype = 'user';
$record->courseid = 0;
$record->categoryid = 0;

foreach ($data as $key => $value) {
$record->$key = $value;
Expand Down

0 comments on commit 44ae083

Please sign in to comment.