From 0496d985ada78d0e5b53694c6036689c216177d7 Mon Sep 17 00:00:00 2001 From: CORDEL Yannick Date: Mon, 25 Aug 2025 17:21:01 +0800 Subject: [PATCH] MDL-76022 enrol_manual: Manual user enrolment includes group --- public/enrol/manual/ajax.php | 5 ++++ .../enrol/manual/classes/enrol_users_form.php | 23 +++++++++++++++++++ public/enrol/manual/lang/en/enrol_manual.php | 2 ++ .../manual/tests/behat/quickenrolment.feature | 20 ++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/public/enrol/manual/ajax.php b/public/enrol/manual/ajax.php index 4e8af20fffe..da2ac8133ed 100644 --- a/public/enrol/manual/ajax.php +++ b/public/enrol/manual/ajax.php @@ -98,6 +98,7 @@ switch ($action) { $startdateselect = optional_param_array('startdateselect', [], PARAM_INT); $recovergrades = optional_param('recovergrades', 0, PARAM_INT); $timeend = optional_param_array('timeend', [], PARAM_INT); + $group = optional_param('group', null, PARAM_INT); if (empty($roleid)) { $roleid = null; @@ -153,6 +154,7 @@ switch ($action) { $mform = new enrol_manual_enrol_users_form(null, (object)["context" => $context]); $userenroldata = [ + 'group' => $group, 'startdate' => $timestart, 'timeend' => $timeend, ]; @@ -175,6 +177,9 @@ switch ($action) { if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) { foreach ($users as $user) { $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend, null, $recovergrades); + if ($group && has_capability('moodle/course:managegroups', $context)) { + groups_add_member($group, $user->id); + } } $outcome->count += count($users); foreach ($cohorts as $cohort) { diff --git a/public/enrol/manual/classes/enrol_users_form.php b/public/enrol/manual/classes/enrol_users_form.php index be81de496ee..5fd79b0e5c1 100644 --- a/public/enrol/manual/classes/enrol_users_form.php +++ b/public/enrol/manual/classes/enrol_users_form.php @@ -122,6 +122,29 @@ class enrol_manual_enrol_users_form extends moodleform { $mform->addElement('select', 'roletoassign', get_string('assignrole', 'enrol_manual'), $roles); $mform->setDefault('roletoassign', $instance->roleid); + if (has_capability('moodle/course:managegroups', $context)) { + $options = []; + foreach (groups_get_all_groups($course->id) as $group) { + $options[$group->id] = $group->name; + } + + if ($options) { + $mform->addElement( + 'checkbox', + 'showgroups', + get_string('addtogroup', 'enrol_manual'), + get_string('showgroups', 'enrol_manual'), + ); + $mform->addElement( + 'select', + 'group', + null, + $options, + ); + $mform->hideIf('group', 'showgroups', 'notchecked'); + } + } + $mform->addAdvancedStatusElement('main'); $mform->addElement('checkbox', 'recovergrades', get_string('recovergrades', 'enrol')); diff --git a/public/enrol/manual/lang/en/enrol_manual.php b/public/enrol/manual/lang/en/enrol_manual.php index 01e9ea801c6..bbe55f14b0a 100644 --- a/public/enrol/manual/lang/en/enrol_manual.php +++ b/public/enrol/manual/lang/en/enrol_manual.php @@ -22,6 +22,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['addtogroup'] = 'Add to group'; $string['advanced'] = 'Advanced'; $string['alterstatus'] = 'Alter status'; $string['altertimeend'] = 'Alter end time'; @@ -68,6 +69,7 @@ $string['selection'] = 'Selection'; $string['selectusers'] = 'Select users'; $string['selectcohorts'] = 'Select cohorts'; $string['sendexpirynotificationstask'] = "Manual enrolment send expiry notifications task"; +$string['showgroups'] = 'Show groups'; $string['status'] = 'Enable manual enrolments'; $string['status_desc'] = 'Allow course access of internally enrolled users. This should be kept enabled in most cases.'; $string['status_help'] = 'This setting determines whether users can be enrolled manually, via a link in the course administration settings, by a user with appropriate permissions such as a teacher.'; diff --git a/public/enrol/manual/tests/behat/quickenrolment.feature b/public/enrol/manual/tests/behat/quickenrolment.feature index d0b720abc67..55a624bc82a 100644 --- a/public/enrol/manual/tests/behat/quickenrolment.feature +++ b/public/enrol/manual/tests/behat/quickenrolment.feature @@ -212,6 +212,26 @@ Feature: Teacher can search and enrol users one by one into the course And I type "Q994" And I should see "No suggestions" + @javascript + Scenario: Add participant to a group + Given I navigate to course participants + When I press "Enrol users" + # No group created yet. + Then I should not see "Add to group" in the "Enrol users" "dialogue" + And I should not see "Show groups" in the "Enrol users" "dialogue" + And I click on "Cancel" "button" in the "Enrol users" "dialogue" + And I navigate to course participants + And the following "groups" exist: + | name | course | idnumber | + | Group 1 | C001 | G1 | + # Now we have a group, we can test adding a user. + And I press "Enrol users" + And I set the field "Select users" to "student001" + And I click on "showgroups" "checkbox" + And I set the field "Add to group" to "Group 1" + And I click on "Enrol users" "button" in the "Enrol users" "dialogue" + And I should see "Group 1" in the "Student 001" "table_row" + # The following tests are commented out as a result of MDL-66339. # @javascript # Scenario: Enrol user from participants page