Skip to content

Commit

Permalink
MDL-61564 enrol_cohort: Allow multiple cohort selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
danmarsden committed Oct 3, 2021
1 parent 1a9bee6 commit 988936e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
51 changes: 29 additions & 22 deletions enrol/cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,30 @@ public function can_add_instance($courseid) {
public function add_instance($course, array $fields = null) {
global $CFG;

if (!empty($fields['customint2']) && $fields['customint2'] == COHORT_CREATE_GROUP) {
// Create a new group for the cohort if requested.
$context = context_course::instance($course->id);
require_capability('moodle/course:managegroups', $context);
$groupid = enrol_cohort_create_new_group($course->id, $fields['customint1']);
$fields['customint2'] = $groupid;
// Allows multiple cohorts to be set on creation.
if (!empty($fields['customint1'])) {
$fields2 = $fields;
if (!is_array($fields['customint1'])) {
$fields['customint1'] = array($fields['customint1']);
}
foreach ($fields['customint1'] as $cid) {
$fields2['customint1'] = $cid;
if (!empty($fields['customint2']) && $fields['customint2'] == COHORT_CREATE_GROUP) {
// Create a new group for the cohort if requested.
$context = context_course::instance($course->id);
require_capability('moodle/course:managegroups', $context);
$groupid = enrol_cohort_create_new_group($course->id, $cid);
$fields2['customint2'] = $groupid;
}
$result = parent::add_instance($course, $fields2);
}
} else {
$result = parent::add_instance($course, $fields);
}

$result = parent::add_instance($course, $fields);

require_once("$CFG->dirroot/enrol/cohort/locallib.php");
$trace = new null_progress_trace();
enrol_cohort_sync($trace, $course->id);
$trace->finished();

return $result;
}

Expand Down Expand Up @@ -417,15 +426,11 @@ public function use_standard_editing_ui() {
* @return bool
*/
public function edit_instance_form($instance, MoodleQuickForm $mform, $coursecontext) {
global $DB;

$mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
$mform->setType('name', PARAM_TEXT);

$options = $this->get_status_options();
$mform->addElement('select', 'status', get_string('status', 'enrol_cohort'), $options);

$options = ['contextid' => $coursecontext->id, 'multiple' => false];
$options = ['contextid' => $coursecontext->id, 'multiple' => true];
$mform->addElement('cohort', 'customint1', get_string('cohort', 'cohort'), $options);

if ($instance->id) {
Expand Down Expand Up @@ -456,31 +461,33 @@ public function edit_instance_form($instance, MoodleQuickForm $mform, $coursecon
public function edit_instance_validation($data, $files, $instance, $context) {
global $DB;
$errors = array();

// Allows multiple cohorts to be selected.
list($sql1, $params1) = $DB->get_in_or_equal($data['customint1'], SQL_PARAMS_NAMED);
$params = array(
'roleid' => $data['roleid'],
'customint1' => $data['customint1'],
'courseid' => $data['courseid'],
'id' => $data['id']
);
$sql = "roleid = :roleid AND customint1 = :customint1 AND courseid = :courseid AND enrol = 'cohort' AND id <> :id";
$params = array_merge($params, $params1);
$sql = "roleid = :roleid AND customint1 $sql1 AND courseid = :courseid AND enrol = 'cohort' AND id <> :id";
if ($DB->record_exists_select('enrol', $sql, $params)) {
$errors['roleid'] = get_string('instanceexists', 'enrol_cohort');
$errors['customint1'] = get_string('instanceexists', 'enrol_cohort');
}
$validstatus = array_keys($this->get_status_options());
$validcohorts = array_keys($this->get_cohort_options($instance, $context));
$validroles = array_keys($this->get_role_options($instance, $context));
$validgroups = array_keys($this->get_group_options($context));
$tovalidate = array(
'name' => PARAM_TEXT,
'status' => $validstatus,
'customint1' => $validcohorts,
'roleid' => $validroles,
'customint2' => $validgroups
);
$typeerrors = $this->validate_param_types($data, $tovalidate);
$errors = array_merge($errors, $typeerrors);

// Check that the cohorts passed are valid.
if (!empty(array_diff($data['customint1'], $validcohorts))) {
$errors['customint1'] = get_string('invaliddata', 'error');
}
return $errors;
}
}
Expand Down
34 changes: 34 additions & 0 deletions enrol/cohort/tests/behat/enrolcohorts.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@enrol @enrol_cohort
Feature: Enrol multiple cohorts

Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher001 | Teacher | 001 | teacher001@example.com |
And the following "cohorts" exist:
| name | idnumber | visible |
| Alpha1 | A1 | 1 |
| Beta2 | B1 | 1 |
And the following "courses" exist:
| fullname | shortname | format | startdate |
| Course 001 | C001 | weeks | ##1 month ago## |
And the following "course enrolments" exist:
| user | course | role | timestart |
| teacher001 | C001 | editingteacher | ##1 month ago## |

@javascript
Scenario: Add multiple cohorts to the course
When I log in as "teacher001"
And I am on "Course 001" course homepage
And I navigate to course participants
And I navigate to "Users > Enrolment methods" in current page administration
And I select "Cohort sync" from the "Add method" singleselect
And I open the autocomplete suggestions list
And I click on "Alpha1" item in the autocomplete list
And "Alpha1" "autocomplete_selection" should exist
And I click on "Beta2" item in the autocomplete list
And "Alpha1" "autocomplete_selection" should exist
And "Beta2" "autocomplete_selection" should exist
And I press "Add method"
Then I should see "Cohort sync (Beta2 - Student)"
And I should see "Cohort sync (Alpha1 - Student)"

0 comments on commit 988936e

Please sign in to comment.