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