Commit 44ae0838 authored by Juan Leyva's avatar Juan Leyva
Browse files

MDL-60813 calendar: Fix core_calendar_get_calendar_events for categories

Users couldn’t see category events when filtering by categories in the
core_calendar_get_calendar_events WS.
parent 0164f50e
......@@ -119,6 +119,7 @@ class event_mapper implements event_mapper_interface {
'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(),
......
......@@ -226,25 +226,36 @@ class core_calendar_external extends external_api {
}
$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;
......@@ -254,29 +265,14 @@ class core_calendar_external extends external_api {
}
}
$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);
......
......@@ -533,6 +533,7 @@ class core_calendar_container_testcase extends advanced_testcase {
$record->timesort = 0;
$record->type = 1;
$record->courseid = 0;
$record->categoryid = 0;
foreach ($properties as $name => $value) {
$record->$name = $value;
......
......@@ -465,6 +465,7 @@ class core_calendar_event_factory_testcase extends advanced_testcase {
$record->timesort = 0;
$record->type = 1;
$record->courseid = 0;
$record->categoryid = 0;
foreach ($properties as $name => $value) {
$record->$name = $value;
......
......@@ -147,6 +147,7 @@ class core_calendar_event_mapper_testcase extends advanced_testcase {
$record->timesort = 0;
$record->type = 1;
$record->courseid = 0;
$record->categoryid = 0;
foreach ($properties as $name => $value) {
$record->$name = $value;
......
......@@ -485,31 +485,58 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
// 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']);
}
/**
......
......@@ -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;
......
......@@ -136,6 +136,7 @@ class core_calendar_repeat_event_collection_testcase extends advanced_testcase {
$record->timesort = 0;
$record->type = 1;
$record->courseid = 0;
$record->categoryid = 0;
foreach ($properties as $name => $value) {
$record->$name = $value;
......
......@@ -1124,6 +1124,7 @@ EOD;
$record->timesort = 0;
$record->eventtype = 'user';
$record->courseid = 0;
$record->categoryid = 0;
foreach ($data as $key => $value) {
$record->$key = $value;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment