3 require_once("../config.php");
4 require_once("../user/lib.php");
5 require_once("../lib/countries.php");
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";
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 !!!");
32 $admin->userid = $user->id;
34 if (! insert_record("user_admins", $admin)) {
35 error("Could not make user $user->id an admin !!!");
38 if (! $user = get_record("user", "id", $user->id)) { // Double check
39 error("User ID was incorrect (can't find it)");
42 if (! $site = get_site()) {
43 error("Could not find site-level course");
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 !!!");
54 $USER->loggedin = true;
55 $USER->site = $CFG->wwwroot;
57 $USER->teacher["$site->id"] = true;
59 redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$site->id");
62 if (! $site = get_site()) {
63 error("Could not find site-level course");
70 error("You must be an administrator to edit users this way.");
73 if ($newuser) { // Create a new user
74 $user->firstname = "";
76 $user->username = "changeme";
79 $user->lang = $CFG->lang;
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!");
89 redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$site->id");
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!");
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");
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"));
124 notify(get_string("deletednot", "", "$user->firstname $user->lastname"));
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") {
143 if ($columnsort == $sort) {
144 $$column = $string[$column];
146 $$column = "<A HREF=\"user.php?sort=$columnsort&dir=$columndir&search=$search\">".$string[$column]."</A>";
150 if ($sort == "name") {
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!");
158 notify(get_string("nousersmatching", "", $search));
163 $usercount = count($users);
164 print_heading("$usercount/$usertotalcount ".get_string("users"));
167 $a->end = $page + $recordsperpage;
168 if ($a->end > $usercount) {
169 $a->end = $usercount;
171 echo "<TABLE align=center cellpadding=10><TR>";
174 $prevpage = $page - $recordsperpage;
178 $options["dir"] = $dir;
179 $options["page"] = 0;
180 $options["sort"] = $sort;
181 $options["search"] = $search;
182 print_single_button("user.php", $options, " << ");
184 $options["page"] = $prevpage;
185 print_single_button("user.php", $options, " < ");
188 print_heading(get_string("displayingusers", "", $a));
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, " > ");
198 $options["page"] = $usercount-$recordsperpage;
199 print_single_button("user.php", $options, " >> ");
201 echo "</TD></TR></TABLE>";
205 foreach ($users as $key => $user) {
206 $users[$key]->country = $COUNTRIES[$user->country];
208 if ($sort == "country") { // Need to resort by full country name, not code
209 foreach ($users as $user) {
210 $susers[$user->id] = $user->country;
213 foreach ($susers as $key => $value) {
214 $nusers[] = $users[$key];
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") {
226 $deletebutton = "<A HREF=\"user.php?delete=$user->id\" TARGET=\"$strdeletecheck\">$strdelete</A>";
228 if ($user->lastaccess) {
229 $strlastaccess = format_time(time() - $user->lastaccess);
231 $strlastaccess = get_string("never");
233 $table->data[] = array ("<A HREF=\"../user/view.php?id=$user->id&course=$site->id\">$user->firstname $user->lastname</A>",
238 "<A HREF=\"../user/edit.php?id=$user->id&course=$site->id\">$stredit</A>",
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\">";
247 echo "<INPUT type=\"button\" onClick=\"document.location='user.php';\" value=\"$strshowallusers\">";
250 echo "</TD></TR></TABLE>";
254 print_heading("<A HREF=\"user.php?newuser=true\">".get_string("addnewuser")."</A>");