MDL-30472 Course Making user lists sortable when lastacess field is hidden
[moodle.git] / user / index.php
CommitLineData
aa6c1ced 1<?php
f9903ed0 2
3// Lists all the users within a given course
4
cc038b47 5 require_once('../config.php');
68b38e8d 6 require_once($CFG->libdir.'/tablelib.php');
99d19c13 7 require_once($CFG->libdir.'/filelib.php');
951b22a8 8
9 define('USER_SMALL_CLASS', 20); // Below this is considered small
10 define('USER_LARGE_CLASS', 200); // Above this is considered large
cc038b47 11 define('DEFAULT_PAGE_SIZE', 20);
36075e09 12 define('SHOW_ALL_PAGE_SIZE', 5000);
073af1a6 13 define('MODE_BRIEF', 0);
14 define('MODE_USERDETAILS', 1);
f9903ed0 15
cc038b47 16 $page = optional_param('page', 0, PARAM_INT); // which page to show
17 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // how many per page
0f21a964 18 $mode = optional_param('mode', NULL, PARAM_INT); // use the MODE_ constants
5f9e296a 19 $accesssince = optional_param('accesssince',0,PARAM_INT); // filter by last access. -1 = never
93de0ac2
PS
20 $search = optional_param('search','',PARAM_RAW); // make sure it is processed with p() or s() when sending to output!
21 $roleid = optional_param('roleid', 0, PARAM_INT); // optional roleid, 0 means all enrolled users (or all on the frontpage)
e957cc26 22
5f9e296a 23 $contextid = optional_param('contextid', 0, PARAM_INT); // one of this or
24 $courseid = optional_param('id', 0, PARAM_INT); // this are required
3aac07d8 25
a6855934 26 $PAGE->set_url('/user/index.php', array(
aa6c1ced
PS
27 'page' => $page,
28 'perpage' => $perpage,
29 'mode' => $mode,
30 'accesssince' => $accesssince,
31 'search' => $search,
32 'roleid' => $roleid,
33 'contextid' => $contextid,
4e487ce2 34 'courseid' => $courseid));
f1e24e7a 35
e957cc26 36 if ($contextid) {
4f0c2d00
PS
37 $context = get_context_instance_by_id($contextid, MUST_EXIST);
38 if ($context->contextlevel != CONTEXT_COURSE) {
06e84d52 39 print_error('invalidcontext');
e957cc26 40 }
4f0c2d00 41 $course = $DB->get_record('course', array('id'=>$context->instanceid), '*', MUST_EXIST);
e957cc26 42 } else {
3712f7aa 43 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
4f0c2d00 44 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
f9903ed0 45 }
87a13824 46 // not needed anymore
47 unset($contextid);
48 unset($courseid);
f9903ed0 49
224aa44a 50 require_login($course);
f9903ed0 51
1118434a 52 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
4f0c2d00
PS
53 $isfrontpage = ($course->id == SITEID);
54
165088f6 55 $frontpagectx = get_context_instance(CONTEXT_COURSE, SITEID);
224aa44a 56
4f0c2d00 57 if ($isfrontpage) {
c07dfe70 58 $PAGE->set_pagelayout('admin');
1118434a 59 require_capability('moodle/site:viewparticipants', $systemcontext);
4f0c2d00 60 } else {
c07dfe70 61 $PAGE->set_pagelayout('incourse');
4f0c2d00 62 require_capability('moodle/course:viewparticipants', $context);
6d3c57f4 63 }
64
d7270311 65 $rolenamesurl = new moodle_url("$CFG->wwwroot/user/index.php?contextid=$context->id&sifirst=&silast=");
66
4f0c2d00
PS
67 $allroles = get_all_roles();
68 $roles = get_profile_roles($context);
69 $allrolenames = array();
70 if ($isfrontpage) {
71 $rolenames = array(0=>get_string('allsiteusers', 'role'));
72 } else {
73 $rolenames = array(0=>get_string('allparticipants'));
74 }
5f9e296a 75
4f0c2d00
PS
76 foreach ($allroles as $role) {
77 $allrolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on
78 if (isset($roles[$role->id])) {
79 $rolenames[$role->id] = $allrolenames[$role->id];
41d7209c 80 }
6d3c57f4 81 }
82
4f0c2d00
PS
83 // make sure other roles may not be selected by any means
84 if (empty($rolenames[$roleid])) {
85 print_error('noparticipants');
5f9e296a 86 }
87
88 // no roles to display yet?
165088f6 89 // frontpage course is an exception, on the front page course we should display all users
4f0c2d00 90 if (empty($rolenames) && !$isfrontpage) {
294a176f 91 if (has_capability('moodle/role:assign', $context)) {
87a13824 92 redirect($CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id);
41d7209c 93 } else {
06e84d52 94 print_error('noparticipants');
41d7209c 95 }
96 }
97
cc038b47 98 add_to_log($course->id, 'user', 'view all', 'index.php?id='.$course->id, '');
f9903ed0 99
77c645df 100 $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
f1e24e7a 101
0aa759b0 102 $countries = get_string_manager()->get_list_of_countries();
68b38e8d 103
cc038b47 104 $strnever = get_string('never');
68b38e8d 105
a59e5a1c 106 $datestring->year = get_string('year');
107 $datestring->years = get_string('years');
cc038b47 108 $datestring->day = get_string('day');
109 $datestring->days = get_string('days');
110 $datestring->hour = get_string('hour');
111 $datestring->hours = get_string('hours');
112 $datestring->min = get_string('min');
113 $datestring->mins = get_string('mins');
114 $datestring->sec = get_string('sec');
115 $datestring->secs = get_string('secs');
d578afc8 116
ff3caf30 117 if ($mode !== NULL) {
073af1a6 118 $mode = (int)$mode;
119 $SESSION->userindexmode = $mode;
ff3caf30 120 } else if (isset($SESSION->userindexmode)) {
073af1a6 121 $mode = (int)$SESSION->userindexmode;
ff3caf30 122 } else {
073af1a6 123 $mode = MODE_BRIEF;
ff3caf30 124 }
125
4cd0d3a0 126/// Check to see if groups are being used in this course
ff3caf30 127/// and if so, set $currentgroup to reflect the current group
128
ffc536af 129 $groupmode = groups_get_course_groupmode($course); // Groups are being used
130 $currentgroup = groups_get_course_group($course, true);
ff3caf30 131
cc038b47 132 if (!$currentgroup) { // To make some other functions work better later
133 $currentgroup = NULL;
134 }
ff3caf30 135
728a4ebe 136 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
2cb2ce61 137
ee8aca27 138 if ($course->id===SITEID) {
139 $PAGE->navbar->ignore_active();
140 }
141
caa8363f 142 $PAGE->navbar->add(get_string('participants'));
143 $PAGE->set_title("$course->shortname: ".get_string('participants'));
144 $PAGE->set_heading($course->fullname);
03d9401e 145 $PAGE->set_pagetype('course-view-' . $course->format);
99cca847 146 $PAGE->add_body_class('path-user'); // So we can style it independently
03d9401e 147 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
caa8363f 148
149 echo $OUTPUT->header();
0be6f678 150
99cca847
MD
151 echo '<div class="userlist">';
152
caa8363f 153 if ($isseparategroups and (!$currentgroup) ) {
154 // The user is not in the group so show message and exit
8fa89bfd 155 echo $OUTPUT->heading(get_string("notingroup"));
0ab75c34 156 echo $OUTPUT->footer();
ff3caf30 157 exit;
158 }
159
03d9401e 160
cc038b47 161 // Should use this variable so that we don't break stuff every time a variable is added or changed.
a6855934 162 $baseurl = new moodle_url('/user/index.php', array(
d7270311 163 'contextid' => $context->id,
164 'roleid' => $roleid,
165 'id' => $course->id,
166 'perpage' => $perpage,
167 'accesssince' => $accesssince,
168 'search' => s($search)));
ff3caf30 169
224aa44a 170/// setting up tags
e957cc26 171 if ($course->id == SITEID) {
1242eb8f 172 $filtertype = 'site';
e957cc26 173 } else if ($course->id && !$currentgroup) {
1242eb8f 174 $filtertype = 'course';
e957cc26 175 $filterselect = $course->id;
1242eb8f 176 } else {
177 $filtertype = 'group';
178 $filterselect = $currentgroup;
179 }
1242eb8f 180
03d9401e 181
1242eb8f 182
3468d58a 183/// Get the hidden field list
224aa44a 184 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
3468d58a 185 $hiddenfields = array(); // teachers and admins are allowed to see everything
186 } else {
187 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
188 }
189
5ff311f0 190 if (isset($hiddenfields['lastaccess'])) {
191 // do not allow access since filtering
192 $accesssince = 0;
193 }
bbbf2d40 194
ff3caf30 195/// Print settings and things in a table across the top
4e487ce2 196 $controlstable = new html_table();
16be8974 197 $controlstable->attributes['class'] = 'controls';
4e487ce2 198 $controlstable->cellspacing = 0;
199 $controlstable->data[] = new html_table_row();
ff3caf30 200
a72784d3 201/// Print my course menus
df997f84 202 if ($mycourses = enrol_get_my_courses()) {
7da0af9f 203 $courselist = array();
a6855934 204 $popupurl = new moodle_url('/user/index.php?roleid='.$roleid.'&sifirst=&silast=');
7da0af9f 205 foreach ($mycourses as $mycourse) {
8ebbb06a
SH
206 $coursecontext = get_context_instance(CONTEXT_COURSE, $mycourse->id);
207 $courselist[$mycourse->id] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
7da0af9f 208 }
1118434a 209 if (has_capability('moodle/site:viewparticipants', $systemcontext)) {
5f9e296a 210 unset($courselist[SITEID]);
8ebbb06a 211 $courselist = array(SITEID => format_string($SITE->shortname, true, array('context' => $systemcontext))) + $courselist;
5f9e296a 212 }
f8dab966 213 $select = new single_select($popupurl, 'id', $courselist, $course->id, array(''=>'choosedots'), 'courseform');
d7270311 214 $select->set_label(get_string('mycourses'));
f8dab966 215 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
7da0af9f 216 }
87a13824 217
b60cae86 218 $controlstable->data[0]->cells[] = groups_print_course_menu($course, $baseurl->out(), true);
ff3caf30 219
5ff311f0 220 if (!isset($hiddenfields['lastaccess'])) {
221 // get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
222 // we need to make it diferently for normal courses and site course
4f0c2d00 223 if (!$isfrontpage) {
5ff311f0 224 $minlastaccess = $DB->get_field_sql('SELECT min(timeaccess)
225 FROM {user_lastaccess}
226 WHERE courseid = ?
227 AND timeaccess != 0', array($course->id));
228 $lastaccess0exists = $DB->record_exists('user_lastaccess', array('courseid'=>$course->id, 'timeaccess'=>0));
229 } else {
230 $minlastaccess = $DB->get_field_sql('SELECT min(lastaccess)
231 FROM {user}
232 WHERE lastaccess != 0');
233 $lastaccess0exists = $DB->record_exists('user', array('lastaccess'=>0));
234 }
0749caef 235
5ff311f0 236 $now = usergetmidnight(time());
237 $timeaccess = array();
d7270311 238 $baseurl->remove_params('accesssince');
87a13824 239
5ff311f0 240 // makes sense for this to go first.
241 $timeoptions[0] = get_string('selectperiod');
87a13824 242
5ff311f0 243 // days
244 for ($i = 1; $i < 7; $i++) {
245 if (strtotime('-'.$i.' days',$now) >= $minlastaccess) {
246 $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
247 }
ca792680 248 }
5ff311f0 249 // weeks
250 for ($i = 1; $i < 10; $i++) {
251 if (strtotime('-'.$i.' weeks',$now) >= $minlastaccess) {
252 $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
253 }
ca792680 254 }
5ff311f0 255 // months
256 for ($i = 2; $i < 12; $i++) {
257 if (strtotime('-'.$i.' months',$now) >= $minlastaccess) {
258 $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
259 }
260 }
261 // try a year
262 if (strtotime('-1 year',$now) >= $minlastaccess) {
263 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
31b71336 264 }
87a13824 265
5ff311f0 266 if (!empty($lastaccess0exists)) {
267 $timeoptions[-1] = get_string('never');
268 }
87a13824 269
5ff311f0 270 if (count($timeoptions) > 1) {
f8dab966 271 $select = new single_select($baseurl, 'accesssince', $timeoptions, $accesssince, null, 'timeoptions');
d7270311 272 $select->set_label(get_string('usersnoaccesssince'));
f8dab966 273 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
5ff311f0 274 }
77c645df 275 }
87a13824 276
073af1a6 277 $formatmenu = array( '0' => get_string('brief'),
278 '1' => get_string('userdetails'));
f8dab966 279 $select = new single_select($baseurl, 'mode', $formatmenu, $mode, null, 'formatmenu');
d7270311 280 $select->set_label(get_string('userlist'));
4e487ce2 281 $userlistcell = new html_table_cell();
16be8974 282 $userlistcell->attributes['class'] = 'right';
f8dab966 283 $userlistcell->text = $OUTPUT->render($select);
4e487ce2 284 $controlstable->data[0]->cells[] = $userlistcell;
285
16be8974 286 echo html_writer::table($controlstable);
ff3caf30 287
224aa44a 288 if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) { /// Display info about the group
ffc536af 289 if ($group = groups_get_group($currentgroup)) {
87a13824 290 if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
4e487ce2 291 $groupinfotable = new html_table();
16be8974 292 $groupinfotable->attributes['class'] = 'groupinfobox';
4e487ce2 293 $picturecell = new html_table_cell();
16be8974 294 $picturecell->attributes['class'] = 'left side picture';
b60cae86 295 $picturecell->text = print_group_picture($group, $course->id, true, true, false);
aa6c1ced 296
4e487ce2 297 $contentcell = new html_table_cell();
16be8974 298 $contentcell->attributes['class'] = 'content';
4e487ce2 299
300 $contentheading = $group->name;
224aa44a 301 if (has_capability('moodle/course:managegroups', $context)) {
a6855934 302 $aurl = new moodle_url('/group/group.php', array('id' => $group->id, 'courseid' => $group->courseid));
c63923bd 303 $contentheading .= '&nbsp;' . $OUTPUT->action_icon($aurl, new pix_icon('t/edit', get_string('editgroupprofile')));
2c27cd19 304 }
8bdc9cac 305
64f93798 306 $group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group', 'description', $group->id);
8bdc9cac
SH
307 if (!isset($group->descriptionformat)) {
308 $group->descriptionformat = FORMAT_MOODLE;
309 }
367a75fa
SH
310 $options = array('overflowdiv'=>true);
311 $contentcell->text = $OUTPUT->heading($contentheading, 3) . format_text($group->description, $group->descriptionformat, $options);
8cea545e 312 $groupinfotable->data[] = new html_table_row(array($picturecell, $contentcell));
16be8974 313 echo html_writer::table($groupinfotable);
ff3caf30 314 }
315 }
316 }
317
77c645df 318 /// Define a table showing a list of users in the current role selection
319
1d40c70b 320 $tablecolumns = array('userpic', 'fullname');
43aa295b 321 $extrafields = get_extra_user_fields($context);
d6cc2341 322 $tableheaders = array(get_string('userpic'), get_string('fullnameuser'));
43aa295b 323 if ($mode === MODE_BRIEF) {
324 foreach ($extrafields as $field) {
325 $tablecolumns[] = $field;
326 $tableheaders[] = get_user_field_name($field);
327 }
328 }
073af1a6 329 if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
77c645df 330 $tablecolumns[] = 'city';
331 $tableheaders[] = get_string('city');
332 }
073af1a6 333 if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
77c645df 334 $tablecolumns[] = 'country';
335 $tableheaders[] = get_string('country');
336 }
337 if (!isset($hiddenfields['lastaccess'])) {
338 $tablecolumns[] = 'lastaccess';
339 $tableheaders[] = get_string('lastaccess');
340 }
341
77c645df 342 if ($bulkoperations) {
664fe87f 343 $tablecolumns[] = 'select';
77c645df 344 $tableheaders[] = get_string('select');
345 }
346
04186cd9 347 $table = new flexible_table('user-index-participants-'.$course->id);
77c645df 348 $table->define_columns($tablecolumns);
349 $table->define_headers($tableheaders);
d7270311 350 $table->define_baseurl($baseurl->out());
0be6f678 351
5ff311f0 352 if (!isset($hiddenfields['lastaccess'])) {
353 $table->sortable(true, 'lastaccess', SORT_DESC);
1b80a504
AB
354 } else {
355 $table->sortable(true, 'firstname', SORT_ASC);
5ff311f0 356 }
357
664fe87f 358 $table->no_sorting('roles');
359 $table->no_sorting('groups');
360 $table->no_sorting('groupings');
361 $table->no_sorting('select');
77c645df 362
363 $table->set_attribute('cellspacing', '0');
04186cd9 364 $table->set_attribute('id', 'participants');
77c645df 365 $table->set_attribute('class', 'generaltable generalbox');
366
367 $table->set_control_variables(array(
368 TABLE_VAR_SORT => 'ssort',
369 TABLE_VAR_HIDE => 'shide',
370 TABLE_VAR_SHOW => 'sshow',
371 TABLE_VAR_IFIRST => 'sifirst',
372 TABLE_VAR_ILAST => 'silast',
373 TABLE_VAR_PAGE => 'spage'
374 ));
375 $table->setup();
376
77c645df 377 // we are looking for all users with this role assigned in this context or higher
4f0c2d00 378 $contextlist = get_related_contexts_string($context);
5f9e296a 379
505c641a 380 list($esql, $params) = get_enrolled_sql($context, NULL, $currentgroup, true);
4f0c2d00
PS
381 $joins = array("FROM {user} u");
382 $wheres = array();
383
43aa295b 384 $extrasql = get_extra_user_fields_sql($context, 'u', '', array(
385 'id', 'username', 'firstname', 'lastname', 'email', 'city', 'country',
386 'picture', 'lang', 'timezone', 'maildisplay', 'imagealt', 'lastaccess'));
387
4f0c2d00
PS
388 if ($isfrontpage) {
389 $select = "SELECT u.id, u.username, u.firstname, u.lastname,
5f9e296a 390 u.email, u.city, u.country, u.picture,
46505ee7 391 u.lang, u.timezone, u.maildisplay, u.imagealt,
43aa295b 392 u.lastaccess$extrasql";
4f0c2d00
PS
393 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // everybody on the frontpage usually
394 if ($accesssince) {
395 $wheres[] = get_user_lastaccess_sql($accesssince);
396 }
397
398 } else {
399 $select = "SELECT u.id, u.username, u.firstname, u.lastname,
5f9e296a 400 u.email, u.city, u.country, u.picture,
46505ee7 401 u.lang, u.timezone, u.maildisplay, u.imagealt,
43aa295b 402 COALESCE(ul.timeaccess, 0) AS lastaccess$extrasql";
4f0c2d00
PS
403 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // course enrolled users only
404 $joins[] = "LEFT JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = :courseid)"; // not everybody accessed course yet
405 $params['courseid'] = $course->id;
406 if ($accesssince) {
407 $wheres[] = get_course_lastaccess_sql($accesssince);
5f9e296a 408 }
165088f6 409 }
5f9e296a 410
4f0c2d00
PS
411 // performance hacks - we preload user contexts together with accounts
412 list($ccselect, $ccjoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
413 $select .= $ccselect;
414 $joins[] = $ccjoin;
c0527567 415
4f0c2d00
PS
416
417 // limit list to users with some role only
418 if ($roleid) {
419 $wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $contextlist)";
420 $params['roleid'] = $roleid;
c0527567 421 }
0be6f678 422
4f0c2d00
PS
423 $from = implode("\n", $joins);
424 if ($wheres) {
425 $where = "WHERE " . implode(" AND ", $wheres);
165088f6 426 } else {
4f0c2d00 427 $where = "";
165088f6 428 }
4f0c2d00
PS
429
430 $totalcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
77c645df 431
432 if (!empty($search)) {
ce8c75ee 433 $fullname = $DB->sql_fullname('u.firstname','u.lastname');
f63ac65a
PS
434 $wheres[] = "(". $DB->sql_like($fullname, ':search1', false, false) .
435 " OR ". $DB->sql_like('email', ':search2', false, false) .
436 " OR ". $DB->sql_like('idnumber', ':search3', false, false) .") ";
ce8c75ee 437 $params['search1'] = "%$search%";
438 $params['search2'] = "%$search%";
439 $params['search3'] = "%$search%";
77c645df 440 }
441
0f21a964
PS
442 list($twhere, $tparams) = $table->get_sql_where();
443 if ($twhere) {
444 $wheres[] = $twhere;
445 $params = array_merge($params, $tparams);
77c645df 446 }
447
4f0c2d00
PS
448 $from = implode("\n", $joins);
449 if ($wheres) {
450 $where = "WHERE " . implode(" AND ", $wheres);
451 } else {
452 $where = "";
77c645df 453 }
454
455 if ($table->get_sql_sort()) {
4d93bc9e 456 $sort = ' ORDER BY '.$table->get_sql_sort();
77c645df 457 } else {
4d93bc9e 458 $sort = '';
77c645df 459 }
460
4f0c2d00 461 $matchcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
77c645df 462
281917e9 463 $table->initialbars(true);
77c645df 464 $table->pagesize($perpage, $matchcount);
465
4f0c2d00
PS
466 // list of users at the current visible page - paging makes it relatively short
467 $userlist = $DB->get_recordset_sql("$select $from $where $sort", $params, $table->get_page_start(), $table->get_page_size());
3e219038 468
5f9e296a 469 /// If there are multiple Roles in the course, then show a drop down menu for switching
6d3c57f4 470 if (count($rolenames) > 1) {
77c645df 471 echo '<div class="rolesform">';
5f9e296a 472 echo '<label for="rolesform_jump">'.get_string('currentrole', 'role').'&nbsp;</label>';
f8dab966 473 echo $OUTPUT->single_select($rolenamesurl, 'roleid', $rolenames, $roleid, null, 'rolesform');
0f870a49 474 echo '</div>';
5f9e296a 475
476 } else if (count($rolenames) == 1) {
477 // when all users with the same role - print its name
478 echo '<div class="rolesform">';
995f2d51 479 echo get_string('role').get_string('labelsep', 'langconfig');
5f9e296a 480 $rolename = reset($rolenames);
481 echo $rolename;
482 echo '</div>';
77c645df 483 }
3e219038 484
5f9e296a 485 if ($roleid > 0) {
b90e2f19 486 $a->number = $totalcount;
4f0c2d00 487 $a->role = $rolenames[$roleid];
5f9e296a 488 $heading = format_string(get_string('xuserswiththerole', 'role', $a));
f642a21a 489
490 if ($currentgroup and $group) {
491 $a->group = $group->name;
5f9e296a 492 $heading .= ' ' . format_string(get_string('ingroup', 'role', $a));
493 }
f642a21a 494
495 if ($accesssince) {
496 $a->timeperiod = $timeoptions[$accesssince];
5f9e296a 497 $heading .= ' ' . format_string(get_string('inactiveformorethan', 'role', $a));
f642a21a 498 }
499
500 $heading .= ": $a->number";
d02eeded 501 if (user_can_assign($context, $roleid)) {
3e219038 502 $heading .= ' <a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?roleid='.$roleid.'&amp;contextid='.$context->id.'">';
b5d0cafc 503 $heading .= '<img src="'.$OUTPUT->pix_url('i/edit') . '" class="icon" alt="" /></a>';
3e219038 504 }
04a7ba52 505 echo $OUTPUT->heading($heading, 3);
3e219038 506 } else {
d9e29086
DM
507 if ($course->id != SITEID && has_capability('moodle/course:enrolreview', $context)) {
508 $editlink = $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $course->id)),
509 new pix_icon('i/edit', get_string('edit')));
15234a92 510 } else {
511 $editlink = '';
512 }
5f9e296a 513 if ($course->id == SITEID and $roleid < 0) {
514 $strallparticipants = get_string('allsiteusers', 'role');
515 } else {
516 $strallparticipants = get_string('allparticipants');
517 }
23f5ee25 518 if ($matchcount < $totalcount) {
995f2d51 519 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount.'/'.$totalcount . $editlink, 3);
23f5ee25 520 } else {
995f2d51 521 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount . $editlink, 3);
23f5ee25 522 }
3e219038 523 }
3e219038 524
77c645df 525
526 if ($bulkoperations) {
2ea56951 527 echo '<form action="action_redir.php" method="post" id="participantsform">';
c1138797 528 echo '<div>';
d4a1fcaf 529 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
838c7d29 530 echo '<input type="hidden" name="returnto" value="'.s(me()).'" />';
77c645df 531 }
532
4f0c2d00 533 if ($mode === MODE_USERDETAILS) { // Print simple listing
77c645df 534 if ($totalcount < 1) {
8fa89bfd 535 echo $OUTPUT->heading(get_string('nothingtodisplay'));
77c645df 536 } else {
537 if ($totalcount > $perpage) {
538
539 $firstinitial = $table->get_initial_first();
540 $lastinitial = $table->get_initial_last();
541 $strall = get_string('all');
61bb07c2 542 $alpha = explode(',', get_string('alphabet', 'langconfig'));
77c645df 543
544 // Bar of first initials
545
546 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
547 if(!empty($firstinitial)) {
d7270311 548 echo '<a href="'.$baseurl->out().'&amp;sifirst=">'.$strall.'</a>';
77c645df 549 } else {
550 echo '<strong>'.$strall.'</strong>';
551 }
552 foreach ($alpha as $letter) {
553 if ($letter == $firstinitial) {
554 echo ' <strong>'.$letter.'</strong>';
ee4a52c5 555 } else {
d7270311 556 echo ' <a href="'.$baseurl->out().'&amp;sifirst='.$letter.'">'.$letter.'</a>';
ee4a52c5 557 }
ee4a52c5 558 }
77c645df 559 echo '</div>';
560
561 // Bar of last initials
562
563 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
564 if(!empty($lastinitial)) {
d7270311 565 echo '<a href="'.$baseurl->out().'&amp;silast=">'.$strall.'</a>';
77c645df 566 } else {
567 echo '<strong>'.$strall.'</strong>';
568 }
569 foreach ($alpha as $letter) {
570 if ($letter == $lastinitial) {
571 echo ' <strong>'.$letter.'</strong>';
572 } else {
d7270311 573 echo ' <a href="'.$baseurl->out().'&amp;silast='.$letter.'">'.$letter.'</a>';
ee4a52c5 574 }
575 }
77c645df 576 echo '</div>';
577
929d7a83 578 $pagingbar = new paging_bar($matchcount, intval($table->get_page_start() / $perpage), $perpage, $baseurl);
da9ac19f 579 $pagingbar->pagevar = 'spage';
105666bb 580 echo $OUTPUT->render($pagingbar);
77c645df 581 }
582
583 if ($matchcount > 0) {
1f807bc4 584 $usersprinted = array();
ce8c75ee 585 foreach ($userlist as $user) {
1f807bc4 586 if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
587 continue;
588 }
589 $usersprinted[] = $user->id; /// Add new user to the array of users printed
590
4f0c2d00 591 context_instance_preload($user);
c351150f 592
593 $context = get_context_instance(CONTEXT_COURSE, $course->id);
4f0c2d00 594 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
c351150f 595
0aa759b0 596 $countries = get_string_manager()->get_list_of_countries();
c351150f 597
598 /// Get the hidden field list
599 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
600 $hiddenfields = array();
601 } else {
602 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
603 }
604 $table = new html_table();
16be8974 605 $table->attributes['class'] = 'userinfobox';
c351150f 606
607 $row = new html_table_row();
608 $row->cells[0] = new html_table_cell();
16be8974 609 $row->cells[0]->attributes['class'] = 'left side';
f2f085ee 610
9c5cafd6 611 $row->cells[0]->text = $OUTPUT->user_picture($user, array('size' => 100, 'courseid'=>$course->id));
c351150f 612 $row->cells[1] = new html_table_cell();
16be8974 613 $row->cells[1]->attributes['class'] = 'content';
c351150f 614
615 $row->cells[1]->text = $OUTPUT->container(fullname($user, has_capability('moodle/site:viewfullnames', $context)), 'username');
616 $row->cells[1]->text .= $OUTPUT->container_start('info');
617
618 if (!empty($user->role)) {
995f2d51 619 $row->cells[1]->text .= get_string('role').get_string('labelsep', 'langconfig').$user->role.'<br />';
c351150f 620 }
474f6bfe 621 if ($user->maildisplay == 1 or ($user->maildisplay == 2 and ($course->id != SITEID) and !isguestuser()) or
43aa295b 622 has_capability('moodle/course:viewhiddenuserfields', $context) or
623 in_array('email', $extrafields)) {
995f2d51 624 $row->cells[1]->text .= get_string('email').get_string('labelsep', 'langconfig').html_writer::link("mailto:$user->email", $user->email) . '<br />';
c351150f 625 }
43aa295b 626 foreach ($extrafields as $field) {
627 if ($field === 'email') {
628 // Skip email because it was displayed with different
629 // logic above (because this page is intended for
630 // students too)
631 continue;
632 }
633 $row->cells[1]->text .= get_user_field_name($field) .
634 get_string('labelsep', 'langconfig') . s($user->{$field}) . '<br />';
635 }
c351150f 636 if (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) {
995f2d51 637 $row->cells[1]->text .= get_string('city').get_string('labelsep', 'langconfig');
c351150f 638 if ($user->city && !isset($hiddenfields['city'])) {
639 $row->cells[1]->text .= $user->city;
640 }
641 if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) {
642 if ($user->city && !isset($hiddenfields['city'])) {
643 $row->cells[1]->text .= ', ';
644 }
645 $row->cells[1]->text .= $countries[$user->country];
646 }
647 $row->cells[1]->text .= '<br />';
648 }
649
650 if (!isset($hiddenfields['lastaccess'])) {
651 if ($user->lastaccess) {
995f2d51 652 $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').userdate($user->lastaccess);
c351150f 653 $row->cells[1]->text .= '&nbsp; ('. format_time(time() - $user->lastaccess, $datestring) .')';
654 } else {
995f2d51 655 $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').get_string('never');
c351150f 656 }
657 }
658
659 $row->cells[1]->text .= $OUTPUT->container_end();
3aac07d8 660
c351150f 661 $row->cells[2] = new html_table_cell();
16be8974 662 $row->cells[2]->attributes['class'] = 'links';
c351150f 663 $row->cells[2]->text = '';
3aac07d8 664
c351150f 665 $links = array();
666
667 if ($CFG->bloglevel > 0) {
a6855934 668 $links[] = html_writer::link(new moodle_url('/blog/index.php?userid='.$user->id), get_string('blogs','blog'));
c351150f 669 }
670
671 if (!empty($CFG->enablenotes) and (has_capability('moodle/notes:manage', $context) || has_capability('moodle/notes:view', $context))) {
a6855934 672 $links[] = html_writer::link(new moodle_url('/notes/index.php?course=' . $course->id. '&user='.$user->id), get_string('notes','notes'));
c351150f 673 }
674
675 if (has_capability('moodle/site:viewreports', $context) or has_capability('moodle/user:viewuseractivitiesreport', $usercontext)) {
a6855934 676 $links[] = html_writer::link(new moodle_url('/course/user.php?id='. $course->id .'&user='. $user->id), get_string('activity'));
c351150f 677 }
678
4f0c2d00 679 if ($USER->id != $user->id && !session_is_loggedinas() && has_capability('moodle/user:loginas', $context) && !is_siteadmin($user->id)) {
a6855934 680 $links[] = html_writer::link(new moodle_url('/course/loginas.php?id='. $course->id .'&user='. $user->id .'&sesskey='. sesskey()), get_string('loginas'));
c351150f 681 }
682
a6855934 683 $links[] = html_writer::link(new moodle_url('/user/view.php?id='. $user->id .'&course='. $course->id), get_string('fullprofile') . '...');
3aac07d8 684
f8dab966 685 $row->cells[2]->text .= implode('', $links);
c351150f 686
687 if (!empty($messageselect)) {
688 $row->cells[2]->text .= '<br /><input type="checkbox" name="user'.$user->id.'" /> ';
689 }
690 $table->data = array($row);
16be8974 691 echo html_writer::table($table);
ee4a52c5 692 }
77c645df 693
694 } else {
8fa89bfd 695 echo $OUTPUT->heading(get_string('nothingtodisplay'));
ee4a52c5 696 }
697 }
77c645df 698
699 } else {
700 $countrysort = (strpos($sort, 'country') !== false);
701 $timeformat = get_string('strftimedate');
3997cb40 702
703
03cedd62 704 if ($userlist) {
664fe87f 705
1f807bc4 706 $usersprinted = array();
ce8c75ee 707 foreach ($userlist as $user) {
1f807bc4 708 if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
709 continue;
710 }
711 $usersprinted[] = $user->id; /// Add new user to the array of users printed
712
4f0c2d00 713 context_instance_preload($user);
0be6f678 714
04186cd9 715 if ($user->lastaccess) {
716 $lastaccess = format_time(time() - $user->lastaccess, $datestring);
77c645df 717 } else {
718 $lastaccess = $strnever;
719 }
720
04186cd9 721 if (empty($user->country)) {
77c645df 722 $country = '';
723
724 } else {
725 if($countrysort) {
04186cd9 726 $country = '('.$user->country.') '.$countries[$user->country];
ee4a52c5 727 }
728 else {
04186cd9 729 $country = $countries[$user->country];
ee4a52c5 730 }
77c645df 731 }
0be6f678 732
4f0c2d00 733 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
0be6f678 734
b08261a1 735 if ($piclink = ($USER->id == $user->id || has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext))) {
22ae509e 736 $profilelink = '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></strong>';
737 } else {
0be6f678 738 $profilelink = '<strong>'.fullname($user).'</strong>';
22ae509e 739 }
0be6f678 740
9c5cafd6 741 $data = array ($OUTPUT->user_picture($user, array('size' => 35, 'courseid'=>$course->id)), $profilelink);
22ae509e 742
43aa295b 743 if ($mode === MODE_BRIEF) {
744 foreach ($extrafields as $field) {
745 $data[] = $user->{$field};
746 }
747 }
073af1a6 748 if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
04186cd9 749 $data[] = $user->city;
77c645df 750 }
073af1a6 751 if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
77c645df 752 $data[] = $country;
753 }
754 if (!isset($hiddenfields['lastaccess'])) {
755 $data[] = $lastaccess;
756 }
664fe87f 757
758 if (isset($userlist_extra) && isset($userlist_extra[$user->id])) {
759 $ras = $userlist_extra[$user->id]['ra'];
760 $rastring = '';
761 foreach ($ras AS $key=>$ra) {
4f0c2d00 762 $rolename = $allrolenames[$ra['roleid']] ;
664fe87f 763 if ($ra['ctxlevel'] == CONTEXT_COURSECAT) {
073af1a6 764 $rastring .= $rolename. ' @ ' . '<a href="'.$CFG->wwwroot.'/course/category.php?id='.$ra['ctxinstanceid'].'">'.s($ra['ccname']).'</a>';
664fe87f 765 } elseif ($ra['ctxlevel'] == CONTEXT_SYSTEM) {
073af1a6 766 $rastring .= $rolename. ' - ' . get_string('globalrole','role');
664fe87f 767 } else {
768 $rastring .= $rolename;
769 }
664fe87f 770 }
06e84d52 771 $data[] = $rastring;
a32e05df 772 if ($groupmode != 0) {
664fe87f 773 // htmlescape with s() and implode the array
774 $data[] = implode(', ', array_map('s',$userlist_extra[$user->id]['group']));
98da6021 775 $data[] = implode(', ', array_map('s', $userlist_extra[$user->id]['gping']));
664fe87f 776 }
664fe87f 777 }
778
77c645df 779 if ($bulkoperations) {
2ea56951 780 $data[] = '<input type="checkbox" class="usercheckbox" name="user'.$user->id.'" />';
77c645df 781 }
782 $table->add_data($data);
ee4a52c5 783 }
ee4a52c5 784 }
b90e2f19 785
77c645df 786 $table->print_html();
787
788 }
789
790 if ($bulkoperations) {
c1138797 791 echo '<br /><div class="buttons">';
2ea56951
PS
792 echo '<input type="button" id="checkall" value="'.get_string('selectall').'" /> ';
793 echo '<input type="button" id="checknone" value="'.get_string('deselectall').'" /> ';
82d561ee 794 $displaylist = array();
576ad290 795 $displaylist['messageselect.php'] = get_string('messageselectadd');
90658eef 796 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
eca3af25 797 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
798 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
799 }
576ad290 800
62357718 801 echo $OUTPUT->help_icon('withselectedusers');
26acc814 802 echo html_writer::tag('label', get_string("withselectedusers"), array('for'=>'formactionid'));
2ea56951
PS
803 echo html_writer::select($displaylist, 'formaction', '', array(''=>'choosedots'), array('id'=>'formactionid'));
804
70ce923e 805 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
2ea56951 806 echo '<noscript style="display:inline">';
f53f73ca 807 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
2ea56951
PS
808 echo '</noscript>';
809 echo '</div></div>';
c1138797 810 echo '</form>';
d88c5043 811
2ea56951
PS
812 $module = array('name'=>'core_user', 'fullpath'=>'/user/module.js');
813 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
77c645df 814 }
b90e2f19 815
3a8f4b90 816 if (has_capability('moodle/site:viewparticipants', $context) && $totalcount > ($perpage*3)) {
8c275093 817 echo '<form action="index.php" class="searchform"><div><input type="hidden" name="id" value="'.$course->id.'" />'.get_string('search').':&nbsp;'."\n";
3593af19 818 echo '<input type="text" name="search" value="'.s($search).'" />&nbsp;<input type="submit" value="'.get_string('search').'" /></div></form>'."\n";
77c645df 819 }
b90e2f19 820
d7270311 821 $perpageurl = clone($baseurl);
822 $perpageurl->remove_params('perpage');
77c645df 823 if ($perpage == SHOW_ALL_PAGE_SIZE) {
d7270311 824 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
0f4c64b7 825 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
77c645df 826
827 } else if ($matchcount > 0 && $perpage < $matchcount) {
d7270311 828 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
0f4c64b7 829 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $matchcount)), array(), 'showall');
b90e2f19 830 }
77c645df 831
99cca847
MD
832 echo '</div>'; // userlist
833
0ab75c34 834 echo $OUTPUT->footer();
f9903ed0 835
03cedd62 836 if ($userlist) {
ce8c75ee 837 $userlist->close();
03cedd62 838 }
77c645df 839
840
0749caef 841function get_course_lastaccess_sql($accesssince='') {
31b71336 842 if (empty($accesssince)) {
843 return '';
844 }
845 if ($accesssince == -1) { // never
4f0c2d00 846 return 'ul.timeaccess = 0';
31b71336 847 } else {
4f0c2d00 848 return 'ul.timeaccess != 0 AND ul.timeaccess < '.$accesssince;
0749caef 849 }
850}
851
852function get_user_lastaccess_sql($accesssince='') {
853 if (empty($accesssince)) {
854 return '';
855 }
856 if ($accesssince == -1) { // never
4f0c2d00 857 return 'u.lastaccess = 0';
0749caef 858 } else {
4f0c2d00 859 return 'u.lastaccess != 0 AND u.lastaccess < '.$accesssince;
31b71336 860 }
861}