Automatic installer.php lang files by installer_builder (20080610)
[moodle.git] / admin / user.php
blobe9736ca78fc813df46906bf4203a90a3906a9e5e
1 <?php // $Id$
3 require_once('../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
6 $newuser = optional_param('newuser', 0, PARAM_BOOL);
7 $delete = optional_param('delete', 0, PARAM_INT);
8 $confirm = optional_param('confirm', '', PARAM_ALPHANUM); //md5 confirmation hash
9 $confirmuser = optional_param('confirmuser', 0, PARAM_INT);
10 $sort = optional_param('sort', 'name', PARAM_ALPHA);
11 $dir = optional_param('dir', 'ASC', PARAM_ALPHA);
12 $page = optional_param('page', 0, PARAM_INT);
13 $perpage = optional_param('perpage', 30, PARAM_INT); // how many per page
14 $search = trim(optional_param('search', '', PARAM_RAW));
15 $lastinitial = optional_param('lastinitial', '', PARAM_CLEAN); // only show students with this last initial
16 $firstinitial = optional_param('firstinitial', '', PARAM_CLEAN); // only show students with this first initial
18 if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) { // Should never happen
19 redirect('index.php');
22 if (empty($CFG->rolesactive)) { // No admin user yet.
24 $user = new object();
25 $user->firstname = get_string('admin');
26 $user->lastname = get_string('user');
27 $user->username = 'admin';
28 $user->password = hash_internal_user_password('admin');
29 $user->email = 'root@localhost';
30 $user->confirmed = 1;
31 $user->lang = $CFG->lang;
32 $user->maildisplay = 1;
33 $user->timemodified = time();
35 if (! $user->id = insert_record('user', $user)) {
36 error("SERIOUS ERROR: Could not create admin user record !!!");
39 if (! $user = get_record('user', 'id', $user->id)) { // Double check.
40 error("User ID was incorrect (can't find it)");
44 // Assign the default admin role to the new user.
45 if (!$adminroles = get_roles_with_capability('moodle/legacy:admin', CAP_ALLOW)) {
46 error('No admin role could be found');
48 foreach ($adminroles as $adminrole) {
49 role_assign($adminrole->id, $user->id, 0, $sitecontext->id);
51 set_config('rolesactive', 1);
54 if (! $site = get_site()) {
55 error("Could not find site-level course");
58 // Log the user in.
59 $USER = $user;
60 $USER->loggedin = true;
61 $USER->sessionIP = md5(getremoteaddr()); // Store the current IP in the session
62 $USER->site = $CFG->wwwroot;
63 $USER->admin = true;
64 $USER->newadminuser = true;
66 sesskey(); // For added security, used to check script parameters
68 load_all_capabilities();
70 redirect("$CFG->wwwroot/user/edit.php?id=$user->id&amp;course=$site->id"); // Edit thyself
71 exit;
73 } else {
74 if (! $site = get_site()) {
75 error("Could not find site-level course");
79 require_login();
81 $adminroot = admin_get_root();
83 if ($newuser) {
84 admin_externalpage_setup('addnewuser', $adminroot);
85 } else {
86 admin_externalpage_setup('editusers', $adminroot);
89 if (empty($CFG->loginhttps)) {
90 $securewwwroot = $CFG->wwwroot;
91 } else {
92 $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
95 if ($newuser) { // Create a new user
97 if (!has_capability('moodle/user:create', $sitecontext)) {
98 error('You do not have the required permission to create new users.');
101 if (!$user = get_record('user', 'username', 'changeme')) { // half finished user from another time
103 $user = new object();
104 $user->auth = 'manual';
105 $user->firstname = '';
106 $user->lastname = '';
107 $user->username = 'changeme';
108 $user->password = '';
109 $user->email = '';
110 $user->lang = $CFG->lang;
111 $user->confirmed = 1;
112 $user->timemodified = time();
114 if (! $user->id = insert_record('user', $user)) {
115 error('Could not start a new user!');
119 redirect("$securewwwroot/user/edit.php?id=$user->id&amp;course=$site->id");
121 } else { // List all users for editing
123 if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
124 error('You do not have the required permission to edit/delete users.');
127 $stredit = get_string('edit');
128 $strdelete = get_string('delete');
129 $strdeletecheck = get_string('deletecheck');
130 $strsearch = get_string('search');
131 $strshowallusers = get_string('showallusers');
133 admin_externalpage_print_header($adminroot);
135 if ($confirmuser and confirm_sesskey()) {
136 if (!$user = get_record('user', 'id', $confirmuser)) {
137 error("No such user!");
140 $confirmeduser = new object();
141 $confirmeduser->id = $confirmuser;
142 $confirmeduser->confirmed = 1;
143 $confirmeduser->timemodified = time();
145 if (update_record('user', $confirmeduser)) {
146 notify(get_string('userconfirmed', '', fullname($user, true)) );
147 } else {
148 notify(get_string('usernotconfirmed', '', fullname($user, true)));
151 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
153 if (!has_capability('moodle/user:delete', $sitecontext)) {
154 error('You do not have the required permission to delete a user.');
157 if (!$user = get_record('user', 'id', $delete)) {
158 error("No such user!");
161 $primaryadmin = get_admin();
162 if ($user->id == $primaryadmin->id) {
163 error("You are not allowed to delete the primary admin user!");
166 if ($confirm != md5($delete)) {
167 $fullname = fullname($user, true);
168 print_heading(get_string('deleteuser', 'admin'));
169 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
170 notice_yesno(get_string('deletecheckfull', '', "'$fullname'"), 'user.php', 'user.php', $optionsyes, NULL, 'post', 'get');
171 admin_externalpage_print_footer($adminroot);
172 die;
173 } else if (data_submitted() and !$user->deleted) {
174 $updateuser = new object();
175 $updateuser->id = $user->id;
176 $updateuser->deleted = 1;
177 $updateuser->username = addslashes("$user->email.".time()); // Remember it just in case
178 $updateuser->email = ''; // Clear this field to free it up
179 $updateuser->idnumber = ''; // Clear this field to free it up
180 $updateuser->timemodified = time();
181 if (update_record('user', $updateuser)) {
182 // not sure if this is needed. unenrol_student($user->id); // From all courses
183 delete_records('role_assignments', 'userid', $user->id); // unassign all roles
184 // remove all context assigned on this user?
185 notify(get_string('deletedactivity', '', fullname($user, true)) );
186 } else {
187 notify(get_string('deletednot', '', fullname($user, true)));
192 // Carry on with the user listing
194 $columns = array("firstname", "lastname", "email", "city", "country", "lastaccess");
196 foreach ($columns as $column) {
197 $string[$column] = get_string("$column");
198 if ($sort != $column) {
199 $columnicon = "";
200 if ($column == "lastaccess") {
201 $columndir = "DESC";
202 } else {
203 $columndir = "ASC";
205 } else {
206 $columndir = $dir == "ASC" ? "DESC":"ASC";
207 if ($column == "lastaccess") {
208 $columnicon = $dir == "ASC" ? "up":"down";
209 } else {
210 $columnicon = $dir == "ASC" ? "down":"up";
212 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\" />";
215 $$column = "<a href=\"user.php?sort=$column&amp;dir=$columndir&amp;search=".urlencode(stripslashes($search))."&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial\">".$string[$column]."</a>$columnicon";
218 if ($sort == "name") {
219 $sort = "firstname";
222 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, $search, $firstinitial, $lastinitial);
223 $usercount = get_users(false);
224 $usersearchcount = get_users(false, $search, true, "", "", $firstinitial, $lastinitial);
226 if ($search or $firstinitial or $lastinitial) {
227 print_heading("$usersearchcount / $usercount ".get_string('users'));
228 $usercount = $usersearchcount;
229 } else {
230 print_heading("$usercount ".get_string('users'));
233 $alphabet = explode(',', get_string('alphabet'));
234 $strall = get_string('all');
237 /// Bar of first initials
239 echo "<center><p align=\"center\">";
240 echo get_string("firstname")." : ";
241 if ($firstinitial) {
242 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
243 "perpage=$perpage&amp;lastinitial=$lastinitial\">$strall</a> ";
244 } else {
245 echo " <b>$strall</b> ";
247 foreach ($alphabet as $letter) {
248 if ($letter == $firstinitial) {
249 echo " <b>$letter</b> ";
250 } else {
251 echo " <a href=\"user.php?sort=firstname&amp;dir=ASC&amp;".
252 "perpage=$perpage&amp;lastinitial=$lastinitial&amp;firstinitial=$letter\">$letter</a> ";
255 echo "<br />";
257 /// Bar of last initials
259 echo get_string("lastname")." : ";
260 if ($lastinitial) {
261 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
262 "perpage=$perpage&amp;firstinitial=$firstinitial\">$strall</a> ";
263 } else {
264 echo " <b>$strall</b> ";
266 foreach ($alphabet as $letter) {
267 if ($letter == $lastinitial) {
268 echo " <b>$letter</b> ";
269 } else {
270 echo " <a href=\"user.php?sort=lastname&amp;dir=ASC&amp;".
271 "perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$letter\">$letter</a> ";
274 echo "</p>";
275 echo "</center>";
277 print_paging_bar($usercount, $page, $perpage,
278 "user.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;search=".urlencode(stripslashes($search))."&amp;");
280 flush();
283 if (!$users) {
284 $match = array();
285 if ($search !== '') {
286 $match[] = s($search);
288 if ($firstinitial) {
289 $match[] = get_string('firstname').": $firstinitial"."___";
291 if ($lastinitial) {
292 $match[] = get_string('lastname').": $lastinitial"."___";
294 $matchstring = implode(", ", $match);
295 print_heading(get_string('nousersmatching', '', $matchstring));
297 $table = NULL;
299 } else {
301 $countries = get_list_of_countries();
303 foreach ($users as $key => $user) {
304 if (!empty($user->country)) {
305 $users[$key]->country = $countries[$user->country];
308 if ($sort == "country") { // Need to resort by full country name, not code
309 foreach ($users as $user) {
310 $susers[$user->id] = $user->country;
312 asort($susers);
313 foreach ($susers as $key => $value) {
314 $nusers[] = $users[$key];
316 $users = $nusers;
319 $table->head = array ("$firstname / $lastname", $email, $city, $country, $lastaccess, "", "", "");
320 $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center");
321 $table->width = "95%";
322 foreach ($users as $user) {
323 if ($user->username == 'changeme' or $user->username == 'guest') {
324 continue; // do not dispaly dummy new user and guest here
327 if ($user->id == $USER->id) {
328 $deletebutton = "";
329 } else {
330 if (has_capability('moodle/user:delete', $sitecontext)) {
331 $deletebutton = "<a href=\"user.php?delete=$user->id&amp;sesskey=$USER->sesskey\">$strdelete</a>";
332 } else {
333 $deletebutton ="";
337 if (has_capability('moodle/user:update', $sitecontext)) {
338 $editbutton = "<a href=\"$securewwwroot/user/edit.php?id=$user->id&amp;course=$site->id\">$stredit</a>";
339 if ($user->confirmed == 0) {
340 $confirmbutton = "<a href=\"user.php?confirmuser=$user->id&amp;sesskey=$USER->sesskey\">" . get_string('confirm') . "</a>";
341 } else {
342 $confirmbutton = "";
344 } else {
345 $editbutton ="";
346 if ($user->confirmed == 0) {
347 $confirmbutton = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
348 } else {
349 $confirmbutton = "";
353 if ($user->lastaccess) {
354 $strlastaccess = format_time(time() - $user->lastaccess);
355 } else {
356 $strlastaccess = get_string('never');
358 $fullname = fullname($user, true);
360 $table->data[] = array ("<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>",
361 "$user->email",
362 "$user->city",
363 "$user->country",
364 $strlastaccess,
365 $editbutton,
366 $deletebutton,
367 $confirmbutton);
371 echo "<table class=\"searchbox\" align=\"center\" cellpadding=\"10\"><tr><td>";
372 echo "<form action=\"user.php\" method=\"get\">";
373 echo "<input type=\"text\" name=\"search\" value=\"".s($search, true)."\" size=\"20\" />";
374 echo "<input type=\"submit\" value=\"$strsearch\" />";
375 if ($search) {
376 echo "<input type=\"button\" onclick=\"document.location='user.php';\" value=\"$strshowallusers\" />";
378 echo "</form>";
379 echo "</td></tr></table>";
381 if (has_capability('moodle/user:create', $sitecontext)) {
382 print_heading("<a href=\"user.php?newuser=true&amp;sesskey=$USER->sesskey\">".get_string('addnewuser')."</a>");
384 if (!empty($table)) {
385 print_table($table);
386 print_paging_bar($usercount, $page, $perpage,
387 "user.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage".
388 "&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;search=".urlencode(stripslashes($search))."&amp;");
389 if (has_capability('moodle/user:create', $sitecontext)) {
390 print_heading("<a href=\"user.php?newuser=true&amp;sesskey=$USER->sesskey\">".get_string("addnewuser")."</a>");
395 admin_externalpage_print_footer($adminroot);