Merge branch 'MDL-33932_22' of git://github.com/grabs/moodle into MOODLE_22_STABLE
[moodle.git] / admin / user.php
blob56e457e27c1ef0cd79bb859b039d3262579d4ccb
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 if ($sort == "name") {
181 $sort = "firstname";
184 list($extrasql, $params) = $ufiltering->get_sql_filter();
185 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
186 $extrasql, $params, $context);
187 $usercount = get_users(false);
188 $usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
190 if ($extrasql !== '') {
191 echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
192 $usercount = $usersearchcount;
193 } else {
194 echo $OUTPUT->heading("$usercount ".get_string('users'));
197 $strall = get_string('all');
199 $baseurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
200 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
202 flush();
205 if (!$users) {
206 $match = array();
207 echo $OUTPUT->heading(get_string('nousersfound'));
209 $table = NULL;
211 } else {
213 $countries = get_string_manager()->get_list_of_countries(false);
214 if (empty($mnethosts)) {
215 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
218 foreach ($users as $key => $user) {
219 if (isset($countries[$user->country])) {
220 $users[$key]->country = $countries[$user->country];
223 if ($sort == "country") { // Need to resort by full country name, not code
224 foreach ($users as $user) {
225 $susers[$user->id] = $user->country;
227 asort($susers);
228 foreach ($susers as $key => $value) {
229 $nusers[] = $users[$key];
231 $users = $nusers;
234 $override = new stdClass();
235 $override->firstname = 'firstname';
236 $override->lastname = 'lastname';
237 $fullnamelanguage = get_string('fullnamedisplay', '', $override);
238 if (($CFG->fullnamedisplay == 'firstname lastname') or
239 ($CFG->fullnamedisplay == 'firstname') or
240 ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname' )) {
241 $fullnamedisplay = "$firstname / $lastname";
242 } else { // ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'lastname firstname')
243 $fullnamedisplay = "$lastname / $firstname";
246 $table = new html_table();
247 $table->head = array ();
248 $table->align = array();
249 $table->head[] = $fullnamedisplay;
250 $table->align[] = 'left';
251 foreach ($extracolumns as $field) {
252 $table->head[] = ${$field};
253 $table->align[] = 'left';
255 $table->head[] = $city;
256 $table->align[] = 'left';
257 $table->head[] = $country;
258 $table->align[] = 'left';
259 $table->head[] = $lastaccess;
260 $table->align[] = 'left';
261 $table->head[] = get_string('edit');
262 $table->align[] = 'center';
263 $table->head[] = "";
264 $table->align[] = 'center';
266 $table->width = "95%";
267 foreach ($users as $user) {
268 if (isguestuser($user)) {
269 continue; // do not display guest here
272 $buttons = array();
273 $lastcolumn = '';
275 // delete button
276 if (has_capability('moodle/user:delete', $sitecontext)) {
277 if (is_mnet_remote_user($user) or $user->id == $USER->id or is_siteadmin($user)) {
278 // no deleting of self, mnet accounts or admins allowed
279 } else {
280 $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));
284 // suspend button
285 if (has_capability('moodle/user:update', $sitecontext)) {
286 if (is_mnet_remote_user($user)) {
287 // mnet users have special access control, they can not be deleted the standard way or suspended
288 $accessctrl = 'allow';
289 if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid))) {
290 $accessctrl = $acl->accessctrl;
292 $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
293 $buttons[] = " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey=".sesskey()."\">".get_string($changeaccessto, 'mnet') . " access</a>)";
295 } else {
296 if ($user->suspended) {
297 $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));
298 } else {
299 if ($user->id == $USER->id or is_siteadmin($user)) {
300 // no suspending of admins or self!
301 } else {
302 $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));
309 // edit button
310 if (has_capability('moodle/user:update', $sitecontext)) {
311 // prevent editing of admins by non-admins
312 if (is_siteadmin($USER) or !is_siteadmin($user)) {
313 $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));
317 // the last column - confirm or mnet info
318 if (is_mnet_remote_user($user)) {
319 // all mnet users are confirmed, let's print just the name of the host there
320 if (isset($mnethosts[$user->mnethostid])) {
321 $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid]->name;
322 } else {
323 $lastcolumn = get_string($accessctrl, 'mnet');
326 } else if ($user->confirmed == 0) {
327 if (has_capability('moodle/user:update', $sitecontext)) {
328 $lastcolumn = html_writer::link(new moodle_url($returnurl, array('confirmuser'=>$user->id, 'sesskey'=>sesskey())), $strconfirm);
329 } else {
330 $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
334 if ($user->lastaccess) {
335 $strlastaccess = format_time(time() - $user->lastaccess);
336 } else {
337 $strlastaccess = get_string('never');
339 $fullname = fullname($user, true);
341 $row = array ();
342 $row[] = "<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>";
343 foreach ($extracolumns as $field) {
344 $row[] = $user->{$field};
346 $row[] = $user->city;
347 $row[] = $user->country;
348 $row[] = $strlastaccess;
349 if ($user->suspended) {
350 foreach ($row as $k=>$v) {
351 $row[$k] = html_writer::tag('span', $v, array('class'=>'usersuspended'));
354 $row[] = implode(' ', $buttons);
355 $row[] = $lastcolumn;
356 $table->data[] = $row;
360 // add filters
361 $ufiltering->display_add();
362 $ufiltering->display_active();
364 if (has_capability('moodle/user:create', $sitecontext)) {
365 echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
367 if (!empty($table)) {
368 echo html_writer::table($table);
369 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
370 if (has_capability('moodle/user:create', $sitecontext)) {
371 echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
375 echo $OUTPUT->footer();