Merge branch 'm20_MDL-26672_aiccfix' of git://github.com/danmarsden/moodle
[moodle.git] / admin / user.php
blobd2ad915cece6a2c51c3594770fa140f9b90ac14d
1 <?php
3 require_once('../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
5 require_once($CFG->dirroot.'/user/filters/lib.php');
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 $ru = optional_param('ru', '2', PARAM_INT); // show remote users
15 $lu = optional_param('lu', '2', PARAM_INT); // show local users
16 $acl = optional_param('acl', '0', PARAM_INT); // id of user to tweak mnet ACL (requires $access)
19 admin_externalpage_setup('editusers');
21 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
22 $site = get_site();
24 if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
25 print_error('nopermissions', 'error', '', 'edit/delete users');
28 $stredit = get_string('edit');
29 $strdelete = get_string('delete');
30 $strdeletecheck = get_string('deletecheck');
31 $strshowallusers = get_string('showallusers');
33 if (empty($CFG->loginhttps)) {
34 $securewwwroot = $CFG->wwwroot;
35 } else {
36 $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
39 $returnurl = "$CFG->wwwroot/$CFG->admin/user.php";
41 if ($confirmuser and confirm_sesskey()) {
42 if (!$user = $DB->get_record('user', array('id'=>$confirmuser))) {
43 print_error('nousers');
46 $auth = get_auth_plugin($user->auth);
48 $result = $auth->user_confirm($user->username, $user->secret);
50 if ($result == AUTH_CONFIRM_OK or $result == AUTH_CONFIRM_ALREADY) {
51 redirect($returnurl);
52 } else {
53 echo $OUTPUT->header();
54 redirect($returnurl, get_string('usernotconfirmed', '', fullname($user, true)));
57 } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
59 require_capability('moodle/user:delete', $sitecontext);
61 $user = $DB->get_record('user', array('id'=>$delete), '*', MUST_EXIST);
63 if (is_siteadmin($user->id)) {
64 print_error('useradminodelete', 'error');
67 if ($confirm != md5($delete)) {
68 echo $OUTPUT->header();
69 $fullname = fullname($user, true);
70 echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
71 $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
72 echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), new moodle_url('user.php', $optionsyes), 'user.php');
73 echo $OUTPUT->footer();
74 die;
75 } else if (data_submitted() and !$user->deleted) {
76 if (delete_user($user)) {
77 session_gc(); // remove stale sessions
78 redirect($returnurl);
79 } else {
80 session_gc(); // remove stale sessions
81 echo $OUTPUT->header();
82 echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
85 } else if ($acl and confirm_sesskey()) {
86 if (!has_capability('moodle/user:delete', $sitecontext)) {
87 // TODO: this should be under a separate capability
88 print_error('nopermissions', 'error', '', 'modify the NMET access control list');
90 if (!$user = $DB->get_record('user', array('id'=>$acl))) {
91 print_error('nousers', 'error');
93 if (!is_mnet_remote_user($user)) {
94 print_error('usermustbemnet', 'error');
96 $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
97 if ($accessctrl != 'allow' and $accessctrl != 'deny') {
98 print_error('invalidaccessparameter', 'error');
100 $aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid));
101 if (empty($aclrecord)) {
102 $aclrecord = new stdClass();
103 $aclrecord->mnet_host_id = $user->mnethostid;
104 $aclrecord->username = $user->username;
105 $aclrecord->accessctrl = $accessctrl;
106 $DB->insert_record('mnet_sso_access_control', $aclrecord);
107 } else {
108 $aclrecord->accessctrl = $accessctrl;
109 $DB->update_record('mnet_sso_access_control', $aclrecord);
111 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
112 redirect($returnurl);
115 // create the user filter form
116 $ufiltering = new user_filtering();
117 echo $OUTPUT->header();
119 // Carry on with the user listing
121 $columns = array("firstname", "lastname", "email", "city", "country", "lastaccess");
123 foreach ($columns as $column) {
124 $string[$column] = get_string("$column");
125 if ($sort != $column) {
126 $columnicon = "";
127 if ($column == "lastaccess") {
128 $columndir = "DESC";
129 } else {
130 $columndir = "ASC";
132 } else {
133 $columndir = $dir == "ASC" ? "DESC":"ASC";
134 if ($column == "lastaccess") {
135 $columnicon = $dir == "ASC" ? "up":"down";
136 } else {
137 $columnicon = $dir == "ASC" ? "down":"up";
139 $columnicon = " <img src=\"" . $OUTPUT->pix_url('t/' . $columnicon) . "\" alt=\"\" />";
142 $$column = "<a href=\"user.php?sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
145 if ($sort == "name") {
146 $sort = "firstname";
149 list($extrasql, $params) = $ufiltering->get_sql_filter();
150 $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '', $extrasql, $params);
151 $usercount = get_users(false);
152 $usersearchcount = get_users(false, '', true, null, "", '', '', '', '', '*', $extrasql, $params);
154 if ($extrasql !== '') {
155 echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
156 $usercount = $usersearchcount;
157 } else {
158 echo $OUTPUT->heading("$usercount ".get_string('users'));
161 $strall = get_string('all');
163 $baseurl = new moodle_url('user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
164 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
166 flush();
169 if (!$users) {
170 $match = array();
171 echo $OUTPUT->heading(get_string('nousersfound'));
173 $table = NULL;
175 } else {
177 $countries = get_string_manager()->get_list_of_countries(false);
178 if (empty($mnethosts)) {
179 $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
182 foreach ($users as $key => $user) {
183 if (!empty($user->country)) {
184 $users[$key]->country = $countries[$user->country];
187 if ($sort == "country") { // Need to resort by full country name, not code
188 foreach ($users as $user) {
189 $susers[$user->id] = $user->country;
191 asort($susers);
192 foreach ($susers as $key => $value) {
193 $nusers[] = $users[$key];
195 $users = $nusers;
198 $override = new stdClass();
199 $override->firstname = 'firstname';
200 $override->lastname = 'lastname';
201 $fullnamelanguage = get_string('fullnamedisplay', '', $override);
202 if (($CFG->fullnamedisplay == 'firstname lastname') or
203 ($CFG->fullnamedisplay == 'firstname') or
204 ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname' )) {
205 $fullnamedisplay = "$firstname / $lastname";
206 } else { // ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'lastname firstname')
207 $fullnamedisplay = "$lastname / $firstname";
210 $table = new html_table();
211 $table->head = array ($fullnamedisplay, $email, $city, $country, $lastaccess, "", "", "");
212 $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center");
213 $table->width = "95%";
214 foreach ($users as $user) {
215 if (isguestuser($user)) {
216 continue; // do not display guest here
219 if ($user->id == $USER->id or is_siteadmin($user)) {
220 $deletebutton = "";
221 } else {
222 if (has_capability('moodle/user:delete', $sitecontext)) {
223 $deletebutton = "<a href=\"user.php?delete=$user->id&amp;sesskey=".sesskey()."\">$strdelete</a>";
224 } else {
225 $deletebutton ="";
229 if (has_capability('moodle/user:update', $sitecontext) and (is_siteadmin($USER) or !is_siteadmin($user)) and !is_mnet_remote_user($user)) {
230 $editbutton = "<a href=\"$securewwwroot/user/editadvanced.php?id=$user->id&amp;course=$site->id\">$stredit</a>";
231 if ($user->confirmed == 0) {
232 $confirmbutton = "<a href=\"user.php?confirmuser=$user->id&amp;sesskey=".sesskey()."\">" . get_string('confirm') . "</a>";
233 } else {
234 $confirmbutton = "";
236 } else {
237 $editbutton ="";
238 if ($user->confirmed == 0) {
239 $confirmbutton = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
240 } else {
241 $confirmbutton = "";
245 // for remote users, shuffle columns around and display MNET stuff
246 if (is_mnet_remote_user($user)) {
247 $accessctrl = 'allow';
248 if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid))) {
249 $accessctrl = $acl->accessctrl;
251 $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
252 // delete button in confirm column - remote users should already be confirmed
253 // TODO: no delete for remote users, for now. new userid, delete flag, unique on username/host...
254 $confirmbutton = "";
255 // ACL in delete column
256 $deletebutton = get_string($accessctrl, 'mnet');
257 if (has_capability('moodle/user:delete', $sitecontext)) {
258 // TODO: this should be under a separate capability
259 $deletebutton .= " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey=".sesskey()."\">"
260 . get_string($changeaccessto, 'mnet') . " access</a>)";
262 // mnet info in edit column
263 if (isset($mnethosts[$user->mnethostid])) {
264 $editbutton = $mnethosts[$user->mnethostid]->name;
268 if ($user->lastaccess) {
269 $strlastaccess = format_time(time() - $user->lastaccess);
270 } else {
271 $strlastaccess = get_string('never');
273 $fullname = fullname($user, true);
275 $table->data[] = array ("<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>",
276 "$user->email",
277 "$user->city",
278 "$user->country",
279 $strlastaccess,
280 $editbutton,
281 $deletebutton,
282 $confirmbutton);
286 // add filters
287 $ufiltering->display_add();
288 $ufiltering->display_active();
290 if (has_capability('moodle/user:create', $sitecontext)) {
291 echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
293 if (!empty($table)) {
294 echo html_writer::table($table);
295 echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
296 if (has_capability('moodle/user:create', $sitecontext)) {
297 echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
301 echo $OUTPUT->footer();