Merge branch 'MDL-49314-master' of git://github.com/jleyva/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2015-04-02 11:06:02 +02:00
commit f8da6281a0
5 changed files with 717 additions and 0 deletions

View File

@ -114,4 +114,124 @@ class core_completion_external extends external_api {
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.9
*/
public static function get_activities_completion_status_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'Course ID'),
'userid' => new external_value(PARAM_INT, 'User ID'),
)
);
}
/**
* Get Activities completion status
*
* @param int $courseid ID of the Course
* @param int $userid ID of the User
* @return array of activities progress and warnings
* @throws moodle_exception
* @since Moodle 2.9
* @throws moodle_exception
*/
public static function get_activities_completion_status($courseid, $userid) {
global $CFG, $USER;
require_once($CFG->libdir . '/grouplib.php');
$warnings = array();
$arrayparams = array(
'courseid' => $courseid,
'userid' => $userid,
);
$params = self::validate_parameters(self::get_activities_completion_status_parameters(), $arrayparams);
$course = get_course($params['courseid']);
$user = core_user::get_user($params['userid'], 'id', MUST_EXIST);
$context = context_course::instance($course->id);
self::validate_context($context);
// Check that current user have permissions to see this user's activities.
if ($user->id != $USER->id) {
require_capability('report/progress:view', $context);
if (!groups_user_groups_visible($course, $user->id)) {
// We are not in the same group!
throw new moodle_exception('accessdenied', 'admin');
}
}
$completion = new completion_info($course);
$activities = $completion->get_activities();
$progresses = $completion->get_progress_all();
$userprogress = $progresses[$user->id];
$results = array();
foreach ($activities as $activity) {
// Check if current user has visibility on this activity.
if (!$activity->uservisible) {
continue;
}
// Get progress information and state.
if (array_key_exists($activity->id, $userprogress->progress)) {
$thisprogress = $userprogress->progress[$activity->id];
$state = $thisprogress->completionstate;
$timecompleted = $thisprogress->timemodified;
} else {
$state = COMPLETION_INCOMPLETE;
$timecompleted = 0;
}
$results[] = array(
'cmid' => $activity->id,
'modname' => $activity->modname,
'instance' => $activity->instance,
'state' => $state,
'timecompleted' => $timecompleted,
'tracking' => $activity->completion
);
}
$results = array(
'statuses' => $results,
'warnings' => $warnings
);
return $results;
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.9
*/
public static function get_activities_completion_status_returns() {
return new external_single_structure(
array(
'statuses' => new external_multiple_structure(
new external_single_structure(
array(
'cmid' => new external_value(PARAM_INT, 'comment ID'),
'modname' => new external_value(PARAM_PLUGIN, 'activity module name'),
'instance' => new external_value(PARAM_INT, 'instance ID'),
'state' => new external_value(PARAM_INT, 'completion state value:
0 means incomplete, 1 complete,
2 complete pass, 3 complete fail'),
'timecompleted' => new external_value(PARAM_INT, 'timestamp for completed activity'),
'tracking' => new external_value(PARAM_INT, 'type of tracking:
0 means none, 1 manual, 2 automatic'),
), 'Activity'
), 'List of activities status'
),
'warnings' => new external_warnings()
)
);
}
}

View File

@ -87,4 +87,103 @@ class core_completion_externallib_testcase extends externallib_advanced_testcase
$this->assertEquals(0, $completiondata->completionstate);
$this->assertTrue($result['status']);
}
/**
* Test update_activity_completion_status
*/
public function test_get_activities_completion_status() {
global $DB, $CFG;
$this->resetAfterTest(true);
$CFG->enablecompletion = true;
$student = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1,
'groupmode' => SEPARATEGROUPS,
'groupmodeforce' => 1));
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
array('completion' => 1));
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
array('completion' => 1));
$assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
array('completion' => 1, 'visible' => 0));
$cmdata = get_coursemodule_from_id('data', $data->cmid);
$cmforum = get_coursemodule_from_id('forum', $forum->cmid);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
// Teacher and student in different groups initially.
groups_add_member($group1->id, $student->id);
groups_add_member($group2->id, $teacher->id);
$this->setUser($student);
// Forum complete.
$completion = new completion_info($course);
$completion->update_state($cmforum, COMPLETION_COMPLETE);
$result = core_completion_external::get_activities_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::get_activities_completion_status_returns(), $result);
// We added 4 activities, but only 3 with completion enabled and one of those is hidden.
$this->assertCount(2, $result['statuses']);
$activitiesfound = 0;
foreach ($result['statuses'] as $status) {
if ($status['cmid'] == $forum->cmid and $status['modname'] == 'forum' and $status['instance'] == $forum->id) {
$activitiesfound++;
$this->assertEquals(COMPLETION_COMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
} else if ($status['cmid'] == $data->cmid and $status['modname'] == 'data' and $status['instance'] == $data->id) {
$activitiesfound++;
$this->assertEquals(COMPLETION_INCOMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
}
}
$this->assertEquals(2, $activitiesfound);
// Teacher should see students status, they are in different groups but the teacher can access all groups.
$this->setUser($teacher);
$result = core_completion_external::get_activities_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::get_activities_completion_status_returns(), $result);
// We added 3 activities, but only 2 with completion enabled.
$this->assertCount(2, $result['statuses']);
// Change teacher role capabilities (disable access al goups).
$context = context_course::instance($course->id);
assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context);
accesslib_clear_all_caches_for_unit_testing();
try {
$result = core_completion_external::get_activities_completion_status($course->id, $student->id);
$this->fail('Exception expected due to groups permissions.');
} catch (moodle_exception $e) {
$this->assertEquals('accessdenied', $e->errorcode);
}
// Now add the teacher in the same group.
groups_add_member($group1->id, $teacher->id);
$result = core_completion_external::get_activities_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::get_activities_completion_status_returns(), $result);
// We added 3 activities, but only 2 with completion enabled.
$this->assertCount(2, $result['statuses']);
}
}

