Merge branch 'MDL-32350-master' of https://github.com/lucaboesch/moodle
[moodle.git] / admin / user.php
CommitLineData
0df0df23 1<?php
f9903ed0 2
4cd174bf 3 require_once('../config.php');
38d2d43b 4 require_once($CFG->libdir.'/adminlib.php');
b28247fe 5 require_once($CFG->libdir.'/authlib.php');
cd1edf9e 6 require_once($CFG->dirroot.'/user/filters/lib.php');
bb78e249 7 require_once($CFG->dirroot.'/user/lib.php');
7710da22 8
7710da22 9 $delete = optional_param('delete', 0, PARAM_INT);
10 $confirm = optional_param('confirm', '', PARAM_ALPHANUM); //md5 confirmation hash
11 $confirmuser = optional_param('confirmuser', 0, PARAM_INT);
1fdc0c6a 12 $sort = optional_param('sort', 'name', PARAM_ALPHANUM);
7710da22 13 $dir = optional_param('dir', 'ASC', PARAM_ALPHA);
14 $page = optional_param('page', 0, PARAM_INT);
15 $perpage = optional_param('perpage', 30, PARAM_INT); // how many per page
d14383e0 16 $ru = optional_param('ru', '2', PARAM_INT); // show remote users
17 $lu = optional_param('lu', '2', PARAM_INT); // show local users
18 $acl = optional_param('acl', '0', PARAM_INT); // id of user to tweak mnet ACL (requires $access)
4ad72c28
PS
19 $suspend = optional_param('suspend', 0, PARAM_INT);
20 $unsuspend = optional_param('unsuspend', 0, PARAM_INT);
b28247fe 21 $unlock = optional_param('unlock', 0, PARAM_INT);
1ae083e4 22
23 admin_externalpage_setup('editusers');
ad6226fb 24
bf006d2c 25 $sitecontext = context_system::instance();
ad6226fb 26 $site = get_site();
27
28 if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
8e9d88f2 29 print_error('nopermissions', 'error', '', 'edit/delete users');
bee4702d 30 }
0c30fdc9 31
ad6226fb 32 $stredit = get_string('edit');
33 $strdelete = get_string('delete');
34 $strdeletecheck = get_string('deletecheck');
ad6226fb 35 $strshowallusers = get_string('showallusers');
4ad72c28
PS
36 $strsuspend = get_string('suspenduser', 'admin');
37 $strunsuspend = get_string('unsuspenduser', 'admin');
b28247fe 38 $strunlock = get_string('unlockaccount', 'admin');
4ad72c28 39 $strconfirm = get_string('confirm');
f9903ed0 40
4ad72c28 41 $returnurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage, 'page'=>$page));
f9903ed0 42
95065006
AG
43 // The $user variable is also used outside of these if statements.
44 $user = null;
ad6226fb 45 if ($confirmuser and confirm_sesskey()) {
4ad72c28
PS
46 require_capability('moodle/user:update', $sitecontext);
47 if (!$user = $DB->get_record('user', array('id'=>$confirmuser, 'mnethostid'=>$CFG->mnet_localhost_id))) {
8e9d88f2 48 print_error('nousers');
df9c6677 49 }
0c30fdc9 50
84791c32 51 $auth = get_auth_plugin($user->auth);
0c30fdc9 52
b9a66360 53 $result = $auth->user_confirm($user->username, $user->secret);
84791c32 54
55 if ($result == AUTH_CONFIRM_OK or $result == AUTH_CONFIRM_ALREADY) {
19bae8cd 56 redirect($returnurl);
ad6226fb 57 } else {
61ef8f9f 58 echo $OUTPUT->header();
19bae8cd 59 redirect($returnurl, get_string('usernotconfirmed', '', fullname($user, true)));
f9903ed0 60 }
38d2d43b 61
ad6226fb 62 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
4f622c38 63 require_capability('moodle/user:delete', $sitecontext);
38d2d43b 64
4ad72c28 65 $user = $DB->get_record('user', array('id'=>$delete, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
38d2d43b 66
3009095b
AT
67 if ($user->deleted) {
68 print_error('usernotdeleteddeleted', 'error');
69 }
a26a1a5b 70 if (is_siteadmin($user->id)) {
4f622c38 71 print_error('useradminodelete', 'error');
0c30fdc9 72 }
f9903ed0 73
ad6226fb 74 if ($confirm != md5($delete)) {
61ef8f9f 75 echo $OUTPUT->header();
ad6226fb 76 $fullname = fullname($user, true);
2fff8846 77 echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
65377780 78
ad6226fb 79 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
65377780
AD
80 $deleteurl = new moodle_url($returnurl, $optionsyes);
81 $deletebutton = new single_button($deleteurl, get_string('delete'), 'post');
82
83 echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), $deletebutton, $returnurl);
73d6f52f 84 echo $OUTPUT->footer();
ad6226fb 85 die;
3009095b 86 } else if (data_submitted()) {
90afcf32 87 if (delete_user($user)) {
d79d5ac2 88 \core\session\manager::gc(); // Remove stale sessions.
19bae8cd 89 redirect($returnurl);
ad6226fb 90 } else {
d79d5ac2 91 \core\session\manager::gc(); // Remove stale sessions.
61ef8f9f 92 echo $OUTPUT->header();
8fbce1c8 93 echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
2b25f2a0 94 }
a3447e10 95 }
ad6226fb 96 } else if ($acl and confirm_sesskey()) {
4ad72c28 97 if (!has_capability('moodle/user:update', $sitecontext)) {
8e9d88f2 98 print_error('nopermissions', 'error', '', 'modify the NMET access control list');
4cd174bf 99 }
b9a66360 100 if (!$user = $DB->get_record('user', array('id'=>$acl))) {
8e9d88f2 101 print_error('nousers', 'error');
ad6226fb 102 }
103 if (!is_mnet_remote_user($user)) {
8e9d88f2 104 print_error('usermustbemnet', 'error');
ad6226fb 105 }
106 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
107 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
8e9d88f2 108 print_error('invalidaccessparameter', 'error');
ad6226fb 109 }
b9a66360 110 $aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid));
ad6226fb 111 if (empty($aclrecord)) {
a226a972 112 $aclrecord = new stdClass();
ad6226fb 113 $aclrecord->mnet_host_id = $user->mnethostid;
114 $aclrecord->username = $user->username;
115 $aclrecord->accessctrl = $accessctrl;
795a08ad 116 $DB->insert_record('mnet_sso_access_control', $aclrecord);
ad6226fb 117 } else {
118 $aclrecord->accessctrl = $accessctrl;
795a08ad 119 $DB->update_record('mnet_sso_access_control', $aclrecord);
ad6226fb 120 }
b9a66360 121 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
19bae8cd 122 redirect($returnurl);
4ad72c28
PS
123
124 } else if ($suspend and confirm_sesskey()) {
125 require_capability('moodle/user:update', $sitecontext);
126
127 if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
128 if (!is_siteadmin($user) and $USER->id != $user->id and $user->suspended != 1) {
129 $user->suspended = 1;
bb78e249 130 // Force logout.
d79d5ac2 131 \core\session\manager::kill_user_sessions($user->id);
bb78e249 132 user_update_user($user, false);
4ad72c28
PS
133 }
134 }
135 redirect($returnurl);
136
137 } else if ($unsuspend and confirm_sesskey()) {
138 require_capability('moodle/user:update', $sitecontext);
139
140 if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
141 if ($user->suspended != 0) {
142 $user->suspended = 0;
bb78e249 143 user_update_user($user, false);
4ad72c28
PS
144 }
145 }
146 redirect($returnurl);
b28247fe
PS
147
148 } else if ($unlock and confirm_sesskey()) {
149 require_capability('moodle/user:update', $sitecontext);
150
151 if ($user = $DB->get_record('user', array('id'=>$unlock, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
152 login_unlock_account($user);
153 }
154 redirect($returnurl);
ad6226fb 155 }
38d2d43b 156
cd1edf9e 157 // create the user filter form
158 $ufiltering = new user_filtering();
61ef8f9f 159 echo $OUTPUT->header();
20207b82 160
ad6226fb 161 // Carry on with the user listing
1fdc0c6a 162 $context = context_system::instance();
163 $extracolumns = get_extra_user_fields($context);
95065006
AG
164 // Get all user name fields as an array.
165 $allusernamefields = get_all_user_name_fields(false, null, null, null, true);
166 $columns = array_merge($allusernamefields, $extracolumns, array('city', 'country', 'lastaccess'));
b4a34829 167
ad6226fb 168 foreach ($columns as $column) {
1fdc0c6a 169 $string[$column] = get_user_field_name($column);
ad6226fb 170 if ($sort != $column) {
171 $columnicon = "";
172 if ($column == "lastaccess") {
173 $columndir = "DESC";
174 } else {
175 $columndir = "ASC";
d14383e0 176 }
ad6226fb 177 } else {
178 $columndir = $dir == "ASC" ? "DESC":"ASC";
179 if ($column == "lastaccess") {
06b7ed0a 180 $columnicon = ($dir == "ASC") ? "sort_desc" : "sort_asc";
d14383e0 181 } else {
06b7ed0a 182 $columnicon = ($dir == "ASC") ? "sort_asc" : "sort_desc";
d14383e0 183 }
2375f37c
DW
184 $columnicon = $OUTPUT->pix_icon('t/' . $columnicon, get_string(strtolower($columndir)), 'core',
185 ['class' => 'iconsort']);
ad6226fb 186
bb09fb11 187 }
cd1edf9e 188 $$column = "<a href=\"user.php?sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
ad6226fb 189 }
a3447e10 190
95065006
AG
191 // We need to check that alternativefullnameformat is not set to '' or language.
192 // We don't need to check the fullnamedisplay setting here as the fullname function call further down has
193 // the override parameter set to true.
194 $fullnamesetting = $CFG->alternativefullnameformat;
1fc2bb35 195 // If we are using language or it is empty, then retrieve the default user names of just 'firstname' and 'lastname'.
95065006 196 if ($fullnamesetting == 'language' || empty($fullnamesetting)) {
1fc2bb35
AG
197 // Set $a variables to return 'firstname' and 'lastname'.
198 $a = new stdClass();
199 $a->firstname = 'firstname';
200 $a->lastname = 'lastname';
201 // Getting the fullname display will ensure that the order in the language file is maintained.
202 $fullnamesetting = get_string('fullnamedisplay', null, $a);
95065006
AG
203 }
204
205 // Order in string will ensure that the name columns are in the correct order.
206 $usernames = order_in_string($allusernamefields, $fullnamesetting);
207 $fullnamedisplay = array();
208 foreach ($usernames as $name) {
209 // Use the link from $$column for sorting on the user's name.
210 $fullnamedisplay[] = ${$name};
211 }
212 // All of the names are in one column. Put them into a string and separate them with a /.
213 $fullnamedisplay = implode(' / ', $fullnamedisplay);
214 // If $sort = name then it is the default for the setting and we should use the first name to sort by.
215 if ($sort == "name") {
216 // Use the first item in the array.
217 $sort = reset($usernames);
ad6226fb 218 }
cd1edf9e 219
624a690b 220 list($extrasql, $params) = $ufiltering->get_sql_filter();
1fdc0c6a 221 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
222 $extrasql, $params, $context);
ad6226fb 223 $usercount = get_users(false);
a019ffcd 224 $usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
d578afc8 225
cd1edf9e 226 if ($extrasql !== '') {
2fff8846 227 echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
ad6226fb 228 $usercount = $usersearchcount;
229 } else {
2fff8846 230 echo $OUTPUT->heading("$usercount ".get_string('users'));
ad6226fb 231 }
d578afc8 232
ad6226fb 233 $strall = get_string('all');
20207b82 234
4ad72c28 235 $baseurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
929d7a83 236 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
6c2ae454 237
ad6226fb 238 flush();
6c2ae454 239
6c2ae454 240
ad6226fb 241 if (!$users) {
242 $match = array();
2fff8846 243 echo $OUTPUT->heading(get_string('nousersfound'));
488acd1b 244
ad6226fb 245 $table = NULL;
a3447e10 246
ad6226fb 247 } else {
ad81212e 248
0aa759b0 249 $countries = get_string_manager()->get_list_of_countries(false);
ad6226fb 250 if (empty($mnethosts)) {
b9a66360 251 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
ad6226fb 252 }
5833a6c8 253
ad6226fb 254 foreach ($users as $key => $user) {
d93cafea 255 if (isset($countries[$user->country])) {
ad6226fb 256 $users[$key]->country = $countries[$user->country];
4a6a574f 257 }
ad6226fb 258 }
fc07f6ad
LB
259 if ($sort == "country") {
260 // Need to resort by full country name, not code.
ad6226fb 261 foreach ($users as $user) {
262 $susers[$user->id] = $user->country;
ad81212e 263 }
fc07f6ad
LB
264 // Sort by country name, according to $dir.
265 if ($dir === 'DESC') {
266 arsort($susers);
267 } else {
268 asort($susers);
269 }
ad6226fb 270 foreach ($susers as $key => $value) {
271 $nusers[] = $users[$key];
ad81212e 272 }
ad6226fb 273 $users = $nusers;
274 }
488acd1b 275
414a4a91 276 $table = new html_table();
1fdc0c6a 277 $table->head = array ();
6fc61f2d 278 $table->colclasses = array();
1fdc0c6a 279 $table->head[] = $fullnamedisplay;
6fc61f2d 280 $table->attributes['class'] = 'admintable generaltable';
1fdc0c6a 281 foreach ($extracolumns as $field) {
282 $table->head[] = ${$field};
1fdc0c6a 283 }
284 $table->head[] = $city;
1fdc0c6a 285 $table->head[] = $country;
1fdc0c6a 286 $table->head[] = $lastaccess;
4ad72c28 287 $table->head[] = get_string('edit');
6fc61f2d 288 $table->colclasses[] = 'centeralign';
1fdc0c6a 289 $table->head[] = "";
6fc61f2d 290 $table->colclasses[] = 'centeralign';
1fdc0c6a 291
6fc61f2d 292 $table->id = "users";
ad6226fb 293 foreach ($users as $user) {
4ad72c28
PS
294 $buttons = array();
295 $lastcolumn = '';
296
297 // delete button
298 if (has_capability('moodle/user:delete', $sitecontext)) {
299 if (is_mnet_remote_user($user) or $user->id == $USER->id or is_siteadmin($user)) {
300 // no deleting of self, mnet accounts or admins allowed
ad6226fb 301 } else {
663640f5
DW
302 $url = new moodle_url($returnurl, array('delete'=>$user->id, 'sesskey'=>sesskey()));
303 $buttons[] = html_writer::link($url, $OUTPUT->pix_icon('t/delete', $strdelete));
488acd1b 304 }
c750592a 305 }
488acd1b 306
4ad72c28
PS
307 // suspend button
308 if (has_capability('moodle/user:update', $sitecontext)) {
309 if (is_mnet_remote_user($user)) {
310 // mnet users have special access control, they can not be deleted the standard way or suspended
311 $accessctrl = 'allow';
312 if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid))) {
313 $accessctrl = $acl->accessctrl;
314 }
315 $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
316 $buttons[] = " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey=".sesskey()."\">".get_string($changeaccessto, 'mnet') . " access</a>)";
317
488acd1b 318 } else {
4ad72c28 319 if ($user->suspended) {
663640f5
DW
320 $url = new moodle_url($returnurl, array('unsuspend'=>$user->id, 'sesskey'=>sesskey()));
321 $buttons[] = html_writer::link($url, $OUTPUT->pix_icon('t/show', $strunsuspend));
4ad72c28
PS
322 } else {
323 if ($user->id == $USER->id or is_siteadmin($user)) {
324 // no suspending of admins or self!
325 } else {
663640f5
DW
326 $url = new moodle_url($returnurl, array('suspend'=>$user->id, 'sesskey'=>sesskey()));
327 $buttons[] = html_writer::link($url, $OUTPUT->pix_icon('t/hide', $strsuspend));
4ad72c28
PS
328 }
329 }
330
b28247fe 331 if (login_is_lockedout($user)) {
663640f5
DW
332 $url = new moodle_url($returnurl, array('unlock'=>$user->id, 'sesskey'=>sesskey()));
333 $buttons[] = html_writer::link($url, $OUTPUT->pix_icon('t/unlock', $strunlock));
b28247fe 334 }
488acd1b 335 }
4ad72c28
PS
336 }
337
338 // edit button
339 if (has_capability('moodle/user:update', $sitecontext)) {
340 // prevent editing of admins by non-admins
341 if (is_siteadmin($USER) or !is_siteadmin($user)) {
7eb50b32 342 $url = new moodle_url('/user/editadvanced.php', array('id'=>$user->id, 'course'=>$site->id));
663640f5 343 $buttons[] = html_writer::link($url, $OUTPUT->pix_icon('t/edit', $stredit));
d14383e0 344 }
ad6226fb 345 }
d14383e0 346
4ad72c28 347 // the last column - confirm or mnet info
ad6226fb 348 if (is_mnet_remote_user($user)) {
4ad72c28 349 // all mnet users are confirmed, let's print just the name of the host there
34d4b268 350 if (isset($mnethosts[$user->mnethostid])) {
4ad72c28
PS
351 $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid]->name;
352 } else {
353 $lastcolumn = get_string($accessctrl, 'mnet');
354 }
355
356 } else if ($user->confirmed == 0) {
357 if (has_capability('moodle/user:update', $sitecontext)) {
358 $lastcolumn = html_writer::link(new moodle_url($returnurl, array('confirmuser'=>$user->id, 'sesskey'=>sesskey())), $strconfirm);
359 } else {
360 $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
34d4b268 361 }
8516afd5 362 }
2447921f 363
ad6226fb 364 if ($user->lastaccess) {
365 $strlastaccess = format_time(time() - $user->lastaccess);
7340c743 366 } else {
ad6226fb 367 $strlastaccess = get_string('never');
7340c743 368 }
ad6226fb 369 $fullname = fullname($user, true);
370
1fdc0c6a 371 $row = array ();
372 $row[] = "<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>";
373 foreach ($extracolumns as $field) {
374 $row[] = $user->{$field};
375 }
376 $row[] = $user->city;
377 $row[] = $user->country;
378 $row[] = $strlastaccess;
4ad72c28
PS
379 if ($user->suspended) {
380 foreach ($row as $k=>$v) {
381 $row[$k] = html_writer::tag('span', $v, array('class'=>'usersuspended'));
382 }
383 }
384 $row[] = implode(' ', $buttons);
385 $row[] = $lastcolumn;
1fdc0c6a 386 $table->data[] = $row;
d14383e0 387 }
ad6226fb 388 }
d14383e0 389
cd1edf9e 390 // add filters
391 $ufiltering->display_add();
392 $ufiltering->display_active();
ad6226fb 393
ad6226fb 394 if (!empty($table)) {
82d305fa 395 echo html_writer::start_tag('div', array('class'=>'no-overflow'));
16be8974 396 echo html_writer::table($table);
82d305fa 397 echo html_writer::end_tag('div');
929d7a83 398 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
8d29ac98
BB
399 }
400 if (has_capability('moodle/user:create', $sitecontext)) {
7eb50b32 401 $url = new moodle_url('/user/editadvanced.php', array('id' => -1));
8d29ac98 402 echo $OUTPUT->single_button($url, get_string('addnewuser'), 'get');
ad6226fb 403 }
6c2ae454 404
73d6f52f 405 echo $OUTPUT->footer();