MDL-17929 enrol_meta: create new group if selected
AMOS BEGIN CPY [creategroup,enrol_cohort],[creategroup,enrol_meta] AMOS END
This commit is contained in:
@@ -51,6 +51,9 @@ if ($mform->is_cancelled()) {
|
||||
redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
|
||||
|
||||
} else if ($data = $mform->get_data()) {
|
||||
if (!empty($data->customint2) && $data->customint2 == ENROL_META_CREATE_GROUP) {
|
||||
$data->customint2 = enrol_meta_create_new_group($course->id, $data->link);
|
||||
}
|
||||
$eid = $enrol->add_instance($course, array('customint1' => $data->link,
|
||||
'customint2' => $data->customint2));
|
||||
enrol_meta_sync($course->id);
|
||||
|
||||
@@ -65,8 +65,11 @@ class enrol_meta_addinstance_form extends moodleform {
|
||||
$rs->close();
|
||||
|
||||
$groups = array(0 => get_string('none'));
|
||||
if (has_capability('moodle/course:managegroups', context_course::instance($course->id))) {
|
||||
$groups[ENROL_META_CREATE_GROUP] = get_string('creategroup', 'enrol_meta');
|
||||
}
|
||||
foreach (groups_get_all_groups($course->id) as $group) {
|
||||
$groups[$group->id] = format_string($group->name, true, array('context' => course_context::instance($course->id)));
|
||||
$groups[$group->id] = format_string($group->name, true, array('context' => context_course::instance($course->id)));
|
||||
}
|
||||
|
||||
$mform->addElement('header','general', get_string('pluginname', 'enrol_meta'));
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
$string['addgroup'] = 'Add to group';
|
||||
$string['coursesort'] = 'Sort course list';
|
||||
$string['coursesort_help'] = 'This determines whether the list of courses that can be linked are sorted by sort order (i.e. the order set in Site administration > Courses > Manage courses and categories) or alphabetically by course setting.';
|
||||
$string['creategroup'] = 'Create new group';
|
||||
$string['defaultgroupnametext'] = '{$a->name} course {$a->increment}';
|
||||
$string['linkedcourse'] = 'Link course';
|
||||
$string['meta:config'] = 'Configure meta enrol instances';
|
||||
$string['meta:selectaslinked'] = 'Select course as meta linked';
|
||||
|
||||
@@ -24,6 +24,11 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* ENROL_META_CREATE_GROUP constant for automatically creating a group for a meta course.
|
||||
*/
|
||||
define('ENROL_META_CREATE_GROUP', -1);
|
||||
|
||||
/**
|
||||
* Meta course enrolment plugin.
|
||||
* @author Petr Skoda
|
||||
|
||||
@@ -599,3 +599,35 @@ function enrol_meta_sync($courseid = NULL, $verbose = false) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new group with the course's name.
|
||||
*
|
||||
* @param int $courseid
|
||||
* @param int $linkedcourseid
|
||||
* @return int $groupid Group ID for this cohort.
|
||||
*/
|
||||
function enrol_meta_create_new_group($courseid, $linkedcourseid) {
|
||||
global $DB, $CFG;
|
||||
|
||||
require_once($CFG->dirroot.'/group/lib.php');
|
||||
|
||||
$coursename = $DB->get_field('course', 'fullname', array('id' => $linkedcourseid), MUST_EXIST);
|
||||
$a = new stdClass();
|
||||
$a->name = $coursename;
|
||||
$a->increment = '';
|
||||
$inc = 1;
|
||||
$groupname = trim(get_string('defaultgroupnametext', 'enrol_meta', $a));
|
||||
// Check to see if the group name already exists in this course. Add an incremented number if it does.
|
||||
while ($DB->record_exists('groups', array('name' => $groupname, 'courseid' => $courseid))) {
|
||||
$a->increment = '(' . (++$inc) . ')';
|
||||
$groupname = trim(get_string('defaultgroupnametext', 'enrol_meta', $a));
|
||||
}
|
||||
// Create a new group for the course meta sync.
|
||||
$groupdata = new stdClass();
|
||||
$groupdata->courseid = $courseid;
|
||||
$groupdata->name = $groupname;
|
||||
$groupid = groups_create_group($groupdata);
|
||||
|
||||
return $groupid;
|
||||
}
|
||||
|
||||
@@ -68,6 +68,24 @@ Feature: Enrolments are synchronised with meta courses
|
||||
And I should not see "Groupcourse 2" in the "Student 3" "table_row"
|
||||
And I should not see "Groupcourse 2" in the "Student 4" "table_row"
|
||||
|
||||
@javascript
|
||||
Scenario: Add meta enrolment instance with auto-created groups
|
||||
When I follow "Course 3"
|
||||
And I navigate to "Enrolment methods" node in "Course administration > Users"
|
||||
And I set the field "Add method" to "Course meta link"
|
||||
And I wait to be redirected
|
||||
And I set the following fields to these values:
|
||||
| Link course | Course 1 |
|
||||
| Add to group | Create new group |
|
||||
And I press "Add method"
|
||||
And I navigate to "Enrolled users" node in "Course administration > Users"
|
||||
Then I should see "Course 1 course" in the "Student 1" "table_row"
|
||||
And I should see "Course 1 course" in the "Student 2" "table_row"
|
||||
And I should see "Course 1 course" in the "Student 3" "table_row"
|
||||
And I should see "Course 1 course" in the "Student 4" "table_row"
|
||||
And I navigate to "Groups" node in "Course administration > Users"
|
||||
And the "Groups" select box should contain "Course 1 course (4)"
|
||||
|
||||
@javascript
|
||||
Scenario: Backup and restore of meta enrolment instance
|
||||
When I follow "Course 3"
|
||||
|
||||
@@ -644,4 +644,41 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
|
||||
$this->assertEventLegacyData($expectedlegacyeventdata, $event);
|
||||
$this->assertEventContextNotUsed($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a new group with the name of the course is created.
|
||||
*/
|
||||
public function test_enrol_meta_create_new_group() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
// Create two courses.
|
||||
$course = $this->getDataGenerator()->create_course(array('fullname' => 'Mathematics'));
|
||||
$course2 = $this->getDataGenerator()->create_course(array('fullname' => 'Physics'));
|
||||
$metacourse = $this->getDataGenerator()->create_course(array('fullname' => 'All sciences'));
|
||||
// Run the function.
|
||||
$groupid = enrol_meta_create_new_group($metacourse->id, $course->id);
|
||||
// Check the results.
|
||||
$group = $DB->get_record('groups', array('id' => $groupid));
|
||||
// The group name should match the course name.
|
||||
$this->assertEquals('Mathematics course', $group->name);
|
||||
// Group course id should match the course id.
|
||||
$this->assertEquals($metacourse->id, $group->courseid);
|
||||
|
||||
// Create a group that will have the same name as the course.
|
||||
$groupdata = new stdClass();
|
||||
$groupdata->courseid = $metacourse->id;
|
||||
$groupdata->name = 'Physics course';
|
||||
groups_create_group($groupdata);
|
||||
// Create a group for the course 2 in metacourse.
|
||||
$groupid = enrol_meta_create_new_group($metacourse->id, $course2->id);
|
||||
$groupinfo = $DB->get_record('groups', array('id' => $groupid));
|
||||
// Check that the group name has been changed.
|
||||
$this->assertEquals('Physics course (2)', $groupinfo->name);
|
||||
|
||||
// Create a group for the course 2 in metacourse.
|
||||
$groupid = enrol_meta_create_new_group($metacourse->id, $course2->id);
|
||||
$groupinfo = $DB->get_record('groups', array('id' => $groupid));
|
||||
// Check that the group name has been changed.
|
||||
$this->assertEquals('Physics course (3)', $groupinfo->name);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user