Merge branch 'MDL-40354-master' of git://github.com/ankitagarwal/moodle

This commit is contained in:
Marina Glancy 2013-07-10 13:15:37 +10:00
commit 06832223d4
2 changed files with 226 additions and 1 deletions

View File

@ -776,7 +776,7 @@ function groups_get_activity_allowed_groups($cm,$userid=0) {
// If visible groups mode, or user has the accessallgroups capability,
// then they can access all groups for the activity...
$context = context_module::instance($cm->id);
if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context, $userid)) {
return groups_get_all_groups($cm->course, 0, $cm->groupingid);
} else {
// ...otherwise they can only access groups they belong to
@ -812,6 +812,44 @@ function groups_course_module_visible($cm, $userid=null) {
return false;
}
/**
* Determine if a given group is visible to user or not in a given context.
*
* @since Moodle 2.6
* @param int $groupid Group id to test. 0 for all groups.
* @param stdClass $course Course object.
* @param stdClass $cm Course module object.
* @param int $userid user id to test against. Defaults to $USER.
* @return boolean true if visible, false otherwise
*/
function groups_group_visible($groupid, $course, $cm = null, $userid = null) {
global $USER;
if (empty($userid)) {
$userid = $USER->id;
}
$groupmode = empty($cm) ? groups_get_course_groupmode($course) : groups_get_activity_groupmode($cm, $course);
if ($groupmode == NOGROUPS || $groupmode == VISIBLEGROUPS) {
// Groups are not used, or everything is visible, no need to go any further.
return true;
}
$context = empty($cm) ? context_course::instance($course->id) : context_module::instance($cm->id);
if (has_capability('moodle/site:accessallgroups', $context, $userid)) {
// User can see everything. Groupid = 0 is handled here as well.
return true;
} else if ($groupid != 0) {
// Group mode is separate, and user doesn't have access all groups capability. Check if user can see requested group.
$groups = empty($cm) ? groups_get_all_groups($course->id, $userid) : groups_get_activity_allowed_groups($cm, $userid);
if (array_key_exists($groupid, $groups)) {
// User can see the group.
return true;
}
}
return false;
}
/**
* Internal method, sets up $SESSION->activegroup and verifies previous value
*

View File

@ -405,4 +405,191 @@ class grouplib_testcase extends advanced_testcase {
$this->assertContains($group3->id, $groupkeys);
$this->assertContains($group4->id, $groupkeys);
}
/**
* Tests for groups_group_visible.
*/
public function test_groups_group_visible() {
global $CFG, $DB;
$generator = $this->getDataGenerator();
$this->resetAfterTest();
$this->setAdminUser();
// Create a course category, course and groups.
$cat = $generator->create_category(array('parent' => 0));
$course = $generator->create_course(array('category' => $cat->id));
$coursecontext = context_course::instance($course->id);
$group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
$group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
$group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
$group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4'));
// Create cm.
$assign = $generator->create_module("assign", array('course' => $course->id));
$cm = get_coursemodule_from_instance("assign", $assign->id);
// Create users.
$user1 = $generator->create_user();
$user2 = $generator->create_user();
$user3 = $generator->create_user();
// Enrol users into the course.
$generator->enrol_user($user1->id, $course->id);
$generator->enrol_user($user2->id, $course->id);
// Assign groups.
groups_add_member($group1, $user2);
// Give capability at course level to the user to access all groups.
$role = $DB->get_field("role", "id", array("shortname" => "manager"));
$generator->enrol_user($user3->id, $course->id, $role);
// Make sure the user has the capability.
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role, $coursecontext->id);
// No groups , not forced.
$result = groups_group_visible($group1->id, $course, null, $user1->id);
$this->assertTrue($result);
$result = groups_group_visible(0, $course, null, $user1->id);
$this->assertTrue($result); // Requesting all groups.
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertFalse($result); // Cm with separate groups.
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with visible groups.
// No groups, forced.
$course->groupmode = NOGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_group_visible($group1->id, $course, null, $user1->id);
$this->assertTrue($result);
$result = groups_group_visible(0, $course, null, $user1->id);
$this->assertTrue($result); // Requesting all groups.
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with visible groups.
// Visible groups, forced.
$course->groupmode = VISIBLEGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_group_visible($group1->id, $course, null, $user1->id);
$this->assertTrue($result);
$result = groups_group_visible(0, $course, null, $user1->id);
$this->assertTrue($result); // Requesting all groups.
$cm->groupmode = NOGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with visible groups.
// Visible groups, not forced.
$course->groupmode = VISIBLEGROUPS;
$course->groupmodeforce = false;
update_course($course);
$result = groups_group_visible($group1->id, $course, null, $user1->id);
$this->assertTrue($result);
$result = groups_group_visible(0, $course, null, $user1->id);
$this->assertTrue($result); // Requesting all groups.
$cm->groupmode = NOGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertFalse($result); // Cm with separate groups.
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with visible groups.
// Separate groups, forced.
$course->groupmode = SEPARATEGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_group_visible($group1->id, $course, null, $user1->id);
$this->assertFalse($result);
$result = groups_group_visible($group1->id, $course, null, $user2->id);
$this->assertTrue($result);
$result = groups_group_visible(0, $course, null, $user2->id);
$this->assertFalse($result); // Requesting all groups.
$result = groups_group_visible(0, $course, null, $user3->id);
$this->assertTrue($result); // Requesting all groups.
$result = groups_group_visible($group1->id, $course, null, $user3->id);
$this->assertTrue($result); // Make sure user with access to all groups can see any group.
$cm->groupmode = NOGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertFalse($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertFalse($result); // Cm with separate groups.
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_group_visible($group1->id, $course, $cm, $user3->id);
$this->assertTrue($result); // Make sure user with access to all groups can see any group.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertFalse($result); // Cm with visible groups.
// Separate groups, not forced.
$course->groupmode = SEPARATEGROUPS;
$course->groupmodeforce = false;
update_course($course);
$result = groups_group_visible($group1->id, $course, null, $user1->id);
$this->assertFalse($result);
$result = groups_group_visible($group1->id, $course, null, $user2->id);
$this->assertTrue($result);
$result = groups_group_visible(0, $course, null, $user2->id);
$this->assertFalse($result); // Requesting all groups.
$result = groups_group_visible(0, $course, null, $user3->id);
$this->assertTrue($result); // Requesting all groups.
$cm->groupmode = NOGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertFalse($result); // Cm with separate groups.
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
$this->assertTrue($result); // Cm with visible groups.
}
}