/** * Test that a new group with the name of the cohort is created. */ public function test_enrol_cohort_create_new_group() { global $DB; $this->resetAfterTest(); // Create a category. $category = $this->getDataGenerator()->create_category(); // Create two courses. $course = $this->getDataGenerator()->create_course(array('category' => $category->id)); $course2 = $this->getDataGenerator()->create_course(array('category' => $category->id)); // Create a cohort. $cohort = $this->getDataGenerator()->create_cohort(array('context' => context_coursecat::instance($category->id)->id)); // Run the function. $groupid = enrol_cohort_create_new_group($course->id, $cohort->id); // Check the results. $group = $DB->get_record('groups', array('id' => $groupid)); // The group name should match the cohort name. $this->assertEquals($cohort->name . ' cohort', $group->name); // Group course id should match the course id. $this->assertEquals($course->id, $group->courseid); // Create a group that will have the same name as the cohort. $groupdata = new stdClass(); $groupdata->courseid = $course2->id; $groupdata->name = $cohort->name . ' cohort'; groups_create_group($groupdata); // Create a group for the cohort in course 2. $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id); $groupinfo = $DB->get_record('groups', array('id' => $groupid)); // Check that the group name has been changed. $this->assertEquals($cohort->name . ' cohort (2)', $groupinfo->name); // Create another group that will have the same name as a generated cohort. $groupdata = new stdClass(); $groupdata->courseid = $course2->id; $groupdata->name = $cohort->name . ' cohort (2)'; groups_create_group($groupdata); // Create a group for the cohort in course 2. $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id); $groupinfo = $DB->get_record('groups', array('id' => $groupid)); // Check that the group name has been changed. $this->assertEquals($cohort->name . ' cohort (3)', $groupinfo->name); }
$instance->roleid = $data->roleid; $instance->customint2 = $data->customint2; $instance->timemodified = time(); // Create a new group for the cohort if requested. if ($data->customint2 == COHORT_CREATE_GROUP) { require_capability('moodle/course:managegroups', $context); $groupid = enrol_cohort_create_new_group($course->id, $data->customint1); $instance->customint2 = $groupid; } $DB->update_record('enrol', $instance); \core\event\enrol_instance_updated::create_from_record($instance)->trigger(); } else { // Create a new group for the cohort if requested. if ($data->customint2 == COHORT_CREATE_GROUP) { require_capability('moodle/course:managegroups', $context); $groupid = enrol_cohort_create_new_group($course->id, $data->customint1); $enrol->add_instance($course, array('name' => $data->name, 'status' => $data->status, 'customint1' => $data->customint1, 'roleid' => $data->roleid, 'customint2' => $groupid)); } else { $enrol->add_instance($course, array('name' => $data->name, 'status' => $data->status, 'customint1' => $data->customint1, 'roleid' => $data->roleid, 'customint2' => $data->customint2)); } if (!empty($data->submitbuttonnext)) { $returnurl = new moodle_url($PAGE->url); $returnurl->param('message', 'added'); } } $trace = new null_progress_trace(); enrol_cohort_sync($trace, $course->id); $trace->finished(); redirect($returnurl); } }
/** * Update instance of enrol plugin. * @param stdClass $instance * @param stdClass $data modified instance fields * @return boolean */ public function update_instance($instance, $data) { global $CFG; // NOTE: no cohort changes here!!! $context = context_course::instance($instance->courseid); if ($data->roleid != $instance->roleid) { // The sync script can only add roles, for perf reasons it does not modify them. $params = array('contextid' => $context->id, 'roleid' => $instance->roleid, 'component' => 'enrol_cohort', 'itemid' => $instance->id); role_unassign_all($params); } // Create a new group for the cohort if requested. if ($data->customint2 == COHORT_CREATE_GROUP) { require_capability('moodle/course:managegroups', $context); $groupid = enrol_cohort_create_new_group($instance->courseid, $data->customint1); $data->customint2 = $groupid; } $result = parent::update_instance($instance, $data); require_once "{$CFG->dirroot}/enrol/cohort/locallib.php"; $trace = new null_progress_trace(); enrol_cohort_sync($trace, $instance->courseid); $trace->finished(); return $result; }