From 253c0412970a4d5356216c40cebda7c9d8593c2a Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 19 Oct 2022 09:47:20 +0100 Subject: [PATCH] MDL-75805 admin: allow multiselect when editing site administrators. --- admin/roles/admins.php | 63 +++++++++++++------ .../classes/admins_existing_selector.php | 1 - .../classes/admins_potential_selector.php | 1 - lang/en/deprecated.txt | 2 + lang/en/role.php | 8 ++- 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/admin/roles/admins.php b/admin/roles/admins.php index a4b034a8aec..8dfb5de196c 100644 --- a/admin/roles/admins.php +++ b/admin/roles/admins.php @@ -25,8 +25,10 @@ require_once(__DIR__ . '/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); -$confirmadd = optional_param('confirmadd', 0, PARAM_INT); -$confirmdel = optional_param('confirmdel', 0, PARAM_INT); +$addusersaction = optional_param('add', false, PARAM_BOOL); +$addusers = optional_param('addusers', '', PARAM_SEQUENCE); +$removeusersaction = optional_param('remove', false, PARAM_BOOL); +$removeusers = optional_param('removeusers', '', PARAM_SEQUENCE); $PAGE->set_url('/admin/roles/admins.php'); @@ -38,33 +40,47 @@ if (!is_siteadmin()) { $admisselector = new core_role_admins_existing_selector(); $potentialadmisselector = new core_role_admins_potential_selector(); -if (optional_param('add', false, PARAM_BOOL) and confirm_sesskey()) { +if ($addusersaction) { if ($userstoadd = $potentialadmisselector->get_selected_users()) { - $user = reset($userstoadd); - $username = $potentialadmisselector->output_user($user); + $usernames = array_map(static function(stdClass $user) use ($potentialadmisselector): string { + return $potentialadmisselector->output_user($user); + }, $userstoadd); + + $userids = implode(',', array_keys($usernames)); + echo $OUTPUT->header(); - $yesurl = new moodle_url('/admin/roles/admins.php', array('confirmadd'=>$user->id, 'sesskey'=>sesskey())); - echo $OUTPUT->confirm(get_string('confirmaddadmin', 'core_role', $username), $yesurl, $PAGE->url); + echo $OUTPUT->confirm(get_string('confirmaddadmins', 'core_role') . html_writer::alist($usernames), + new moodle_url('/admin/roles/admins.php', ['addusers' => $userids, 'sesskey' => sesskey()]), $PAGE->url); echo $OUTPUT->footer(); die; } -} else if (optional_param('remove', false, PARAM_BOOL) and confirm_sesskey()) { +} else if ($removeusersaction) { if ($userstoremove = $admisselector->get_selected_users()) { - $user = reset($userstoremove); - if ($USER->id == $user->id) { - // Can not remove self. - } else { - $username = $admisselector->output_user($user); + + // Can not remove self. + $userstoremove = array_filter($userstoremove, static function(int $userid): bool { + global $USER; + return $userid != $USER->id; + }, ARRAY_FILTER_USE_KEY); + + if ($userstoremove) { + $usernames = array_map(static function(stdClass $user) use ($admisselector): string { + return $admisselector->output_user($user); + }, $userstoremove); + + $userids = implode(',', array_keys($usernames)); + echo $OUTPUT->header(); - $yesurl = new moodle_url('/admin/roles/admins.php', array('confirmdel'=>$user->id, 'sesskey'=>sesskey())); - echo $OUTPUT->confirm(get_string('confirmdeladmin', 'core_role', $username), $yesurl, $PAGE->url); + echo $OUTPUT->confirm(get_string('confirmremoveadmins', 'core_role') . html_writer::alist($usernames), + new moodle_url('/admin/roles/admins.php', ['removeusers' => $userids, 'sesskey' => sesskey()]), $PAGE->url); echo $OUTPUT->footer(); die; } } -} else if (optional_param('main', false, PARAM_BOOL) and confirm_sesskey()) { +} else if (optional_param('main', false, PARAM_BOOL) && confirm_sesskey()) { + // Setting main administrator will choose the first selected user in the case of multiple selections. if ($newmain = $admisselector->get_selected_users()) { $newmain = reset($newmain); $newmain = $newmain->id; @@ -91,7 +107,7 @@ if (optional_param('add', false, PARAM_BOOL) and confirm_sesskey()) { } } -} else if ($confirmadd and confirm_sesskey()) { +} else if ($addusers && confirm_sesskey()) { $admins = array(); foreach (explode(',', $CFG->siteadmins) as $admin) { $admin = (int)$admin; @@ -102,7 +118,9 @@ if (optional_param('add', false, PARAM_BOOL) and confirm_sesskey()) { $logstringold = implode(', ', $admins); - $admins[$confirmadd] = $confirmadd; + foreach (explode(',', $addusers) as $userid) { + $admins[$userid] = $userid; + } $logstringnew = implode(', ', $admins); @@ -111,7 +129,7 @@ if (optional_param('add', false, PARAM_BOOL) and confirm_sesskey()) { redirect($PAGE->url); -} else if ($confirmdel and confirm_sesskey() and $confirmdel != $USER->id) { +} else if ($removeusers && confirm_sesskey()) { $admins = array(); foreach (explode(',', $CFG->siteadmins) as $admin) { $admin = (int)$admin; @@ -122,7 +140,12 @@ if (optional_param('add', false, PARAM_BOOL) and confirm_sesskey()) { $logstringold = implode(', ', $admins); - unset($admins[$confirmdel]); + // Can not remove self. + foreach (explode(',', $removeusers) as $userid) { + if ($userid != $USER->id) { + unset($admins[$userid]); + } + } $logstringnew = implode(', ', $admins); diff --git a/admin/roles/classes/admins_existing_selector.php b/admin/roles/classes/admins_existing_selector.php index 722dc45bcca..531887f0fb6 100644 --- a/admin/roles/classes/admins_existing_selector.php +++ b/admin/roles/classes/admins_existing_selector.php @@ -37,7 +37,6 @@ class core_role_admins_existing_selector extends user_selector_base { if (is_null($name)) { $name = 'removeselect'; } - $options['multiselect'] = false; $options['includecustomfields'] = true; parent::__construct($name, $options); } diff --git a/admin/roles/classes/admins_potential_selector.php b/admin/roles/classes/admins_potential_selector.php index 9d44cad91fa..183dde81b6f 100644 --- a/admin/roles/classes/admins_potential_selector.php +++ b/admin/roles/classes/admins_potential_selector.php @@ -38,7 +38,6 @@ class core_role_admins_potential_selector extends user_selector_base { if (is_null($name)) { $name = 'addselect'; } - $options['multiselect'] = false; $options['includecustomfields'] = true; $options['exclude'] = explode(',', $CFG->siteadmins); parent::__construct($name, $options); diff --git a/lang/en/deprecated.txt b/lang/en/deprecated.txt index eb39eddda20..7eb397a7b93 100644 --- a/lang/en/deprecated.txt +++ b/lang/en/deprecated.txt @@ -71,3 +71,5 @@ match,core_user matchofthefollowing,core_user placeholdertypeorselect,core_user selectfiltertype,core_user +confirmaddadmin,core_role +confirmdeladmin,core_role diff --git a/lang/en/role.php b/lang/en/role.php index 994768db8fc..d96e43813a7 100644 --- a/lang/en/role.php +++ b/lang/en/role.php @@ -142,8 +142,8 @@ $string['competency:usercompetencyview'] = 'View a user competency'; $string['competency:userevidencemanage'] = 'Manage evidence of prior learning'; $string['competency:userevidencemanageown'] = 'Manage own evidence of prior learning'; $string['competency:userevidenceview'] = 'View evidence of prior learning of a user'; -$string['confirmaddadmin'] = 'Do you really want to add user {$a} as new site administrator?'; -$string['confirmdeladmin'] = 'Do you really want to remove user {$a} from the list of site administrators?'; +$string['confirmaddadmins'] = 'Are you sure you want to add the following users to the list of site administrators?'; +$string['confirmremoveadmins'] = 'Are you sure you want to remove the following users from the list of site administrators?'; $string['confirmroleprevent'] = 'Do you really want to remove "{$a->role}" from the list of allowed roles for capability "{$a->cap}" in context "{$a->context}"?'; $string['confirmroleunprohibit'] = 'Do you really want to remove "{$a->role}" from the list of prohibited roles for capability "{$a->cap}" in context "{$a->context}"?'; $string['confirmunassign'] = 'Are you sure you wish to remove this role from this user?'; @@ -519,3 +519,7 @@ $string['privacy:metadata:role_capabilities:tableexplanation'] = 'The capabiliti $string['privacy:metadata:role_capabilities:timemodified'] = 'The date when the capability was created or modified.'; $string['privacy:metadata:role_cohortroles'] = 'Roles to cohort'; $string['course:togglecompletion'] = 'Manually mark activities as complete'; + +// Deprecated since Moodle 4.2. +$string['confirmaddadmin'] = 'Do you really want to add user {$a} as new site administrator?'; +$string['confirmdeladmin'] = 'Do you really want to remove user {$a} from the list of site administrators?';