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
);
22 $resendemail = optional_param('resendemail', 0, PARAM_INT
);
24 admin_externalpage_setup('editusers');
26 $sitecontext = context_system
::instance();
29 if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
30 print_error('nopermissions', 'error', '', 'edit/delete users');
33 $stredit = get_string('edit');
34 $strdelete = get_string('delete');
35 $strdeletecheck = get_string('deletecheck');
36 $strshowallusers = get_string('showallusers');
37 $strsuspend = get_string('suspenduser', 'admin');
38 $strunsuspend = get_string('unsuspenduser', 'admin');
39 $strunlock = get_string('unlockaccount', 'admin');
40 $strconfirm = get_string('confirm');
41 $strresendemail = get_string('resendemail');
43 $returnurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage, 'page'=>$page));
45 // The $user variable is also used outside of these if statements.
47 if ($confirmuser and confirm_sesskey()) {
48 require_capability('moodle/user:update', $sitecontext);
49 if (!$user = $DB->get_record('user', array('id'=>$confirmuser, 'mnethostid'=>$CFG->mnet_localhost_id
))) {
50 print_error('nousers');
53 $auth = get_auth_plugin($user->auth
);
55 $result = $auth->user_confirm($user->username
, $user->secret
);
57 if ($result == AUTH_CONFIRM_OK
or $result == AUTH_CONFIRM_ALREADY
) {
60 echo $OUTPUT->header();
61 redirect($returnurl, get_string('usernotconfirmed', '', fullname($user, true)));
64 } else if ($resendemail && confirm_sesskey()) {
65 if (!$user = $DB->get_record('user', ['id' => $resendemail, 'mnethostid' => $CFG->mnet_localhost_id
, 'deleted' => 0])) {
66 print_error('nousers');
69 // Prevent spamming users who are already confirmed.
70 if ($user->confirmed
) {
71 print_error('alreadyconfirmed');
74 $returnmsg = get_string('emailconfirmsentsuccess');
75 $messagetype = \core\output\notification
::NOTIFY_SUCCESS
;
76 if (!send_confirmation_email($user)) {
77 $returnmsg = get_string('emailconfirmsentfailure');
78 $messagetype = \core\output\notification
::NOTIFY_ERROR
;
81 redirect($returnurl, $returnmsg, null, $messagetype);
82 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
83 require_capability('moodle/user:delete', $sitecontext);
85 $user = $DB->get_record('user', array('id'=>$delete, 'mnethostid'=>$CFG->mnet_localhost_id
), '*', MUST_EXIST
);
88 print_error('usernotdeleteddeleted', 'error');
90 if (is_siteadmin($user->id
)) {
91 print_error('useradminodelete', 'error');
94 if ($confirm != md5($delete)) {
95 echo $OUTPUT->header();
96 $fullname = fullname($user, true);
97 echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
99 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
100 $deleteurl = new moodle_url($returnurl, $optionsyes);
101 $deletebutton = new single_button($deleteurl, get_string('delete'), 'post');
103 echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), $deletebutton, $returnurl);
104 echo $OUTPUT->footer();
106 } else if (data_submitted()) {
107 if (delete_user($user)) {
108 \core\session\manager
::gc(); // Remove stale sessions.
109 redirect($returnurl);
111 \core\session\manager
::gc(); // Remove stale sessions.
112 echo $OUTPUT->header();
113 echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
116 } else if ($acl and confirm_sesskey()) {
117 if (!has_capability('moodle/user:update', $sitecontext)) {
118 print_error('nopermissions', 'error', '', 'modify the NMET access control list');
120 if (!$user = $DB->get_record('user', array('id'=>$acl))) {
121 print_error('nousers', 'error');
123 if (!is_mnet_remote_user($user)) {
124 print_error('usermustbemnet', 'error');
126 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA
));
127 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
128 print_error('invalidaccessparameter', 'error');
130 $aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username
, 'mnet_host_id'=>$user->mnethostid
));
131 if (empty($aclrecord)) {
132 $aclrecord = new stdClass();
133 $aclrecord->mnet_host_id
= $user->mnethostid
;
134 $aclrecord->username
= $user->username
;
135 $aclrecord->accessctrl
= $accessctrl;
136 $DB->insert_record('mnet_sso_access_control', $aclrecord);
138 $aclrecord->accessctrl
= $accessctrl;
139 $DB->update_record('mnet_sso_access_control', $aclrecord);
141 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
142 redirect($returnurl);
144 } else if ($suspend and confirm_sesskey()) {
145 require_capability('moodle/user:update', $sitecontext);
147 if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id
, 'deleted'=>0))) {
148 if (!is_siteadmin($user) and $USER->id
!= $user->id
and $user->suspended
!= 1) {
149 $user->suspended
= 1;
151 \core\session\manager
::kill_user_sessions($user->id
);
152 user_update_user($user, false);
155 redirect($returnurl);
157 } else if ($unsuspend and confirm_sesskey()) {
158 require_capability('moodle/user:update', $sitecontext);
160 if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id
, 'deleted'=>0))) {
161 if ($user->suspended
!= 0) {
162 $user->suspended
= 0;
163 user_update_user($user, false);
166 redirect($returnurl);
168 } else if ($unlock and confirm_sesskey()) {
169 require_capability('moodle/user:update', $sitecontext);
171 if ($user = $DB->get_record('user', array('id'=>$unlock, 'mnethostid'=>$CFG->mnet_localhost_id
, 'deleted'=>0))) {
172 login_unlock_account($user);
174 redirect($returnurl);
177 // create the user filter form
178 $ufiltering = new user_filtering();
179 echo $OUTPUT->header();
181 // Carry on with the user listing
182 $context = context_system
::instance();
183 // These columns are always shown in the users list.
184 $requiredcolumns = array('city', 'country', 'lastaccess');
185 // Extra columns containing the extra user fields, excluding the required columns (city and country, to be specific).
186 $extracolumns = get_extra_user_fields($context, $requiredcolumns);
187 // Get all user name fields as an array.
188 $allusernamefields = get_all_user_name_fields(false, null, null, null, true);
189 $columns = array_merge($allusernamefields, $extracolumns, $requiredcolumns);
191 foreach ($columns as $column) {
192 $string[$column] = get_user_field_name($column);
193 if ($sort != $column) {
195 if ($column == "lastaccess") {
201 $columndir = $dir == "ASC" ?
"DESC":"ASC";
202 if ($column == "lastaccess") {
203 $columnicon = ($dir == "ASC") ?
"sort_desc" : "sort_asc";
205 $columnicon = ($dir == "ASC") ?
"sort_asc" : "sort_desc";
207 $columnicon = $OUTPUT->pix_icon('t/' . $columnicon, get_string(strtolower($columndir)), 'core',
208 ['class' => 'iconsort']);
211 $
$column = "<a href=\"user.php?sort=$column&dir=$columndir\">".$string[$column]."</a>$columnicon";
214 // We need to check that alternativefullnameformat is not set to '' or language.
215 // We don't need to check the fullnamedisplay setting here as the fullname function call further down has
216 // the override parameter set to true.
217 $fullnamesetting = $CFG->alternativefullnameformat
;
218 // If we are using language or it is empty, then retrieve the default user names of just 'firstname' and 'lastname'.
219 if ($fullnamesetting == 'language' ||
empty($fullnamesetting)) {
220 // Set $a variables to return 'firstname' and 'lastname'.
222 $a->firstname
= 'firstname';
223 $a->lastname
= 'lastname';
224 // Getting the fullname display will ensure that the order in the language file is maintained.
225 $fullnamesetting = get_string('fullnamedisplay', null, $a);
228 // Order in string will ensure that the name columns are in the correct order.
229 $usernames = order_in_string($allusernamefields, $fullnamesetting);
230 $fullnamedisplay = array();
231 foreach ($usernames as $name) {
232 // Use the link from $$column for sorting on the user's name.
233 $fullnamedisplay[] = $
{$name};
235 // All of the names are in one column. Put them into a string and separate them with a /.
236 $fullnamedisplay = implode(' / ', $fullnamedisplay);
237 // If $sort = name then it is the default for the setting and we should use the first name to sort by.
238 if ($sort == "name") {
239 // Use the first item in the array.
240 $sort = reset($usernames);
243 list($extrasql, $params) = $ufiltering->get_sql_filter();
244 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
245 $extrasql, $params, $context);
246 $usercount = get_users(false);
247 $usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
249 if ($extrasql !== '') {
250 echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
251 $usercount = $usersearchcount;
253 echo $OUTPUT->heading("$usercount ".get_string('users'));
256 $strall = get_string('all');
258 $baseurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
259 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
266 echo $OUTPUT->heading(get_string('nousersfound'));
272 $countries = get_string_manager()->get_list_of_countries(true);
273 if (empty($mnethosts)) {
274 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
277 foreach ($users as $key => $user) {
278 if (isset($countries[$user->country
])) {
279 $users[$key]->country
= $countries[$user->country
];
282 if ($sort == "country") {
283 // Need to resort by full country name, not code.
284 foreach ($users as $user) {
285 $susers[$user->id
] = $user->country
;
287 // Sort by country name, according to $dir.
288 if ($dir === 'DESC') {
293 foreach ($susers as $key => $value) {
294 $nusers[] = $users[$key];
299 $table = new html_table();
300 $table->head
= array ();
301 $table->colclasses
= array();
302 $table->head
[] = $fullnamedisplay;
303 $table->attributes
['class'] = 'admintable generaltable table-sm';
304 foreach ($extracolumns as $field) {
305 $table->head
[] = $
{$field};
307 $table->head
[] = $city;
308 $table->head
[] = $country;
309 $table->head
[] = $lastaccess;
310 $table->head
[] = get_string('edit');
311 $table->colclasses
[] = 'centeralign';
313 $table->colclasses
[] = 'centeralign';
315 $table->id
= "users";
316 foreach ($users as $user) {
321 if (has_capability('moodle/user:delete', $sitecontext)) {
322 if (is_mnet_remote_user($user) or $user->id
== $USER->id
or is_siteadmin($user)) {
323 // no deleting of self, mnet accounts or admins allowed
325 $url = new moodle_url($returnurl, array('delete'=>$user->id
, 'sesskey'=>sesskey()));
326 $buttons[] = html_writer
::link($url, $OUTPUT->pix_icon('t/delete', $strdelete));
331 if (has_capability('moodle/user:update', $sitecontext)) {
332 if (is_mnet_remote_user($user)) {
333 // mnet users have special access control, they can not be deleted the standard way or suspended
334 $accessctrl = 'allow';
335 if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username
, 'mnet_host_id'=>$user->mnethostid
))) {
336 $accessctrl = $acl->accessctrl
;
338 $changeaccessto = ($accessctrl == 'deny' ?
'allow' : 'deny');
339 $buttons[] = " (<a href=\"?acl={$user->id}&accessctrl=$changeaccessto&sesskey=".sesskey()."\">".get_string($changeaccessto, 'mnet') . " access</a>)";
342 if ($user->suspended
) {
343 $url = new moodle_url($returnurl, array('unsuspend'=>$user->id
, 'sesskey'=>sesskey()));
344 $buttons[] = html_writer
::link($url, $OUTPUT->pix_icon('t/show', $strunsuspend));
346 if ($user->id
== $USER->id
or is_siteadmin($user)) {
347 // no suspending of admins or self!
349 $url = new moodle_url($returnurl, array('suspend'=>$user->id
, 'sesskey'=>sesskey()));
350 $buttons[] = html_writer
::link($url, $OUTPUT->pix_icon('t/hide', $strsuspend));
354 if (login_is_lockedout($user)) {
355 $url = new moodle_url($returnurl, array('unlock'=>$user->id
, 'sesskey'=>sesskey()));
356 $buttons[] = html_writer
::link($url, $OUTPUT->pix_icon('t/unlock', $strunlock));
362 if (has_capability('moodle/user:update', $sitecontext)) {
363 // prevent editing of admins by non-admins
364 if (is_siteadmin($USER) or !is_siteadmin($user)) {
365 $url = new moodle_url('/user/editadvanced.php', array('id'=>$user->id
, 'course'=>$site->id
));
366 $buttons[] = html_writer
::link($url, $OUTPUT->pix_icon('t/edit', $stredit));
370 // the last column - confirm or mnet info
371 if (is_mnet_remote_user($user)) {
372 // all mnet users are confirmed, let's print just the name of the host there
373 if (isset($mnethosts[$user->mnethostid
])) {
374 $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid
]->name
;
376 $lastcolumn = get_string($accessctrl, 'mnet');
379 } else if ($user->confirmed
== 0) {
380 if (has_capability('moodle/user:update', $sitecontext)) {
381 $lastcolumn = html_writer
::link(new moodle_url($returnurl, array('confirmuser'=>$user->id
, 'sesskey'=>sesskey())), $strconfirm);
383 $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
386 $lastcolumn .= ' | ' . html_writer
::link(new moodle_url($returnurl,
388 'resendemail' => $user->id
,
389 'sesskey' => sesskey()
394 if ($user->lastaccess
) {
395 $strlastaccess = format_time(time() - $user->lastaccess
);
397 $strlastaccess = get_string('never');
399 $fullname = fullname($user, true);
402 $row[] = "<a href=\"../user/view.php?id=$user->id&course=$site->id\">$fullname</a>";
403 foreach ($extracolumns as $field) {
404 $row[] = s($user->{$field});
406 $row[] = $user->city
;
407 $row[] = $user->country
;
408 $row[] = $strlastaccess;
409 if ($user->suspended
) {
410 foreach ($row as $k=>$v) {
411 $row[$k] = html_writer
::tag('span', $v, array('class'=>'usersuspended'));
414 $row[] = implode(' ', $buttons);
415 $row[] = $lastcolumn;
416 $table->data
[] = $row;
421 $ufiltering->display_add();
422 $ufiltering->display_active();
424 if (!empty($table)) {
425 echo html_writer
::start_tag('div', array('class'=>'no-overflow'));
426 echo html_writer
::table($table);
427 echo html_writer
::end_tag('div');
428 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
430 if (has_capability('moodle/user:create', $sitecontext)) {
431 $url = new moodle_url('/user/editadvanced.php', array('id' => -1));
432 echo $OUTPUT->single_button($url, get_string('addnewuser'), 'get');
435 echo $OUTPUT->footer();