MDL-8096 - user edit form splitting, refactoring, cleanup and fixing - work in progre...
[moodle.git] / admin / user.php
CommitLineData
4cfc6400 1<?php // $Id$
f9903ed0 2
4cd174bf 3 require_once('../config.php');
38d2d43b 4 require_once($CFG->libdir.'/adminlib.php');
7710da22 5
7710da22 6 $delete = optional_param('delete', 0, PARAM_INT);
7 $confirm = optional_param('confirm', '', PARAM_ALPHANUM); //md5 confirmation hash
8 $confirmuser = optional_param('confirmuser', 0, PARAM_INT);
9 $sort = optional_param('sort', 'name', PARAM_ALPHA);
10 $dir = optional_param('dir', 'ASC', PARAM_ALPHA);
11 $page = optional_param('page', 0, PARAM_INT);
12 $perpage = optional_param('perpage', 30, PARAM_INT); // how many per page
38d2d43b 13 $search = trim(optional_param('search', '', PARAM_RAW));
7710da22 14 $lastinitial = optional_param('lastinitial', '', PARAM_CLEAN); // only show students with this last initial
15 $firstinitial = optional_param('firstinitial', '', PARAM_CLEAN); // only show students with this first initial
d14383e0 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
ad6226fb 20 $adminroot = admin_get_root();
21 admin_externalpage_setup('editusers', $adminroot);
22
7340c743 23 // Let's see if we have *any* mnet users. Just ask for a single record
24 $mnet_users = get_records_select('user', " auth='mnet' AND mnethostid != '{$CFG->mnet_localhost_id}' ", '', '*', '0', '1');
25 if(is_array($mnet_users) && count($mnet_users) > 0) {
26 $mnet_auth_users = true;
27 } else {
28 $mnet_auth_users = false;
d14383e0 29 }
7340c743 30
31 if($mnet_auth_users) {
32 // Determine which users we are looking at (local, remote, or both). Start with both.
33 if (!isset($_SESSION['admin-user-remoteusers'])) {
34 $_SESSION['admin-user-remoteusers'] = 1;
35 $_SESSION['admin-user-localusers'] = 1;
36 }
37 if ($ru == 0 or $ru == 1) {
38 $_SESSION['admin-user-remoteusers'] = $ru;
39 }
40 if ($lu == 0 or $lu == 1) {
41 $_SESSION['admin-user-localusers'] = $lu;
42 }
43 $remoteusers = $_SESSION['admin-user-remoteusers'];
44 $localusers = $_SESSION['admin-user-localusers'];
45
46 // if neither remote nor local, set to sensible local only
47 if (!$remoteusers and !$localusers) {
48 $_SESSION['admin-user-localusers'] = 1;
49 $localusers = 1;
50 }
d14383e0 51 }
351039ae 52
ad6226fb 53 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
54 $site = get_site();
55
56 if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
57 error('You do not have the required permission to edit/delete users.');
bee4702d 58 }
0c30fdc9 59
ad6226fb 60 $stredit = get_string('edit');
61 $strdelete = get_string('delete');
62 $strdeletecheck = get_string('deletecheck');
63 $strsearch = get_string('search');
64 $strshowallusers = get_string('showallusers');
f9903ed0 65
ad6226fb 66 admin_externalpage_print_header($adminroot);
f9903ed0 67
ad6226fb 68 if ($confirmuser and confirm_sesskey()) {
69 if (!$user = get_record('user', 'id', $confirmuser)) {
70 error("No such user!");
df9c6677 71 }
0c30fdc9 72
ad6226fb 73 $confirmeduser = new object();
74 $confirmeduser->id = $confirmuser;
75 $confirmeduser->confirmed = 1;
76 $confirmeduser->timemodified = time();
0c30fdc9 77
ad6226fb 78 if (update_record('user', $confirmeduser)) {
79 notify(get_string('userconfirmed', '', fullname($user, true)) );
80 } else {
81 notify(get_string('usernotconfirmed', '', fullname($user, true)));
f9903ed0 82 }
38d2d43b 83
ad6226fb 84 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
0c30fdc9 85
ad6226fb 86 if (!has_capability('moodle/user:delete', $sitecontext)) {
87 error('You do not have the required permission to delete a user.');
a3447e10 88 }
38d2d43b 89
ad6226fb 90 if (!$user = get_record('user', 'id', $delete)) {
91 error("No such user!");
92 }
38d2d43b 93
ad6226fb 94 $primaryadmin = get_admin();
95 if ($user->id == $primaryadmin->id) {
96 error("You are not allowed to delete the primary admin user!");
0c30fdc9 97 }
f9903ed0 98
ad6226fb 99 if ($confirm != md5($delete)) {
100 $fullname = fullname($user, true);
101 print_heading(get_string('deleteuser', 'admin'));
102 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
103 notice_yesno(get_string('deletecheckfull', '', "'$fullname'"), 'user.php', 'user.php', $optionsyes, NULL, 'post', 'get');
104 admin_externalpage_print_footer($adminroot);
105 die;
106 } else if (data_submitted() and !$user->deleted) {
107 $updateuser = new object();
108 $updateuser->id = $user->id;
109 $updateuser->deleted = 1;
110 $updateuser->username = addslashes("$user->email.".time()); // Remember it just in case
111 $updateuser->email = ''; // Clear this field to free it up
112 $updateuser->idnumber = ''; // Clear this field to free it up
113 $updateuser->timemodified = time();
114 if (update_record('user', $updateuser)) {
115 // not sure if this is needed. unenrol_student($user->id); // From all courses
116 delete_records('role_assignments', 'userid', $user->id); // unassign all roles
117 // remove all context assigned on this user?
118 notify(get_string('deletedactivity', '', fullname($user, true)) );
119 } else {
120 notify(get_string('deletednot', '', fullname($user, true)));
2b25f2a0 121 }
a3447e10 122 }
ad6226fb 123 } else if ($acl and confirm_sesskey()) {
124 if (!has_capability('moodle/user:delete', $sitecontext)) {
125 // TODO: this should be under a separate capability
126 error('You are not permitted to modify the MNET access control list.');
4cd174bf 127 }
ad6226fb 128 if (!$user = get_record('user', 'id', $acl)) {
129 error("No such user.");
130 }
131 if (!is_mnet_remote_user($user)) {
132 error('Users in the MNET access control list must be remote MNET users.');
133 }
134 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
135 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
136 error('Invalid access parameter.');
137 }
138 $aclrecord = get_record('mnet_sso_access_control', 'username', $user->username, 'mnet_host_id', $user->mnethostid);
139 if (empty($aclrecord)) {
140 $aclrecord = new object();
141 $aclrecord->mnet_host_id = $user->mnethostid;
142 $aclrecord->username = $user->username;
143 $aclrecord->accessctrl = $accessctrl;
144 if (!insert_record('mnet_sso_access_control', $aclrecord)) {
145 error("Database error - Couldn't modify the MNET access control list.");
8516afd5 146 }
ad6226fb 147 } else {
148 $aclrecord->accessctrl = $accessctrl;
149 if (!update_record('mnet_sso_access_control', $aclrecord)) {
150 error("Database error - Couldn't modify the MNET access control list.");
0c30fdc9 151 }
ad6226fb 152 }
153 $mnethosts = get_records('mnet_host', '', '', 'id', 'id,wwwroot,name');
154 notify("MNET access control list updated: username '$user->username' from host '"
155 . $mnethosts[$user->mnethostid]->name
156 . "' access now set to '$accessctrl'.");
157 }
38d2d43b 158
ad6226fb 159 // Carry on with the user listing
b4a34829 160
ad6226fb 161 $columns = array("firstname", "lastname", "email", "city", "country", "lastaccess");
b4a34829 162
ad6226fb 163 foreach ($columns as $column) {
164 $string[$column] = get_string("$column");
165 if ($sort != $column) {
166 $columnicon = "";
167 if ($column == "lastaccess") {
168 $columndir = "DESC";
169 } else {
170 $columndir = "ASC";
d14383e0 171 }
ad6226fb 172 } else {
173 $columndir = $dir == "ASC" ? "DESC":"ASC";
174 if ($column == "lastaccess") {
175 $columnicon = $dir == "ASC" ? "up":"down";
d14383e0 176 } else {
ad6226fb 177 $columnicon = $dir == "ASC" ? "down":"up";
d14383e0 178 }
ad6226fb 179 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\" />";
180
bb09fb11 181 }
ad6226fb 182 $$column = "<a href=\"user.php?sort=$column&amp;dir=$columndir&amp;search=".urlencode(stripslashes($search))."&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial\">".$string[$column]."</a>$columnicon";
183 }
a3447e10 184
ad6226fb 185 if ($sort == "name") {
186 $sort = "firstname";
187 }
188
189 // tell the query which users we are looking at (local, remote, or both)
190 $remotewhere = '';
191 if($mnet_auth_users && ($localusers XOR $remoteusers)) {
192 if ($localusers) {
193 $remotewhere .= " and mnethostid = {$CFG->mnet_localhost_id} ";
194 } else {
195 $remotewhere .= " and mnethostid <> {$CFG->mnet_localhost_id} ";
196 }
197 }
198
199 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, $search, $firstinitial, $lastinitial, $remotewhere);
200 $usercount = get_users(false);
201 $usersearchcount = get_users(false, $search, true, "", "", $firstinitial, $lastinitial);
d578afc8 202
ad6226fb 203 if ($search or $firstinitial or $lastinitial) {
204 print_heading("$usersearchcount / $usercount ".get_string('users'));
205 $usercount = $usersearchcount;
206 } else {
207 print_heading("$usercount ".get_string('users'));
208 }
d578afc8 209
ad6226fb 210 $alphabet = explode(',', get_string('alphabet'));
211 $strall = get_string('all');
6c2ae454 212
d578afc8 213
ad6226fb 214 /// Bar of first initials
215
216 echo "<center><p align=\"center\">";
217 echo get_string("firstname")." : ";
218 if ($firstinitial) {
219 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
220 "perpage=$perpage&amp;lastinitial=$lastinitial\">$strall</a> ";
221 } else {
222 echo " <b>$strall</b> ";
223 }
224 foreach ($alphabet as $letter) {
225 if ($letter == $firstinitial) {
226 echo " <b>$letter</b> ";
227 } else {
228 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
229 "perpage=$perpage&amp;lastinitial=$lastinitial&amp;firstinitial=$letter\">$letter</a> ";
d14383e0 230 }
ad6226fb 231 }
232 echo "<br />";
233
234 /// Bar of last initials
235
236 echo get_string("lastname")." : ";
237 if ($lastinitial) {
238 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
239 "perpage=$perpage&amp;firstinitial=$firstinitial\">$strall</a> ";
240 } else {
241 echo " <b>$strall</b> ";
242 }
243 foreach ($alphabet as $letter) {
244 if ($letter == $lastinitial) {
245 echo " <b>$letter</b> ";
49e16355 246 } else {
ad6226fb 247 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
248 "perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$letter\">$letter</a> ";
49e16355 249 }
ad6226fb 250 }
251 echo "</p>";
252 echo "</center>";
6c2ae454 253
ad6226fb 254 print_paging_bar($usercount, $page, $perpage,
255 "user.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;search=".urlencode(stripslashes($search))."&amp;");
6c2ae454 256
ad6226fb 257 flush();
6c2ae454 258
6c2ae454 259
ad6226fb 260 if (!$users) {
261 $match = array();
262 if ($search !== '') {
263 $match[] = s($search);
488acd1b 264 }
ad6226fb 265 if ($firstinitial) {
266 $match[] = get_string('firstname').": $firstinitial"."___";
488acd1b 267 }
488acd1b 268 if ($lastinitial) {
ad6226fb 269 $match[] = get_string('lastname').": $lastinitial"."___";
488acd1b 270 }
ad6226fb 271 $matchstring = implode(", ", $match);
272 print_heading(get_string('nousersmatching', '', $matchstring));
488acd1b 273
ad6226fb 274 $table = NULL;
a3447e10 275
ad6226fb 276 } else {
ad81212e 277
ad6226fb 278 $countries = get_list_of_countries();
279 if (empty($mnethosts)) {
280 $mnethosts = get_records('mnet_host', '', '', 'id', 'id,wwwroot,name');
281 }
5833a6c8 282
ad6226fb 283 foreach ($users as $key => $user) {
284 if (!empty($user->country)) {
285 $users[$key]->country = $countries[$user->country];
4a6a574f 286 }
ad6226fb 287 }
288 if ($sort == "country") { // Need to resort by full country name, not code
289 foreach ($users as $user) {
290 $susers[$user->id] = $user->country;
ad81212e 291 }
ad6226fb 292 asort($susers);
293 foreach ($susers as $key => $value) {
294 $nusers[] = $users[$key];
ad81212e 295 }
ad6226fb 296 $users = $nusers;
297 }
488acd1b 298
ad6226fb 299 $mainadmin = get_admin();
300 $table->head = array ("$firstname / $lastname", $email, $city, $country, $lastaccess, "", "", "");
301 $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center");
302 $table->width = "95%";
303 foreach ($users as $user) {
304 if ($user->username == 'changeme' or $user->username == 'guest') {
305 continue; // do not dispaly dummy new user and guest here
d14383e0 306 }
488acd1b 307
ad6226fb 308 if ($user->id == $USER->id) {
309 $deletebutton = "";
310 } else {
311 if (has_capability('moodle/user:delete', $sitecontext)) {
312 $deletebutton = "<a href=\"user.php?delete=$user->id&amp;sesskey=$USER->sesskey\">$strdelete</a>";
313 } else {
314 $deletebutton ="";
488acd1b 315 }
c750592a 316 }
488acd1b 317
ad6226fb 318 if (has_capability('moodle/user:update', $sitecontext) and ($user->id==$USER->id or $user->id != $mainadmin->id) and !is_mnet_remote_user($user)) {
319 $editbutton = "<a href=\"../user/editadvanced.php?id=$user->id&amp;course=$site->id\">$stredit</a>";
320 if ($user->confirmed == 0) {
321 $confirmbutton = "<a href=\"user.php?confirmuser=$user->id&amp;sesskey=$USER->sesskey\">" . get_string('confirm') . "</a>";
488acd1b 322 } else {
ad6226fb 323 $confirmbutton = "";
488acd1b 324 }
ad6226fb 325 } else {
326 $editbutton ="";
327 if ($user->confirmed == 0) {
328 $confirmbutton = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
488acd1b 329 } else {
d14383e0 330 $confirmbutton = "";
d14383e0 331 }
ad6226fb 332 }
d14383e0 333
ad6226fb 334 // for remote users, shuffle columns around and display MNET stuff
335 if (is_mnet_remote_user($user)) {
336 $accessctrl = 'allow';
337 if ($acl = get_record('mnet_sso_access_control', 'username', $user->username, 'mnet_host_id', $user->mnethostid)) {
338 $accessctrl = $acl->accessctrl;
488acd1b 339 }
ad6226fb 340 $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
341 // delete button in confirm column - remote users should already be confirmed
342 // TODO: no delete for remote users, for now. new userid, delete flag, unique on username/host...
343 $confirmbutton = "";
344 // ACL in delete column
345 $deletebutton = get_string($accessctrl, 'mnet');
346 if (has_capability('moodle/user:delete', $sitecontext)) {
347 // TODO: this should be under a separate capability
348 $deletebutton .= " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey={$USER->sesskey}\">"
349 . get_string($changeaccessto, 'mnet') . " access</a>)";
350 }
351 // mnet info in edit column
352 $editbutton = $mnethosts[$user->mnethostid]->name;
8516afd5 353 }
2447921f 354
ad6226fb 355 if ($user->lastaccess) {
356 $strlastaccess = format_time(time() - $user->lastaccess);
7340c743 357 } else {
ad6226fb 358 $strlastaccess = get_string('never');
7340c743 359 }
ad6226fb 360 $fullname = fullname($user, true);
361
362 $table->data[] = array ("<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>",
363 "$user->email",
364 "$user->city",
365 "$user->country",
366 $strlastaccess,
367 $editbutton,
368 $deletebutton,
369 $confirmbutton);
d14383e0 370 }
ad6226fb 371 }
d14383e0 372
ad6226fb 373 if($mnet_auth_users) {
374 echo "<p style=\"text-align:center\">";
375 if ($localusers == 1 && $remoteusers == 1) {
376 echo '<a href="?lu=0">'.get_string('hidelocal','mnet').'</a> | ';
377 } elseif ($localusers == 0) {
378 echo '<a href="?lu=1">'.get_string('showlocal','mnet').'</a> | ';
379 } else {
380 echo get_string('hidelocal','mnet').' | ';
ab6d2992 381 }
ad6226fb 382 if ($localusers == 1 && $remoteusers == 1) {
383 echo '<a href="?ru=0">'.get_string('hideremote','mnet').'</a>';
384 } elseif ($remoteusers == 0) {
385 echo '<a href="?ru=1">'.get_string('showremote','mnet').'</a>';
386 } else {
387 echo get_string('hideremote','mnet');
388 }
389 echo "</p>";
390 }
38d2d43b 391
ad6226fb 392 echo "<table class=\"searchbox\" align=\"center\" cellpadding=\"10\"><tr><td>";
393 echo "<form action=\"user.php\" method=\"get\">";
394 echo "<input type=\"text\" name=\"search\" value=\"".s($search, true)."\" size=\"20\" />";
395 echo "<input type=\"submit\" value=\"$strsearch\" />";
396 if ($search) {
397 echo "<input type=\"button\" onclick=\"document.location='user.php';\" value=\"$strshowallusers\" />";
398 }
399 echo "</form>";
400 echo "</td></tr></table>";
401
402 if (has_capability('moodle/user:create', $sitecontext)) {
403 print_heading('<a href="../user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
404 }
405 if (!empty($table)) {
406 print_table($table);
407 print_paging_bar($usercount, $page, $perpage,
408 "user.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage".
409 "&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;search=".urlencode(stripslashes($search))."&amp;");
38d2d43b 410 if (has_capability('moodle/user:create', $sitecontext)) {
ad6226fb 411 print_heading('<a href="../user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
488acd1b 412 }
ad6226fb 413 }
6c2ae454 414
c750592a 415
ad6226fb 416 admin_externalpage_print_footer($adminroot);
417
f9903ed0 418
4cd174bf 419?>