Minor fixes for XHTML compliance
[pfb-moodle.git] / admin / user.php
blob9a16a17dcb11752fe311b38e507b39c71f2ed411
1 <?php // $Id$
3 require_once('../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
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
13 $search = trim(optional_param('search', '', PARAM_RAW));
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
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)
20 $adminroot = admin_get_root();
21 admin_externalpage_setup('editusers', $adminroot);
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;
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;
37 if ($ru == 0 or $ru == 1) {
38 $_SESSION['admin-user-remoteusers'] = $ru;
40 if ($lu == 0 or $lu == 1) {
41 $_SESSION['admin-user-localusers'] = $lu;
43 $remoteusers = $_SESSION['admin-user-remoteusers'];
44 $localusers = $_SESSION['admin-user-localusers'];
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;
53 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
54 $site = get_site();
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.');
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');
66 admin_externalpage_print_header($adminroot);
68 if ($confirmuser and confirm_sesskey()) {
69 if (!$user = get_record('user', 'id', $confirmuser)) {
70 error("No such user!");
73 $confirmeduser = new object();
74 $confirmeduser->id = $confirmuser;
75 $confirmeduser->confirmed = 1;
76 $confirmeduser->timemodified = time();
78 if (update_record('user', $confirmeduser)) {
79 notify(get_string('userconfirmed', '', fullname($user, true)) );
80 } else {
81 notify(get_string('usernotconfirmed', '', fullname($user, true)));
84 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
86 if (!has_capability('moodle/user:delete', $sitecontext)) {
87 error('You do not have the required permission to delete a user.');
90 if (!$user = get_record('user', 'id', $delete)) {
91 error("No such user!");
94 $primaryadmin = get_admin();
95 if ($user->id == $primaryadmin->id) {
96 error("You are not allowed to delete the primary admin user!");
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 //following code is also used in auth sync scripts
108 $updateuser = new object();
109 $updateuser->id = $user->id;
110 $updateuser->deleted = 1;
111 $updateuser->username = addslashes("$user->email.".time()); // Remember it just in case
112 $updateuser->email = ''; // Clear this field to free it up
113 $updateuser->idnumber = ''; // Clear this field to free it up
114 $updateuser->timemodified = time();
115 if (update_record('user', $updateuser)) {
116 // not sure if this is needed. unenrol_student($user->id); // From all courses
117 delete_records('role_assignments', 'userid', $user->id); // unassign all roles
118 // remove all context assigned on this user?
119 notify(get_string('deletedactivity', '', fullname($user, true)) );
120 } else {
121 notify(get_string('deletednot', '', fullname($user, true)));
124 } else if ($acl and confirm_sesskey()) {
125 if (!has_capability('moodle/user:delete', $sitecontext)) {
126 // TODO: this should be under a separate capability
127 error('You are not permitted to modify the MNET access control list.');
129 if (!$user = get_record('user', 'id', $acl)) {
130 error("No such user.");
132 if (!is_mnet_remote_user($user)) {
133 error('Users in the MNET access control list must be remote MNET users.');
135 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
136 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
137 error('Invalid access parameter.');
139 $aclrecord = get_record('mnet_sso_access_control', 'username', $user->username, 'mnet_host_id', $user->mnethostid);
140 if (empty($aclrecord)) {
141 $aclrecord = new object();
142 $aclrecord->mnet_host_id = $user->mnethostid;
143 $aclrecord->username = $user->username;
144 $aclrecord->accessctrl = $accessctrl;
145 if (!insert_record('mnet_sso_access_control', $aclrecord)) {
146 error("Database error - Couldn't modify the MNET access control list.");
148 } else {
149 $aclrecord->accessctrl = $accessctrl;
150 if (!update_record('mnet_sso_access_control', $aclrecord)) {
151 error("Database error - Couldn't modify the MNET access control list.");
154 $mnethosts = get_records('mnet_host', '', '', 'id', 'id,wwwroot,name');
155 notify("MNET access control list updated: username '$user->username' from host '"
156 . $mnethosts[$user->mnethostid]->name
157 . "' access now set to '$accessctrl'.");
160 // Carry on with the user listing
162 $columns = array("firstname", "lastname", "email", "city", "country", "lastaccess");
164 foreach ($columns as $column) {
165 $string[$column] = get_string("$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" ? "up":"down";
177 } else {
178 $columnicon = $dir == "ASC" ? "down":"up";
180 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\" />";
183 $$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";
186 if ($sort == "name") {
187 $sort = "firstname";
190 // tell the query which users we are looking at (local, remote, or both)
191 $remotewhere = '';
192 if($mnet_auth_users && ($localusers XOR $remoteusers)) {
193 if ($localusers) {
194 $remotewhere .= " and mnethostid = {$CFG->mnet_localhost_id} ";
195 } else {
196 $remotewhere .= " and mnethostid <> {$CFG->mnet_localhost_id} ";
200 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, $search, $firstinitial, $lastinitial, $remotewhere);
201 $usercount = get_users(false);
202 $usersearchcount = get_users(false, $search, true, "", "", $firstinitial, $lastinitial);
204 if ($search or $firstinitial or $lastinitial) {
205 print_heading("$usersearchcount / $usercount ".get_string('users'));
206 $usercount = $usersearchcount;
207 } else {
208 print_heading("$usercount ".get_string('users'));
211 $alphabet = explode(',', get_string('alphabet'));
212 $strall = get_string('all');
215 /// Bar of first initials
217 echo "<p style=\"text-align:center\">";
218 echo get_string("firstname")." : ";
219 if ($firstinitial) {
220 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
221 "perpage=$perpage&amp;lastinitial=$lastinitial\">$strall</a> ";
222 } else {
223 echo " <b>$strall</b> ";
225 foreach ($alphabet as $letter) {
226 if ($letter == $firstinitial) {
227 echo " <b>$letter</b> ";
228 } else {
229 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
230 "perpage=$perpage&amp;lastinitial=$lastinitial&amp;firstinitial=$letter\">$letter</a> ";
233 echo "<br />";
235 /// Bar of last initials
237 echo get_string("lastname")." : ";
238 if ($lastinitial) {
239 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
240 "perpage=$perpage&amp;firstinitial=$firstinitial\">$strall</a> ";
241 } else {
242 echo " <b>$strall</b> ";
244 foreach ($alphabet as $letter) {
245 if ($letter == $lastinitial) {
246 echo " <b>$letter</b> ";
247 } else {
248 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
249 "perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$letter\">$letter</a> ";
252 echo "</p>";
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;");
257 flush();
260 if (!$users) {
261 $match = array();
262 if ($search !== '') {
263 $match[] = s($search);
265 if ($firstinitial) {
266 $match[] = get_string('firstname').": $firstinitial"."___";
268 if ($lastinitial) {
269 $match[] = get_string('lastname').": $lastinitial"."___";
271 $matchstring = implode(", ", $match);
272 print_heading(get_string('nousersmatching', '', $matchstring));
274 $table = NULL;
276 } else {
278 $countries = get_list_of_countries();
279 if (empty($mnethosts)) {
280 $mnethosts = get_records('mnet_host', '', '', 'id', 'id,wwwroot,name');
283 foreach ($users as $key => $user) {
284 if (!empty($user->country)) {
285 $users[$key]->country = $countries[$user->country];
288 if ($sort == "country") { // Need to resort by full country name, not code
289 foreach ($users as $user) {
290 $susers[$user->id] = $user->country;
292 asort($susers);
293 foreach ($susers as $key => $value) {
294 $nusers[] = $users[$key];
296 $users = $nusers;
299 $mainadmin = get_admin();
300 if(empty($CFG->loginhttps)) {
301 $securewwwroot = $CFG->wwwroot;
302 } else {
303 $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
306 $table->head = array ("$firstname / $lastname", $email, $city, $country, $lastaccess, "", "", "");
307 $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center");
308 $table->width = "95%";
309 foreach ($users as $user) {
310 if ($user->username == 'guest') {
311 continue; // do not dispaly dummy new user and guest here
314 if ($user->id == $USER->id) {
315 $deletebutton = "";
316 } else {
317 if (has_capability('moodle/user:delete', $sitecontext)) {
318 $deletebutton = "<a href=\"user.php?delete=$user->id&amp;sesskey=$USER->sesskey\">$strdelete</a>";
319 } else {
320 $deletebutton ="";
324 if (has_capability('moodle/user:update', $sitecontext) and ($user->id==$USER->id or $user->id != $mainadmin->id) and !is_mnet_remote_user($user)) {
325 $editbutton = "<a href=\"$securewwwroot/user/editadvanced.php?id=$user->id&amp;course=$site->id\">$stredit</a>";
326 if ($user->confirmed == 0) {
327 $confirmbutton = "<a href=\"user.php?confirmuser=$user->id&amp;sesskey=$USER->sesskey\">" . get_string('confirm') . "</a>";
328 } else {
329 $confirmbutton = "";
331 } else {
332 $editbutton ="";
333 if ($user->confirmed == 0) {
334 $confirmbutton = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
335 } else {
336 $confirmbutton = "";
340 // for remote users, shuffle columns around and display MNET stuff
341 if (is_mnet_remote_user($user)) {
342 $accessctrl = 'allow';
343 if ($acl = get_record('mnet_sso_access_control', 'username', $user->username, 'mnet_host_id', $user->mnethostid)) {
344 $accessctrl = $acl->accessctrl;
346 $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
347 // delete button in confirm column - remote users should already be confirmed
348 // TODO: no delete for remote users, for now. new userid, delete flag, unique on username/host...
349 $confirmbutton = "";
350 // ACL in delete column
351 $deletebutton = get_string($accessctrl, 'mnet');
352 if (has_capability('moodle/user:delete', $sitecontext)) {
353 // TODO: this should be under a separate capability
354 $deletebutton .= " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey={$USER->sesskey}\">"
355 . get_string($changeaccessto, 'mnet') . " access</a>)";
357 // mnet info in edit column
358 if (isset($mnethosts[$user->mnethostid])) {
359 $editbutton = $mnethosts[$user->mnethostid]->name;
363 if ($user->lastaccess) {
364 $strlastaccess = format_time(time() - $user->lastaccess);
365 } else {
366 $strlastaccess = get_string('never');
368 $fullname = fullname($user, true);
370 $table->data[] = array ("<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>",
371 "$user->email",
372 "$user->city",
373 "$user->country",
374 $strlastaccess,
375 $editbutton,
376 $deletebutton,
377 $confirmbutton);
381 if($mnet_auth_users) {
382 echo "<p style=\"text-align:center\">";
383 if ($localusers == 1 && $remoteusers == 1) {
384 echo '<a href="?lu=0">'.get_string('hidelocal','mnet').'</a> | ';
385 } elseif ($localusers == 0) {
386 echo '<a href="?lu=1">'.get_string('showlocal','mnet').'</a> | ';
387 } else {
388 echo get_string('hidelocal','mnet').' | ';
390 if ($localusers == 1 && $remoteusers == 1) {
391 echo '<a href="?ru=0">'.get_string('hideremote','mnet').'</a>';
392 } elseif ($remoteusers == 0) {
393 echo '<a href="?ru=1">'.get_string('showremote','mnet').'</a>';
394 } else {
395 echo get_string('hideremote','mnet');
397 echo "</p>";
400 echo "<table class=\"searchbox\" style=\"margin-left:auto;margin-right:auto\" cellpadding=\"10\"><tr><td>";
401 echo "<form action=\"user.php\" method=\"get\"><fieldset class=\"invisiblefieldset\">";
402 echo "<input type=\"text\" name=\"search\" value=\"".s($search, true)."\" size=\"20\" />";
403 echo "<input type=\"submit\" value=\"$strsearch\" />";
404 if ($search) {
405 echo "<input type=\"button\" onclick=\"document.location='user.php';\" value=\"$strshowallusers\" />";
407 echo "</fieldset></form>";
408 echo "</td></tr></table>";
410 if (has_capability('moodle/user:create', $sitecontext)) {
411 print_heading('<a href="../user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
413 if (!empty($table)) {
414 print_table($table);
415 print_paging_bar($usercount, $page, $perpage,
416 "user.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage".
417 "&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;search=".urlencode(stripslashes($search))."&amp;");
418 if (has_capability('moodle/user:create', $sitecontext)) {
419 print_heading('<a href="../user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
424 admin_externalpage_print_footer($adminroot);