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 if (empty($CFG->loginhttps
)) {
42 $securewwwroot = $CFG->wwwroot
;
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 if ($confirmuser and confirm_sesskey()) {
50 require_capability('moodle/user:update', $sitecontext);
51 if (!$user = $DB->get_record('user', array('id'=>$confirmuser, 'mnethostid'=>$CFG->mnet_localhost_id
))) {
52 print_error('nousers');
55 $auth = get_auth_plugin($user->auth
);
57 $result = $auth->user_confirm($user->username
, $user->secret
);
59 if ($result == AUTH_CONFIRM_OK
or $result == AUTH_CONFIRM_ALREADY
) {
62 echo $OUTPUT->header();
63 redirect($returnurl, get_string('usernotconfirmed', '', fullname($user, true)));
66 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
67 require_capability('moodle/user:delete', $sitecontext);
69 $user = $DB->get_record('user', array('id'=>$delete, 'mnethostid'=>$CFG->mnet_localhost_id
), '*', MUST_EXIST
);
71 if (is_siteadmin($user->id
)) {
72 print_error('useradminodelete', 'error');
75 if ($confirm != md5($delete)) {
76 echo $OUTPUT->header();
77 $fullname = fullname($user, true);
78 echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
79 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
80 echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), new moodle_url($returnurl, $optionsyes), $returnurl);
81 echo $OUTPUT->footer();
83 } else if (data_submitted() and !$user->deleted
) {
84 if (delete_user($user)) {
85 \core\session\manager
::gc(); // Remove stale sessions.
88 \core\session\manager
::gc(); // Remove stale sessions.
89 echo $OUTPUT->header();
90 echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
93 } else if ($acl and confirm_sesskey()) {
94 if (!has_capability('moodle/user:update', $sitecontext)) {
95 print_error('nopermissions', 'error', '', 'modify the NMET access control list');
97 if (!$user = $DB->get_record('user', array('id'=>$acl))) {
98 print_error('nousers', 'error');
100 if (!is_mnet_remote_user($user)) {
101 print_error('usermustbemnet', 'error');
103 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA
));
104 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
105 print_error('invalidaccessparameter', 'error');
107 $aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username
, 'mnet_host_id'=>$user->mnethostid
));
108 if (empty($aclrecord)) {
109 $aclrecord = new stdClass();
110 $aclrecord->mnet_host_id
= $user->mnethostid
;
111 $aclrecord->username
= $user->username
;
112 $aclrecord->accessctrl
= $accessctrl;
113 $DB->insert_record('mnet_sso_access_control', $aclrecord);
115 $aclrecord->accessctrl
= $accessctrl;
116 $DB->update_record('mnet_sso_access_control', $aclrecord);
118 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
119 redirect($returnurl);
121 } else if ($suspend and confirm_sesskey()) {
122 require_capability('moodle/user:update', $sitecontext);
124 if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id
, 'deleted'=>0))) {
125 if (!is_siteadmin($user) and $USER->id
!= $user->id
and $user->suspended
!= 1) {
126 $user->suspended
= 1;
128 \core\session\manager
::kill_user_sessions($user->id
);
129 user_update_user($user, false);
132 redirect($returnurl);
134 } else if ($unsuspend and confirm_sesskey()) {
135 require_capability('moodle/user:update', $sitecontext);
137 if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id
, 'deleted'=>0))) {
138 if ($user->suspended
!= 0) {
139 $user->suspended
= 0;
140 user_update_user($user, false);
143 redirect($returnurl);
145 } else if ($unlock and confirm_sesskey()) {
146 require_capability('moodle/user:update', $sitecontext);
148 if ($user = $DB->get_record('user', array('id'=>$unlock, 'mnethostid'=>$CFG->mnet_localhost_id
, 'deleted'=>0))) {
149 login_unlock_account($user);
151 redirect($returnurl);
154 // create the user filter form
155 $ufiltering = new user_filtering();
156 echo $OUTPUT->header();
158 // Carry on with the user listing
159 $context = context_system
::instance();
160 $extracolumns = get_extra_user_fields($context);
161 $columns = array_merge(array('firstname', 'lastname'), $extracolumns,
162 array('city', 'country', 'lastaccess'));
164 foreach ($columns as $column) {
165 $string[$column] = get_user_field_name($column);
166 if ($sort != $column) {
168 if ($column == "lastaccess") {
174 $columndir = $dir == "ASC" ?
"DESC":"ASC";
175 if ($column == "lastaccess") {
176 $columnicon = ($dir == "ASC") ?
"sort_desc" : "sort_asc";
178 $columnicon = ($dir == "ASC") ?
"sort_asc" : "sort_desc";
180 $columnicon = "<img class='iconsort' src=\"" . $OUTPUT->pix_url('t/' . $columnicon) . "\" alt=\"\" />";
183 $
$column = "<a href=\"user.php?sort=$column&dir=$columndir\">".$string[$column]."</a>$columnicon";
186 $override = new stdClass();
187 $override->firstname
= 'firstname';
188 $override->lastname
= 'lastname';
189 $fullnamelanguage = get_string('fullnamedisplay', '', $override);
190 if (($CFG->fullnamedisplay
== 'firstname lastname') or
191 ($CFG->fullnamedisplay
== 'firstname') or
192 ($CFG->fullnamedisplay
== 'language' and $fullnamelanguage == 'firstname lastname' )) {
193 $fullnamedisplay = "$firstname / $lastname";
194 if ($sort == "name") { // If sort has already been set to something else then ignore.
197 } else { // ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'lastname firstname').
198 $fullnamedisplay = "$lastname / $firstname";
199 if ($sort == "name") { // This should give the desired sorting based on fullnamedisplay.
204 list($extrasql, $params) = $ufiltering->get_sql_filter();
205 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
206 $extrasql, $params, $context);
207 $usercount = get_users(false);
208 $usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
210 if ($extrasql !== '') {
211 echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
212 $usercount = $usersearchcount;
214 echo $OUTPUT->heading("$usercount ".get_string('users'));
217 $strall = get_string('all');
219 $baseurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
220 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
227 echo $OUTPUT->heading(get_string('nousersfound'));
233 $countries = get_string_manager()->get_list_of_countries(false);
234 if (empty($mnethosts)) {
235 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
238 foreach ($users as $key => $user) {
239 if (isset($countries[$user->country
])) {
240 $users[$key]->country
= $countries[$user->country
];
243 if ($sort == "country") { // Need to resort by full country name, not code
244 foreach ($users as $user) {
245 $susers[$user->id
] = $user->country
;
248 foreach ($susers as $key => $value) {
249 $nusers[] = $users[$key];
254 $table = new html_table();
255 $table->head
= array ();
256 $table->colclasses
= array();
257 $table->head
[] = $fullnamedisplay;
258 $table->attributes
['class'] = 'admintable generaltable';
259 $table->colclasses
[] = 'leftalign';
260 foreach ($extracolumns as $field) {
261 $table->head
[] = $
{$field};
262 $table->colclasses
[] = 'leftalign';
264 $table->head
[] = $city;
265 $table->colclasses
[] = 'leftalign';
266 $table->head
[] = $country;
267 $table->colclasses
[] = 'leftalign';
268 $table->head
[] = $lastaccess;
269 $table->colclasses
[] = 'leftalign';
270 $table->head
[] = get_string('edit');
271 $table->colclasses
[] = 'centeralign';
273 $table->colclasses
[] = 'centeralign';
275 $table->id
= "users";
276 foreach ($users as $user) {
281 if (has_capability('moodle/user:delete', $sitecontext)) {
282 if (is_mnet_remote_user($user) or $user->id
== $USER->id
or is_siteadmin($user)) {
283 // no deleting of self, mnet accounts or admins allowed
285 $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));
290 if (has_capability('moodle/user:update', $sitecontext)) {
291 if (is_mnet_remote_user($user)) {
292 // mnet users have special access control, they can not be deleted the standard way or suspended
293 $accessctrl = 'allow';
294 if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username
, 'mnet_host_id'=>$user->mnethostid
))) {
295 $accessctrl = $acl->accessctrl
;
297 $changeaccessto = ($accessctrl == 'deny' ?
'allow' : 'deny');
298 $buttons[] = " (<a href=\"?acl={$user->id}&accessctrl=$changeaccessto&sesskey=".sesskey()."\">".get_string($changeaccessto, 'mnet') . " access</a>)";
301 if ($user->suspended
) {
302 $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));
304 if ($user->id
== $USER->id
or is_siteadmin($user)) {
305 // no suspending of admins or self!
307 $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));
311 if (login_is_lockedout($user)) {
312 $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));
318 if (has_capability('moodle/user:update', $sitecontext)) {
319 // prevent editing of admins by non-admins
320 if (is_siteadmin($USER) or !is_siteadmin($user)) {
321 $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));
325 // the last column - confirm or mnet info
326 if (is_mnet_remote_user($user)) {
327 // all mnet users are confirmed, let's print just the name of the host there
328 if (isset($mnethosts[$user->mnethostid
])) {
329 $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid
]->name
;
331 $lastcolumn = get_string($accessctrl, 'mnet');
334 } else if ($user->confirmed
== 0) {
335 if (has_capability('moodle/user:update', $sitecontext)) {
336 $lastcolumn = html_writer
::link(new moodle_url($returnurl, array('confirmuser'=>$user->id
, 'sesskey'=>sesskey())), $strconfirm);
338 $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
342 if ($user->lastaccess
) {
343 $strlastaccess = format_time(time() - $user->lastaccess
);
345 $strlastaccess = get_string('never');
347 $fullname = fullname($user, true);
350 $row[] = "<a href=\"../user/view.php?id=$user->id&course=$site->id\">$fullname</a>";
351 foreach ($extracolumns as $field) {
352 $row[] = $user->{$field};
354 $row[] = $user->city
;
355 $row[] = $user->country
;
356 $row[] = $strlastaccess;
357 if ($user->suspended
) {
358 foreach ($row as $k=>$v) {
359 $row[$k] = html_writer
::tag('span', $v, array('class'=>'usersuspended'));
362 $row[] = implode(' ', $buttons);
363 $row[] = $lastcolumn;
364 $table->data
[] = $row;
369 $ufiltering->display_add();
370 $ufiltering->display_active();
372 if (has_capability('moodle/user:create', $sitecontext)) {
373 echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
375 if (!empty($table)) {
376 echo html_writer
::start_tag('div', array('class'=>'no-overflow'));
377 echo html_writer
::table($table);
378 echo html_writer
::end_tag('div');
379 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
380 if (has_capability('moodle/user:create', $sitecontext)) {
381 echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
385 echo $OUTPUT->footer();