View File

@ -99,6 +99,7 @@ $functions = array(
'type' => 'read',
'capabilities'=> 'moodle/cohort:view',
),
// Comments related functions.
'core_comment_get_comments' => array(
@ -109,6 +110,16 @@ $functions = array(
'capabilities' => 'moodle/comment:view',
),
// Completion related functions.
'core_completion_get_activities_completion_status' => array(
'classname' => 'core_completion_external',
'methodname' => 'get_activities_completion_status',
'description' => 'Return the activities completion status for a user in a course.',
'type' => 'read',
'capabilities' => '',
),
// Grade related functions.
'core_grades_get_grades' => array(
@ -1046,6 +1057,7 @@ $services = array(
'core_comment_get_comments',
'mod_forum_view_forum',
'core_course_view_course',
'core_completion_get_activities_completion_status',
),
'enabled' => 0,
'restrictedusers' => 0,

View File

@ -1056,3 +1056,44 @@ function groups_get_course_data($courseid, cache $cache = null) {
}
return $data;
}
/**
* Determine if the current user can see at least one of the groups of the specified user.
*
* @param stdClass $course Course object.
* @param int $userid user id to check against.
* @param stdClass $cm Course module object. Optional, just for checking at activity level instead course one.
* @return boolean true if visible, false otherwise
* @since Moodle 2.9
*/
function groups_user_groups_visible($course, $userid, $cm = null) {
global $USER;
$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)) {
// User can see everything.
return true;
} else {
// Group mode is separate, and user doesn't have access all groups capability.
if (empty($cm)) {
$usergroups = groups_get_all_groups($course->id, $userid);
$currentusergroups = groups_get_all_groups($course->id, $USER->id);
} else {
$usergroups = groups_get_activity_allowed_groups($cm, $userid);
$currentusergroups = groups_get_activity_allowed_groups($cm, $USER->id);
}
$samegroups = array_intersect_key($currentusergroups, $usergroups);
if (!empty($samegroups)) {
// We share groups!
return true;
}
}
return false;
}

View File

