Skip to content

Commit

Permalink
MDL-27628 enrol_meta: Use new multi-course selector
Browse files Browse the repository at this point in the history
Allows adding multiple instances of the meta enrol plugin at once.

I had to add support to the autocomplete element for filling the field from behat
when it was not using tags. I also had to make the shortnames more easily searchable
and unique in the behat feature file.
  • Loading branch information
Damyon Wiese committed Mar 9, 2016
1 parent 3e66b0f commit 6f326bd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 67 deletions.
62 changes: 36 additions & 26 deletions enrol/meta/lib.php
Expand Up @@ -132,21 +132,30 @@ public function course_updated($inserted, $course, $data) {
* Add new instance of enrol plugin.
* @param object $course
* @param array $fields instance fields
* @return int id of new instance, null if can not be created
* @return int id of last instance, null if can not be created
*/
public function add_instance($course, array $fields = null) {
global $CFG;

require_once("$CFG->dirroot/enrol/meta/locallib.php");

if (!empty($fields['customint2']) && $fields['customint2'] == ENROL_META_CREATE_GROUP) {
$context = context_course::instance($course->id);
require_capability('moodle/course:managegroups', $context);
$groupid = enrol_meta_create_new_group($course->id, $fields['customint1']);
$fields['customint2'] = $groupid;
// Support creating multiple at once.
if (is_array($fields['customint1'])) {
$courses = array_unique($fields['customint1']);
} else {
$courses = array($fields['customint1']);
}
foreach ($courses as $courseid) {
if (!empty($fields['customint2']) && $fields['customint2'] == ENROL_META_CREATE_GROUP) {
$context = context_course::instance($course->id);
require_capability('moodle/course:managegroups', $context);
$groupid = enrol_meta_create_new_group($course->id, $courseid);
$fields['customint2'] = $groupid;
}

$result = parent::add_instance($course, $fields);
$fields['customint1'] = $courseid;
$result = parent::add_instance($course, $fields);
}

enrol_meta_sync($course->id);

Expand Down Expand Up @@ -258,7 +267,7 @@ protected function get_course_options($instance, $coursecontext) {
}

// TODO: this has to be done via ajax or else it will fail very badly on large sites!
$courses = array('' => get_string('choosedots'));
$courses = array();
$select = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";

Expand Down Expand Up @@ -313,10 +322,14 @@ protected function get_group_options($coursecontext) {
public function edit_instance_form($instance, MoodleQuickForm $mform, $coursecontext) {
global $DB;

$courses = $this->get_course_options($instance, $coursecontext);
$groups = $this->get_group_options($coursecontext);

$mform->addElement('select', 'customint1', get_string('linkedcourse', 'enrol_meta'), $courses);
$options = array(
'requiredcapabilities' => array('enrol/meta:selectaslinked'),
'multiple' => true,
'excludecourseid' => $coursecontext->instanceid
);
$mform->addElement('course', 'customint1', get_string('linkedcourse', 'enrol_meta'), $options);
$mform->addRule('customint1', get_string('required'), 'required', null, 'client');
if (!empty($instance->id)) {
$mform->freeze('customint1');
Expand All @@ -343,28 +356,25 @@ public function edit_instance_validation($data, $files, $instance, $context) {
$c = false;

if (!empty($data['customint1'])) {
$c = $DB->get_record('course', array('id' => $data['customint1']));
}

if (!$c) {
$errors['customint1'] = get_string('required');
} else {
$coursecontext = context_course::instance($c->id);
$existing = $DB->get_records('enrol', array('enrol' => 'meta', 'courseid' => $thiscourseid), '', 'customint1, id');
if (!$c->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
$errors['customint1'] = get_string('error');
} else if (!has_capability('enrol/meta:selectaslinked', $coursecontext)) {
$errors['customint1'] = get_string('error');
} else if ($c->id == SITEID or $c->id == $thiscourseid or isset($existing[$c->id])) {
$errors['customint1'] = get_string('error');
foreach ($data['customint1'] as $courseid) {
$c = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$coursecontext = context_course::instance($c->id);
$existing = $DB->get_records('enrol', array('enrol' => 'meta', 'courseid' => $thiscourseid), '', 'customint1, id');
if (!$c->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
$errors['customint1'] = get_string('error');
} else if (!has_capability('enrol/meta:selectaslinked', $coursecontext)) {
$errors['customint1'] = get_string('error');
} else if ($c->id == SITEID or $c->id == $thiscourseid or isset($existing[$c->id])) {
$errors['customint1'] = get_string('error');
}
}
} else {
$errors['customint1'] = get_string('required');
}

$validcourses = array_keys($this->get_course_options($instance, $context));
$validgroups = array_keys($this->get_group_options($context));

$tovalidate = array(
'customint1' => $validcourses,
'customint2' => $validgroups
);
$typeerrors = $this->validate_param_types($data, $tovalidate);
Expand Down
64 changes: 25 additions & 39 deletions enrol/meta/tests/behat/enrol_meta.feature
@@ -1,4 +1,4 @@
@enrol @enrol_meta
@enrol @enrol_meta @javascript
Feature: Enrolments are synchronised with meta courses
In order to simplify enrolments in parent courses
As a teacher
Expand All @@ -13,21 +13,21 @@ Feature: Enrolments are synchronised with meta courses
| student4 | Student | 4 | student4@asd.com |
And the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
| Course 2 | C2 |
| Course 3 | C3 |
| Course 1 | C1C1 |
| Course 2 | C2C2 |
| Course 3 | C3C3 |
And the following "groups" exist:
| name | course | idnumber |
| Groupcourse 1 | C3 | G1 |
| Groupcourse 2 | C3 | G2 |
| Groupcourse 1 | C3C3 | G1 |
| Groupcourse 2 | C3C3 | G2 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
| student2 | C1 | student |
| student3 | C1 | student |
| student4 | C1 | student |
| student1 | C2 | student |
| student2 | C2 | student |
| student1 | C1C1 | student |
| student2 | C1C1 | student |
| student3 | C1C1 | student |
| student4 | C1C1 | student |
| student1 | C2C2 | student |
| student2 | C2C2 | student |
And I log in as "admin"
And I navigate to "Manage enrol plugins" node in "Site administration > Plugins > Enrolments"
And I click on "Enable" "link" in the "Course meta link" "table_row"
Expand All @@ -37,26 +37,21 @@ Feature: Enrolments are synchronised with meta courses
Scenario: Add meta enrolment instance without groups
When I follow "Course 3"
And I add "Course meta link" enrolment method with:
| Link course | Course 1 |
| Link course | C1C1 |
And I navigate to "Enrolled users" node in "Course administration > Users"
Then I should see "Student 1"
And I should see "Student 4"
And I should not see "Groupcourse" in the "table.userenrolment" "css_element"

Scenario: Add meta enrolment instance with groups
When I follow "Course 3"
And I navigate to "Enrolment methods" node in "Course administration > Users"
And I select "Course meta link" from the "Add method" singleselect
And I set the following fields to these values:
| Link course | Course 1 |
And I add "Course meta link" enrolment method with:
| Link course | C1C1 |
| Add to group | Groupcourse 1 |
And I press "Add method"
And I set the field "Add method" to "Course meta link"
And I press "Go"
And I set the following fields to these values:
| Link course | Course 2 |
And I follow "Course 3"
And I add "Course meta link" enrolment method with:
| Link course | C2C2 |
| Add to group | Groupcourse 2 |
And I press "Add method"
And I navigate to "Enrolled users" node in "Course administration > Users"
Then I should see "Groupcourse 1" in the "Student 1" "table_row"
And I should see "Groupcourse 1" in the "Student 2" "table_row"
Expand All @@ -69,13 +64,9 @@ Feature: Enrolments are synchronised with meta courses

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 press "Go"
And I set the following fields to these values:
| Link course | Course 1 |
And I add "Course meta link" enrolment method with:
| Link course | C1C1 |
| 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"
Expand All @@ -86,17 +77,12 @@ Feature: Enrolments are synchronised with meta courses

Scenario: Backup and restore of meta enrolment instance
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 press "Go"
And I set the following fields to these values:
| Link course | Course 1 |
And I add "Course meta link" enrolment method with:
| Link course | C1C1 |
| Add to group | Groupcourse 1 |
And I press "Add method"
And I select "Course meta link" from the "Add method" singleselect
And I set the following fields to these values:
| Link course | Course 2 |
And I press "Add method"
And I follow "Course 3"
And I add "Course meta link" enrolment method with:
| Link course | C2C2 |
When I backup "Course 3" course using this options:
| Confirmation | Filename | test_backup.mbz |
And I click on "Restore" "link" in the "test_backup.mbz" "table_row"
Expand Down

0 comments on commit 6f326bd

Please sign in to comment.