3 require_once('../config.php');
4 require_once($CFG->libdir
.'/adminlib.php');
6 $delete = optional_param('delete', 0, PARAM_INT
);
7 $confirm = optional_param('confirm', '', PARAM_ALPHANUM
); //md5 confirmation hash
8 $confirmuser = optional_param('confirmuser', 0, PARAM_INT
);
9 $sort = optional_param('sort', 'name', PARAM_ALPHA
);
10 $dir = optional_param('dir', 'ASC', PARAM_ALPHA
);
11 $page = optional_param('page', 0, PARAM_INT
);
12 $perpage = optional_param('perpage', 30, PARAM_INT
); // how many per page
13 $search = trim(optional_param('search', '', PARAM_RAW
));
14 $lastinitial = optional_param('lastinitial', '', PARAM_CLEAN
); // only show students with this last initial
15 $firstinitial = optional_param('firstinitial', '', PARAM_CLEAN
); // only show students with this first initial
16 $ru = optional_param('ru', '2', PARAM_INT
); // show remote users
17 $lu = optional_param('lu', '2', PARAM_INT
); // show local users
18 $acl = optional_param('acl', '0', PARAM_INT
); // id of user to tweak mnet ACL (requires $access)
20 $adminroot = admin_get_root();
21 admin_externalpage_setup('editusers', $adminroot);
23 // Let's see if we have *any* mnet users. Just ask for a single record
24 $mnet_users = get_records_select('user', " auth='mnet' AND mnethostid != '{$CFG->mnet_localhost_id}' ", '', '*', '0', '1');
25 if(is_array($mnet_users) && count($mnet_users) > 0) {
26 $mnet_auth_users = true;
28 $mnet_auth_users = false;
31 if($mnet_auth_users) {
32 // Determine which users we are looking at (local, remote, or both). Start with both.
33 if (!isset($_SESSION['admin-user-remoteusers'])) {
34 $_SESSION['admin-user-remoteusers'] = 1;
35 $_SESSION['admin-user-localusers'] = 1;
37 if ($ru == 0 or $ru == 1) {
38 $_SESSION['admin-user-remoteusers'] = $ru;
40 if ($lu == 0 or $lu == 1) {
41 $_SESSION['admin-user-localusers'] = $lu;
43 $remoteusers = $_SESSION['admin-user-remoteusers'];
44 $localusers = $_SESSION['admin-user-localusers'];
46 // if neither remote nor local, set to sensible local only
47 if (!$remoteusers and !$localusers) {
48 $_SESSION['admin-user-localusers'] = 1;
53 $sitecontext = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
56 if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
57 error('You do not have the required permission to edit/delete users.');
60 $stredit = get_string('edit');
61 $strdelete = get_string('delete');
62 $strdeletecheck = get_string('deletecheck');
63 $strsearch = get_string('search');
64 $strshowallusers = get_string('showallusers');
66 if (empty($CFG->loginhttps
)) {
67 $securewwwroot = $CFG->wwwroot
;
69 $securewwwroot = str_replace('http:','https:',$CFG->wwwroot
);
72 admin_externalpage_print_header($adminroot);
74 if ($confirmuser and confirm_sesskey()) {
75 if (!$user = get_record('user', 'id', $confirmuser)) {
76 error("No such user!");
79 $confirmeduser = new object();
80 $confirmeduser->id
= $confirmuser;
81 $confirmeduser->confirmed
= 1;
82 $confirmeduser->timemodified
= time();
84 if (update_record('user', $confirmeduser)) {
85 notify(get_string('userconfirmed', '', fullname($user, true)) );
87 notify(get_string('usernotconfirmed', '', fullname($user, true)));
90 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
92 if (!has_capability('moodle/user:delete', $sitecontext)) {
93 error('You do not have the required permission to delete a user.');
96 if (!$user = get_record('user', 'id', $delete)) {
97 error("No such user!");
100 $primaryadmin = get_admin();
101 if ($user->id
== $primaryadmin->id
) {
102 error("You are not allowed to delete the primary admin user!");
105 if ($confirm != md5($delete)) {
106 $fullname = fullname($user, true);
107 print_heading(get_string('deleteuser', 'admin'));
108 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
109 notice_yesno(get_string('deletecheckfull', '', "'$fullname'"), 'user.php', 'user.php', $optionsyes, NULL, 'post', 'get');
110 admin_externalpage_print_footer($adminroot);
112 } else if (data_submitted() and !$user->deleted
) {
113 //following code is also used in auth sync scripts
114 $updateuser = new object();
115 $updateuser->id
= $user->id
;
116 $updateuser->deleted
= 1;
117 $updateuser->username
= addslashes("$user->email.".time()); // Remember it just in case
118 $updateuser->email
= ''; // Clear this field to free it up
119 $updateuser->idnumber
= ''; // Clear this field to free it up
120 $updateuser->timemodified
= time();
121 if (update_record('user', $updateuser)) {
122 // not sure if this is needed. unenrol_student($user->id); // From all courses
123 delete_records('role_assignments', 'userid', $user->id
); // unassign all roles
124 // remove all context assigned on this user?
125 notify(get_string('deletedactivity', '', fullname($user, true)) );
127 notify(get_string('deletednot', '', fullname($user, true)));
130 } else if ($acl and confirm_sesskey()) {
131 if (!has_capability('moodle/user:delete', $sitecontext)) {
132 // TODO: this should be under a separate capability
133 error('You are not permitted to modify the MNET access control list.');
135 if (!$user = get_record('user', 'id', $acl)) {
136 error("No such user.");
138 if (!is_mnet_remote_user($user)) {
139 error('Users in the MNET access control list must be remote MNET users.');
141 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA
));
142 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
143 error('Invalid access parameter.');
145 $aclrecord = get_record('mnet_sso_access_control', 'username', $user->username
, 'mnet_host_id', $user->mnethostid
);
146 if (empty($aclrecord)) {
147 $aclrecord = new object();
148 $aclrecord->mnet_host_id
= $user->mnethostid
;
149 $aclrecord->username
= $user->username
;
150 $aclrecord->accessctrl
= $accessctrl;
151 if (!insert_record('mnet_sso_access_control', $aclrecord)) {
152 error("Database error - Couldn't modify the MNET access control list.");
155 $aclrecord->accessctrl
= $accessctrl;
156 if (!update_record('mnet_sso_access_control', $aclrecord)) {
157 error("Database error - Couldn't modify the MNET access control list.");
160 $mnethosts = get_records('mnet_host', '', '', 'id', 'id,wwwroot,name');
161 notify("MNET access control list updated: username '$user->username' from host '"
162 . $mnethosts[$user->mnethostid
]->name
163 . "' access now set to '$accessctrl'.");
166 // Carry on with the user listing
168 $columns = array("firstname", "lastname", "email", "city", "country", "lastaccess");
170 foreach ($columns as $column) {
171 $string[$column] = get_string("$column");
172 if ($sort != $column) {
174 if ($column == "lastaccess") {
180 $columndir = $dir == "ASC" ?
"DESC":"ASC";
181 if ($column == "lastaccess") {
182 $columnicon = $dir == "ASC" ?
"up":"down";
184 $columnicon = $dir == "ASC" ?
"down":"up";
186 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\" />";
189 $
$column = "<a href=\"user.php?sort=$column&dir=$columndir&search=".urlencode(stripslashes($search))."&firstinitial=$firstinitial&lastinitial=$lastinitial\">".$string[$column]."</a>$columnicon";
192 if ($sort == "name") {
196 // tell the query which users we are looking at (local, remote, or both)
198 if($mnet_auth_users && ($localusers XOR $remoteusers)) {
200 $remotewhere .= " and mnethostid = {$CFG->mnet_localhost_id} ";
202 $remotewhere .= " and mnethostid <> {$CFG->mnet_localhost_id} ";
206 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, $search, $firstinitial, $lastinitial, $remotewhere);
207 $usercount = get_users(false);
208 $usersearchcount = get_users(false, $search, true, "", "", $firstinitial, $lastinitial);
210 if ($search or $firstinitial or $lastinitial) {
211 print_heading("$usersearchcount / $usercount ".get_string('users'));
212 $usercount = $usersearchcount;
214 print_heading("$usercount ".get_string('users'));
217 $alphabet = explode(',', get_string('alphabet'));
218 $strall = get_string('all');
221 /// Bar of first initials
223 echo "<p style=\"text-align:center\">";
224 echo get_string("firstname")." : ";
226 echo " <a href=\"user.php?sort=firstname&dir=ASC&".
227 "perpage=$perpage&lastinitial=$lastinitial\">$strall</a> ";
229 echo " <b>$strall</b> ";
231 foreach ($alphabet as $letter) {
232 if ($letter == $firstinitial) {
233 echo " <b>$letter</b> ";
235 echo " <a href=\"user.php?sort=firstname&dir=ASC&".
236 "perpage=$perpage&lastinitial=$lastinitial&firstinitial=$letter\">$letter</a> ";
241 /// Bar of last initials
243 echo get_string("lastname")." : ";
245 echo " <a href=\"user.php?sort=lastname&dir=ASC&".
246 "perpage=$perpage&firstinitial=$firstinitial\">$strall</a> ";
248 echo " <b>$strall</b> ";
250 foreach ($alphabet as $letter) {
251 if ($letter == $lastinitial) {
252 echo " <b>$letter</b> ";
254 echo " <a href=\"user.php?sort=lastname&dir=ASC&".
255 "perpage=$perpage&firstinitial=$firstinitial&lastinitial=$letter\">$letter</a> ";
260 print_paging_bar($usercount, $page, $perpage,
261 "user.php?sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&search=".urlencode(stripslashes($search))."&");
268 if ($search !== '') {
269 $match[] = s($search);
272 $match[] = get_string('firstname').": $firstinitial"."___";
275 $match[] = get_string('lastname').": $lastinitial"."___";
277 $matchstring = implode(", ", $match);
278 print_heading(get_string('nousersmatching', '', $matchstring));
284 $countries = get_list_of_countries();
285 if (empty($mnethosts)) {
286 $mnethosts = get_records('mnet_host', '', '', 'id', 'id,wwwroot,name');
289 foreach ($users as $key => $user) {
290 if (!empty($user->country
)) {
291 $users[$key]->country
= $countries[$user->country
];
294 if ($sort == "country") { // Need to resort by full country name, not code
295 foreach ($users as $user) {
296 $susers[$user->id
] = $user->country
;
299 foreach ($susers as $key => $value) {
300 $nusers[] = $users[$key];
305 $mainadmin = get_admin();
307 $table->head
= array ("$firstname / $lastname", $email, $city, $country, $lastaccess, "", "", "");
308 $table->align
= array ("left", "left", "left", "left", "left", "center", "center", "center");
309 $table->width
= "95%";
310 foreach ($users as $user) {
311 if ($user->username
== 'guest') {
312 continue; // do not dispaly dummy new user and guest here
315 if ($user->id
== $USER->id
) {
318 if (has_capability('moodle/user:delete', $sitecontext)) {
319 $deletebutton = "<a href=\"user.php?delete=$user->id&sesskey=$USER->sesskey\">$strdelete</a>";
325 if (has_capability('moodle/user:update', $sitecontext) and ($user->id
==$USER->id
or $user->id
!= $mainadmin->id
) and !is_mnet_remote_user($user)) {
326 $editbutton = "<a href=\"$securewwwroot/user/editadvanced.php?id=$user->id&course=$site->id\">$stredit</a>";
327 if ($user->confirmed
== 0) {
328 $confirmbutton = "<a href=\"user.php?confirmuser=$user->id&sesskey=$USER->sesskey\">" . get_string('confirm') . "</a>";
334 if ($user->confirmed
== 0) {
335 $confirmbutton = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
341 // for remote users, shuffle columns around and display MNET stuff
342 if (is_mnet_remote_user($user)) {
343 $accessctrl = 'allow';
344 if ($acl = get_record('mnet_sso_access_control', 'username', $user->username
, 'mnet_host_id', $user->mnethostid
)) {
345 $accessctrl = $acl->accessctrl
;
347 $changeaccessto = ($accessctrl == 'deny' ?
'allow' : 'deny');
348 // delete button in confirm column - remote users should already be confirmed
349 // TODO: no delete for remote users, for now. new userid, delete flag, unique on username/host...
351 // ACL in delete column
352 $deletebutton = get_string($accessctrl, 'mnet');
353 if (has_capability('moodle/user:delete', $sitecontext)) {
354 // TODO: this should be under a separate capability
355 $deletebutton .= " (<a href=\"?acl={$user->id}&accessctrl=$changeaccessto&sesskey={$USER->sesskey}\">"
356 . get_string($changeaccessto, 'mnet') . " access</a>)";
358 // mnet info in edit column
359 if (isset($mnethosts[$user->mnethostid
])) {
360 $editbutton = $mnethosts[$user->mnethostid
]->name
;
364 if ($user->lastaccess
) {
365 $strlastaccess = format_time(time() - $user->lastaccess
);
367 $strlastaccess = get_string('never');
369 $fullname = fullname($user, true);
371 $table->data
[] = array ("<a href=\"../user/view.php?id=$user->id&course=$site->id\">$fullname</a>",
382 if($mnet_auth_users) {
383 echo "<p style=\"text-align:center\">";
384 if ($localusers == 1 && $remoteusers == 1) {
385 echo '<a href="?lu=0">'.get_string('hidelocal','mnet').'</a> | ';
386 } elseif ($localusers == 0) {
387 echo '<a href="?lu=1">'.get_string('showlocal','mnet').'</a> | ';
389 echo get_string('hidelocal','mnet').' | ';
391 if ($localusers == 1 && $remoteusers == 1) {
392 echo '<a href="?ru=0">'.get_string('hideremote','mnet').'</a>';
393 } elseif ($remoteusers == 0) {
394 echo '<a href="?ru=1">'.get_string('showremote','mnet').'</a>';
396 echo get_string('hideremote','mnet');
401 echo "<table class=\"searchbox\" style=\"margin-left:auto;margin-right:auto\" cellpadding=\"10\"><tr><td>";
402 echo "<form action=\"user.php\" method=\"get\"><fieldset class=\"invisiblefieldset\">";
403 echo "<input type=\"text\" name=\"search\" value=\"".s($search, true)."\" size=\"20\" />";
404 echo "<input type=\"submit\" value=\"$strsearch\" />";
406 echo "<input type=\"button\" onclick=\"document.location='user.php';\" value=\"$strshowallusers\" />";
408 echo "</fieldset></form>";
409 echo "</td></tr></table>";
411 if (has_capability('moodle/user:create', $sitecontext)) {
412 print_heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
414 if (!empty($table)) {
416 print_paging_bar($usercount, $page, $perpage,
417 "user.php?sort=$sort&dir=$dir&perpage=$perpage".
418 "&firstinitial=$firstinitial&lastinitial=$lastinitial&search=".urlencode(stripslashes($search))."&");
419 if (has_capability('moodle/user:create', $sitecontext)) {
420 print_heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
425 admin_externalpage_print_footer($adminroot);