/**
* This function creates a group with a particular name, adds it to the grouping,
* and then adds the current user to that group.
*
* @param string $name The name to assign to the newly created group.
* @return int The ID of the group that was group that was created.
*
*/
public function create_group($name)
{
global $DB, $USER;
$sgs = new skills_group_setting($this->courseid);
$group = new stdClass();
if ($name === null || trim($name) == '') {
$name = $this->name_empty_group();
}
$group->name = $name;
$group->courseid = $this->courseid;
$groupid = groups_create_group($group);
groups_assign_grouping($sgs->get_grouping_id(), $groupid);
groups_add_member($groupid, $USER->id);
return $groupid;
}
/**
* Validate log message for assigning a group to a grouping is already
* belongs to
*/
public function test_version1importlogsduplicategroupingassignment()
{
global $CFG, $DB;
require_once $CFG->dirroot . '/group/lib.php';
$userid = $this->create_test_user();
$courseid = $this->create_test_course();
$context = context_course::instance($courseid);
$roleid = $this->create_test_role();
$group = new stdClass();
$group->courseid = $courseid;
$group->name = 'rlipname';
$group->id = groups_create_group($group);
$grouping = new stdClass();
$grouping->courseid = $courseid;
$grouping->name = 'rlipname';
$grouping->id = groups_create_grouping($grouping);
groups_assign_grouping($grouping->id, $group->id);
// Make sure they already have a role assignment.
role_assign($roleid, $userid, $context->id);
// Base data used every time.
$basedata = array('action' => 'create', 'context' => 'course', 'instance' => 'rlipshortname', 'role' => 'rlipshortname', 'group' => 'rlipname', 'grouping' => 'rlipname');
// Username.
$data = $basedata;
$data['username'] = 'rlipusername';
$expectedmessage = "[enrolment.csv line 2] User with username \"rlipusername\" enrolled in course with shortname ";
$expectedmessage .= "\"rlipshortname\". Assigned user with username \"rlipusername\" to group with name \"rlipname\". ";
$expectedmessage .= "Group with name \"rlipname\" is already assigned to grouping with name \"rlipname\".\n";
$this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
$DB->delete_records('user_enrolments');
$DB->delete_records('groups_members');
// Email.
$data = $basedata;
$data['email'] = '[email protected]';
$expectedmessage = "[enrolment.csv line 2] User with email \"[email protected]\" enrolled in course with shortname ";
$expectedmessage .= "\"rlipshortname\". Assigned user with email \"[email protected]\" to group with name ";
$expectedmessage .= "\"rlipname\". Group with name \"rlipname\" is already assigned to grouping with name \"rlipname\".\n";
$this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
$DB->delete_records('user_enrolments');
$DB->delete_records('groups_members');
// Idnumber.
$data = $basedata;
$data['idnumber'] = 'rlipidnumber';
$expectedmessage = "[enrolment.csv line 2] User with idnumber \"rlipidnumber\" enrolled in course with shortname ";
$expectedmessage .= "\"rlipshortname\". Assigned user with idnumber \"rlipidnumber\" to group with name \"rlipname\". ";
$expectedmessage .= "Group with name \"rlipname\" is already assigned to grouping with name \"rlipname\".\n";
$this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
$DB->delete_records('user_enrolments');
$DB->delete_records('groups_members');
// Username, email.
$data = $basedata;
$data['username'] = 'rlipusername';
$data['email'] = '[email protected]';
$expectedmessage = "[enrolment.csv line 2] User with username \"rlipusername\", email \"[email protected]\" ";
$expectedmessage .= "enrolled in course with shortname \"rlipshortname\". Assigned user with username \"rlipusername\", ";
$expectedmessage .= "email \"[email protected]\" to group with name \"rlipname\". Group with name \"rlipname\" is ";
$expectedmessage .= "already assigned to grouping with name \"rlipname\".\n";
$this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
$DB->delete_records('user_enrolments');
$DB->delete_records('groups_members');
// Username, idnumber.
$data = $basedata;
$data['username'] = 'rlipusername';
$data['idnumber'] = 'rlipidnumber';
$expectedmessage = "[enrolment.csv line 2] User with username \"rlipusername\", idnumber \"rlipidnumber\" enrolled in ";
$expectedmessage .= "course with shortname \"rlipshortname\". Assigned user with username \"rlipusername\", idnumber ";
$expectedmessage .= "\"rlipidnumber\" to group with name \"rlipname\". Group with name \"rlipname\" is already assigned ";
$expectedmessage .= "to grouping with name \"rlipname\".\n";
$this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
$DB->delete_records('user_enrolments');
$DB->delete_records('groups_members');
// Email, idnumber.
$data = $basedata;
$data['email'] = '[email protected]';
$data['idnumber'] = 'rlipidnumber';
$expectedmessage = "[enrolment.csv line 2] User with email \"[email protected]\", idnumber \"rlipidnumber\" ";
$expectedmessage .= "enrolled in course with shortname \"rlipshortname\". Assigned user with email ";
$expectedmessage .= "\"[email protected]\", idnumber \"rlipidnumber\" to group with name \"rlipname\". Group with ";
$expectedmessage .= "name \"rlipname\" is already assigned to grouping with name \"rlipname\".\n";
$this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
$DB->delete_records('user_enrolments');
$DB->delete_records('groups_members');
// Username, email, idnumber.
$data = $basedata;
$data['username'] = 'rlipusername';
$data['email'] = '[email protected]';
$data['idnumber'] = 'rlipidnumber';
$expectedmessage = "[enrolment.csv line 2] User with username \"rlipusername\", email \"[email protected]\", ";
$expectedmessage .= "idnumber \"rlipidnumber\" enrolled in course with shortname \"rlipshortname\". Assigned user with ";
$expectedmessage .= "username \"rlipusername\", email \"[email protected]\", idnumber \"rlipidnumber\" to group ";
$expectedmessage .= "with name \"rlipname\". Group with name \"rlipname\" is already assigned to grouping with name ";
$expectedmessage .= "\"rlipname\".\n";
$this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
$DB->delete_records('user_enrolments');
$DB->delete_records('groups_members');
}
/**
* This unit test checks that groups_get_all_groups returns groups in
* alphabetical order even if they are in a grouping.
*/
public function test_groups_ordering()
{
$generator = $this->getDataGenerator();
$this->resetAfterTest();
// Create a course category and course.
$cat = $generator->create_category(array('parent' => 0));
$course = $generator->create_course(array('category' => $cat->id));
$grouping = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping'));
// Create groups in reverse order.
$group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
$group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
// Assign the groups to the grouping in reverse order.
$this->assertTrue(groups_assign_grouping($grouping->id, $group2->id));
$this->assertTrue(groups_assign_grouping($grouping->id, $group1->id));
// Get all groups and check they are alphabetical.
$groups = array_values(groups_get_all_groups($course->id, 0));
$this->assertEquals('Group 1', $groups[0]->name);
$this->assertEquals('Group 2', $groups[1]->name);
// Now check the same is true when accessed by grouping.
$groups = array_values(groups_get_all_groups($course->id, 0, $grouping->id));
$this->assertEquals('Group 1', $groups[0]->name);
$this->assertEquals('Group 2', $groups[1]->name);
}
/**
* Test is_user_access_restricted_by_group()
*
* The underlying groups system is more thoroughly tested in lib/tests/grouplib_test.php
*/
public function test_is_user_access_restricted_by_group() {
global $DB, $CFG, $USER;
$this->resetAfterTest(true);
// Create a course
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
// Create a mod_assign instance
$assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
$cm_info = get_fast_modinfo($course)->instances['assign'][$assign->id];
// Create and enrol a student
// Enrolment is necessary for groups to work
$studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
$student = $this->getDataGenerator()->create_user();
role_assign($studentrole->id, $student->id, $coursecontext);
$enrolplugin = enrol_get_plugin('manual');
$enrolplugin->add_instance($course);
$enrolinstances = enrol_get_instances($course->id, false);
foreach ($enrolinstances as $enrolinstance) {
if ($enrolinstance->enrol === 'manual') {
break;
}
}
$enrolplugin->enrol_user($enrolinstance, $student->id);
// Switch to a student and reload the context info
$this->setUser($student);
$cm_info = $this->refresh_cm_info($course, $assign);
// Create up a teacher
$teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
$teacher = $this->getDataGenerator()->create_user();
role_assign($teacherrole->id, $teacher->id, $coursecontext);
// Create 2 groupings
$grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id, 'name' => 'grouping1'));
$grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id, 'name' => 'grouping2'));
// Create 2 groups and put them in the groupings
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'idnumber' => 'group1'));
groups_assign_grouping($grouping1->id, $group1->id);
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'idnumber' => 'group2'));
groups_assign_grouping($grouping2->id, $group2->id);
// If groups are disabled, the activity isn't restricted.
$CFG->enablegroupmembersonly = false;
$this->assertFalse($cm_info->is_user_access_restricted_by_group());
// If groups are on but "group members only" is off, the activity isn't restricted.
$CFG->enablegroupmembersonly = true;
$cm_info->groupmembersonly = NOGROUPS;
$this->assertFalse($cm_info->is_user_access_restricted_by_group());
// If "group members only" is on but user is in the wrong group, the activity is restricted.
$cm_info->groupmembersonly = SEPARATEGROUPS;
$cm_info->groupingid = $grouping1->id;
$this->assertTrue(groups_add_member($group2, $USER));
$this->assertTrue($cm_info->is_user_access_restricted_by_group());
// If the user is in the required group, the activity isn't restricted.
groups_remove_member($group2, $USER);
$this->assertTrue(groups_add_member($group1, $USER));
$cm_info = $this->refresh_cm_info($course, $assign);
$this->assertFalse($cm_info->is_user_access_restricted_by_group());
// Switch to a teacher and reload the context info
$this->setUser($teacher);
$cm_info = $this->refresh_cm_info($course, $assign);
// If the user isn't in the required group but has 'moodle/site:accessallgroups', the activity isn't restricted.
$this->assertTrue(has_capability('moodle/site:accessallgroups', $coursecontext));
$this->assertFalse($cm_info->is_user_access_restricted_by_group());
}
/**
* Updates all parent cluster's groupings with the existence of a group for this cluster
*
* @param int $clusterid The cluster to check the parents for
* @param boolean $include_children If true, make child cluster-groups trickle up the tree
* @return boolean Returns true to satisfy event handlers
*/
function cluster_groups_update_grouping_closure($clusterid, $include_children = false)
{
global $CFG, $CURMAN;
if (empty($CFG->enablegroupings) || empty($CURMAN->config->cluster_groupings) || !cluster_groups_grouping_allowed($clusterid)) {
return true;
}
$cluster = new cluster($clusterid);
//get the group id for this cluster
if ($groupid = groups_get_group_by_name(SITEID, $cluster->name)) {
//go through the chain of parent clusters
while (!empty($cluster->parent)) {
$cluster = new cluster($cluster->parent);
//add this to grouping if applicable
$grouping = groups_get_grouping_by_name(SITEID, $cluster->name);
if ($grouping = groups_get_grouping($grouping)) {
groups_assign_grouping($grouping->id, $groupid);
//recurse into children if possible
if ($include_children) {
//get all child clusters
$child_cluster_ids = cluster_groups_get_child_clusters($cluster->id);
foreach ($child_cluster_ids as $child_cluster_id) {
//children only
if ($child_cluster_id != $cluster->id) {
$child_cluster = new cluster($child_cluster_id);
//make sure the group exists
if ($child_groupid = groups_get_group_by_name(SITEID, $child_cluster->name) and cluster_groups_cluster_allows_groups($child_cluster->id)) {
groups_assign_grouping($grouping->id, $child_groupid);
}
}
}
}
}
}
}
return true;
}
/**
* This function creates the groups for testing. We enroll the last users first, rather than
* the ones at the start. This way I can use user0 for testing.
*
*/
protected function create_groups()
{
for ($i = 0; $i < self::NUMBEROFGROUPS; $i++) {
$group = $this->getDataGenerator()->create_group(array('courseid' => $this->courseid));
groups_assign_grouping($this->groupingid, $group->id);
$this->groupids[] = $group->id;
groups_add_member($group->id, $this->users[self::NUMBEROFUSERS - 2 * $i - 1]->id);
groups_add_member($group->id, $this->users[self::NUMBEROFUSERS - 2 * $i - 2]->id);
}
}
/**
* Tests the behaviour of the counter in unique_sql_parameter().
*
* There was a problem with static counters used to implement a sequence of
* parameter placeholders (MDL-53481). As always with static variables, it
* is a bit tricky to unit test the behaviour reliably as it depends on the
* actual tests executed and also their order.
*
* To minimise risk of false expected behaviour, this test method should be
* first one where {@link core_availability\tree::get_user_list_sql()} is
* used. We also use higher number of condition instances to increase the
* risk of the counter collision, should there remain a problem.
*/
public function test_unique_sql_parameter_behaviour()
{
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator();
// Create a test course with multiple groupings and groups and a student in each of them.
$course = $generator->create_course();
$user = $generator->create_user();
$studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
$generator->enrol_user($user->id, $course->id, $studentroleid);
// The total number of groupings and groups must not be greater than 61.
// There is a limit in MySQL on the max number of joined tables.
$groups = [];
for ($i = 0; $i < 25; $i++) {
$group = $generator->create_group(array('courseid' => $course->id));
groups_add_member($group, $user);
$groups[] = $group;
}
$groupings = [];
for ($i = 0; $i < 25; $i++) {
$groupings[] = $generator->create_grouping(array('courseid' => $course->id));
}
foreach ($groupings as $grouping) {
foreach ($groups as $group) {
groups_assign_grouping($grouping->id, $group->id);
}
}
$info = new \core_availability\mock_info($course);
// Make a huge tree with 'AND' of all groups and groupings conditions.
$conditions = [];
foreach ($groups as $group) {
$conditions[] = \availability_group\condition::get_json($group->id);
}
foreach ($groupings as $groupingid) {
$conditions[] = \availability_grouping\condition::get_json($grouping->id);
}
shuffle($conditions);
$tree = new tree(tree::get_root_json($conditions));
list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
// This must not throw exception.
$DB->fix_sql_params($sql, $params);
}
/**
* Validate that the import prevents assigning a user to the same grouping
* twice
*/
public function test_version1importpreventsduplicategroupgroupingassignments()
{
global $CFG, $DB;
require_once $CFG->dirroot . '/group/lib.php';
// Set up the "pre-existing" group.
$groupid = $this->create_test_group(self::$courseid);
// Set up the "pre-existing" grouping.
$groupingid = $this->create_test_grouping(self::$courseid);
// Assign the group to the grouping.
groups_assign_grouping($groupingid, $groupid);
// Validate setup.
$this->assertEquals($DB->count_records('groupings_groups'), 1);
// Run the import.
$data = $this->get_core_enrolment_data();
$data['group'] = 'rlipgroup';
$data['grouping'] = 'rlipgrouping';
$this->run_core_enrolment_import($data, false);
// Compare data.
$this->assertEquals($DB->count_records('groupings_groups'), 1);
}
请发表评论