adding some strings
[moodle-linuxchix.git] / admin / user.php
blob8b19695ca2928a4098192eb785535e15102eccef
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 //following code is also used in auth sync scripts
113 $updateuser = new object();
114 $updateuser->id = $user->id;
115 $updateuser->deleted = 1;
116 $updateuser->username = addslashes("$user->email.".time()); // Remember it just in case
117 $updateuser->email = ''; // Clear this field to free it up
118 $updateuser->idnumber = ''; // Clear this field to free it up
119 $updateuser->timemodified = time();
120 if (update_record('user', $updateuser)) {
121 // not sure if this is needed. unenrol_student($user->id); // From all courses
122 delete_records('role_assignments', 'userid', $user->id); // unassign all roles
123 // remove all context assigned on this user?
124 notify(get_string('deletedactivity', '', fullname($user, true)) );
125 } else {
126 notify(get_string('deletednot', '', fullname($user, true)));
129 } else if ($acl and confirm_sesskey()) {
130 if (!has_capability('moodle/user:delete', $sitecontext)) {
131 // TODO: this should be under a separate capability
132 error('You are not permitted to modify the MNET access control list.');
134 if (!$user = get_record('user', 'id', $acl)) {
135 error("No such user.", '', true);
137 if (!is_mnet_remote_user($user)) {
138 error('Users in the MNET access control list must be remote MNET users.');
140 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
141 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
142 error('Invalid access parameter.');
144 $aclrecord = get_record('mnet_sso_access_control', 'username', $user->username, 'mnet_host_id', $user->mnethostid);
145 if (empty($aclrecord)) {
146 $aclrecord = new object();
147 $aclrecord->mnet_host_id = $user->mnethostid;
148 $aclrecord->username = $user->username;
149 $aclrecord->accessctrl = $accessctrl;
150 if (!insert_record('mnet_sso_access_control', $aclrecord)) {
151 error("Database error - Couldn't modify the MNET access control list.", '', true);
153 } else {
154 $aclrecord->accessctrl = $accessctrl;
155 if (!update_record('mnet_sso_access_control', $aclrecord)) {
156 error("Database error - Couldn't modify the MNET access control list.", '', true);
159 $mnethosts = get_records('mnet_host', '', '', 'id', 'id,wwwroot,name');
160 notify("MNET access control list updated: username '$user->username' from host '"
161 . $mnethosts[$user->mnethostid]->name
162 . "' access now set to '$accessctrl'.");
165 // Carry on with the user listing
167 $columns = array("firstname", "lastname", "email", "city", "country", "lastaccess");
169 foreach ($columns as $column) {
170 $string[$column] = get_string("$column");
171 if ($sort != $column) {
172 $columnicon = "";
173 if ($column == "lastaccess") {
174 $columndir = "DESC";
175 } else {
176 $columndir = "ASC";
178 } else {
179 $columndir = $dir == "ASC" ? "DESC":"ASC";
180 if ($column == "lastaccess") {
181 $columnicon = $dir == "ASC" ? "up":"down";
182 } else {
183 $columnicon = $dir == "ASC" ? "down":"up";
185 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\" />";
188 $$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";
191 if ($sort == "name") {
192 $sort = "firstname";
195 // tell the query which users we are looking at (local, remote, or both)
196 $remotewhere = '';
197 if($mnet_auth_users && ($localusers XOR $remoteusers)) {
198 if ($localusers) {
199 $remotewhere .= " and mnethostid = {$CFG->mnet_localhost_id} ";
200 } else {
201 $remotewhere .= " and mnethostid <> {$CFG->mnet_localhost_id} ";
205 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, $search, $firstinitial, $lastinitial, $remotewhere);
206 $usercount = get_users(false);
207 $usersearchcount = get_users(false, $search, true, "", "", $firstinitial, $lastinitial);
209 if ($search or $firstinitial or $lastinitial) {
210 print_heading("$usersearchcount / $usercount ".get_string('users'));
211 $usercount = $usersearchcount;
212 } else {
213 print_heading("$usercount ".get_string('users'));
216 $alphabet = explode(',', get_string('alphabet'));
217 $strall = get_string('all');
220 /// Bar of first initials
222 echo "<p style=\"text-align:center\">";
223 echo get_string("firstname")." : ";
224 if ($firstinitial) {
225 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
226 "perpage=$perpage&amp;lastinitial=$lastinitial\">$strall</a> ";
227 } else {
228 echo " <b>$strall</b> ";
230 foreach ($alphabet as $letter) {
231 if ($letter == $firstinitial) {
232 echo " <b>$letter</b> ";
233 } else {
234 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
235 "perpage=$perpage&amp;lastinitial=$lastinitial&amp;firstinitial=$letter\">$letter</a> ";
238 echo "<br />";
240 /// Bar of last initials
242 echo get_string("lastname")." : ";
243 if ($lastinitial) {
244 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
245 "perpage=$perpage&amp;firstinitial=$firstinitial\">$strall</a> ";
246 } else {
247 echo " <b>$strall</b> ";
249 foreach ($alphabet as $letter) {
250 if ($letter == $lastinitial) {
251 echo " <b>$letter</b> ";
252 } else {
253 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
254 "perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$letter\">$letter</a> ";
257 echo "</p>";
259 print_paging_bar($usercount, $page, $perpage,
260 "user.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;search=".urlencode(stripslashes($search))."&amp;");
262 flush();
265 if (!$users) {
266 $match = array();
267 if ($search !== '') {
268 $match[] = s($search);
270 if ($firstinitial) {
271 $match[] = get_string('firstname').": $firstinitial"."___";
273 if ($lastinitial) {
274 $match[] = get_string('lastname').": $lastinitial"."___";
276 $matchstring = implode(", ", $match);
277 print_heading(get_string('nousersmatching', '', $matchstring));
279 $table = NULL;
281 } else {
283 $countries = get_list_of_countries();
284 if (empty($mnethosts)) {
285 $mnethosts = get_records('mnet_host', '', '', 'id', 'id,wwwroot,name');
288 foreach ($users as $key => $user) {
289 if (!empty($user->country)) {
290 $users[$key]->country = $countries[$user->country];
293 if ($sort == "country") { // Need to resort by full country name, not code
294 foreach ($users as $user) {
295 $susers[$user->id] = $user->country;
297 asort($susers);
298 foreach ($susers as $key => $value) {
299 $nusers[] = $users[$key];
301 $users = $nusers;
304 $mainadmin = get_admin();
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="'.$securewwwroot.'/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="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
424 admin_externalpage_print_footer();