Skip to content

Commit

Permalink
MDL-61005 core_cohort: Allow teachers to view site-wide cohorts
Browse files Browse the repository at this point in the history
  • Loading branch information
danmarsden authored and sarjona committed Feb 14, 2018
1 parent a4f914b commit d406ddc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ function cohort_get_invisible_contexts() {
$excludedcontexts = array();
foreach ($records as $ctx) {
context_helper::preload_from_record($ctx);
if (context::instance_by_id($ctx->id) == context_system::instance()) {
continue; // System context cohorts should be available and permissions already checked.
}
if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), context::instance_by_id($ctx->id))) {
$excludedcontexts[] = $ctx->id;
}
Expand Down
15 changes: 15 additions & 0 deletions cohort/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,13 @@ public function test_search_cohorts() {
$user = $this->getDataGenerator()->create_user();
$catuser = $this->getDataGenerator()->create_user();
$catcreator = $this->getDataGenerator()->create_user();
$courseuser = $this->getDataGenerator()->create_user();
$category = $this->getDataGenerator()->create_category();
$othercategory = $this->getDataGenerator()->create_category();
$course = $this->getDataGenerator()->create_course();
$syscontext = context_system::instance();
$catcontext = context_coursecat::instance($category->id);
$coursecontext = context_course::instance($course->id);

// Fetching default authenticated user role.
$userroles = get_archetype_roles('user');
Expand All @@ -481,18 +484,24 @@ public function test_search_cohorts() {
// Creating specific roles.
$creatorrole = create_role('Creator role', 'creatorrole', 'creator role description');
$userrole = create_role('User role', 'userrole', 'user role description');
$courserole = create_role('Course user role', 'courserole', 'course user role description');

assign_capability('moodle/cohort:manage', CAP_ALLOW, $creatorrole, $syscontext->id);
assign_capability('moodle/cohort:view', CAP_ALLOW, $courserole, $syscontext->id);

// Check for parameter $includes = 'parents'.
role_assign($creatorrole, $creator->id, $syscontext->id);
role_assign($creatorrole, $catcreator->id, $catcontext->id);
role_assign($userrole, $user->id, $syscontext->id);
role_assign($userrole, $catuser->id, $catcontext->id);

// Enrol user in the course.
$this->getDataGenerator()->enrol_user($courseuser->id, $course->id, 'courserole');

$syscontext = array('contextid' => context_system::instance()->id);
$catcontext = array('contextid' => context_coursecat::instance($category->id)->id);
$othercatcontext = array('contextid' => context_coursecat::instance($othercategory->id)->id);
$coursecontext = array('contextid' => context_course::instance($course->id)->id);

$cohort1 = $this->getDataGenerator()->create_cohort(array_merge($syscontext, array('name' => 'Cohortsearch 1')));
$cohort2 = $this->getDataGenerator()->create_cohort(array_merge($catcontext, array('name' => 'Cohortsearch 2')));
Expand Down Expand Up @@ -543,6 +552,12 @@ public function test_search_cohorts() {
$result = core_cohort_external::search_cohorts("Cohortsearch", $syscontext, 'all');
$this->assertEquals(3, count($result['cohorts']));

// A user in the course context with the system cohort:view capability. Check that all the system cohorts are returned.
$this->setUser($courseuser);
$result = core_cohort_external::search_cohorts("Cohortsearch", $coursecontext, 'all');
$this->assertEquals(1, count($result['cohorts']));
$this->assertEquals('Cohortsearch 1', $result['cohorts'][$cohort1->id]->name);

// Detect invalid parameter $includes.
$this->setUser($creator);
try {
Expand Down

0 comments on commit d406ddc

Please sign in to comment.