/**
  * Tests loading a class from /availability/classes.
  */
 public function test_capability_checker()
 {
     global $CFG, $DB;
     $this->resetAfterTest();
     // Create a course with teacher and student.
     $generator = $this->getDataGenerator();
     $course = $generator->create_course();
     $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
     $teacher = $generator->create_user();
     $student = $generator->create_user();
     $generator->enrol_user($teacher->id, $course->id, $roleids['teacher']);
     $generator->enrol_user($student->id, $course->id, $roleids['student']);
     // Check a capability which they both have.
     $context = context_course::instance($course->id);
     $checker = new capability_checker($context);
     $result = array_keys($checker->get_users_by_capability('mod/forum:replypost'));
     sort($result);
     $this->assertEquals(array($teacher->id, $student->id), $result);
     // And one that only teachers have.
     $result = array_keys($checker->get_users_by_capability('mod/forum:deleteanypost'));
     $this->assertEquals(array($teacher->id), $result);
     // Check the caching is working.
     $before = $DB->perf_get_queries();
     $result = array_keys($checker->get_users_by_capability('mod/forum:deleteanypost'));
     $this->assertEquals(array($teacher->id), $result);
     $this->assertEquals($before, $DB->perf_get_queries());
 }
Esempio n. 2
0
 public function filter_user_list(array $users, $not, \core_availability\info $info, \core_availability\capability_checker $checker)
 {
     global $CFG, $DB;
     // List users for this course who match the condition.
     $groupingusers = $DB->get_records_sql("\n                SELECT DISTINCT gm.userid\n                  FROM {groupings_groups} gg\n                  JOIN {groups_members} gm ON gm.groupid = gg.groupid\n                 WHERE gg.groupingid = ?", array($this->get_grouping_id($info)));
     // List users who have access all groups.
     $aagusers = $checker->get_users_by_capability('moodle/site:accessallgroups');
     // Filter the user list.
     $result = array();
     foreach ($users as $id => $user) {
         // Always include users with access all groups.
         if (array_key_exists($id, $aagusers)) {
             $result[$id] = $user;
             continue;
         }
         // Other users are included or not based on grouping membership.
         $allow = array_key_exists($id, $groupingusers);
         if ($not) {
             $allow = !$allow;
         }
         if ($allow) {
             $result[$id] = $user;
         }
     }
     return $result;
 }
Esempio n. 3
0
 public function filter_user_list(array $users, $not, \core_availability\info $info, \core_availability\capability_checker $checker)
 {
     global $CFG, $DB;
     // If the array is empty already, just return it.
     if (!$users) {
         return $users;
     }
     require_once $CFG->libdir . '/grouplib.php';
     $course = $info->get_course();
     // List users for this course who match the condition.
     if ($this->groupid) {
         $groupusers = groups_get_members($this->groupid, 'u.id', 'u.id ASC');
     } else {
         $groupusers = $DB->get_records_sql("\n                    SELECT DISTINCT gm.userid\n                      FROM {groups} g\n                      JOIN {groups_members} gm ON gm.groupid = g.id\n                     WHERE g.courseid = ?", array($course->id));
     }
     // List users who have access all groups.
     $aagusers = $checker->get_users_by_capability('moodle/site:accessallgroups');
     // Filter the user list.
     $result = array();
     foreach ($users as $id => $user) {
         // Always include users with access all groups.
         if (array_key_exists($id, $aagusers)) {
             $result[$id] = $user;
             continue;
         }
         // Other users are included or not based on group membership.
         $allow = array_key_exists($id, $groupusers);
         if ($not) {
             $allow = !$allow;
         }
         if ($allow) {
             $result[$id] = $user;
         }
     }
     return $result;
 }