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();
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 $returnurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage, 'page'=>$page));
43 // The $user variable is also used outside of these if statements.
45 if ($confirmuser and confirm_sesskey()) {
46 require_capability('moodle/user:update', $sitecontext);
47 if (!$user = $DB->get_record('user', array('id'=>$confirmuser, 'mnethostid'=>$CFG->mnet_localhost_id
))) {
48 print_error('nousers');
51 $auth = get_auth_plugin($user->auth
);
53 $result = $auth->user_confirm($user->username
, $user->secret
);
55 if ($result == AUTH_CONFIRM_OK
or $result == AUTH_CONFIRM_ALREADY
) {
58 echo $OUTPUT->header();
59 redirect($returnurl, get_string('usernotconfirmed', '', fullname($user, true)));
62 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
63 require_capability('moodle/user:delete', $sitecontext);
65 $user = $DB->get_record('user', array('id'=>$delete, 'mnethostid'=>$CFG->mnet_localhost_id
), '*', MUST_EXIST
);
68 print_error('usernotdeleteddeleted', 'error');
70 if (is_siteadmin($user->id
)) {
71 print_error('useradminodelete', 'error');
74 if ($confirm != md5($delete)) {
75 echo $OUTPUT->header();
76 $fullname = fullname($user, true);
77 echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
79 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
80 $deleteurl = new moodle_url($returnurl, $optionsyes);
81 $deletebutton = new single_button($deleteurl, get_string('delete'), 'post');
83 echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), $deletebutton, $returnurl);
84 echo $OUTPUT->footer();
86 } else if (data_submitted()) {
87 if (delete_user($user)) {
88 \core\session\manager
::gc(); // Remove stale sessions.
91 \core\session\manager
::gc(); // Remove stale sessions.
92 echo $OUTPUT->header();
93 echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
96 } else if ($acl and confirm_sesskey()) {
97 if (!has_capability('moodle/user:update', $sitecontext)) {
98 print_error('nopermissions', 'error', '', 'modify the NMET access control list');
100 if (!$user = $DB->get_record('user', array('id'=>$acl))) {
101 print_error('nousers', 'error');
103 if (!is_mnet_remote_user($user)) {
104 print_error('usermustbemnet', 'error');
106 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA
));
107 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
108 print_error('invalidaccessparameter', 'error');
110 $aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username
, 'mnet_host_id'=>$user->mnethostid
));
111 if (empty($aclrecord)) {
112 $aclrecord = new stdClass();
113 $aclrecord->mnet_host_id
= $user->mnethostid
;
114 $aclrecord->username
= $user->username
;
115 $aclrecord->accessctrl
= $accessctrl;
116 $DB->insert_record('mnet_sso_access_control', $aclrecord);
118 $aclrecord->accessctrl
= $accessctrl;
119 $DB->update_record('mnet_sso_access_control', $aclrecord);
121 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
122 redirect($returnurl);
124 } else if ($suspend and confirm_sesskey()) {
125 require_capability('moodle/user:update', $sitecontext);
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;
131 \core\session\manager
::kill_user_sessions($user->id
);
132 user_update_user($user, false);
135 redirect($returnurl);
137 } else if ($unsuspend and confirm_sesskey()) {
138 require_capability('moodle/user:update', $sitecontext);
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;
143 user_update_user($user, false);
146 redirect($returnurl);
148 } else if ($unlock and confirm_sesskey()) {
149 require_capability('moodle/user:update', $sitecontext);
151 if ($user = $DB->get_record('user', array('id'=>$unlock, 'mnethostid'=>$CFG->mnet_localhost_id
, 'deleted'=>0))) {
152 login_unlock_account($user);
154 redirect($returnurl);
157 // create the user filter form
158 $ufiltering = new user_filtering();
159 echo $OUTPUT->header();
161 // Carry on with the user listing
162 $context = context_system
::instance();
163 // These columns are always shown in the users list.
164 $requiredcolumns = array('city', 'country', 'lastaccess');
165 // Extra columns containing the extra user fields, excluding the required columns (city and country, to be specific).
166 $extracolumns = get_extra_user_fields($context, $requiredcolumns);
167 // Get all user name fields as an array.
168 $allusernamefields = get_all_user_name_fields(false, null, null, null, true);
169 $columns = array_merge($allusernamefields, $extracolumns, $requiredcolumns);
171 foreach ($columns as $column) {
172 $string[$column] = get_user_field_name($column);
173 if ($sort != $column) {
175 if ($column == "lastaccess") {
181 $columndir = $dir == "ASC" ?
"DESC":"ASC";
182 if ($column == "lastaccess") {
183 $columnicon = ($dir == "ASC") ?
"sort_desc" : "sort_asc";
185 $columnicon = ($dir == "ASC") ?
"sort_asc" : "sort_desc";
187 $columnicon = $OUTPUT->pix_icon('t/' . $columnicon, get_string(strtolower($columndir)), 'core',
188 ['class' => 'iconsort']);
191 $
$column = "<a href=\"user.php?sort=$column&dir=$columndir\">".$string[$column]."</a>$columnicon";
194 // We need to check that alternativefullnameformat is not set to '' or language.
195 // We don't need to check the fullnamedisplay setting here as the fullname function call further down has
196 // the override parameter set to true.
197 $fullnamesetting = $CFG->alternativefullnameformat
;
198 // If we are using language or it is empty, then retrieve the default user names of just 'firstname' and 'lastname'.
199 if ($fullnamesetting == 'language' ||
empty($fullnamesetting)) {
200 // Set $a variables to return 'firstname' and 'lastname'.
202 $a->firstname
= 'firstname';
203 $a->lastname
= 'lastname';
204 // Getting the fullname display will ensure that the order in the language file is maintained.
205 $fullnamesetting = get_string('fullnamedisplay', null, $a);
208 // Order in string will ensure that the name columns are in the correct order.
209 $usernames = order_in_string($allusernamefields, $fullnamesetting);
210 $fullnamedisplay = array();
211 foreach ($usernames as $name) {
212 // Use the link from $$column for sorting on the user's name.
213 $fullnamedisplay[] = $
{$name};
215 // All of the names are in one column. Put them into a string and separate them with a /.
216 $fullnamedisplay = implode(' / ', $fullnamedisplay);
217 // If $sort = name then it is the default for the setting and we should use the first name to sort by.
218 if ($sort == "name") {
219 // Use the first item in the array.
220 $sort = reset($usernames);
223 list($extrasql, $params) = $ufiltering->get_sql_filter();
224 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
225 $extrasql, $params, $context);
226 $usercount = get_users(false);
227 $usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
229 if ($extrasql !== '') {
230 echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
231 $usercount = $usersearchcount;
233 echo $OUTPUT->heading("$usercount ".get_string('users'));
236 $strall = get_string('all');
238 $baseurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
239 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
246 echo $OUTPUT->heading(get_string('nousersfound'));
252 $countries = get_string_manager()->get_list_of_countries(true);
253 if (empty($mnethosts)) {
254 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
257 foreach ($users as $key => $user) {
258 if (isset($countries[$user->country
])) {
259 $users[$key]->country
= $countries[$user->country
];
262 if ($sort == "country") {
263 // Need to resort by full country name, not code.
264 foreach ($users as $user) {
265 $susers[$user->id
] = $user->country
;
267 // Sort by country name, according to $dir.
268 if ($dir === 'DESC') {
273 foreach ($susers as $key => $value) {
274 $nusers[] = $users[$key];
279 $table = new html_table();
280 $table->head
= array ();
281 $table->colclasses
= array();
282 $table->head
[] = $fullnamedisplay;
283 $table->attributes
['class'] = 'admintable generaltable';
284 foreach ($extracolumns as $field) {
285 $table->head
[] = $
{$field};
287 $table->head
[] = $city;
288 $table->head
[] = $country;
289 $table->head
[] = $lastaccess;
290 $table->head
[] = get_string('edit');
291 $table->colclasses
[] = 'centeralign';
293 $table->colclasses
[] = 'centeralign';
295 $table->id
= "users";
296 foreach ($users as $user) {
301 if (has_capability('moodle/user:delete', $sitecontext)) {
302 if (is_mnet_remote_user($user) or $user->id
== $USER->id
or is_siteadmin($user)) {
303 // no deleting of self, mnet accounts or admins allowed
305 $url = new moodle_url($returnurl, array('delete'=>$user->id
, 'sesskey'=>sesskey()));
306 $buttons[] = html_writer
::link($url, $OUTPUT->pix_icon('t/delete', $strdelete));
311 if (has_capability('moodle/user:update', $sitecontext)) {
312 if (is_mnet_remote_user($user)) {
313 // mnet users have special access control, they can not be deleted the standard way or suspended
314 $accessctrl = 'allow';
315 if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username
, 'mnet_host_id'=>$user->mnethostid
))) {
316 $accessctrl = $acl->accessctrl
;
318 $changeaccessto = ($accessctrl == 'deny' ?
'allow' : 'deny');
319 $buttons[] = " (<a href=\"?acl={$user->id}&accessctrl=$changeaccessto&sesskey=".sesskey()."\">".get_string($changeaccessto, 'mnet') . " access</a>)";
322 if ($user->suspended
) {
323 $url = new moodle_url($returnurl, array('unsuspend'=>$user->id
, 'sesskey'=>sesskey()));
324 $buttons[] = html_writer
::link($url, $OUTPUT->pix_icon('t/show', $strunsuspend));
326 if ($user->id
== $USER->id
or is_siteadmin($user)) {
327 // no suspending of admins or self!
329 $url = new moodle_url($returnurl, array('suspend'=>$user->id
, 'sesskey'=>sesskey()));
330 $buttons[] = html_writer
::link($url, $OUTPUT->pix_icon('t/hide', $strsuspend));
334 if (login_is_lockedout($user)) {
335 $url = new moodle_url($returnurl, array('unlock'=>$user->id
, 'sesskey'=>sesskey()));
336 $buttons[] = html_writer
::link($url, $OUTPUT->pix_icon('t/unlock', $strunlock));
342 if (has_capability('moodle/user:update', $sitecontext)) {
343 // prevent editing of admins by non-admins
344 if (is_siteadmin($USER) or !is_siteadmin($user)) {
345 $url = new moodle_url('/user/editadvanced.php', array('id'=>$user->id
, 'course'=>$site->id
));
346 $buttons[] = html_writer
::link($url, $OUTPUT->pix_icon('t/edit', $stredit));
350 // the last column - confirm or mnet info
351 if (is_mnet_remote_user($user)) {
352 // all mnet users are confirmed, let's print just the name of the host there
353 if (isset($mnethosts[$user->mnethostid
])) {
354 $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid
]->name
;
356 $lastcolumn = get_string($accessctrl, 'mnet');
359 } else if ($user->confirmed
== 0) {
360 if (has_capability('moodle/user:update', $sitecontext)) {
361 $lastcolumn = html_writer
::link(new moodle_url($returnurl, array('confirmuser'=>$user->id
, 'sesskey'=>sesskey())), $strconfirm);
363 $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
367 if ($user->lastaccess
) {
368 $strlastaccess = format_time(time() - $user->lastaccess
);
370 $strlastaccess = get_string('never');
372 $fullname = fullname($user, true);
375 $row[] = "<a href=\"../user/view.php?id=$user->id&course=$site->id\">$fullname</a>";
376 foreach ($extracolumns as $field) {
377 $row[] = $user->{$field};
379 $row[] = $user->city
;
380 $row[] = $user->country
;
381 $row[] = $strlastaccess;
382 if ($user->suspended
) {
383 foreach ($row as $k=>$v) {
384 $row[$k] = html_writer
::tag('span', $v, array('class'=>'usersuspended'));
387 $row[] = implode(' ', $buttons);
388 $row[] = $lastcolumn;
389 $table->data
[] = $row;
394 $ufiltering->display_add();
395 $ufiltering->display_active();
397 if (!empty($table)) {
398 echo html_writer
::start_tag('div', array('class'=>'no-overflow'));
399 echo html_writer
::table($table);
400 echo html_writer
::end_tag('div');
401 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
403 if (has_capability('moodle/user:create', $sitecontext)) {
404 $url = new moodle_url('/user/editadvanced.php', array('id' => -1));
405 echo $OUTPUT->single_button($url, get_string('addnewuser'), 'get');
408 echo $OUTPUT->footer();