From 73985b14257bf24434d0813c296c86cdfd06826a Mon Sep 17 00:00:00 2001 From: Sergey Gorbatov Date: Thu, 26 Mar 2015 21:12:52 +0400 Subject: [PATCH 1/3] MDL-49380 enrol_cohort: Automatically create a group if needed --- enrol/cohort/edit.php | 15 ++++++++++++++- enrol/cohort/edit_form.php | 6 +++++- enrol/cohort/lang/en/enrol_cohort.php | 1 + enrol/cohort/lib.php | 21 +++++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/enrol/cohort/edit.php b/enrol/cohort/edit.php index 02db3a13698..7675f367aec 100644 --- a/enrol/cohort/edit.php +++ b/enrol/cohort/edit.php @@ -89,9 +89,22 @@ if ($mform->is_cancelled()) { $instance->roleid = $data->roleid; $instance->customint2 = $data->customint2; $instance->timemodified = time(); + if ((int)$data->customint2 == -1) { + 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)); + if ((int)$data->customint2 == -1) { + 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..f9137899716 100644 --- a/enrol/cohort/edit_form.php +++ b/enrol/cohort/edit_form.php @@ -39,7 +39,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')); + } 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..b12e6a80470 100644 --- a/enrol/cohort/lang/en/enrol_cohort.php +++ b/enrol/cohort/lang/en/enrol_cohort.php @@ -30,3 +30,4 @@ $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..d139ce9a4e9 100644 --- a/enrol/cohort/lib.php +++ b/enrol/cohort/lib.php @@ -323,3 +323,24 @@ class enrol_cohort_plugin extends enrol_plugin { function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) { return false; } + + /** + * If group has been created in course returns id group, if not, create a group and return id group. + * @param int $courseid + * @param int $cohortid + * @return int $groupid + */ +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); + } + return $groupid; +} \ No newline at end of file From df9dbc9f42f1a1aa34d70933e407b0383feb31b8 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Fri, 27 Mar 2015 09:17:23 +0800 Subject: [PATCH 2/3] MDL-49380 enrol_cohort: Unit tests and formatting update. --- enrol/cohort/lib.php | 19 ++++---- enrol/cohort/tests/cohortlib_test.php | 62 +++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 enrol/cohort/tests/cohortlib_test.php diff --git a/enrol/cohort/lib.php b/enrol/cohort/lib.php index d139ce9a4e9..b18fd750dd0 100644 --- a/enrol/cohort/lib.php +++ b/enrol/cohort/lib.php @@ -324,16 +324,17 @@ function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) { return false; } - /** - * If group has been created in course returns id group, if not, create a group and return id group. - * @param int $courseid - * @param int $cohortid - * @return int $groupid - */ +/** + * 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; - $cohort = $DB->get_record('cohort', array('id' => $cohortid)); - $groupid = $DB->get_record('groups', array('name' => $cohort->name, 'courseid' => $courseid)); + 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 { diff --git a/enrol/cohort/tests/cohortlib_test.php b/enrol/cohort/tests/cohortlib_test.php new file mode 100644 index 00000000000..824e677815a --- /dev/null +++ b/enrol/cohort/tests/cohortlib_test.php @@ -0,0 +1,62 @@ +. + +/** + * 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 a course. + $course = $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, $group->name); + // Group course id should match the course id. + $this->assertEquals($course->id, $group->courseid); + } +} \ No newline at end of file From 7efd0c0adbfaa0ff1826e827f71b88a8fe484bde Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Wed, 1 Apr 2015 14:04:11 +0800 Subject: [PATCH 3/3] MDL-49380 enrol_cohort: Update to code and introduction of a constant. --- enrol/cohort/edit.php | 6 +++-- enrol/cohort/edit_form.php | 7 +++--- enrol/cohort/lang/en/enrol_cohort.php | 1 + enrol/cohort/lib.php | 36 ++++++++++++++++++++------- enrol/cohort/tests/cohortlib_test.php | 16 ++++++++++-- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/enrol/cohort/edit.php b/enrol/cohort/edit.php index 7675f367aec..b921f15bffd 100644 --- a/enrol/cohort/edit.php +++ b/enrol/cohort/edit.php @@ -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, diff --git a/enrol/cohort/edit_form.php b/enrol/cohort/edit_form.php index f9137899716..2a02a29dd98 100644 --- a/enrol/cohort/edit_form.php +++ b/enrol/cohort/edit_form.php @@ -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)); } diff --git a/enrol/cohort/lang/en/enrol_cohort.php b/enrol/cohort/lang/en/enrol_cohort.php index b12e6a80470..446beae2ab1 100644 --- a/enrol/cohort/lang/en/enrol_cohort.php +++ b/enrol/cohort/lang/en/enrol_cohort.php @@ -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.'; diff --git a/enrol/cohort/lib.php b/enrol/cohort/lib.php index b18fd750dd0..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 @@ -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; } \ No newline at end of file diff --git a/enrol/cohort/tests/cohortlib_test.php b/enrol/cohort/tests/cohortlib_test.php index 824e677815a..4850eaa2021 100644 --- a/enrol/cohort/tests/cohortlib_test.php +++ b/enrol/cohort/tests/cohortlib_test.php @@ -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); } } \ No newline at end of file