MDL-49380 enrol_cohort: Update to code and introduction of a constant.
authorAdrian Greeve <adrian@moodle.com>
Wed, 1 Apr 2015 06:04:11 +0000 (14:04 +0800)
committerAdrian Greeve <adrian@moodle.com>
Thu, 2 Apr 2015 01:49:35 +0000 (09:49 +0800)
enrol/cohort/edit.php
enrol/cohort/edit_form.php
enrol/cohort/lang/en/enrol_cohort.php
enrol/cohort/lib.php
enrol/cohort/tests/cohortlib_test.php

index 7675f36..b921f15 100644 (file)
@@ -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,
index f913789..2a02a29 100644 (file)
@@ -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));
         }
index b12e6a8..446beae 100644 (file)
@@ -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.';
index b18fd75..571d01d 100644 (file)
 
 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
index 824e677..4850eaa 100644 (file)
@@ -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