Added a search form to the "edit users" admin page yay!
[moodle.git] / admin / user.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
b0e3a925 3 require_once("../config.php");
4 require_once("../user/lib.php");
5 require_once("../lib/countries.php");
f9903ed0 6
07358d40 7 $recordsperpage = 30;
584f0a10 8
d578afc8 9 optional_variable($newuser, "");
10 optional_variable($delete, "");
11 optional_variable($confirm, "");
12 optional_variable($sort, "name");
13 optional_variable($dir, "ASC");
584f0a10 14 optional_variable($page, 0);
c750592a 15 optional_variable($search, "");
d578afc8 16
9fa49e22 17 if (! record_exists("user_admins")) { // No admin user yet
f9903ed0 18 $user->firstname = "Admin";
19 $user->lastname = "User";
20 $user->username = "admin";
403bb50d 21 $user->password = md5("admin");
f9903ed0 22 $user->email = "root@localhost";
23 $user->confirmed = 1;
177e50ed 24 $user->lang = $CFG->lang;
793f20b8 25 $user->maildisplay = 1;
f9903ed0 26 $user->timemodified = time();
27
a3447e10 28 if (! $user->id = insert_record("user", $user)) {
29 error("SERIOUS ERROR: Could not create admin user record !!!");
f9903ed0 30 }
31
ebc3bd2b 32 $admin->userid = $user->id;
f9903ed0 33
34 if (! insert_record("user_admins", $admin)) {
a3447e10 35 error("Could not make user $user->id an admin !!!");
f9903ed0 36 }
37
a3447e10 38 if (! $user = get_record("user", "id", $user->id)) { // Double check
f9903ed0 39 error("User ID was incorrect (can't find it)");
40 }
41
2b25f2a0 42 if (! $site = get_site()) {
f9903ed0 43 error("Could not find site-level course");
44 }
45
ebc3bd2b 46 $teacher->userid = $user->id;
2b25f2a0 47 $teacher->course = $site->id;
f9903ed0 48 $teacher->authority = 1;
49 if (! insert_record("user_teachers", $teacher)) {
50 error("Could not make user $id a teacher of site-level course !!!");
51 }
52
53 $USER = $user;
54 $USER->loggedin = true;
403bb50d 55 $USER->site = $CFG->wwwroot;
f9903ed0 56 $USER->admin = true;
2b25f2a0 57 $USER->teacher["$site->id"] = true;
a3447e10 58
2b25f2a0 59 redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$site->id");
a3447e10 60
61 } else {
2b25f2a0 62 if (! $site = get_site()) {
a3447e10 63 error("Could not find site-level course");
64 }
f9903ed0 65 }
66
67 require_login();
68
69 if (!isadmin()) {
70 error("You must be an administrator to edit users this way.");
71 }
72
a3447e10 73 if ($newuser) { // Create a new user
2b25f2a0 74 $user->firstname = "";
75 $user->lastname = "";
76 $user->username = "changeme";
a3447e10 77 $user->password = "";
78 $user->email = "";
177e50ed 79 $user->lang = $CFG->lang;
a3447e10 80 $user->confirmed = 1;
81 $user->timemodified = time();
f9903ed0 82
a3447e10 83 if (! $user->id = insert_record("user", $user)) {
2b25f2a0 84 if (!$user = get_record("user", "username", "changeme")) { // half finished user from another time
85 error("Could not start a new user!");
86 }
a3447e10 87 }
f9903ed0 88
2b25f2a0 89 redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$site->id");
a3447e10 90
2b25f2a0 91 } else { // List all users for editing
f9903ed0 92
bb09fb11 93 $stredituser = get_string("edituser");
94 $stradministration = get_string("administration");
95 $stredit = get_string("edit");
96 $strdelete = get_string("delete");
97 $strdeletecheck = get_string("deletecheck");
c750592a 98 $strsearch = get_string("search");
99 $strshowallusers = get_string("showallusers");
bb09fb11 100
dfb6e4ac 101 print_header("$site->shortname: $stredituser", $site->fullname,
2be8b28f 102 "<A HREF=\"index.php\">$stradministration</A> -> $stredituser");
bb09fb11 103
d578afc8 104 if ($delete) { // Delete a selected user, after confirmation
105 if (!$user = get_record("user", "id", "$delete")) {
106 error("No such user!");
107 }
108 if ($confirm != md5($delete)) {
109 notice_yesno(get_string("deletecheckfull", "", "'$user->firstname $user->lastname'"),
110 "user.php?delete=$delete&confirm=".md5($delete), "user.php");
111
112 exit;
66c95f32 113 } else if (!$user->deleted) {
d578afc8 114 $user->deleted = "1";
dfe952f2 115 $user->username = "$user->email.".time(); // Remember it just in case
d578afc8 116 $user->email = ""; // Clear this field to free it up
117 $user->timemodified = time();
118 if (update_record("user", $user)) {
119 unenrol_student($user->id); // From all courses
120 remove_teacher($user->id); // From all courses
121 remove_admin($user->id);
122 notify(get_string("deletedactivity", "", "$user->firstname $user->lastname"));
123 } else {
124 notify(get_string("deletednot", "", "$user->firstname $user->lastname"));
bb09fb11 125 }
126 }
127 }
a3447e10 128
d578afc8 129 // Carry on with the user listing
130
c750592a 131 $usertotalcount = get_users_count();
2ee50b1a 132
d578afc8 133 $columns = array("name", "email", "city", "country", "lastaccess");
134
135 foreach ($columns as $column) {
136 $string[$column] = get_string("$column");
137 $columnsort = "$column";
138 if ($column == "lastaccess") {
139 $columndir = "DESC";
140 } else {
141 $columndir = "ASC";
142 }
143 if ($columnsort == $sort) {
144 $$column = $string[$column];
145 } else {
c750592a 146 $$column = "<A HREF=\"user.php?sort=$columnsort&dir=$columndir&search=$search\">".$string[$column]."</A>";
d578afc8 147 }
148 }
149
150 if ($sort == "name") {
151 $sort = "firstname";
152 }
153
c750592a 154 if (!$users = get_users_listing($sort, $dir, $page, $recordsperpage, $search)) {
155 if (!$users = get_users_listing($sort, $dir, $page, $recordsperpage)) {
156 error("No users found!");
157 } else {
158 notify(get_string("nousersmatching", "", $search));
159 $search = "";
2ee50b1a 160 }
c750592a 161 }
162
163 $usercount = count($users);
164 print_heading("$usercount/$usertotalcount ".get_string("users"));
165
166 $a->start = $page;
167 $a->end = $page + $recordsperpage;
168 if ($a->end > $usercount) {
169 $a->end = $usercount;
170 }
171 echo "<TABLE align=center cellpadding=10><TR>";
172 echo "<TD>";
173 if ($page) {
174 $prevpage = $page - $recordsperpage;
175 if ($prevpage < 0) {
176 $prevpage = 0;
2ee50b1a 177 }
c750592a 178 $options["dir"] = $dir;
179 $options["page"] = 0;
180 $options["sort"] = $sort;
181 $options["search"] = $search;
182 print_single_button("user.php", $options, " << ");
2ee50b1a 183 echo "</TD><TD>";
c750592a 184 $options["page"] = $prevpage;
185 print_single_button("user.php", $options, " < ");
186 }
187 echo "</TD><TD>";
188 print_heading(get_string("displayingusers", "", $a));
189 echo "</TD><TD>";
190 $nextpage = $page + $recordsperpage;
191 if ($nextpage < $usercount) {
192 $options["dir"] = $dir;
193 $options["page"] = $nextpage;
194 $options["sort"] = $sort;
195 $options["search"] = $search;
196 print_single_button("user.php", $options, " > ");
2ee50b1a 197 echo "</TD><TD>";
c750592a 198 $options["page"] = $usercount-$recordsperpage;
199 print_single_button("user.php", $options, " >> ");
200 }
201 echo "</TD></TR></TABLE>";
a3447e10 202
c750592a 203 flush();
ad81212e 204
c750592a 205 foreach ($users as $key => $user) {
206 $users[$key]->country = $COUNTRIES[$user->country];
207 }
208 if ($sort == "country") { // Need to resort by full country name, not code
209 foreach ($users as $user) {
210 $susers[$user->id] = $user->country;
ad81212e 211 }
c750592a 212 asort($susers);
213 foreach ($susers as $key => $value) {
214 $nusers[] = $users[$key];
ad81212e 215 }
c750592a 216 $users = $nusers;
217 }
ad81212e 218
c750592a 219 $table->head = array ($name, $email, $city, $country, $lastaccess, "", "");
220 $table->align = array ("LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "CENTER", "CENTER");
221 $table->width = "95%";
222 foreach ($users as $user) {
223 if ($user->id == $USER->id or $user->username == "changeme") {
224 $deletebutton = "";
225 } else {
226 $deletebutton = "<A HREF=\"user.php?delete=$user->id\" TARGET=\"$strdeletecheck\">$strdelete</A>";
f9903ed0 227 }
c750592a 228 if ($user->lastaccess) {
229 $strlastaccess = format_time(time() - $user->lastaccess);
230 } else {
231 $strlastaccess = get_string("never");
232 }
233 $table->data[] = array ("<A HREF=\"../user/view.php?id=$user->id&course=$site->id\">$user->firstname $user->lastname</A>",
234 "$user->email",
235 "$user->city",
236 "$user->country",
237 $strlastaccess,
238 "<A HREF=\"../user/edit.php?id=$user->id&course=$site->id\">$stredit</A>",
239 $deletebutton);
240 }
2447921f 241
c750592a 242 echo "<TABLE align=center cellpadding=10><TR><TD>";
243 echo "<FORM ACTION=user.php METHOD=POST>";
244 echo "<INPUT TYPE=text NAME=search VALUE=\"$search\" SIZE=20>";
245 echo "<INPUT TYPE=submit VALUE=\"$strsearch\">";
246 if ($search) {
247 echo "<INPUT type=\"button\" onClick=\"document.location='user.php';\" value=\"$strshowallusers\">";
f9903ed0 248 }
c750592a 249 echo "</FORM>";
250 echo "</TD></TR></TABLE>";
251
252 print_table($table);
253
254 print_heading("<A HREF=\"user.php?newuser=true\">".get_string("addnewuser")."</A>");
255
a3447e10 256 print_footer();
f9903ed0 257 }
f9903ed0 258
259?>