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.
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';
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");
60 $USER->loggedin
= true;
61 $USER->sessionIP
= md5(getremoteaddr()); // Store the current IP in the session
62 $USER->site
= $CFG->wwwroot
;
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&course=$site->id"); // Edit thyself
74 if (! $site = get_site()) {
75 error("Could not find site-level course");
81 $adminroot = admin_get_root();
84 admin_externalpage_setup('addnewuser', $adminroot);
86 admin_externalpage_setup('editusers', $adminroot);
89 if (empty($CFG->loginhttps
)) {
90 $securewwwroot = $CFG->wwwroot
;
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
= '';
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&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)) );
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);
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)) );
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) {
200 if ($column == "lastaccess") {
206 $columndir = $dir == "ASC" ?
"DESC":"ASC";
207 if ($column == "lastaccess") {
208 $columnicon = $dir == "ASC" ?
"up":"down";
210 $columnicon = $dir == "ASC" ?
"down":"up";
212 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\" />";
215 $
$column = "<a href=\"user.php?sort=$column&dir=$columndir&search=".urlencode(stripslashes($search))."&firstinitial=$firstinitial&lastinitial=$lastinitial\">".$string[$column]."</a>$columnicon";
218 if ($sort == "name") {
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;
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")." : ";
242 echo " <a href=\"user.php?sort=firstname&dir=ASC&".
243 "perpage=$perpage&lastinitial=$lastinitial\">$strall</a> ";
245 echo " <b>$strall</b> ";
247 foreach ($alphabet as $letter) {
248 if ($letter == $firstinitial) {
249 echo " <b>$letter</b> ";
251 echo " <a href=\"user.php?sort=firstname&dir=ASC&".
252 "perpage=$perpage&lastinitial=$lastinitial&firstinitial=$letter\">$letter</a> ";
257 /// Bar of last initials
259 echo get_string("lastname")." : ";
261 echo " <a href=\"user.php?sort=lastname&dir=ASC&".
262 "perpage=$perpage&firstinitial=$firstinitial\">$strall</a> ";
264 echo " <b>$strall</b> ";
266 foreach ($alphabet as $letter) {
267 if ($letter == $lastinitial) {
268 echo " <b>$letter</b> ";
270 echo " <a href=\"user.php?sort=lastname&dir=ASC&".
271 "perpage=$perpage&firstinitial=$firstinitial&lastinitial=$letter\">$letter</a> ";
277 print_paging_bar($usercount, $page, $perpage,
278 "user.php?sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&search=".urlencode(stripslashes($search))."&");
285 if ($search !== '') {
286 $match[] = s($search);
289 $match[] = get_string('firstname').": $firstinitial"."___";
292 $match[] = get_string('lastname').": $lastinitial"."___";
294 $matchstring = implode(", ", $match);
295 print_heading(get_string('nousersmatching', '', $matchstring));
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
;
313 foreach ($susers as $key => $value) {
314 $nusers[] = $users[$key];
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
) {
330 if (has_capability('moodle/user:delete', $sitecontext)) {
331 $deletebutton = "<a href=\"user.php?delete=$user->id&sesskey=$USER->sesskey\">$strdelete</a>";
337 if (has_capability('moodle/user:update', $sitecontext)) {
338 $editbutton = "<a href=\"$securewwwroot/user/edit.php?id=$user->id&course=$site->id\">$stredit</a>";
339 if ($user->confirmed
== 0) {
340 $confirmbutton = "<a href=\"user.php?confirmuser=$user->id&sesskey=$USER->sesskey\">" . get_string('confirm') . "</a>";
346 if ($user->confirmed
== 0) {
347 $confirmbutton = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
353 if ($user->lastaccess
) {
354 $strlastaccess = format_time(time() - $user->lastaccess
);
356 $strlastaccess = get_string('never');
358 $fullname = fullname($user, true);
360 $table->data
[] = array ("<a href=\"../user/view.php?id=$user->id&course=$site->id\">$fullname</a>",
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\" />";
376 echo "<input type=\"button\" onclick=\"document.location='user.php';\" value=\"$strshowallusers\" />";
379 echo "</td></tr></table>";
381 if (has_capability('moodle/user:create', $sitecontext)) {
382 print_heading("<a href=\"user.php?newuser=true&sesskey=$USER->sesskey\">".get_string('addnewuser')."</a>");
384 if (!empty($table)) {
386 print_paging_bar($usercount, $page, $perpage,
387 "user.php?sort=$sort&dir=$dir&perpage=$perpage".
388 "&firstinitial=$firstinitial&lastinitial=$lastinitial&search=".urlencode(stripslashes($search))."&");
389 if (has_capability('moodle/user:create', $sitecontext)) {
390 print_heading("<a href=\"user.php?newuser=true&sesskey=$USER->sesskey\">".get_string("addnewuser")."</a>");
395 admin_externalpage_print_footer($adminroot);