MDL-75805 admin: allow multiselect when editing site administrators.

This commit is contained in:
Paul Holden 2022-10-19 09:47:20 +01:00
parent 40a89d8a9a
commit 253c041297
5 changed files with 51 additions and 24 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -71,3 +71,5 @@ match,core_user
matchofthefollowing,core_user
placeholdertypeorselect,core_user
selectfiltertype,core_user
confirmaddadmin,core_role
confirmdeladmin,core_role

View File

@ -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 <strong>{$a}</strong> as new site administrator?';
$string['confirmdeladmin'] = 'Do you really want to remove user <strong>{$a}</strong> 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 <strong>"{$a->role}"</strong> from the list of allowed roles for capability "{$a->cap}" in context "{$a->context}"?';
$string['confirmroleunprohibit'] = 'Do you really want to remove <strong>"{$a->role}"</strong> 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 <strong>{$a}</strong> as new site administrator?';
$string['confirmdeladmin'] = 'Do you really want to remove user <strong>{$a}</strong> from the list of site administrators?';