MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / admin / user.php
bloba68303d912f9ef91a2166182d0341c4ec5ad4f3a
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)
21 admin_externalpage_setup('editusers');
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 if (empty($CFG->loginhttps)) {
67 $securewwwroot = $CFG->wwwroot;
68 } else {
69 $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
72 admin_externalpage_print_header();
74 if ($confirmuser and confirm_sesskey()) {
75 if (!$user = get_record('user', 'id', $confirmuser)) {
76 error("No such user!", '', true);
79 $auth = get_auth_plugin($user->auth);
81 $result = $auth->user_confirm(addslashes($user->username), addslashes($user->secret));
83 if ($result == AUTH_CONFIRM_OK or $result == AUTH_CONFIRM_ALREADY) {
84 notify(get_string('userconfirmed', '', fullname($user, true)) );
85 } else {
86 notify(get_string('usernotconfirmed', '', fullname($user, true)));
89 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
91 if (!has_capability('moodle/user:delete', $sitecontext)) {
92 error('You do not have the required permission to delete a user.');
95 if (!$user = get_record('user', 'id', $delete)) {
96 error("No such user!", '', true);
99 $primaryadmin = get_admin();
100 if ($user->id == $primaryadmin->id) {
101 error("You are not allowed to delete the primary admin user!", '', true);
104 if ($confirm != md5($delete)) {
105 $fullname = fullname($user, true);
106 print_heading(get_string('deleteuser', 'admin'));
107 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
108 notice_yesno(get_string('deletecheckfull', '', "'$fullname'"), 'user.php', 'user.php', $optionsyes, NULL, 'post', 'get');
109 admin_externalpage_print_footer();
110 die;
111 } else if (data_submitted() and !$user->deleted) {
112 if (delete_user($user)) {
113 notify(get_string('deletedactivity', '', fullname($user, true)) );
114 } else {
115 notify(get_string('deletednot', '', fullname($user, true)));
118 } else if ($acl and confirm_sesskey()) {
119 if (!has_capability('moodle/user:delete', $sitecontext)) {
120 // TODO: this should be under a separate capability
121 error('You are not permitted to modify the MNET access control list.');
123 if (!$user = get_record('user', 'id', $acl)) {
124 error("No such user.", '', true);
126 if (!is_mnet_remote_user($user)) {
127 error('Users in the MNET access control list must be remote MNET users.');
129 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
130 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
131 error('Invalid access parameter.');
133 $aclrecord = get_record('mnet_sso_access_control', 'username', $user->username, 'mnet_host_id', $user->mnethostid);
134 if (empty($aclrecord)) {
135 $aclrecord = new object();
136 $aclrecord->mnet_host_id = $user->mnethostid;
137 $aclrecord->username = $user->username;
138 $aclrecord->accessctrl = $accessctrl;
139 if (!insert_record('mnet_sso_access_control', $aclrecord)) {
140 error("Database error - Couldn't modify the MNET access control list.", '', true);
142 } else {
143 $aclrecord->accessctrl = $accessctrl;
144 if (!update_record('mnet_sso_access_control', $aclrecord)) {
145 error("Database error - Couldn't modify the MNET access control list.", '', true);
148 $mnethosts = get_records('mnet_host', '', '', 'id', 'id,wwwroot,name');
149 notify("MNET access control list updated: username '$user->username' from host '"
150 . $mnethosts[$user->mnethostid]->name
151 . "' access now set to '$accessctrl'.");
154 // Carry on with the user listing
156 $columns = array("firstname", "lastname", "email", "city", "country", "lastaccess");
158 foreach ($columns as $column) {
159 $string[$column] = get_string("$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=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\" />";
177 $$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";
180 if ($sort == "name") {
181 $sort = "firstname";
184 // tell the query which users we are looking at (local, remote, or both)
185 $remotewhere = '';
186 if($mnet_auth_users && ($localusers XOR $remoteusers)) {
187 if ($localusers) {
188 $remotewhere .= " and mnethostid = {$CFG->mnet_localhost_id} ";
189 } else {
190 $remotewhere .= " and mnethostid <> {$CFG->mnet_localhost_id} ";
194 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, $search, $firstinitial, $lastinitial, $remotewhere);
195 $usercount = get_users(false);
196 $usersearchcount = get_users(false, $search, true, "", "", $firstinitial, $lastinitial);
198 if ($search or $firstinitial or $lastinitial) {
199 print_heading("$usersearchcount / $usercount ".get_string('users'));
200 $usercount = $usersearchcount;
201 } else {
202 print_heading("$usercount ".get_string('users'));
205 $alphabet = explode(',', get_string('alphabet'));
206 $strall = get_string('all');
209 /// Bar of first initials
211 echo "<p style=\"text-align:center\">";
212 echo get_string("firstname")." : ";
213 if ($firstinitial) {
214 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
215 "perpage=$perpage&amp;lastinitial=$lastinitial\">$strall</a> ";
216 } else {
217 echo " <b>$strall</b> ";
219 foreach ($alphabet as $letter) {
220 if ($letter == $firstinitial) {
221 echo " <b>$letter</b> ";
222 } else {
223 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
224 "perpage=$perpage&amp;lastinitial=$lastinitial&amp;firstinitial=$letter\">$letter</a> ";
227 echo "<br />";
229 /// Bar of last initials
231 echo get_string("lastname")." : ";
232 if ($lastinitial) {
233 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
234 "perpage=$perpage&amp;firstinitial=$firstinitial\">$strall</a> ";
235 } else {
236 echo " <b>$strall</b> ";
238 foreach ($alphabet as $letter) {
239 if ($letter == $lastinitial) {
240 echo " <b>$letter</b> ";
241 } else {
242 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
243 "perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$letter\">$letter</a> ";
246 echo "</p>";
248 print_paging_bar($usercount, $page, $perpage,
249 "user.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;search=".urlencode(stripslashes($search))."&amp;");
251 flush();
254 if (!$users) {
255 $match = array();
256 if ($search !== '') {
257 $match[] = s($search);
259 if ($firstinitial) {
260 $match[] = get_string('firstname').": $firstinitial"."___";
262 if ($lastinitial) {
263 $match[] = get_string('lastname').": $lastinitial"."___";
265 $matchstring = implode(", ", $match);
266 print_heading(get_string('nousersmatching', '', $matchstring));
268 $table = NULL;
270 } else {
272 $countries = get_list_of_countries();
273 if (empty($mnethosts)) {
274 $mnethosts = get_records('mnet_host', '', '', 'id', 'id,wwwroot,name');
277 foreach ($users as $key => $user) {
278 if (!empty($user->country)) {
279 $users[$key]->country = $countries[$user->country];
282 if ($sort == "country") { // Need to resort by full country name, not code
283 foreach ($users as $user) {
284 $susers[$user->id] = $user->country;
286 asort($susers);
287 foreach ($susers as $key => $value) {
288 $nusers[] = $users[$key];
290 $users = $nusers;
293 $mainadmin = get_admin();
295 $table->head = array ("$firstname / $lastname", $email, $city, $country, $lastaccess, "", "", "");
296 $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center");
297 $table->width = "95%";
298 foreach ($users as $user) {
299 if ($user->username == 'guest') {
300 continue; // do not dispaly dummy new user and guest here
303 if ($user->id == $USER->id) {
304 $deletebutton = "";
305 } else {
306 if (has_capability('moodle/user:delete', $sitecontext)) {
307 $deletebutton = "<a href=\"user.php?delete=$user->id&amp;sesskey=$USER->sesskey\">$strdelete</a>";
308 } else {
309 $deletebutton ="";
313 if (has_capability('moodle/user:update', $sitecontext) and ($user->id==$USER->id or $user->id != $mainadmin->id) and !is_mnet_remote_user($user)) {
314 $editbutton = "<a href=\"$securewwwroot/user/editadvanced.php?id=$user->id&amp;course=$site->id\">$stredit</a>";
315 if ($user->confirmed == 0) {
316 $confirmbutton = "<a href=\"user.php?confirmuser=$user->id&amp;sesskey=$USER->sesskey\">" . get_string('confirm') . "</a>";
317 } else {
318 $confirmbutton = "";
320 } else {
321 $editbutton ="";
322 if ($user->confirmed == 0) {
323 $confirmbutton = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
324 } else {
325 $confirmbutton = "";
329 // for remote users, shuffle columns around and display MNET stuff
330 if (is_mnet_remote_user($user)) {
331 $accessctrl = 'allow';
332 if ($acl = get_record('mnet_sso_access_control', 'username', $user->username, 'mnet_host_id', $user->mnethostid)) {
333 $accessctrl = $acl->accessctrl;
335 $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
336 // delete button in confirm column - remote users should already be confirmed
337 // TODO: no delete for remote users, for now. new userid, delete flag, unique on username/host...
338 $confirmbutton = "";
339 // ACL in delete column
340 $deletebutton = get_string($accessctrl, 'mnet');
341 if (has_capability('moodle/user:delete', $sitecontext)) {
342 // TODO: this should be under a separate capability
343 $deletebutton .= " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey={$USER->sesskey}\">"
344 . get_string($changeaccessto, 'mnet') . " access</a>)";
346 // mnet info in edit column
347 if (isset($mnethosts[$user->mnethostid])) {
348 $editbutton = $mnethosts[$user->mnethostid]->name;
352 if ($user->lastaccess) {
353 $strlastaccess = format_time(time() - $user->lastaccess);
354 } else {
355 $strlastaccess = get_string('never');
357 $fullname = fullname($user, true);
359 $table->data[] = array ("<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>",
360 "$user->email",
361 "$user->city",
362 "$user->country",
363 $strlastaccess,
364 $editbutton,
365 $deletebutton,
366 $confirmbutton);
370 if($mnet_auth_users) {
371 echo "<p style=\"text-align:center\">";
372 if ($localusers == 1 && $remoteusers == 1) {
373 echo '<a href="?lu=0">'.get_string('hidelocal','mnet').'</a> | ';
374 } elseif ($localusers == 0) {
375 echo '<a href="?lu=1">'.get_string('showlocal','mnet').'</a> | ';
376 } else {
377 echo get_string('hidelocal','mnet').' | ';
379 if ($localusers == 1 && $remoteusers == 1) {
380 echo '<a href="?ru=0">'.get_string('hideremote','mnet').'</a>';
381 } elseif ($remoteusers == 0) {
382 echo '<a href="?ru=1">'.get_string('showremote','mnet').'</a>';
383 } else {
384 echo get_string('hideremote','mnet');
386 echo "</p>";
389 echo "<table class=\"searchbox\" style=\"margin-left:auto;margin-right:auto\" cellpadding=\"10\"><tr><td>";
390 echo "<form action=\"user.php\" method=\"get\"><fieldset class=\"invisiblefieldset\">";
391 echo "<input type=\"text\" name=\"search\" value=\"".s($search, true)."\" size=\"20\" />";
392 echo "<input type=\"submit\" value=\"$strsearch\" />";
393 if ($search) {
394 echo "<input type=\"button\" onclick=\"document.location='user.php';\" value=\"$strshowallusers\" />";
396 echo "</fieldset></form>";
397 echo "</td></tr></table>";
399 if (has_capability('moodle/user:create', $sitecontext)) {
400 print_heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
402 if (!empty($table)) {
403 print_table($table);
404 print_paging_bar($usercount, $page, $perpage,
405 "user.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage".
406 "&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;search=".urlencode(stripslashes($search))."&amp;");
407 if (has_capability('moodle/user:create', $sitecontext)) {
408 print_heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
413 admin_externalpage_print_footer();