@ -907,4 +907,449 @@ class core_grouplib_testcase extends advanced_testcase {
)),
), groups_sort_menu_options($this->make_group_list(13), $this->make_group_list(2)));
}
/**
* Tests for groups_user_groups_visible.
*/
public function test_groups_user_groups_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(); // Normal user.
$user2 = $generator->create_user(); // Normal user.
$user3 = $generator->create_user(); // Teacher, access all groups.
$user4 = $generator->create_user(); // Normal user.
// Enrol users into the course.
$generator->enrol_user($user1->id, $course->id);
$generator->enrol_user($user2->id, $course->id);
$generator->enrol_user($user4->id, $course->id);
// Assign groups.
// User1 and User4 share groups.
groups_add_member($group1, $user1);
groups_add_member($group2, $user2);
groups_add_member($group1, $user4);
// 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);
// Normal users in different groups.
$this->setUser($user1);
// No groups , not forced.
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertFalse($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// No groups, forced.
$course->groupmode = NOGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result); // Cm with visible groups.
// Visible groups, forced.
$course->groupmode = VISIBLEGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result);
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// Visible groups, not forced.
$course->groupmode = VISIBLEGROUPS;
$course->groupmodeforce = false;
update_course($course);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result);
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertFalse($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// Separate groups, forced.
$course->groupmode = SEPARATEGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertFalse($result);
$result = groups_user_groups_visible($course, $user3->id);
$this->assertFalse($result); // Requesting all groups.
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertFalse($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertFalse($result); // Cm with separate groups.
$result = groups_user_groups_visible($course, $user3->id, $cm);
$this->assertTrue($result);
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertFalse($result); // Cm with visible groups.
// Separate groups, not forced.
$course->groupmode = SEPARATEGROUPS;
$course->groupmodeforce = false;
update_course($course);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertFalse($result);
$result = groups_user_groups_visible($course, $user3->id);
$this->assertFalse($result); // Requesting all groups.
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertFalse($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// Users sharing groups.
// No groups , not forced.
$course->groupmode = NOGROUPS;
$course->groupmodeforce = false;
update_course($course);
$result = groups_user_groups_visible($course, $user4->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user4->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// No groups, forced.
$course->groupmode = NOGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_user_groups_visible($course, $user4->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user4->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user4->id);
$this->assertTrue($result); // Cm with visible groups.
// Visible groups, forced.
$course->groupmode = VISIBLEGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_user_groups_visible($course, $user4->id);
$this->assertTrue($result);
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// Visible groups, not forced.
$course->groupmode = VISIBLEGROUPS;
$course->groupmodeforce = false;
update_course($course);
$result = groups_user_groups_visible($course, $user4->id);
$this->assertTrue($result);
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user4->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// Separate groups, forced.
$course->groupmode = SEPARATEGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_user_groups_visible($course, $user4->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user3->id);
$this->assertFalse($result); // Requesting all groups.
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_user_groups_visible($course, $user3->id, $cm);
$this->assertTrue($result);
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// Separate groups, not forced.
$course->groupmode = SEPARATEGROUPS;
$course->groupmodeforce = false;
update_course($course);
$result = groups_user_groups_visible($course, $user4->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user3->id);
$this->assertFalse($result); // Requesting all groups.
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user4->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// For teacher with access all groups.
// No groups , not forced.
$course->groupmode = NOGROUPS;
$course->groupmodeforce = false;
update_course($course);
$this->setUser($user3);
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result); // Requesting all groups.
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// No groups, forced.
$course->groupmode = NOGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result); // Requesting all groups.
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result); // Cm with visible groups.
// Visible groups, forced.
$course->groupmode = VISIBLEGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result); // Requesting all groups.
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// Visible groups, not forced.
$course->groupmode = VISIBLEGROUPS;
$course->groupmodeforce = false;
update_course($course);
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result); // Requesting all groups.
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// Separate groups, forced.
$course->groupmode = SEPARATEGROUPS;
$course->groupmodeforce = true;
update_course($course);
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result); // Requesting all groups.
$result = groups_user_groups_visible($course, $user3->id);
$this->assertTrue($result); // Requesting all groups.
$result = groups_user_groups_visible($course, $user3->id);
$this->assertTrue($result);
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_user_groups_visible($course, $user3->id, $cm);
$this->assertTrue($result);
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
// Separate groups, not forced.
$course->groupmode = SEPARATEGROUPS;
$course->groupmodeforce = false;
update_course($course);
$result = groups_user_groups_visible($course, $user1->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result);
$result = groups_user_groups_visible($course, $user2->id);
$this->assertTrue($result); // Requesting all groups.
$result = groups_user_groups_visible($course, $user3->id);
$this->assertTrue($result); // Requesting all groups.
$cm->groupmode = NOGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with no groups.
$cm->groupmode = SEPARATEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$result = groups_user_groups_visible($course, $user2->id, $cm);
$this->assertTrue($result); // Cm with separate groups.
$cm->groupmode = VISIBLEGROUPS;
$result = groups_user_groups_visible($course, $user1->id, $cm);
$this->assertTrue($result); // Cm with visible groups.
}
}