OKAY! THIS IS IT! MOODLE 1.6!
[moodle.git] / admin / user.php
blob01814b820c76dd9a6073beb0f73feed073a471e2
1 <?php // $Id$
3 require_once('../config.php');
5 $newuser = optional_param('newuser', 0, PARAM_BOOL);
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 = 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
17 $search = trim($search);
19 $user = new object();
20 $admin = new object();
21 $teacher = new object();
23 if (! record_exists("user_admins")) { // No admin user yet
25 $user->firstname = get_string("admin");
26 $user->lastname = get_string("user");
27 $user->username = "admin";
28 $user->password = hash_internal_user_password("admin");
29 $user->email = "root@localhost";
30 $user->confirmed = 1;
31 $user->lang = $CFG->lang;
32 $user->maildisplay = 1;
33 $user->timemodified = time();
35 if (! $user->id = insert_record("user", $user)) {
36 error("SERIOUS ERROR: Could not create admin user record !!!");
39 $admin->userid = $user->id;
41 if (! insert_record("user_admins", $admin)) {
42 error("Could not make user $user->id an admin !!!");
45 if (! $user = get_record("user", "id", $user->id)) { // Double check
46 error("User ID was incorrect (can't find it)");
49 if (! $site = get_site()) {
50 error("Could not find site-level course");
53 $teacher->userid = $user->id;
54 $teacher->course = $site->id;
55 $teacher->authority = 1;
56 if (! insert_record("user_teachers", $teacher)) {
57 error("Could not make user $id a teacher of site-level course !!!");
60 $USER = $user;
61 $USER->loggedin = true;
62 $USER->sessionIP = md5(getremoteaddr()); // Store the current IP in the session
63 $USER->site = $CFG->wwwroot;
64 $USER->admin = true;
65 $USER->teacher["$site->id"] = true;
66 $USER->newadminuser = true;
67 sesskey(); // for added security, used to check script parameters
69 redirect("$CFG->wwwroot/user/edit.php?id=$user->id&amp;course=$site->id");
70 exit;
72 } else {
73 if (! $site = get_site()) {
74 error("Could not find site-level course");
78 require_login();
80 if (!isadmin()) {
81 error("You must be an administrator to edit users this way.");
84 if ($newuser and confirm_sesskey()) { // Create a new user
85 $user->auth = "manual";
86 $user->firstname = "";
87 $user->lastname = "";
88 $user->username = "changeme";
89 $user->password = "";
90 $user->email = "";
91 $user->lang = $CFG->lang;
92 $user->confirmed = 1;
93 $user->timemodified = time();
95 if (! $user->id = insert_record("user", $user)) {
96 if (!$user = get_record("user", "username", "changeme")) { // half finished user from another time
97 error("Could not start a new user!");
101 redirect("$CFG->wwwroot/user/edit.php?id=$user->id&amp;course=$site->id");
103 } else { // List all users for editing
105 $stredituser = get_string("edituser");
106 $stradministration = get_string("administration");
107 $strusers = get_string("users");
108 $stredit = get_string("edit");
109 $strdelete = get_string("delete");
110 $strdeletecheck = get_string("deletecheck");
111 $strsearch = get_string("search");
112 $strshowallusers = get_string("showallusers");
114 if ($firstinitial or $lastinitial or $search or $page) {
115 print_header("$site->shortname: $stredituser", $site->fullname,
116 "<a href=\"index.php\">$stradministration</a> -> ".
117 "<a href=\"users.php\">$strusers</a> -> ".
118 "<a href=\"user.php\">$stredituser</a>");
119 } else {
120 print_header("$site->shortname: $stredituser", $site->fullname,
121 "<a href=\"index.php\">$stradministration</a> -> ".
122 "<a href=\"users.php\">$strusers</a> -> $stredituser");
125 if ($confirmuser and confirm_sesskey()) {
126 if (!$user = get_record("user", "id", "$confirmuser")) {
127 error("No such user!");
130 $confirmeduser = new object();
131 $confirmeduser->id = $confirmuser;
132 $confirmeduser->confirmed = 1;
133 $confirmeduser->timemodified = time();
135 if (update_record("user", $confirmeduser)) {
136 notify(get_string("userconfirmed", "", fullname($user, true)) );
137 } else {
138 notify(get_string("usernotconfirmed", "", fullname($user, true)));
141 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
142 if (!$user = get_record("user", "id", "$delete")) {
143 error("No such user!");
146 $primaryadmin = get_admin();
147 if ($user->id == $primaryadmin->id) {
148 error("You are not allowed to delete the primary admin user!");
151 if ($confirm != md5($delete)) {
152 $fullname = fullname($user, true);
153 notice_yesno(get_string("deletecheckfull", "", "'$fullname'"),
154 "user.php?delete=$delete&amp;confirm=".md5($delete)."&amp;sesskey=$USER->sesskey", "user.php");
156 exit;
157 } else if (!$user->deleted) {
158 $updateuser = new object();
159 $updateuser->id = $user->id;
160 $updateuser->deleted = "1";
161 $updateuser->username = "$user->email.".time(); // Remember it just in case
162 $updateuser->email = ""; // Clear this field to free it up
163 $updateuser->idnumber = ""; // Clear this field to free it up
164 $updateuser->timemodified = time();
165 if (update_record("user", $updateuser)) {
166 unenrol_student($user->id); // From all courses
167 remove_teacher($user->id); // From all courses
168 remove_admin($user->id);
169 notify(get_string("deletedactivity", "", fullname($user, true)) );
170 } else {
171 notify(get_string("deletednot", "", fullname($user, true)));
176 // Carry on with the user listing
178 $columns = array("firstname", "lastname", "email", "city", "country", "lastaccess");
180 foreach ($columns as $column) {
181 $string[$column] = get_string("$column");
182 if ($sort != $column) {
183 $columnicon = "";
184 if ($column == "lastaccess") {
185 $columndir = "DESC";
186 } else {
187 $columndir = "ASC";
189 } else {
190 $columndir = $dir == "ASC" ? "DESC":"ASC";
191 if ($column == "lastaccess") {
192 $columnicon = $dir == "ASC" ? "up":"down";
193 } else {
194 $columnicon = $dir == "ASC" ? "down":"up";
196 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\" />";
199 $$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";
202 if ($sort == "name") {
203 $sort = "firstname";
206 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, $search, $firstinitial, $lastinitial);
207 $usercount = get_users(false);
208 $usersearchcount = get_users(false, $search, true, "", "", $firstinitial, $lastinitial);
210 if ($search or $firstinitial or $lastinitial) {
211 print_heading("$usersearchcount / $usercount ".get_string("users"));
212 $usercount = $usersearchcount;
213 } else {
214 print_heading("$usercount ".get_string("users"));
217 $alphabet = explode(',', get_string('alphabet'));
218 $strall = get_string("all");
221 /// Bar of first initials
223 echo "<center><p align=\"center\">";
224 echo get_string("firstname")." : ";
225 if ($firstinitial) {
226 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
227 "perpage=$perpage&amp;lastinitial=$lastinitial\">$strall</a> ";
228 } else {
229 echo " <b>$strall</b> ";
231 foreach ($alphabet as $letter) {
232 if ($letter == $firstinitial) {
233 echo " <b>$letter</b> ";
234 } else {
235 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
236 "perpage=$perpage&amp;lastinitial=$lastinitial&amp;firstinitial=$letter\">$letter</a> ";
239 echo "<br />";
241 /// Bar of last initials
243 echo get_string("lastname")." : ";
244 if ($lastinitial) {
245 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
246 "perpage=$perpage&amp;firstinitial=$firstinitial\">$strall</a> ";
247 } else {
248 echo " <b>$strall</b> ";
250 foreach ($alphabet as $letter) {
251 if ($letter == $lastinitial) {
252 echo " <b>$letter</b> ";
253 } else {
254 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
255 "perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$letter\">$letter</a> ";
258 echo "</p>";
259 echo "</center>";
261 print_paging_bar($usercount, $page, $perpage,
262 "user.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;search=".urlencode(stripslashes($search))."&amp;");
264 flush();
267 if (!$users) {
268 $match = array();
269 if ($search !== '') {
270 $match[] = s($search);
272 if ($firstinitial) {
273 $match[] = get_string("firstname").": $firstinitial"."___";
275 if ($lastinitial) {
276 $match[] = get_string("lastname").": $lastinitial"."___";
278 $matchstring = implode(", ", $match);
279 print_heading(get_string("nousersmatching", "", $matchstring));
281 $table = NULL;
283 } else {
285 $countries = get_list_of_countries();
287 foreach ($users as $key => $user) {
288 if (!empty($user->country)) {
289 $users[$key]->country = $countries[$user->country];
292 if ($sort == "country") { // Need to resort by full country name, not code
293 foreach ($users as $user) {
294 $susers[$user->id] = $user->country;
296 asort($susers);
297 foreach ($susers as $key => $value) {
298 $nusers[] = $users[$key];
300 $users = $nusers;
303 $table->head = array ("$firstname / $lastname", $email, $city, $country, $lastaccess, "", "", "");
304 $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center");
305 $table->width = "95%";
306 foreach ($users as $user) {
307 if ($user->id == $USER->id or $user->username == "changeme") {
308 $deletebutton = "";
309 } else {
310 $deletebutton = "<a href=\"user.php?delete=$user->id&amp;sesskey=$USER->sesskey\">$strdelete</a>";
312 if ($user->lastaccess) {
313 $strlastaccess = format_time(time() - $user->lastaccess);
314 } else {
315 $strlastaccess = get_string("never");
317 if ($user->confirmed == 0) {
318 $confirmbutton = "<a href=\"user.php?confirmuser=$user->id&amp;sesskey=$USER->sesskey\">" . get_string("confirm") . "</a>";
319 } else {
320 $confirmbutton = "";
322 $fullname = fullname($user, true);
323 $table->data[] = array ("<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>",
324 "$user->email",
325 "$user->city",
326 "$user->country",
327 $strlastaccess,
328 "<a href=\"../user/edit.php?id=$user->id&amp;course=$site->id\">$stredit</a>",
329 $deletebutton,
330 $confirmbutton);
334 echo "<table class=\"searchbox\" align=\"center\" cellpadding=\"10\"><tr><td>";
335 echo "<form action=\"user.php\" method=\"get\">";
336 echo "<input type=\"text\" name=\"search\" value=\"".s($search, true)."\" size=\"20\" />";
337 echo "<input type=\"submit\" value=\"$strsearch\" />";
338 if ($search) {
339 echo "<input type=\"button\" onclick=\"document.location='user.php';\" value=\"$strshowallusers\" />";
341 echo "</form>";
342 echo "</td></tr></table>";
343 print_heading("<a href=\"user.php?newuser=true&amp;sesskey=$USER->sesskey\">".get_string("addnewuser")."</a>");
345 if (!empty($table)) {
346 print_table($table);
347 print_paging_bar($usercount, $page, $perpage,
348 "user.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage".
349 "&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;search=".urlencode(stripslashes($search))."&amp;");
350 print_heading("<a href=\"user.php?newuser=true&amp;sesskey=$USER->sesskey\">".get_string("addnewuser")."</a>");
354 print_footer();