diff --git a/enrol/cohort/edit.php b/enrol/cohort/edit.php index 02db3a13698..b921f15bffd 100644 --- a/enrol/cohort/edit.php +++ b/enrol/cohort/edit.php @@ -89,9 +89,24 @@ if ($mform->is_cancelled()) { $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); } else { - $enrol->add_instance($course, array('name'=>$data->name, 'status'=>$data->status, 'customint1'=>$data->customint1, 'roleid'=>$data->roleid, 'customint2'=>$data->customint2)); + // 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'); diff --git a/enrol/cohort/edit_form.php b/enrol/cohort/edit_form.php index bb5ee8acfba..2a02a29dd98 100644 --- a/enrol/cohort/edit_form.php +++ b/enrol/cohort/edit_form.php @@ -38,8 +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[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)); } diff --git a/enrol/cohort/lang/en/enrol_cohort.php b/enrol/cohort/lang/en/enrol_cohort.php index 48550f28b16..446beae2ab1 100644 --- a/enrol/cohort/lang/en/enrol_cohort.php +++ b/enrol/cohort/lang/en/enrol_cohort.php @@ -26,7 +26,9 @@ $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.'; $string['status'] = 'Active'; +$string['creategroup'] = 'Create new group'; diff --git a/enrol/cohort/lib.php b/enrol/cohort/lib.php index 1f5a22180d7..571d01dbd4f 100644 --- a/enrol/cohort/lib.php +++ b/enrol/cohort/lib.php @@ -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 @@ -323,3 +328,38 @@ class enrol_cohort_plugin extends enrol_plugin { function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) { return false; } + +/** + * Create a new group with the cohorts name. + * + * @param int $courseid + * @param int $cohortid + * @return int $groupid Group ID for this cohort. + */ +function enrol_cohort_create_new_group($courseid, $cohortid) { + global $DB; + + $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; +} \ No newline at end of file diff --git a/enrol/cohort/tests/cohortlib_test.php b/enrol/cohort/tests/cohortlib_test.php new file mode 100644 index 00000000000..4850eaa2021 --- /dev/null +++ b/enrol/cohort/tests/cohortlib_test.php @@ -0,0 +1,74 @@ +. + +/** + * Cohort enrolment sync functional test. + * + * @package enrol_cohort + * @category phpunit + * @copyright 2015 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot.'/cohort/lib.php'); +require_once($CFG->dirroot.'/group/lib.php'); + +/** + * Contains tests for the cohort library. + * + * @package enrol_cohort + * @copyright 2015 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class enrol_cohort_lib_testcase extends advanced_testcase { + + /** + * 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); + } +} \ No newline at end of file