MDL-47195 libraries: Add horde IMAP from Horde 5.2.1
[moodle.git] / admin / user.php
blob184ada69bb0912d3d00089a801600b2da9ffde7f
1 <?php
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();
26 $site = get_site();
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;
43 } else {
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) {
60 redirect($returnurl);
61 } else {
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();
82 die;
83 } else if (data_submitted() and !$user->deleted) {
84 if (delete_user($user)) {
85 \core\session\manager::gc(); // Remove stale sessions.
86 redirect($returnurl);
87 } else {
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);
114 } else {
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;
127 // Force logout.
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) {
167 $columnicon = "";
168 if ($column == "lastaccess") {
169 $columndir = "DESC";
170 } else {
171 $columndir = "ASC";
173 } else {
174 $columndir = $dir == "ASC" ? "DESC":"ASC";
175 if ($column == "lastaccess") {
176 $columnicon = ($dir == "ASC") ? "sort_desc" : "sort_asc";
177 } else {
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&amp;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.
195 $sort = "firstname";
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.
200 $sort = "lastname";
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;
213 } else {
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);
222 flush();
225 if (!$users) {
226 $match = array();
227 echo $OUTPUT->heading(get_string('nousersfound'));
229 $table = NULL;
231 } else {
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;
247 asort($susers);
248 foreach ($susers as $key => $value) {
249 $nusers[] = $users[$key];
251 $users = $nusers;
254 $table = new html_table();
255 $table->head = array ();
256 $table->colclasses = array();
257 $table->head[] = $fullnamedisplay;
258 $table->attributes['class'] = 'admintable generaltable';
259 foreach ($extracolumns as $field) {
260 $table->head[] = ${$field};
262 $table->head[] = $city;
263 $table->head[] = $country;
264 $table->head[] = $lastaccess;
265 $table->head[] = get_string('edit');
266 $table->colclasses[] = 'centeralign';
267 $table->head[] = "";
268 $table->colclasses[] = 'centeralign';
270 $table->id = "users";
271 foreach ($users as $user) {
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));
306 if (login_is_lockedout($user)) {
307 $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));
312 // edit button
313 if (has_capability('moodle/user:update', $sitecontext)) {
314 // prevent editing of admins by non-admins
315 if (is_siteadmin($USER) or !is_siteadmin($user)) {
316 $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));
320 // the last column - confirm or mnet info
321 if (is_mnet_remote_user($user)) {
322 // all mnet users are confirmed, let's print just the name of the host there
323 if (isset($mnethosts[$user->mnethostid])) {
324 $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid]->name;
325 } else {
326 $lastcolumn = get_string($accessctrl, 'mnet');
329 } else if ($user->confirmed == 0) {
330 if (has_capability('moodle/user:update', $sitecontext)) {
331 $lastcolumn = html_writer::link(new moodle_url($returnurl, array('confirmuser'=>$user->id, 'sesskey'=>sesskey())), $strconfirm);
332 } else {
333 $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
337 if ($user->lastaccess) {
338 $strlastaccess = format_time(time() - $user->lastaccess);
339 } else {
340 $strlastaccess = get_string('never');
342 $fullname = fullname($user, true);
344 $row = array ();
345 $row[] = "<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>";
346 foreach ($extracolumns as $field) {
347 $row[] = $user->{$field};
349 $row[] = $user->city;
350 $row[] = $user->country;
351 $row[] = $strlastaccess;
352 if ($user->suspended) {
353 foreach ($row as $k=>$v) {
354 $row[$k] = html_writer::tag('span', $v, array('class'=>'usersuspended'));
357 $row[] = implode(' ', $buttons);
358 $row[] = $lastcolumn;
359 $table->data[] = $row;
363 // add filters
364 $ufiltering->display_add();
365 $ufiltering->display_active();
367 if (!empty($table)) {
368 echo html_writer::start_tag('div', array('class'=>'no-overflow'));
369 echo html_writer::table($table);
370 echo html_writer::end_tag('div');
371 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
373 if (has_capability('moodle/user:create', $sitecontext)) {
374 $url = new moodle_url($securewwwroot . '/user/editadvanced.php', array('id' => -1));
375 echo $OUTPUT->single_button($url, get_string('addnewuser'), 'get');
378 echo $OUTPUT->footer();