3 require_once("../config.php");
7 optional_variable($newuser, "");
8 optional_variable($delete, "");
9 optional_variable($confirm, "");
10 optional_variable($sort, "name");
11 optional_variable($dir, "ASC");
12 optional_variable($page, 0);
13 optional_variable($search, "");
19 if (! record_exists("user_admins")) { // No admin user yet
21 $user->firstname
= get_string("admin");
22 $user->lastname
= get_string("user");
23 $user->username
= "admin";
24 $user->password
= md5("admin");
25 $user->email
= "root@localhost";
27 $user->lang
= $CFG->lang
;
28 $user->maildisplay
= 1;
29 $user->timemodified
= time();
31 if (! $user->id
= insert_record("user", $user)) {
32 error("SERIOUS ERROR: Could not create admin user record !!!");
35 $admin->userid
= $user->id
;
37 if (! insert_record("user_admins", $admin)) {
38 error("Could not make user $user->id an admin !!!");
41 if (! $user = get_record("user", "id", $user->id
)) { // Double check
42 error("User ID was incorrect (can't find it)");
45 if (! $site = get_site()) {
46 error("Could not find site-level course");
49 $teacher->userid
= $user->id
;
50 $teacher->course
= $site->id
;
51 $teacher->authority
= 1;
52 if (! insert_record("user_teachers", $teacher)) {
53 error("Could not make user $id a teacher of site-level course !!!");
57 $USER->loggedin
= true;
58 $USER->site
= $CFG->wwwroot
;
60 $USER->teacher
["$site->id"] = true;
61 $USER->newadminuser
= true;
63 redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$site->id");
67 if (! $site = get_site()) {
68 error("Could not find site-level course");
75 error("You must be an administrator to edit users this way.");
78 if ($newuser) { // Create a new user
79 $user->firstname
= "";
81 $user->username
= "changeme";
84 $user->lang
= $CFG->lang
;
86 $user->timemodified
= time();
88 if (! $user->id
= insert_record("user", $user)) {
89 if (!$user = get_record("user", "username", "changeme")) { // half finished user from another time
90 error("Could not start a new user!");
94 redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$site->id");
96 } else { // List all users for editing
98 $stredituser = get_string("edituser");
99 $stradministration = get_string("administration");
100 $strusers = get_string("users");
101 $stredit = get_string("edit");
102 $strdelete = get_string("delete");
103 $strdeletecheck = get_string("deletecheck");
104 $strsearch = get_string("search");
105 $strshowallusers = get_string("showallusers");
107 print_header("$site->shortname: $stredituser", $site->fullname
,
108 "<a href=\"index.php\">$stradministration</a> -> <a href=\"users.php\">$strusers</a> -> $stredituser");
110 if ($delete) { // Delete a selected user, after confirmation
111 if (!$user = get_record("user", "id", "$delete")) {
112 error("No such user!");
115 $primaryadmin = get_admin();
116 if ($user->id
== $primaryadmin->id
) {
117 error("You are not allowed to delete the primary admin user!");
120 if ($confirm != md5($delete)) {
121 $fullname = fullname($user, true);
122 notice_yesno(get_string("deletecheckfull", "", "'$fullname'"),
123 "user.php?delete=$delete&confirm=".md5($delete), "user.php");
126 } else if (!$user->deleted
) {
128 $updateuser->id
= $user->id
;
129 $updateuser->deleted
= "1";
130 $updateuser->username
= "$user->email.".time(); // Remember it just in case
131 $updateuser->email
= ""; // Clear this field to free it up
132 $updateuser->timemodified
= time();
133 if (update_record("user", $updateuser)) {
134 unenrol_student($user->id
); // From all courses
135 remove_teacher($user->id
); // From all courses
136 remove_admin($user->id
);
137 notify(get_string("deletedactivity", "", fullname($user, true)) );
139 notify(get_string("deletednot", "", fullname($user, true)));
144 // Carry on with the user listing
147 $columns = array("name", "email", "city", "country", "lastaccess");
149 foreach ($columns as $column) {
150 $string[$column] = get_string("$column");
151 $columnsort = "$column";
152 if ($column == "lastaccess") {
157 if ($columnsort == $sort) {
158 $
$column = $string[$column];
160 $
$column = "<A HREF=\"user.php?sort=$columnsort&dir=$columndir&search=$search\">".$string[$column]."</A>";
164 if ($sort == "name") {
168 if (!$users = get_users_listing($sort, $dir, $page, $recordsperpage, $search)) {
169 if (!$users = get_users_listing($sort, $dir, 0, $recordsperpage)) {
170 error("No users found!");
172 notify(get_string("nousersmatching", "", $search));
177 $usercount = get_users(false);
180 $usersearchcount = get_users(false, $search);
181 print_heading("$usersearchcount / $usercount ".get_string("users"));
182 $usercount = $usersearchcount;
184 print_heading("$usercount ".get_string("users"));
188 $a->end
= $page +
$recordsperpage;
189 if ($a->end
> $usercount) {
190 $a->end
= $usercount;
192 echo "<TABLE align=center cellpadding=10><TR>";
195 $prevpage = $page - $recordsperpage;
199 $options["dir"] = $dir;
200 $options["page"] = 0;
201 $options["sort"] = $sort;
202 $options["search"] = $search;
203 print_single_button("user.php", $options, " << ");
205 $options["page"] = $prevpage;
206 print_single_button("user.php", $options, " < ");
209 print_heading(get_string("displayingusers", "", $a));
211 $nextpage = $page +
$recordsperpage;
212 if ($nextpage < $usercount) {
213 $options["dir"] = $dir;
214 $options["page"] = $nextpage;
215 $options["sort"] = $sort;
216 $options["search"] = $search;
217 print_single_button("user.php", $options, " > ");
219 $options["page"] = $usercount-$recordsperpage;
220 print_single_button("user.php", $options, " >> ");
222 echo "</TD></TR></TABLE>";
226 $countries = get_list_of_countries();
228 foreach ($users as $key => $user) {
229 if (!empty($user->country
)) {
230 $users[$key]->country
= $countries[$user->country
];
233 if ($sort == "country") { // Need to resort by full country name, not code
234 foreach ($users as $user) {
235 $susers[$user->id
] = $user->country
;
238 foreach ($susers as $key => $value) {
239 $nusers[] = $users[$key];
244 $table->head
= array ($name, $email, $city, $country, $lastaccess, "", "");
245 $table->align
= array ("left", "left", "left", "left", "left", "center", "center");
246 $table->width
= "95%";
247 foreach ($users as $user) {
248 if ($user->id
== $USER->id
or $user->username
== "changeme") {
251 $deletebutton = "<a href=\"user.php?delete=$user->id\">$strdelete</a>";
253 if ($user->lastaccess
) {
254 $strlastaccess = format_time(time() - $user->lastaccess
);
256 $strlastaccess = get_string("never");
258 $fullname = fullname($user, true);
259 $table->data
[] = array ("<a href=\"../user/view.php?id=$user->id&course=$site->id\">$fullname</a>",
264 "<a href=\"../user/edit.php?id=$user->id&course=$site->id\">$stredit</a>",
268 echo "<table align=center cellpadding=10><tr><td>";
269 echo "<form action=user.php method=post>";
270 echo "<input type=text name=search value=\"$search\" size=20>";
271 echo "<input type=submit value=\"$strsearch\">";
273 echo "<input type=\"button\" onclick=\"document.location='user.php';\" value=\"$strshowallusers\">";
276 echo "</td></tr></table>";
280 if ($CFG->auth
== "email" ||
$CFG->auth
== "none" ||
$CFG->auth
== "manual"){
281 print_heading("<a href=\"user.php?newuser=true\">".get_string("addnewuser")."</a>");