MDL-49380 enrol_cohort: Update to code and introduction of a constant.
This commit is contained in:
@@ -89,14 +89,16 @@ if ($mform->is_cancelled()) {
|
||||
$instance->roleid = $data->roleid;
|
||||
$instance->customint2 = $data->customint2;
|
||||
$instance->timemodified = time();
|
||||
if ((int)$data->customint2 == -1) {
|
||||
// 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);
|
||||
} else {
|
||||
if ((int)$data->customint2 == -1) {
|
||||
// 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,
|
||||
|
||||
@@ -38,12 +38,11 @@ class enrol_cohort_edit_form extends moodleform {
|
||||
|
||||
$enrol = enrol_get_plugin('cohort');
|
||||
|
||||
|
||||
$groups = array(0 => get_string('none'));
|
||||
if (has_capability('moodle/course:managegroups', $coursecontext)) {
|
||||
$groups = array(0 => get_string('none'), -1 => get_string('creategroup', 'enrol_cohort'));
|
||||
} else {
|
||||
$groups = array(0 => get_string('none'));
|
||||
$groups[COHORT_CREATE_GROUP] = get_string('creategroup', 'enrol_cohort');
|
||||
}
|
||||
|
||||
foreach (groups_get_all_groups($course->id) as $group) {
|
||||
$groups[$group->id] = format_string($group->name, true, array('context'=>$coursecontext));
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ $string['addgroup'] = 'Add to group';
|
||||
$string['assignrole'] = 'Assign role';
|
||||
$string['cohort:config'] = 'Configure cohort instances';
|
||||
$string['cohort:unenrol'] = 'Unenrol suspended users';
|
||||
$string['defaultgroupnametext'] = '{$a->name} cohort{$a->increment}';
|
||||
$string['instanceexists'] = 'Cohort is already synchronised with selected role';
|
||||
$string['pluginname'] = 'Cohort sync';
|
||||
$string['pluginname_desc'] = 'Cohort enrolment plugin synchronises cohort members with course participants.';
|
||||
|
||||
+27
-9
@@ -24,6 +24,11 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* COHORT_CREATEGROUP constant for automatically creating a group for a cohort.
|
||||
*/
|
||||
define('COHORT_CREATE_GROUP', -1);
|
||||
|
||||
/**
|
||||
* Cohort enrolment plugin implementation.
|
||||
* @author Petr Skoda
|
||||
@@ -333,15 +338,28 @@ function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) {
|
||||
*/
|
||||
function enrol_cohort_create_new_group($courseid, $cohortid) {
|
||||
global $DB;
|
||||
$cohort = $DB->get_record('cohort', array('id' => $cohortid));
|
||||
$groupid = $DB->get_record('groups', array('name' => $cohort->name, 'courseid' => $courseid));
|
||||
if (isset($groupid->id)) {
|
||||
$groupid = $groupid->id;
|
||||
} else {
|
||||
$groupdata = new stdClass();
|
||||
$groupdata->courseid = $courseid;
|
||||
$groupdata->name = $cohort->name;
|
||||
$groupid = groups_create_group($groupdata);
|
||||
|
||||
$groupname = $DB->get_field('cohort', 'name', array('id' => $cohortid), MUST_EXIST);
|
||||
$a = new stdClass();
|
||||
$a->name = $groupname;
|
||||
$a->increment = '';
|
||||
$groupname = get_string('defaultgroupnametext', 'enrol_cohort', $a);
|
||||
// Check to see if the cohort group name already exists. Add an incremented number if it does.
|
||||
while ($DB->record_exists('groups', array('name' => $groupname))) {
|
||||
$matches = array();
|
||||
if (!preg_match('/(.*?)\(([0-9]+)\)$/', $groupname, $matches)) {
|
||||
$a->increment = '(2)';
|
||||
} else {
|
||||
$a->increment = '(' . $matches[2]+1 . ')';
|
||||
}
|
||||
$newshortname = get_string('defaultgroupnametext', 'enrol_cohort', $a);
|
||||
$groupname = $newshortname;
|
||||
}
|
||||
// Create a new group for the cohort.
|
||||
$groupdata = new stdClass();
|
||||
$groupdata->courseid = $courseid;
|
||||
$groupdata->name = $groupname;
|
||||
$groupid = groups_create_group($groupdata);
|
||||
|
||||
return $groupid;
|
||||
}
|
||||
@@ -46,8 +46,9 @@ class enrol_cohort_lib_testcase extends advanced_testcase {
|
||||
$this->resetAfterTest();
|
||||
// Create a category.
|
||||
$category = $this->getDataGenerator()->create_category();
|
||||
// Create a course.
|
||||
// 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.
|
||||
@@ -55,8 +56,19 @@ class enrol_cohort_lib_testcase extends advanced_testcase {
|
||||
// Check the results.
|
||||
$group = $DB->get_record('groups', array('id' => $groupid));
|
||||
// The group name should match the cohort name.
|
||||
$this->assertEquals($cohort->name, $group->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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user