MDL-59429 roles: Log add/remove admin users.
[moodle.git] / admin / roles / admins.php
CommitLineData
4f0c2d00 1<?php
4f0c2d00
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
d6c4544d 18 * Select site administrators.
4f0c2d00 19 *
bc7b53fb
PS
20 * @package core_role
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
4f0c2d00
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
d6c4544d 25require_once(__DIR__ . '/../../config.php');
bc7b53fb 26require_once($CFG->libdir.'/adminlib.php');
4f0c2d00
PS
27
28$confirmadd = optional_param('confirmadd', 0, PARAM_INT);
29$confirmdel = optional_param('confirmdel', 0, PARAM_INT);
30
31$PAGE->set_url('/admin/roles/admins.php');
32
33admin_externalpage_setup('admins');
34if (!is_siteadmin()) {
35 die;
36}
37
bc7b53fb 38$admisselector = new core_role_admins_existing_selector();
4f0c2d00
PS
39$admisselector->set_extra_fields(array('username', 'email'));
40
bc7b53fb 41$potentialadmisselector = new core_role_admins_potential_selector();
4f0c2d00
PS
42$potentialadmisselector->set_extra_fields(array('username', 'email'));
43
44if (optional_param('add', false, PARAM_BOOL) and confirm_sesskey()) {
45 if ($userstoadd = $potentialadmisselector->get_selected_users()) {
46 $user = reset($userstoadd);
47 $username = fullname($user) . " ($user->username, $user->email)";
48 echo $OUTPUT->header();
d6c4544d
PS
49 $yesurl = new moodle_url('/admin/roles/admins.php', array('confirmadd'=>$user->id, 'sesskey'=>sesskey()));
50 echo $OUTPUT->confirm(get_string('confirmaddadmin', 'core_role', $username), $yesurl, $PAGE->url);
4f0c2d00
PS
51 echo $OUTPUT->footer();
52 die;
53 }
54
55} else if (optional_param('remove', false, PARAM_BOOL) and confirm_sesskey()) {
56 if ($userstoremove = $admisselector->get_selected_users()) {
57 $user = reset($userstoremove);
58 if ($USER->id == $user->id) {
d6c4544d 59 // Can not remove self.
4f0c2d00
PS
60 } else {
61 $username = fullname($user) . " ($user->username, $user->email)";
62 echo $OUTPUT->header();
d6c4544d
PS
63 $yesurl = new moodle_url('/admin/roles/admins.php', array('confirmdel'=>$user->id, 'sesskey'=>sesskey()));
64 echo $OUTPUT->confirm(get_string('confirmdeladmin', 'core_role', $username), $yesurl, $PAGE->url);
4f0c2d00
PS
65 echo $OUTPUT->footer();
66 die;
67 }
68 }
69
bb6ccfa5
PS
70} else if (optional_param('main', false, PARAM_BOOL) and confirm_sesskey()) {
71 if ($newmain = $admisselector->get_selected_users()) {
72 $newmain = reset($newmain);
73 $newmain = $newmain->id;
74 $admins = array();
d6c4544d 75 foreach (explode(',', $CFG->siteadmins) as $admin) {
bb6ccfa5
PS
76 $admin = (int)$admin;
77 if ($admin) {
78 $admins[$admin] = $admin;
79 }
80 }
81
82 if (isset($admins[$newmain])) {
0be294c0
MH
83 $logstringold = implode(', ', $admins);
84
bb6ccfa5
PS
85 unset($admins[$newmain]);
86 array_unshift($admins, $newmain);
0be294c0
MH
87
88 $logstringnew = implode(', ', $admins);
89
bb6ccfa5 90 set_config('siteadmins', implode(',', $admins));
0be294c0
MH
91 add_to_config_log('siteadmins', $logstringold, $logstringnew, null);
92
bb6ccfa5
PS
93 redirect($PAGE->url);
94 }
95 }
96
4f0c2d00
PS
97} else if ($confirmadd and confirm_sesskey()) {
98 $admins = array();
d6c4544d 99 foreach (explode(',', $CFG->siteadmins) as $admin) {
4f0c2d00
PS
100 $admin = (int)$admin;
101 if ($admin) {
102 $admins[$admin] = $admin;
103 }
104 }
0be294c0
MH
105
106 $logstringold = implode(', ', $admins);
107
4f0c2d00 108 $admins[$confirmadd] = $confirmadd;
0be294c0
MH
109
110 $logstringnew = implode(', ', $admins);
111
4f0c2d00 112 set_config('siteadmins', implode(',', $admins));
0be294c0
MH
113 add_to_config_log('siteadmins', $logstringold, $logstringnew, 'core');
114
4f0c2d00
PS
115 redirect($PAGE->url);
116
117} else if ($confirmdel and confirm_sesskey() and $confirmdel != $USER->id) {
118 $admins = array();
d6c4544d 119 foreach (explode(',', $CFG->siteadmins) as $admin) {
4f0c2d00
PS
120 $admin = (int)$admin;
121 if ($admin) {
122 $admins[$admin] = $admin;
123 }
124 }
0be294c0
MH
125
126 $logstringold = implode(', ', $admins);
127
4f0c2d00 128 unset($admins[$confirmdel]);
0be294c0
MH
129
130 $logstringnew = implode(', ', $admins);
131
4f0c2d00 132 set_config('siteadmins', implode(',', $admins));
0be294c0
MH
133 add_to_config_log('siteadmins', $logstringold, $logstringnew, 'core');
134
4f0c2d00
PS
135 redirect($PAGE->url);
136}
137
d6c4544d 138// Print header.
4f0c2d00
PS
139echo $OUTPUT->header();
140?>
141
142<div id="addadmisform">
d6c4544d 143 <h3 class="main"><?php print_string('manageadmins', 'core_role'); ?></h3>
4f0c2d00
PS
144
145 <form id="assignform" method="post" action="<?php echo $PAGE->url ?>">
146 <div>
147 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
148
149 <table class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">
150 <tr>
151 <td id='existingcell'>
152 <p>
d6c4544d 153 <label for="removeselect"><?php print_string('existingadmins', 'core_role'); ?></label>
4f0c2d00
PS
154 </p>
155 <?php $admisselector->display(); ?>
156 </td>
d6c4544d 157 <td id="buttonscell">
4f0c2d00 158 <p class="arrow_button">
2f3f8e45
DW
159 <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>"
160 title="<?php print_string('add'); ?>" class="btn btn-secondary"/><br />
161 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>"
162 title="<?php print_string('remove'); ?>" class="btn btn-secondary"/><br />
163 <input name="main" id="main" type="submit" value="<?php echo get_string('mainadminset', 'core_role'); ?>"
164 title="<?php print_string('mainadminset', 'core_role'); ?>" class="btn btn-secondary"/>
4f0c2d00
PS
165 </p>
166 </td>
d6c4544d 167 <td id="potentialcell">
4f0c2d00
PS
168 <p>
169 <label for="addselect"><?php print_string('users'); ?></label>
170 </p>
171 <?php $potentialadmisselector->display(); ?>
172 </td>
173 </tr>
174 </table>
175 </div>
176 </form>
177</div>
178
179<?php
180
4f0c2d00 181echo $OUTPUT->footer();