Merge branch 'MDL-57527-master' of git://github.com/danpoltawski/moodle
[moodle.git] / admin / user.php
blob42665249adc612215a5b75c095e34f640d00b642
1 <?php
3 require_once('../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
5 require_once($CFG->libdir.'/authlib.php');
6 require_once($CFG->dirroot.'/user/filters/lib.php');
7 require_once($CFG->dirroot.'/user/lib.php');
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);
12 $sort = optional_param('sort', 'name', PARAM_ALPHANUM);
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
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)
19 $suspend = optional_param('suspend', 0, PARAM_INT);
20 $unsuspend = optional_param('unsuspend', 0, PARAM_INT);
21 $unlock = optional_param('unlock', 0, PARAM_INT);
23 admin_externalpage_setup('editusers');
25 $sitecontext = context_system::instance();
26 $site = get_site();
28 if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
29 print_error('nopermissions', 'error', '', 'edit/delete users');
32 $stredit = get_string('edit');
33 $strdelete = get_string('delete');
34 $strdeletecheck = get_string('deletecheck');
35 $strshowallusers = get_string('showallusers');
36 $strsuspend = get_string('suspenduser', 'admin');
37 $strunsuspend = get_string('unsuspenduser', 'admin');
38 $strunlock = get_string('unlockaccount', 'admin');
39 $strconfirm = get_string('confirm');
41 if (empty($CFG->loginhttps)) {
42 $securewwwroot = $CFG->wwwroot;
43 } else {
44 $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
47 $returnurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage, 'page'=>$page));
49 // The $user variable is also used outside of these if statements.
50 $user = null;
51 if ($confirmuser and confirm_sesskey()) {
52 require_capability('moodle/user:update', $sitecontext);
53 if (!$user = $DB->get_record('user', array('id'=>$confirmuser, 'mnethostid'=>$CFG->mnet_localhost_id))) {
54 print_error('nousers');
57 $auth = get_auth_plugin($user->auth);
59 $result = $auth->user_confirm($user->username, $user->secret);
61 if ($result == AUTH_CONFIRM_OK or $result == AUTH_CONFIRM_ALREADY) {
62 redirect($returnurl);
63 } else {
64 echo $OUTPUT->header();
65 redirect($returnurl, get_string('usernotconfirmed', '', fullname($user, true)));
68 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
69 require_capability('moodle/user:delete', $sitecontext);
71 $user = $DB->get_record('user', array('id'=>$delete, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
73 if ($user->deleted) {
74 print_error('usernotdeleteddeleted', 'error');
76 if (is_siteadmin($user->id)) {
77 print_error('useradminodelete', 'error');
80 if ($confirm != md5($delete)) {
81 echo $OUTPUT->header();
82 $fullname = fullname($user, true);
83 echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
85 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
86 $deleteurl = new moodle_url($returnurl, $optionsyes);
87 $deletebutton = new single_button($deleteurl, get_string('delete'), 'post');
89 echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), $deletebutton, $returnurl);
90 echo $OUTPUT->footer();
91 die;
92 } else if (data_submitted()) {
93 if (delete_user($user)) {
94 \core\session\manager::gc(); // Remove stale sessions.
95 redirect($returnurl);
96 } else {
97 \core\session\manager::gc(); // Remove stale sessions.
98 echo $OUTPUT->header();
99 echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
102 } else if ($acl and confirm_sesskey()) {
103 if (!has_capability('moodle/user:update', $sitecontext)) {
104 print_error('nopermissions', 'error', '', 'modify the NMET access control list');
106 if (!$user = $DB->get_record('user', array('id'=>$acl))) {
107 print_error('nousers', 'error');
109 if (!is_mnet_remote_user($user)) {
110 print_error('usermustbemnet', 'error');
112 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
113 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
114 print_error('invalidaccessparameter', 'error');
116 $aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid));
117 if (empty($aclrecord)) {
118 $aclrecord = new stdClass();
119 $aclrecord->mnet_host_id = $user->mnethostid;
120 $aclrecord->username = $user->username;
121 $aclrecord->accessctrl = $accessctrl;
122 $DB->insert_record('mnet_sso_access_control', $aclrecord);
123 } else {
124 $aclrecord->accessctrl = $accessctrl;
125 $DB->update_record('mnet_sso_access_control', $aclrecord);
127 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
128 redirect($returnurl);
130 } else if ($suspend and confirm_sesskey()) {
131 require_capability('moodle/user:update', $sitecontext);
133 if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
134 if (!is_siteadmin($user) and $USER->id != $user->id and $user->suspended != 1) {
135 $user->suspended = 1;
136 // Force logout.
137 \core\session\manager::kill_user_sessions($user->id);
138 user_update_user($user, false);
141 redirect($returnurl);
143 } else if ($unsuspend and confirm_sesskey()) {
144 require_capability('moodle/user:update', $sitecontext);
146 if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
147 if ($user->suspended != 0) {
148 $user->suspended = 0;
149 user_update_user($user, false);
152 redirect($returnurl);
154 } else if ($unlock and confirm_sesskey()) {
155 require_capability('moodle/user:update', $sitecontext);
157 if ($user = $DB->get_record('user', array('id'=>$unlock, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
158 login_unlock_account($user);
160 redirect($returnurl);
163 // create the user filter form
164 $ufiltering = new user_filtering();
165 echo $OUTPUT->header();
167 // Carry on with the user listing
168 $context = context_system::instance();
169 $extracolumns = get_extra_user_fields($context);
170 // Get all user name fields as an array.
171 $allusernamefields = get_all_user_name_fields(false, null, null, null, true);
172 $columns = array_merge($allusernamefields, $extracolumns, array('city', 'country', 'lastaccess'));
174 foreach ($columns as $column) {
175 $string[$column] = get_user_field_name($column);
176 if ($sort != $column) {
177 $columnicon = "";
178 if ($column == "lastaccess") {
179 $columndir = "DESC";
180 } else {
181 $columndir = "ASC";
183 } else {
184 $columndir = $dir == "ASC" ? "DESC":"ASC";
185 if ($column == "lastaccess") {
186 $columnicon = ($dir == "ASC") ? "sort_desc" : "sort_asc";
187 } else {
188 $columnicon = ($dir == "ASC") ? "sort_asc" : "sort_desc";
190 $columnicon = "<img class='iconsort' src=\"" . $OUTPUT->pix_url('t/' . $columnicon) . "\" alt=\"\" />";
193 $$column = "<a href=\"user.php?sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
196 // We need to check that alternativefullnameformat is not set to '' or language.
197 // We don't need to check the fullnamedisplay setting here as the fullname function call further down has
198 // the override parameter set to true.
199 $fullnamesetting = $CFG->alternativefullnameformat;
200 // If we are using language or it is empty, then retrieve the default user names of just 'firstname' and 'lastname'.
201 if ($fullnamesetting == 'language' || empty($fullnamesetting)) {
202 // Set $a variables to return 'firstname' and 'lastname'.
203 $a = new stdClass();
204 $a->firstname = 'firstname';
205 $a->lastname = 'lastname';
206 // Getting the fullname display will ensure that the order in the language file is maintained.
207 $fullnamesetting = get_string('fullnamedisplay', null, $a);
210 // Order in string will ensure that the name columns are in the correct order.
211 $usernames = order_in_string($allusernamefields, $fullnamesetting);
212 $fullnamedisplay = array();
213 foreach ($usernames as $name) {
214 // Use the link from $$column for sorting on the user's name.
215 $fullnamedisplay[] = ${$name};
217 // All of the names are in one column. Put them into a string and separate them with a /.
218 $fullnamedisplay = implode(' / ', $fullnamedisplay);
219 // If $sort = name then it is the default for the setting and we should use the first name to sort by.
220 if ($sort == "name") {
221 // Use the first item in the array.
222 $sort = reset($usernames);
225 list($extrasql, $params) = $ufiltering->get_sql_filter();
226 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
227 $extrasql, $params, $context);
228 $usercount = get_users(false);
229 $usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
231 if ($extrasql !== '') {
232 echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
233 $usercount = $usersearchcount;
234 } else {
235 echo $OUTPUT->heading("$usercount ".get_string('users'));
238 $strall = get_string('all');
240 $baseurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
241 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
243 flush();
246 if (!$users) {
247 $match = array();
248 echo $OUTPUT->heading(get_string('nousersfound'));
250 $table = NULL;
252 } else {
254 $countries = get_string_manager()->get_list_of_countries(false);
255 if (empty($mnethosts)) {
256 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
259 foreach ($users as $key => $user) {
260 if (isset($countries[$user->country])) {
261 $users[$key]->country = $countries[$user->country];
264 if ($sort == "country") { // Need to resort by full country name, not code
265 foreach ($users as $user) {
266 $susers[$user->id] = $user->country;
268 asort($susers);
269 foreach ($susers as $key => $value) {
270 $nusers[] = $users[$key];
272 $users = $nusers;
275 $table = new html_table();
276 $table->head = array ();
277 $table->colclasses = array();
278 $table->head[] = $fullnamedisplay;
279 $table->attributes['class'] = 'admintable generaltable';
280 foreach ($extracolumns as $field) {
281 $table->head[] = ${$field};
283 $table->head[] = $city;
284 $table->head[] = $country;
285 $table->head[] = $lastaccess;
286 $table->head[] = get_string('edit');
287 $table->colclasses[] = 'centeralign';
288 $table->head[] = "";
289 $table->colclasses[] = 'centeralign';
291 $table->id = "users";
292 foreach ($users as $user) {
293 $buttons = array();
294 $lastcolumn = '';
296 // delete button
297 if (has_capability('moodle/user:delete', $sitecontext)) {
298 if (is_mnet_remote_user($user) or $user->id == $USER->id or is_siteadmin($user)) {
299 // no deleting of self, mnet accounts or admins allowed
300 } else {
301 $buttons[] = html_writer::link(new moodle_url($returnurl, array('delete'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'alt'=>$strdelete, 'class'=>'iconsmall')), array('title'=>$strdelete));
305 // suspend button
306 if (has_capability('moodle/user:update', $sitecontext)) {
307 if (is_mnet_remote_user($user)) {
308 // mnet users have special access control, they can not be deleted the standard way or suspended
309 $accessctrl = 'allow';
310 if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid))) {
311 $accessctrl = $acl->accessctrl;
313 $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
314 $buttons[] = " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey=".sesskey()."\">".get_string($changeaccessto, 'mnet') . " access</a>)";
316 } else {
317 if ($user->suspended) {
318 $buttons[] = html_writer::link(new moodle_url($returnurl, array('unsuspend'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>$strunsuspend, 'class'=>'iconsmall')), array('title'=>$strunsuspend));
319 } else {
320 if ($user->id == $USER->id or is_siteadmin($user)) {
321 // no suspending of admins or self!
322 } else {
323 $buttons[] = html_writer::link(new moodle_url($returnurl, array('suspend'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/hide'), 'alt'=>$strsuspend, 'class'=>'iconsmall')), array('title'=>$strsuspend));
327 if (login_is_lockedout($user)) {
328 $buttons[] = html_writer::link(new moodle_url($returnurl, array('unlock'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/unlock'), 'alt'=>$strunlock, 'class'=>'iconsmall')), array('title'=>$strunlock));
333 // edit button
334 if (has_capability('moodle/user:update', $sitecontext)) {
335 // prevent editing of admins by non-admins
336 if (is_siteadmin($USER) or !is_siteadmin($user)) {
337 $buttons[] = html_writer::link(new moodle_url($securewwwroot.'/user/editadvanced.php', array('id'=>$user->id, 'course'=>$site->id)), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/edit'), 'alt'=>$stredit, 'class'=>'iconsmall')), array('title'=>$stredit));
341 // the last column - confirm or mnet info
342 if (is_mnet_remote_user($user)) {
343 // all mnet users are confirmed, let's print just the name of the host there
344 if (isset($mnethosts[$user->mnethostid])) {
345 $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid]->name;
346 } else {
347 $lastcolumn = get_string($accessctrl, 'mnet');
350 } else if ($user->confirmed == 0) {
351 if (has_capability('moodle/user:update', $sitecontext)) {
352 $lastcolumn = html_writer::link(new moodle_url($returnurl, array('confirmuser'=>$user->id, 'sesskey'=>sesskey())), $strconfirm);
353 } else {
354 $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
358 if ($user->lastaccess) {
359 $strlastaccess = format_time(time() - $user->lastaccess);
360 } else {
361 $strlastaccess = get_string('never');
363 $fullname = fullname($user, true);
365 $row = array ();
366 $row[] = "<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>";
367 foreach ($extracolumns as $field) {
368 $row[] = $user->{$field};
370 $row[] = $user->city;
371 $row[] = $user->country;
372 $row[] = $strlastaccess;
373 if ($user->suspended) {
374 foreach ($row as $k=>$v) {
375 $row[$k] = html_writer::tag('span', $v, array('class'=>'usersuspended'));
378 $row[] = implode(' ', $buttons);
379 $row[] = $lastcolumn;
380 $table->data[] = $row;
384 // add filters
385 $ufiltering->display_add();
386 $ufiltering->display_active();
388 if (!empty($table)) {
389 echo html_writer::start_tag('div', array('class'=>'no-overflow'));
390 echo html_writer::table($table);
391 echo html_writer::end_tag('div');
392 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
394 if (has_capability('moodle/user:create', $sitecontext)) {
395 $url = new moodle_url($securewwwroot . '/user/editadvanced.php', array('id' => -1));
396 echo $OUTPUT->single_button($url, get_string('addnewuser'), 'get');
399 echo $OUTPUT->footer();