f9903ed0 |
1 | <?PHP // $Id$ |
2 | |
3 | require("../config.php"); |
4 | require("../user/lib.php"); |
3f63fdfb |
5 | require("../lib/countries.php"); |
f9903ed0 |
6 | |
584f0a10 |
7 | $recordsperpage = 100; |
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); |
d578afc8 |
15 | |
793f20b8 |
16 | if (! record_exists_sql("SELECT * FROM user_admins")) { // No admin user yet |
f9903ed0 |
17 | $user->firstname = "Admin"; |
18 | $user->lastname = "User"; |
19 | $user->username = "admin"; |
403bb50d |
20 | $user->password = md5("admin"); |
f9903ed0 |
21 | $user->email = "root@localhost"; |
22 | $user->confirmed = 1; |
177e50ed |
23 | $user->lang = $CFG->lang; |
793f20b8 |
24 | $user->maildisplay = 1; |
f9903ed0 |
25 | $user->timemodified = time(); |
26 | |
a3447e10 |
27 | if (! $user->id = insert_record("user", $user)) { |
28 | error("SERIOUS ERROR: Could not create admin user record !!!"); |
f9903ed0 |
29 | } |
30 | |
a3447e10 |
31 | $admin->user = $user->id; |
f9903ed0 |
32 | |
33 | if (! insert_record("user_admins", $admin)) { |
a3447e10 |
34 | error("Could not make user $user->id an admin !!!"); |
f9903ed0 |
35 | } |
36 | |
a3447e10 |
37 | if (! $user = get_record("user", "id", $user->id)) { // Double check |
f9903ed0 |
38 | error("User ID was incorrect (can't find it)"); |
39 | } |
40 | |
2b25f2a0 |
41 | if (! $site = get_site()) { |
f9903ed0 |
42 | error("Could not find site-level course"); |
43 | } |
44 | |
45 | $teacher->user = $user->id; |
2b25f2a0 |
46 | $teacher->course = $site->id; |
f9903ed0 |
47 | $teacher->authority = 1; |
48 | if (! insert_record("user_teachers", $teacher)) { |
49 | error("Could not make user $id a teacher of site-level course !!!"); |
50 | } |
51 | |
52 | $USER = $user; |
53 | $USER->loggedin = true; |
403bb50d |
54 | $USER->site = $CFG->wwwroot; |
f9903ed0 |
55 | $USER->admin = true; |
2b25f2a0 |
56 | $USER->teacher["$site->id"] = true; |
8223d271 |
57 | save_session("USER"); |
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"); |
98 | |
dfb6e4ac |
99 | print_header("$site->shortname: $stredituser", $site->fullname, |
2be8b28f |
100 | "<A HREF=\"index.php\">$stradministration</A> -> $stredituser"); |
bb09fb11 |
101 | |
d578afc8 |
102 | if ($delete) { // Delete a selected user, after confirmation |
103 | if (!$user = get_record("user", "id", "$delete")) { |
104 | error("No such user!"); |
105 | } |
106 | if ($confirm != md5($delete)) { |
107 | notice_yesno(get_string("deletecheckfull", "", "'$user->firstname $user->lastname'"), |
108 | "user.php?delete=$delete&confirm=".md5($delete), "user.php"); |
109 | |
110 | exit; |
66c95f32 |
111 | } else if (!$user->deleted) { |
d578afc8 |
112 | $user->deleted = "1"; |
113 | $user->username = $user->email; // Remember it just in case |
114 | $user->email = ""; // Clear this field to free it up |
115 | $user->timemodified = time(); |
116 | if (update_record("user", $user)) { |
117 | unenrol_student($user->id); // From all courses |
118 | remove_teacher($user->id); // From all courses |
119 | remove_admin($user->id); |
120 | notify(get_string("deletedactivity", "", "$user->firstname $user->lastname")); |
121 | } else { |
122 | notify(get_string("deletednot", "", "$user->firstname $user->lastname")); |
bb09fb11 |
123 | } |
124 | } |
125 | } |
a3447e10 |
126 | |
d578afc8 |
127 | // Carry on with the user listing |
128 | |
2ee50b1a |
129 | if (!$user = get_record_sql("SELECT count(*) as count FROM user WHERE username <> 'guest' AND deleted <> '1'")) { |
130 | error("Could not search for users?"); |
131 | } |
132 | |
133 | $usercount = $user->count; |
134 | |
d578afc8 |
135 | $columns = array("name", "email", "city", "country", "lastaccess"); |
136 | |
137 | foreach ($columns as $column) { |
138 | $string[$column] = get_string("$column"); |
139 | $columnsort = "$column"; |
140 | if ($column == "lastaccess") { |
141 | $columndir = "DESC"; |
142 | } else { |
143 | $columndir = "ASC"; |
144 | } |
145 | if ($columnsort == $sort) { |
146 | $$column = $string[$column]; |
147 | } else { |
148 | $$column = "<A HREF=\"user.php?sort=$columnsort&dir=$columndir\">".$string[$column]."</A>"; |
149 | } |
150 | } |
151 | |
152 | if ($sort == "name") { |
153 | $sort = "firstname"; |
154 | } |
155 | |
584f0a10 |
156 | if ($users = get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess from user WHERE username <> 'guest' |
157 | AND deleted <> '1' ORDER BY $sort $dir LIMIT $page,$recordsperpage")) { |
d578afc8 |
158 | |
2ee50b1a |
159 | print_heading("$usercount ".get_string("users")); |
ad81212e |
160 | |
2ee50b1a |
161 | $a->start = $page; |
162 | $a->end = $page + $recordsperpage; |
163 | if ($a->end > $usercount) { |
164 | $a->end = $usercount; |
165 | } |
166 | echo "<TABLE align=center cellpadding=10><TR>"; |
167 | echo "<TD>"; |
168 | if ($page) { |
169 | $prevpage = $page - $recordsperpage; |
170 | if ($prevpage < 0) { |
171 | $prevpage = 0; |
172 | } |
173 | $options["dir"] = $dir; |
174 | $options["page"] = 0; |
175 | $options["sort"] = $sort; |
176 | print_single_button("user.php", $options, " << "); |
177 | echo "</TD><TD>"; |
178 | $options["page"] = $prevpage; |
179 | print_single_button("user.php", $options, " < "); |
180 | } |
181 | echo "</TD><TD>"; |
182 | print_heading(get_string("displayingusers", "", $a)); |
183 | echo "</TD><TD>"; |
184 | $nextpage = $page + $recordsperpage; |
185 | if ($nextpage < $usercount) { |
186 | $options["dir"] = $dir; |
187 | $options["page"] = $nextpage; |
188 | $options["sort"] = $sort; |
189 | print_single_button("user.php", $options, " > "); |
190 | echo "</TD><TD>"; |
191 | $options["page"] = $usercount-$recordsperpage; |
192 | print_single_button("user.php", $options, " >> "); |
193 | } |
194 | echo "</TD></TR></TABLE>"; |
a3447e10 |
195 | |
ad81212e |
196 | flush(); |
197 | |
198 | foreach ($users as $key => $user) { |
199 | $users[$key]->country = $COUNTRIES[$user->country]; |
200 | } |
201 | if ($sort == "country") { // Need to resort by full country name, not code |
202 | foreach ($users as $user) { |
203 | $susers[$user->id] = $user->country; |
204 | } |
205 | asort($susers); |
206 | foreach ($susers as $key => $value) { |
207 | $nusers[] = $users[$key]; |
208 | } |
209 | $users = $nusers; |
210 | } |
211 | |
d578afc8 |
212 | $table->head = array ($name, $email, $city, $country, $lastaccess, "", ""); |
213 | $table->align = array ("LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "CENTER", "CENTER"); |
274f62e6 |
214 | $table->width = "95%"; |
a3447e10 |
215 | foreach ($users as $user) { |
d578afc8 |
216 | if ($user->id == $USER->id or $user->username == "changeme") { |
217 | $deletebutton = ""; |
218 | } else { |
219 | $deletebutton = "<A HREF=\"user.php?delete=$user->id\" TARGET=\"$strdeletecheck\">$strdelete</A>"; |
220 | } |
221 | if ($user->lastaccess) { |
222 | $strlastaccess = format_time(time() - $user->lastaccess); |
223 | } else { |
224 | $strlastaccess = get_string("never"); |
225 | } |
1d4bc6ed |
226 | $table->data[] = array ("<A HREF=\"../user/view.php?id=$user->id&course=$site->id\">$user->firstname $user->lastname</A>", |
bb09fb11 |
227 | "$user->email", |
228 | "$user->city", |
85382581 |
229 | "$user->country", |
d578afc8 |
230 | $strlastaccess, |
bb09fb11 |
231 | "<A HREF=\"../user/edit.php?id=$user->id&course=$site->id\">$stredit</A>", |
d578afc8 |
232 | $deletebutton); |
f9903ed0 |
233 | } |
a3447e10 |
234 | print_table($table); |
2447921f |
235 | |
236 | print_heading("<A HREF=\"user.php?newuser=true\">".get_string("addnewuser")."</A>"); |
a3447e10 |
237 | } else { |
238 | error("No users found!"); |
239 | |
f9903ed0 |
240 | } |
a3447e10 |
241 | print_footer(); |
f9903ed0 |
242 | } |
f9903ed0 |
243 | |
244 | ?> |