From 375b15a346e72a69ed9bf419d7ffd069ea77261f Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Mon, 16 Jul 2018 10:14:54 +0800 Subject: [PATCH] MDL-60826 calendar: deprecate calendar_get_all_allowed_types --- calendar/lib.php | 70 ------- calendar/tests/lib_test.php | 360 ++++-------------------------------- lib/deprecatedlib.php | 73 ++++++++ 3 files changed, 105 insertions(+), 398 deletions(-) diff --git a/calendar/lib.php b/calendar/lib.php index 57afa757a90..1c0490241e9 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -2570,76 +2570,6 @@ function calendar_get_allowed_types(&$allowed, $course = null, $groups = null, $ } } -/** - * Get all of the allowed types for all of the courses and groups - * the logged in user belongs to. - * - * The returned array will optionally have 5 keys: - * 'user' : true if the logged in user can create user events - * 'site' : true if the logged in user can create site events - * 'category' : array of course categories that the user can create events for - * 'course' : array of courses that the user can create events for - * 'group': array of groups that the user can create events for - * 'groupcourses' : array of courses that the groups belong to (can - * be different from the list in 'course'. - * - * @return array The array of allowed types. - */ -function calendar_get_all_allowed_types() { - global $CFG, $USER, $DB; - - require_once($CFG->libdir . '/enrollib.php'); - - $types = []; - - $allowed = new stdClass(); - - calendar_get_allowed_types($allowed); - - if ($allowed->user) { - $types['user'] = true; - } - - if ($allowed->site) { - $types['site'] = true; - } - - if (coursecat::has_manage_capability_on_any()) { - $types['category'] = coursecat::make_categories_list('moodle/category:manage'); - } - - // This function warms the context cache for the course so the calls - // to load the course context in calendar_get_allowed_types don't result - // in additional DB queries. - $courses = calendar_get_default_courses(null, 'id, groupmode, groupmodeforce', true); - - // We want to pre-fetch all of the groups for each course in a single - // query to avoid calendar_get_allowed_types from hitting the DB for - // each separate course. - $groups = groups_get_all_groups_for_courses($courses); - - foreach ($courses as $course) { - $coursegroups = isset($groups[$course->id]) ? $groups[$course->id] : null; - calendar_get_allowed_types($allowed, $course, $coursegroups); - - if (!empty($allowed->courses)) { - $types['course'][$course->id] = $course; - } - - if (!empty($allowed->groups)) { - $types['groupcourses'][$course->id] = $course; - - if (!isset($types['group'])) { - $types['group'] = array_values($allowed->groups); - } else { - $types['group'] = array_merge($types['group'], array_values($allowed->groups)); - } - } - } - - return $types; -} - /** * See if user can add calendar entries at all used to print the "New Event" button. * diff --git a/calendar/tests/lib_test.php b/calendar/tests/lib_test.php index 8da484cb477..e50e58c2a5e 100644 --- a/calendar/tests/lib_test.php +++ b/calendar/tests/lib_test.php @@ -418,269 +418,6 @@ class core_calendar_lib_testcase extends advanced_testcase { $this->assertCount(3, $events); } - public function test_calendar_get_all_allowed_types_no_types() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $systemcontext = context_system::instance(); - $sitecontext = context_course::instance(SITEID); - $roleid = $generator->create_role(); - - $generator->role_assign($roleid, $user->id, $systemcontext->id); - $generator->role_assign($roleid, $user->id, $sitecontext->id); - $this->setUser($user); - - assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $sitecontext, true); - assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $systemcontext, true); - - $types = calendar_get_all_allowed_types(); - $this->assertEmpty($types); - } - - public function test_calendar_get_all_allowed_types_user() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $context = context_system::instance(); - $roleid = $generator->create_role(); - - $generator->role_assign($roleid, $user->id, $context->id); - $this->setUser($user); - - assign_capability('moodle/calendar:manageownentries', CAP_ALLOW, $roleid, $context, true); - - $types = calendar_get_all_allowed_types(); - $this->assertTrue($types['user']); - - assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $context, true); - - $types = calendar_get_all_allowed_types(); - $this->assertArrayNotHasKey('user', $types); - } - - public function test_calendar_get_all_allowed_types_site() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $context = context_course::instance(SITEID); - $roleid = $generator->create_role(); - - $generator->role_assign($roleid, $user->id, $context->id); - $this->setUser($user); - - assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true); - - $types = calendar_get_all_allowed_types(); - $this->assertTrue($types['site']); - - assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true); - - $types = calendar_get_all_allowed_types(); - $this->assertArrayNotHasKey('site', $types); - } - - public function test_calendar_get_all_allowed_types_course() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $course1 = $generator->create_course(); // Has capability. - $course2 = $generator->create_course(); // Doesn't have capability. - $course3 = $generator->create_course(); // Not enrolled. - $context1 = context_course::instance($course1->id); - $context2 = context_course::instance($course2->id); - $context3 = context_course::instance($course3->id); - $roleid = $generator->create_role(); - $contexts = [$context1, $context2, $context3]; - $enrolledcourses = [$course1, $course2]; - - foreach ($enrolledcourses as $course) { - $generator->enrol_user($user->id, $course->id, 'student'); - } - - foreach ($contexts as $context) { - $generator->role_assign($roleid, $user->id, $context->id); - } - - $this->setUser($user); - - assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true); - assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context2, true); - - // The user only has the correct capability in course 1 so that is the only - // one that should be in the results. - $types = calendar_get_all_allowed_types(); - $typecourses = $types['course']; - $this->assertCount(1, $typecourses); - $this->assertEquals($course1->id, $typecourses[$course1->id]->id); - - assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context2, true); - - // The user only now has the correct capability in both course 1 and 2 so we - // expect both to be in the results. - $types = calendar_get_all_allowed_types(); - $typecourses = $types['course']; - // Sort the results by id ascending to ensure the test is consistent - // and repeatable. - usort($typecourses, function($a, $b) { - $aid = $a->id; - $bid = $b->id; - - if ($aid == $bid) { - return 0; - } - return ($aid < $bid) ? -1 : 1; - }); - - $this->assertCount(2, $typecourses); - $this->assertEquals($course1->id, $typecourses[0]->id); - $this->assertEquals($course2->id, $typecourses[1]->id); - } - - public function test_calendar_get_all_allowed_types_group_no_groups() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $course = $generator->create_course(); - $context = context_course::instance($course->id); - $roleid = $generator->create_role(); - - $generator->enrol_user($user->id, $course->id, 'student'); - $generator->role_assign($roleid, $user->id, $context->id); - - $this->setUser($user); - - assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true); - - // The user has the correct capability in the course but there are - // no groups so we shouldn't see a group type. - $types = calendar_get_all_allowed_types(); - $typecourses = $types['course']; - $this->assertCount(1, $typecourses); - $this->assertEquals($course->id, $typecourses[$course->id]->id); - $this->assertArrayNotHasKey('group', $types); - $this->assertArrayNotHasKey('groupcourses', $types); - } - - public function test_calendar_get_all_allowed_types_group_no_acces_to_diff_groups() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $course = $generator->create_course(); - $context = context_course::instance($course->id); - $group1 = $generator->create_group(array('courseid' => $course->id)); - $group2 = $generator->create_group(array('courseid' => $course->id)); - $roleid = $generator->create_role(); - - $generator->enrol_user($user->id, $course->id, 'student'); - $generator->role_assign($roleid, $user->id, $context->id); - - $this->setUser($user); - - assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true); - assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true); - - // The user has the correct capability in the course but they aren't a member - // of any of the groups and don't have the accessallgroups capability. - $types = calendar_get_all_allowed_types(); - $typecourses = $types['course']; - $this->assertCount(1, $typecourses); - $this->assertEquals($course->id, $typecourses[$course->id]->id); - $this->assertArrayNotHasKey('group', $types); - $this->assertArrayNotHasKey('groupcourses', $types); - } - - public function test_calendar_get_all_allowed_types_group_access_all_groups() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $course1 = $generator->create_course(); - $course2 = $generator->create_course(); - $context1 = context_course::instance($course1->id); - $context2 = context_course::instance($course2->id); - $group1 = $generator->create_group(array('courseid' => $course1->id)); - $group2 = $generator->create_group(array('courseid' => $course1->id)); - $roleid = $generator->create_role(); - - $generator->enrol_user($user->id, $course1->id, 'student'); - $generator->enrol_user($user->id, $course2->id, 'student'); - $generator->role_assign($roleid, $user->id, $context1->id); - $generator->role_assign($roleid, $user->id, $context2->id); - - $this->setUser($user); - - assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true); - assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context2, true); - assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context1, true); - assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context2, true); - - // The user has the correct capability in the course and has - // the accessallgroups capability. - $types = calendar_get_all_allowed_types(); - $typecourses = $types['course']; - $typegroups = $types['group']; - $typegroupcourses = $types['groupcourses']; - $idascfunc = function($a, $b) { - $aid = $a->id; - $bid = $b->id; - - if ($aid == $bid) { - return 0; - } - return ($aid < $bid) ? -1 : 1; - }; - // Sort the results by id ascending to ensure the test is consistent - // and repeatable. - usort($typecourses, $idascfunc); - usort($typegroups, $idascfunc); - - $this->assertCount(2, $typecourses); - $this->assertEquals($course1->id, $typecourses[0]->id); - $this->assertEquals($course2->id, $typecourses[1]->id); - $this->assertCount(1, $typegroupcourses); - $this->assertEquals($course1->id, $typegroupcourses[$course1->id]->id); - $this->assertCount(2, $typegroups); - $this->assertEquals($group1->id, $typegroups[0]->id); - $this->assertEquals($group2->id, $typegroups[1]->id); - } - - public function test_calendar_get_all_allowed_types_group_no_access_all_groups() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $course = $generator->create_course(); - $context = context_course::instance($course->id); - $group1 = $generator->create_group(array('courseid' => $course->id)); - $group2 = $generator->create_group(array('courseid' => $course->id)); - $group3 = $generator->create_group(array('courseid' => $course->id)); - $roleid = $generator->create_role(); - - $generator->enrol_user($user->id, $course->id, 'student'); - $generator->role_assign($roleid, $user->id, $context->id); - $generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user->id)); - $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user->id)); - - $this->setUser($user); - - assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true); - assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true); - - // The user has the correct capability in the course but can't access - // groups that they are not a member of. - $types = calendar_get_all_allowed_types(); - $typegroups = $types['group']; - $typegroupcourses = $types['groupcourses']; - $idascfunc = function($a, $b) { - $aid = $a->id; - $bid = $b->id; - - if ($aid == $bid) { - return 0; - } - return ($aid < $bid) ? -1 : 1; - }; - // Sort the results by id ascending to ensure the test is consistent - // and repeatable. - usort($typegroups, $idascfunc); - - $this->assertCount(1, $typegroupcourses); - $this->assertEquals($course->id, $typegroupcourses[$course->id]->id); - $this->assertCount(2, $typegroups); - $this->assertEquals($group1->id, $typegroups[0]->id); - $this->assertEquals($group2->id, $typegroups[1]->id); - } - public function test_calendar_get_default_courses() { global $USER, $CFG; @@ -766,53 +503,6 @@ class core_calendar_lib_testcase extends advanced_testcase { $this->assertEquals($event->id, $data->events[0]->id); } - public function test_calendar_get_allowed_event_types_no_types() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $systemcontext = context_system::instance(); - $sitecontext = context_course::instance(SITEID); - $roleid = $generator->create_role(); - $generator->role_assign($roleid, $user->id, $systemcontext->id); - $generator->role_assign($roleid, $user->id, $sitecontext->id); - $this->setUser($user); - assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $sitecontext, true); - assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $systemcontext, true); - $types = calendar_get_allowed_event_types(); - foreach ($types as $allowed) { - $this->assertFalse($allowed); - } - } - - public function test_calendar_get_allowed_event_types_user() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $context = context_system::instance(); - $roleid = $generator->create_role(); - $generator->role_assign($roleid, $user->id, $context->id); - $this->setUser($user); - assign_capability('moodle/calendar:manageownentries', CAP_ALLOW, $roleid, $context, true); - $types = calendar_get_allowed_event_types(); - $this->assertTrue($types['user']); - assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $context, true); - $types = calendar_get_allowed_event_types(); - $this->assertFalse($types['user']); - } - - public function test_calendar_get_allowed_event_types_site() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $context = context_course::instance(SITEID); - $roleid = $generator->create_role(); - $generator->role_assign($roleid, $user->id, $context->id); - $this->setUser($user); - assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true); - $types = calendar_get_allowed_event_types(); - $this->assertTrue($types['site']); - assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true); - $types = calendar_get_allowed_event_types(); - $this->assertFalse($types['site']); - } - public function test_calendar_get_allowed_event_types_course() { $generator = $this->getDataGenerator(); $user = $generator->create_user(); @@ -825,25 +515,55 @@ class core_calendar_lib_testcase extends advanced_testcase { $roleid = $generator->create_role(); $contexts = [$context1, $context2, $context3]; $enrolledcourses = [$course1, $course2]; + foreach ($enrolledcourses as $course) { $generator->enrol_user($user->id, $course->id, 'student'); } + foreach ($contexts as $context) { $generator->role_assign($roleid, $user->id, $context->id); } + $this->setUser($user); + assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true); assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context2, true); + // The user only has the correct capability in course 1 so that is the only // one that should be in the results. $types = calendar_get_allowed_event_types($course1->id); $this->assertTrue($types['course']); + assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context1, true); + // The user only now has the correct capability in both course 1 and 2 so we // expect both to be in the results. - $types = calendar_get_allowed_event_types($course1->id); + $types = calendar_get_allowed_event_types($course3->id); $this->assertFalse($types['course']); } + + public function test_calendar_get_allowed_event_types_group_no_acces_to_diff_groups() { + $generator = $this->getDataGenerator(); + $user = $generator->create_user(); + $course = $generator->create_course(); + $context = context_course::instance($course->id); + $roleid = $generator->create_role(); + + $generator->enrol_user($user->id, $course->id, 'student'); + $generator->role_assign($roleid, $user->id, $context->id); + + $this->setUser($user); + + assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true); + assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true); + + // The user has the correct capability in the course but they aren't a member + // of any of the groups and don't have the accessallgroups capability. + $types = calendar_get_allowed_event_types($course->id); + $this->assertTrue($types['course']); + $this->assertFalse($types['group']); + } + public function test_calendar_get_allowed_event_types_group_no_groups() { $generator = $this->getDataGenerator(); $user = $generator->create_user(); @@ -859,23 +579,7 @@ class core_calendar_lib_testcase extends advanced_testcase { $types = calendar_get_allowed_event_types($course->id); $this->assertTrue($types['course']); } - public function test_calendar_get_allowed_event_types_group_no_acces_to_diff_groups() { - $generator = $this->getDataGenerator(); - $user = $generator->create_user(); - $course = $generator->create_course(); - $context = context_course::instance($course->id); - $roleid = $generator->create_role(); - $generator->enrol_user($user->id, $course->id, 'student'); - $generator->role_assign($roleid, $user->id, $context->id); - $this->setUser($user); - assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true); - assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true); - // The user has the correct capability in the course but they aren't a member - // of any of the groups and don't have the accessallgroups capability. - $types = calendar_get_allowed_event_types($course->id); - $this->assertTrue($types['course']); - $this->assertFalse($types['group']); - } + public function test_calendar_get_allowed_event_types_group_access_all_groups() { $generator = $this->getDataGenerator(); $user = $generator->create_user(); diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 9e9b5198a71..a2291280f51 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -6394,3 +6394,76 @@ function message_delete_message($message, $userid) { return \core_message\api::delete_message($userid, $message->id); } + +/** + * Get all of the allowed types for all of the courses and groups + * the logged in user belongs to. + * + * The returned array will optionally have 5 keys: + * 'user' : true if the logged in user can create user events + * 'site' : true if the logged in user can create site events + * 'category' : array of course categories that the user can create events for + * 'course' : array of courses that the user can create events for + * 'group': array of groups that the user can create events for + * 'groupcourses' : array of courses that the groups belong to (can + * be different from the list in 'course'. + * @deprecated since 3.6 + * @return array The array of allowed types. + */ +function calendar_get_all_allowed_types() { + debugging('calendar_get_all_allowed_types() is deprecated. Please use calendar_get_allowed_types() instead.', + DEBUG_DEVELOPER); + + global $CFG, $USER, $DB; + + require_once($CFG->libdir . '/enrollib.php'); + + $types = []; + + $allowed = new stdClass(); + + calendar_get_allowed_types($allowed); + + if ($allowed->user) { + $types['user'] = true; + } + + if ($allowed->site) { + $types['site'] = true; + } + + if (coursecat::has_manage_capability_on_any()) { + $types['category'] = coursecat::make_categories_list('moodle/category:manage'); + } + + // This function warms the context cache for the course so the calls + // to load the course context in calendar_get_allowed_types don't result + // in additional DB queries. + $courses = calendar_get_default_courses(null, 'id, groupmode, groupmodeforce', true); + + // We want to pre-fetch all of the groups for each course in a single + // query to avoid calendar_get_allowed_types from hitting the DB for + // each separate course. + $groups = groups_get_all_groups_for_courses($courses); + + foreach ($courses as $course) { + $coursegroups = isset($groups[$course->id]) ? $groups[$course->id] : null; + calendar_get_allowed_types($allowed, $course, $coursegroups); + + if (!empty($allowed->courses)) { + $types['course'][$course->id] = $course; + } + + if (!empty($allowed->groups)) { + $types['groupcourses'][$course->id] = $course; + + if (!isset($types['group'])) { + $types['group'] = array_values($allowed->groups); + } else { + $types['group'] = array_merge($types['group'], array_values($allowed->groups)); + } + } + } + + return $types; +}