MDL-40275 core Improve documentation for block_base::has_config()
[moodle.git] / admin / user.php
blob9cc9e7037c5b10b2a5295fc866c11d09248c98c9
1 <?php
3 require_once('../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
5 require_once($CFG->dirroot.'/user/filters/lib.php');
7 $delete = optional_param('delete', 0, PARAM_INT);
8 $confirm = optional_param('confirm', '', PARAM_ALPHANUM); //md5 confirmation hash
9 $confirmuser = optional_param('confirmuser', 0, PARAM_INT);
10 $sort = optional_param('sort', 'name', PARAM_ALPHANUM);
11 $dir = optional_param('dir', 'ASC', PARAM_ALPHA);
12 $page = optional_param('page', 0, PARAM_INT);
13 $perpage = optional_param('perpage', 30, PARAM_INT); // how many per page
14 $ru = optional_param('ru', '2', PARAM_INT); // show remote users
15 $lu = optional_param('lu', '2', PARAM_INT); // show local users
16 $acl = optional_param('acl', '0', PARAM_INT); // id of user to tweak mnet ACL (requires $access)
17 $suspend = optional_param('suspend', 0, PARAM_INT);
18 $unsuspend = optional_param('unsuspend', 0, PARAM_INT);
20 admin_externalpage_setup('editusers');
22 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
23 $site = get_site();
25 if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
26 print_error('nopermissions', 'error', '', 'edit/delete users');
29 $stredit = get_string('edit');
30 $strdelete = get_string('delete');
31 $strdeletecheck = get_string('deletecheck');
32 $strshowallusers = get_string('showallusers');
33 $strsuspend = get_string('suspenduser', 'admin');
34 $strunsuspend = get_string('unsuspenduser', 'admin');
35 $strconfirm = get_string('confirm');
37 if (empty($CFG->loginhttps)) {
38 $securewwwroot = $CFG->wwwroot;
39 } else {
40 $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
43 $returnurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage, 'page'=>$page));
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) {
56 redirect($returnurl);
57 } else {
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);
67 if (is_siteadmin($user->id)) {
68 print_error('useradminodelete', 'error');
71 if ($confirm != md5($delete)) {
72 echo $OUTPUT->header();
73 $fullname = fullname($user, true);
74 echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
75 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
76 echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), new moodle_url($returnurl, $optionsyes), $returnurl);
77 echo $OUTPUT->footer();
78 die;
79 } else if (data_submitted() and !$user->deleted) {
80 if (delete_user($user)) {
81 session_gc(); // remove stale sessions
82 redirect($returnurl);
83 } else {
84 session_gc(); // remove stale sessions
85 echo $OUTPUT->header();
86 echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
89 } else if ($acl and confirm_sesskey()) {
90 if (!has_capability('moodle/user:update', $sitecontext)) {
91 print_error('nopermissions', 'error', '', 'modify the NMET access control list');
93 if (!$user = $DB->get_record('user', array('id'=>$acl))) {
94 print_error('nousers', 'error');
96 if (!is_mnet_remote_user($user)) {
97 print_error('usermustbemnet', 'error');
99 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
100 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
101 print_error('invalidaccessparameter', 'error');
103 $aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid));
104 if (empty($aclrecord)) {
105 $aclrecord = new stdClass();
106 $aclrecord->mnet_host_id = $user->mnethostid;
107 $aclrecord->username = $user->username;
108 $aclrecord->accessctrl = $accessctrl;
109 $DB->insert_record('mnet_sso_access_control', $aclrecord);
110 } else {
111 $aclrecord->accessctrl = $accessctrl;
112 $DB->update_record('mnet_sso_access_control', $aclrecord);
114 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
115 redirect($returnurl);
117 } else if ($suspend and confirm_sesskey()) {
118 require_capability('moodle/user:update', $sitecontext);
120 if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
121 if (!is_siteadmin($user) and $USER->id != $user->id and $user->suspended != 1) {
122 $user->suspended = 1;
123 $user->timemodified = time();
124 $DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
125 $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
126 // force logout
127 session_kill_user($user->id);
128 events_trigger('user_updated', $user);
131 redirect($returnurl);
133 } else if ($unsuspend and confirm_sesskey()) {
134 require_capability('moodle/user:update', $sitecontext);
136 if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
137 if ($user->suspended != 0) {
138 $user->suspended = 0;
139 $user->timemodified = time();
140 $DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
141 $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
142 events_trigger('user_updated', $user);
145 redirect($returnurl);
148 // create the user filter form
149 $ufiltering = new user_filtering();
150 echo $OUTPUT->header();
152 // Carry on with the user listing
153 $context = context_system::instance();
154 $extracolumns = get_extra_user_fields($context);
155 $columns = array_merge(array('firstname', 'lastname'), $extracolumns,
156 array('city', 'country', 'lastaccess'));
158 foreach ($columns as $column) {
159 $string[$column] = get_user_field_name($column);
160 if ($sort != $column) {
161 $columnicon = "";
162 if ($column == "lastaccess") {
163 $columndir = "DESC";
164 } else {
165 $columndir = "ASC";
167 } else {
168 $columndir = $dir == "ASC" ? "DESC":"ASC";
169 if ($column == "lastaccess") {
170 $columnicon = $dir == "ASC" ? "up":"down";
171 } else {
172 $columnicon = $dir == "ASC" ? "down":"up";
174 $columnicon = " <img src=\"" . $OUTPUT->pix_url('t/' . $columnicon) . "\" alt=\"\" />";
177 $$column = "<a href=\"user.php?sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
180 $override = new stdClass();
181 $override->firstname = 'firstname';
182 $override->lastname = 'lastname';
183 $fullnamelanguage = get_string('fullnamedisplay', '', $override);
184 if (($CFG->fullnamedisplay == 'firstname lastname') or
185 ($CFG->fullnamedisplay == 'firstname') or
186 ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname' )) {
187 $fullnamedisplay = "$firstname / $lastname";
188 if ($sort == "name") { // If sort has already been set to something else then ignore.
189 $sort = "firstname";
191 } else { // ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'lastname firstname').
192 $fullnamedisplay = "$lastname / $firstname";
193 if ($sort == "name") { // This should give the desired sorting based on fullnamedisplay.
194 $sort = "lastname";
198 list($extrasql, $params) = $ufiltering->get_sql_filter();
200 $usercount = get_users(false);
201 $usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
203 // Exclude guest user from list.
204 $noguestsql = '';
205 if (!empty($extrasql)) {
206 $noguestsql .= ' AND';
208 $noguestsql .= " id <> :guestid";
209 $params['guestid'] = $CFG->siteguest;
210 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
211 $extrasql.$noguestsql, $params, $context);
213 if ($extrasql !== '') {
214 echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
215 $usercount = $usersearchcount;
216 } else {
217 echo $OUTPUT->heading("$usercount ".get_string('users'));
220 $strall = get_string('all');
222 $baseurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
223 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
225 flush();
228 if (!$users) {
229 $match = array();
230 echo $OUTPUT->heading(get_string('nousersfound'));
232 $table = NULL;
234 } else {
236 $countries = get_string_manager()->get_list_of_countries(false);
237 if (empty($mnethosts)) {
238 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
241 foreach ($users as $key => $user) {
242 if (isset($countries[$user->country])) {
243 $users[$key]->country = $countries[$user->country];
246 if ($sort == "country") { // Need to resort by full country name, not code
247 foreach ($users as $user) {
248 $susers[$user->id] = $user->country;
250 asort($susers);
251 foreach ($susers as $key => $value) {
252 $nusers[] = $users[$key];
254 $users = $nusers;
257 $table = new html_table();
258 $table->head = array ();
259 $table->align = array();
260 $table->head[] = $fullnamedisplay;
261 $table->align[] = 'left';
262 foreach ($extracolumns as $field) {
263 $table->head[] = ${$field};
264 $table->align[] = 'left';
266 $table->head[] = $city;
267 $table->align[] = 'left';
268 $table->head[] = $country;
269 $table->align[] = 'left';
270 $table->head[] = $lastaccess;
271 $table->align[] = 'left';
272 $table->head[] = get_string('edit');
273 $table->align[] = 'center';
274 $table->head[] = "";
275 $table->align[] = 'center';
277 $table->width = "95%";
278 foreach ($users as $user) {
279 if (isguestuser($user)) {
280 continue; // do not display guest here
283 $buttons = array();
284 $lastcolumn = '';
286 // delete button
287 if (has_capability('moodle/user:delete', $sitecontext)) {
288 if (is_mnet_remote_user($user) or $user->id == $USER->id or is_siteadmin($user)) {
289 // no deleting of self, mnet accounts or admins allowed
290 } else {
291 $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));
295 // suspend button
296 if (has_capability('moodle/user:update', $sitecontext)) {
297 if (is_mnet_remote_user($user)) {
298 // mnet users have special access control, they can not be deleted the standard way or suspended
299 $accessctrl = 'allow';
300 if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid))) {
301 $accessctrl = $acl->accessctrl;
303 $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
304 $buttons[] = " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey=".sesskey()."\">".get_string($changeaccessto, 'mnet') . " access</a>)";
306 } else {
307 if ($user->suspended) {
308 $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));
309 } else {
310 if ($user->id == $USER->id or is_siteadmin($user)) {
311 // no suspending of admins or self!
312 } else {
313 $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));
320 // edit button
321 if (has_capability('moodle/user:update', $sitecontext)) {
322 // prevent editing of admins by non-admins
323 if (is_siteadmin($USER) or !is_siteadmin($user)) {
324 $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));
328 // the last column - confirm or mnet info
329 if (is_mnet_remote_user($user)) {
330 // all mnet users are confirmed, let's print just the name of the host there
331 if (isset($mnethosts[$user->mnethostid])) {
332 $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid]->name;
333 } else {
334 $lastcolumn = get_string($accessctrl, 'mnet');
337 } else if ($user->confirmed == 0) {
338 if (has_capability('moodle/user:update', $sitecontext)) {
339 $lastcolumn = html_writer::link(new moodle_url($returnurl, array('confirmuser'=>$user->id, 'sesskey'=>sesskey())), $strconfirm);
340 } else {
341 $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
345 if ($user->lastaccess) {
346 $strlastaccess = format_time(time() - $user->lastaccess);
347 } else {
348 $strlastaccess = get_string('never');
350 $fullname = fullname($user, true);
352 $row = array ();
353 $row[] = "<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>";
354 foreach ($extracolumns as $field) {
355 $row[] = $user->{$field};
357 $row[] = $user->city;
358 $row[] = $user->country;
359 $row[] = $strlastaccess;
360 if ($user->suspended) {
361 foreach ($row as $k=>$v) {
362 $row[$k] = html_writer::tag('span', $v, array('class'=>'usersuspended'));
365 $row[] = implode(' ', $buttons);
366 $row[] = $lastcolumn;
367 $table->data[] = $row;
371 // add filters
372 $ufiltering->display_add();
373 $ufiltering->display_active();
375 if (has_capability('moodle/user:create', $sitecontext)) {
376 echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
378 if (!empty($table)) {
379 echo html_writer::table($table);
380 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
381 if (has_capability('moodle/user:create', $sitecontext)) {
382 echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
386 echo $OUTPUT->footer();