Added a search form to the "edit users" admin page yay!
[moodle.git] / admin / user.php
1 <?PHP // $Id$
3         require_once("../config.php");
4         require_once("../user/lib.php");
5     require_once("../lib/countries.php");
7     $recordsperpage = 30;
9     optional_variable($newuser, "");
10     optional_variable($delete, "");
11     optional_variable($confirm, "");
12     optional_variable($sort, "name");
13     optional_variable($dir, "ASC");
14     optional_variable($page, 0);
15     optional_variable($search, "");
17     if (! record_exists("user_admins")) {   // No admin user yet
18         $user->firstname = "Admin";
19         $user->lastname  = "User";
20         $user->username  = "admin";
21         $user->password  = md5("admin");
22         $user->email     = "root@localhost";
23         $user->confirmed = 1;
24         $user->lang = $CFG->lang;
25         $user->maildisplay = 1;
26         $user->timemodified = time();
28         if (! $user->id = insert_record("user", $user)) {
29             error("SERIOUS ERROR: Could not create admin user record !!!");
30         }
32         $admin->userid = $user->id;
34         if (! insert_record("user_admins", $admin)) {
35             error("Could not make user $user->id an admin !!!");
36         }
38         if (! $user = get_record("user", "id", $user->id)) {     // Double check
39             error("User ID was incorrect (can't find it)");
40         }
42         if (! $site = get_site()) {
43             error("Could not find site-level course");
44         }
46         $teacher->userid = $user->id;
47         $teacher->course = $site->id;
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         }
53         $USER = $user;
54         $USER->loggedin = true;
55         $USER->site = $CFG->wwwroot;
56         $USER->admin = true;
57         $USER->teacher["$site->id"] = true;
59         redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$site->id");
61     } else {
62         if (! $site = get_site()) {
63             error("Could not find site-level course");
64         }
65     }
67     require_login();
69     if (!isadmin()) {
70         error("You must be an administrator to edit users this way.");
71     }
73     if ($newuser) {                 // Create a new user
74         $user->firstname = "";
75         $user->lastname  = "";
76         $user->username  = "changeme";
77         $user->password  = "";
78         $user->email     = "";
79         $user->lang      = $CFG->lang;
80         $user->confirmed = 1;
81         $user->timemodified = time();
83         if (! $user->id = insert_record("user", $user)) {
84             if (!$user = get_record("user", "username", "changeme")) {   // half finished user from another time
85                 error("Could not start a new user!");
86             }
87         }
89         redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$site->id");
90         
91     } else {                        // List all users for editing
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");
98         $strsearch = get_string("search");
99         $strshowallusers = get_string("showallusers");
101         print_header("$site->shortname: $stredituser", $site->fullname, 
102                      "<A HREF=\"index.php\">$stradministration</A> -> $stredituser");
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");
112                 exit;
113             } else if (!$user->deleted) {
114                 $user->deleted = "1";
115                 $user->username = "$user->email.".time();  // Remember it just in case
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"));
125                 }
126             }
127         }
129         // Carry on with the user listing
131         $usertotalcount = get_users_count();
133         $columns = array("name", "email", "city", "country", "lastaccess");
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 {
146                $$column = "<A HREF=\"user.php?sort=$columnsort&dir=$columndir&search=$search\">".$string[$column]."</A>";
147             }
148         }
150         if ($sort == "name") {
151             $sort = "firstname";
152         }
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 = "";
160             }
161         }
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;
177             }
178             $options["dir"] = $dir;
179             $options["page"] = 0;
180             $options["sort"] = $sort;
181             $options["search"] = $search;
182             print_single_button("user.php", $options, "  <<  ");
183             echo "</TD><TD>";
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, "  >  ");
197             echo "</TD><TD>";
198             $options["page"] = $usercount-$recordsperpage;
199             print_single_button("user.php", $options, "  >>  ");
200         }
201         echo "</TD></TR></TABLE>";
203         flush();
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;
211             }
212             asort($susers);
213             foreach ($susers as $key => $value) {
214                 $nusers[] = $users[$key];
215             }
216             $users = $nusers;
217         }
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>";
227             }
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         }
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\">";
248         }
249             echo "</FORM>";     
250         echo "</TD></TR></TABLE>";
252         print_table($table);
254         print_heading("<A HREF=\"user.php?newuser=true\">".get_string("addnewuser")."</A>");
256         print_footer();
257     }
259 ?>