Merge branch 'MDL-59364_master' of git://github.com/markn86/moodle
[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');
0ff203b6 26require_once($CFG->dirroot.'/user/lib.php');
a2ed6e69
SH
27require_once($CFG->libdir.'/tablelib.php');
28require_once($CFG->libdir.'/filelib.php');
29
30define('USER_SMALL_CLASS', 20); // Below this is considered small.
31define('USER_LARGE_CLASS', 200); // Above this is considered large.
32define('DEFAULT_PAGE_SIZE', 20);
33define('SHOW_ALL_PAGE_SIZE', 5000);
a2ed6e69
SH
34
35$page = optional_param('page', 0, PARAM_INT); // Which page to show.
36$perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
a2ed6e69
SH
37$accesssince = optional_param('accesssince', 0, PARAM_INT); // Filter by last access. -1 = never.
38$search = optional_param('search', '', PARAM_RAW); // Make sure it is processed with p() or s() when sending to output!
39$roleid = optional_param('roleid', 0, PARAM_INT); // Optional roleid, 0 means all enrolled users (or all on the frontpage).
40$contextid = optional_param('contextid', 0, PARAM_INT); // One of this or.
41$courseid = optional_param('id', 0, PARAM_INT); // This are required.
5b7c500a 42$selectall = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
a2ed6e69
SH
43
44$PAGE->set_url('/user/index.php', array(
45 'page' => $page,
46 'perpage' => $perpage,
a2ed6e69
SH
47 'accesssince' => $accesssince,
48 'search' => $search,
49 'roleid' => $roleid,
50 'contextid' => $contextid,
51 'id' => $courseid));
52
53if ($contextid) {
54 $context = context::instance_by_id($contextid, MUST_EXIST);
55 if ($context->contextlevel != CONTEXT_COURSE) {
56 print_error('invalidcontext');
57 }
58 $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
59} else {
60 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
61 $context = context_course::instance($course->id, MUST_EXIST);
62}
63// Not needed anymore.
64unset($contextid);
65unset($courseid);
f9903ed0 66
a2ed6e69 67require_login($course);
f9903ed0 68
a2ed6e69
SH
69$systemcontext = context_system::instance();
70$isfrontpage = ($course->id == SITEID);
f9903ed0 71
a2ed6e69 72$frontpagectx = context_course::instance(SITEID);
4f0c2d00 73
a2ed6e69
SH
74if ($isfrontpage) {
75 $PAGE->set_pagelayout('admin');
76 require_capability('moodle/site:viewparticipants', $systemcontext);
77} else {
78 $PAGE->set_pagelayout('incourse');
79 require_capability('moodle/course:viewparticipants', $context);
80}
224aa44a 81
a2ed6e69 82$rolenamesurl = new moodle_url("$CFG->wwwroot/user/index.php?contextid=$context->id&sifirst=&silast=");
6d3c57f4 83
a2ed6e69
SH
84$rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true);
85if ($isfrontpage) {
86 $rolenames[0] = get_string('allsiteusers', 'role');
87} else {
88 $rolenames[0] = get_string('allparticipants');
89}
d7270311 90
a2ed6e69
SH
91// Make sure other roles may not be selected by any means.
92if (empty($rolenames[$roleid])) {
93 print_error('noparticipants');
94}
5f9e296a 95
a2ed6e69
SH
96// No roles to display yet?
97// frontpage course is an exception, on the front page course we should display all users.
98if (empty($rolenames) && !$isfrontpage) {
99 if (has_capability('moodle/role:assign', $context)) {
100 redirect($CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id);
101 } else {
4f0c2d00 102 print_error('noparticipants');
5f9e296a 103 }
a2ed6e69 104}
5f9e296a 105
0ff203b6
JL
106// Trigger events.
107user_list_view($course, $context);
a2ed6e69
SH
108
109$bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
110
111$countries = get_string_manager()->get_list_of_countries();
112
113$strnever = get_string('never');
114
115$datestring = new stdClass();
116$datestring->year = get_string('year');
117$datestring->years = get_string('years');
118$datestring->day = get_string('day');
119$datestring->days = get_string('days');
120$datestring->hour = get_string('hour');
121$datestring->hours = get_string('hours');
122$datestring->min = get_string('min');
123$datestring->mins = get_string('mins');
124$datestring->sec = get_string('sec');
125$datestring->secs = get_string('secs');
126
a2ed6e69
SH
127// Check to see if groups are being used in this course
128// and if so, set $currentgroup to reflect the current group.
ff3caf30 129
a2ed6e69
SH
130$groupmode = groups_get_course_groupmode($course); // Groups are being used.
131$currentgroup = groups_get_course_group($course, true);
ff3caf30 132
a2ed6e69
SH
133if (!$currentgroup) { // To make some other functions work better later.
134 $currentgroup = null;
135}
ff3caf30 136
a2ed6e69 137$isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
ff3caf30 138
a2ed6e69
SH
139$PAGE->set_title("$course->shortname: ".get_string('participants'));
140$PAGE->set_heading($course->fullname);
141$PAGE->set_pagetype('course-view-' . $course->format);
142$PAGE->add_body_class('path-user'); // So we can style it independently.
143$PAGE->set_other_editing_capability('moodle/course:manageactivities');
2cb2ce61 144
a2ed6e69 145echo $OUTPUT->header();
4e1f6047 146echo $OUTPUT->heading(get_string('participants'));
caa8363f 147
a2ed6e69 148echo '<div class="userlist">';
0be6f678 149
a2ed6e69
SH
150if ($isseparategroups and (!$currentgroup) ) {
151 // The user is not in the group so show message and exit.
152 echo $OUTPUT->heading(get_string("notingroup"));
153 echo $OUTPUT->footer();
154 exit;
155}
99cca847 156
ff3caf30 157
a2ed6e69
SH
158// Should use this variable so that we don't break stuff every time a variable is added or changed.
159$baseurl = new moodle_url('/user/index.php', array(
160 'contextid' => $context->id,
161 'roleid' => $roleid,
162 'id' => $course->id,
163 'perpage' => $perpage,
164 'accesssince' => $accesssince,
165 'search' => s($search)));
166
167// Setting up tags.
168if ($course->id == SITEID) {
169 $filtertype = 'site';
170} else if ($course->id && !$currentgroup) {
171 $filtertype = 'course';
172 $filterselect = $course->id;
173} else {
174 $filtertype = 'group';
175 $filterselect = $currentgroup;
176}
03d9401e 177
1242eb8f 178
03d9401e 179
a2ed6e69
SH
180// Get the hidden field list.
181if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
182 $hiddenfields = array(); // Teachers and admins are allowed to see everything.
183} else {
c6b44cb4 184 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
a2ed6e69 185}
1242eb8f 186
a2ed6e69
SH
187if (isset($hiddenfields['lastaccess'])) {
188 // Do not allow access since filtering.
189 $accesssince = 0;
190}
3468d58a 191
a2ed6e69
SH
192// Print settings and things in a table across the top.
193$controlstable = new html_table();
194$controlstable->attributes['class'] = 'controls';
195$controlstable->cellspacing = 0;
196$controlstable->data[] = new html_table_row();
197
198// Print my course menus.
199if ($mycourses = enrol_get_my_courses()) {
200 $courselist = array();
201 $popupurl = new moodle_url('/user/index.php?roleid='.$roleid.'&sifirst=&silast=');
202 foreach ($mycourses as $mycourse) {
203 $coursecontext = context_course::instance($mycourse->id);
204 $courselist[$mycourse->id] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
205 }
206 if (has_capability('moodle/site:viewparticipants', $systemcontext)) {
207 unset($courselist[SITEID]);
208 $courselist = array(SITEID => format_string($SITE->shortname, true, array('context' => $systemcontext))) + $courselist;
209 }
210 $select = new single_select($popupurl, 'id', $courselist, $course->id, null, 'courseform');
211 $select->set_label(get_string('mycourses'));
212 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
213}
bbbf2d40 214
24c3db91
DP
215if ($groupmenu = groups_print_course_menu($course, $baseurl->out(), true)) {
216 $controlstable->data[0]->cells[] = $groupmenu;
217}
a2ed6e69
SH
218
219if (!isset($hiddenfields['lastaccess'])) {
220 // Get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
221 // We need to make it diferently for normal courses and site course.
222 if (!$isfrontpage) {
223 $minlastaccess = $DB->get_field_sql('SELECT min(timeaccess)
224 FROM {user_lastaccess}
225 WHERE courseid = ?
226 AND timeaccess != 0', array($course->id));
227 $lastaccess0exists = $DB->record_exists('user_lastaccess', array('courseid' => $course->id, 'timeaccess' => 0));
228 } else {
229 $minlastaccess = $DB->get_field_sql('SELECT min(lastaccess)
230 FROM {user}
231 WHERE lastaccess != 0');
232 $lastaccess0exists = $DB->record_exists('user', array('lastaccess' => 0));
7da0af9f 233 }
87a13824 234
a2ed6e69
SH
235 $now = usergetmidnight(time());
236 $timeaccess = array();
237 $baseurl->remove_params('accesssince');
87a13824 238
a2ed6e69
SH
239 // Makes sense for this to go first.
240 $timeoptions[0] = get_string('selectperiod');
87a13824 241
a2ed6e69
SH
242 // Days.
243 for ($i = 1; $i < 7; $i++) {
244 if (strtotime('-'.$i.' days', $now) >= $minlastaccess) {
245 $timeoptions[strtotime('-'.$i.' days', $now)] = get_string('numdays', 'moodle', $i);
ca792680 246 }
a2ed6e69
SH
247 }
248 // Weeks.
249 for ($i = 1; $i < 10; $i++) {
250 if (strtotime('-'.$i.' weeks', $now) >= $minlastaccess) {
251 $timeoptions[strtotime('-'.$i.' weeks', $now)] = get_string('numweeks', 'moodle', $i);
5ff311f0 252 }
a2ed6e69
SH
253 }
254 // Months.
255 for ($i = 2; $i < 12; $i++) {
256 if (strtotime('-'.$i.' months', $now) >= $minlastaccess) {
257 $timeoptions[strtotime('-'.$i.' months', $now)] = get_string('nummonths', 'moodle', $i);
31b71336 258 }
a2ed6e69
SH
259 }
260 // Try a year.
261 if (strtotime('-1 year', $now) >= $minlastaccess) {
262 $timeoptions[strtotime('-1 year', $now)] = get_string('lastyear');
263 }
87a13824 264
a2ed6e69
SH
265 if (!empty($lastaccess0exists)) {
266 $timeoptions[-1] = get_string('never');
267 }
87a13824 268
a2ed6e69
SH
269 if (count($timeoptions) > 1) {
270 $select = new single_select($baseurl, 'accesssince', $timeoptions, $accesssince, null, 'timeoptions');
271 $select->set_label(get_string('usersnoaccesssince'));
272 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
77c645df 273 }
a2ed6e69 274}
87a13824 275
a2ed6e69
SH
276echo html_writer::table($controlstable);
277
278if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) {
279 // Display info about the group.
280 if ($group = groups_get_group($currentgroup)) {
281 if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
282 $groupinfotable = new html_table();
283 $groupinfotable->attributes['class'] = 'groupinfobox';
284 $picturecell = new html_table_cell();
285 $picturecell->attributes['class'] = 'left side picture';
286 $picturecell->text = print_group_picture($group, $course->id, true, true, false);
287
288 $contentcell = new html_table_cell();
289 $contentcell->attributes['class'] = 'content';
290
291 $contentheading = $group->name;
292 if (has_capability('moodle/course:managegroups', $context)) {
293 $aurl = new moodle_url('/group/group.php', array('id' => $group->id, 'courseid' => $group->courseid));
294 $contentheading .= '&nbsp;' . $OUTPUT->action_icon($aurl, new pix_icon('t/edit', get_string('editgroupprofile')));
295 }
8bdc9cac 296
a2ed6e69
SH
297 $group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group',
298 'description', $group->id);
299 if (!isset($group->descriptionformat)) {
300 $group->descriptionformat = FORMAT_MOODLE;
ff3caf30 301 }
a2ed6e69
SH
302 $options = array('overflowdiv' => true);
303 $formatteddesc = format_text($group->description, $group->descriptionformat, $options);
304 $contentcell->text = $OUTPUT->heading($contentheading, 3) . $formatteddesc;
305 $groupinfotable->data[] = new html_table_row(array($picturecell, $contentcell));
306 echo html_writer::table($groupinfotable);
ff3caf30 307 }
308 }
a2ed6e69 309}
ff3caf30 310
a2ed6e69
SH
311// Define a table showing a list of users in the current role selection.
312$tablecolumns = array();
313$tableheaders = array();
4c7593ff 314if ($bulkoperations) {
a2ed6e69
SH
315 $tablecolumns[] = 'select';
316 $tableheaders[] = get_string('select');
317}
318$tablecolumns[] = 'userpic';
319$tablecolumns[] = 'fullname';
c238e1e2 320
a2ed6e69
SH
321$extrafields = get_extra_user_fields($context);
322$tableheaders[] = get_string('userpic');
323$tableheaders[] = get_string('fullnameuser');
77c645df 324
4c7593ff
MN
325
326foreach ($extrafields as $field) {
327 $tablecolumns[] = $field;
328 $tableheaders[] = get_user_field_name($field);
a2ed6e69 329}
4c7593ff 330if (!isset($hiddenfields['city'])) {
a2ed6e69
SH
331 $tablecolumns[] = 'city';
332 $tableheaders[] = get_string('city');
333}
4c7593ff 334if (!isset($hiddenfields['country'])) {
a2ed6e69
SH
335 $tablecolumns[] = 'country';
336 $tableheaders[] = get_string('country');
337}
338if (!isset($hiddenfields['lastaccess'])) {
339 $tablecolumns[] = 'lastaccess';
e89682d8
DC
340 if ($course->id == SITEID) {
341 // Exception case for viewing participants on site home.
342 $tableheaders[] = get_string('lastsiteaccess');
343 } else {
344 $tableheaders[] = get_string('lastcourseaccess');
345 }
a2ed6e69 346}
77c645df 347
a2ed6e69
SH
348$table = new flexible_table('user-index-participants-'.$course->id);
349$table->define_columns($tablecolumns);
350$table->define_headers($tableheaders);
351$table->define_baseurl($baseurl->out());
5ff311f0 352
28d27e85 353if (!isset($hiddenfields['lastaccess'])) {
354 $table->sortable(true, 'lastaccess', SORT_DESC);
a2ed6e69
SH
355} else {
356 $table->sortable(true, 'firstname', SORT_ASC);
357}
4f0c2d00 358
a2ed6e69
SH
359$table->no_sorting('roles');
360$table->no_sorting('groups');
361$table->no_sorting('groupings');
362$table->no_sorting('select');
363
364$table->set_attribute('cellspacing', '0');
365$table->set_attribute('id', 'participants');
366$table->set_attribute('class', 'generaltable generalbox');
367
368$table->set_control_variables(array(
369 TABLE_VAR_SORT => 'ssort',
370 TABLE_VAR_HIDE => 'shide',
371 TABLE_VAR_SHOW => 'sshow',
372 TABLE_VAR_IFIRST => 'sifirst',
373 TABLE_VAR_ILAST => 'silast',
374 TABLE_VAR_PAGE => 'spage'
375 ));
376$table->setup();
377
378list($esql, $params) = get_enrolled_sql($context, null, $currentgroup, true);
379$joins = array("FROM {user} u");
380$wheres = array();
381
368f0bf8
DC
382$userfields = array('username', 'email', 'city', 'country', 'lang', 'timezone', 'maildisplay');
383$mainuserfields = user_picture::fields('u', $userfields);
384$extrasql = get_extra_user_fields_sql($context, 'u', '', $userfields);
a2ed6e69
SH
385
386if ($isfrontpage) {
387 $select = "SELECT $mainuserfields, u.lastaccess$extrasql";
388 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // Everybody on the frontpage usually.
389 if ($accesssince) {
390 $wheres[] = get_user_lastaccess_sql($accesssince);
391 }
392
393} else {
394 $select = "SELECT $mainuserfields, COALESCE(ul.timeaccess, 0) AS lastaccess$extrasql";
395 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // Course enrolled users only.
396 $joins[] = "LEFT JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = :courseid)"; // Not everybody accessed course yet.
397 $params['courseid'] = $course->id;
398 if ($accesssince) {
399 $wheres[] = get_course_lastaccess_sql($accesssince);
165088f6 400 }
a2ed6e69 401}
5f9e296a 402
a2ed6e69
SH
403// Performance hacks - we preload user contexts together with accounts.
404$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
405$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
406$params['contextlevel'] = CONTEXT_USER;
407$select .= $ccselect;
408$joins[] = $ccjoin;
c0527567 409
4f0c2d00 410
a2ed6e69
SH
411// Limit list to users with some role only.
412if ($roleid) {
419d1abd
DW
413 // We want to query both the current context and parent contexts.
414 list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
415
a2ed6e69 416 $wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $relatedctxsql)";
419d1abd 417 $params = array_merge($params, array('roleid' => $roleid), $relatedctxparams);
a2ed6e69 418}
0be6f678 419
a2ed6e69
SH
420$from = implode("\n", $joins);
421if ($wheres) {
422 $where = "WHERE " . implode(" AND ", $wheres);
423} else {
424 $where = "";
425}
4f0c2d00 426
a2ed6e69 427$totalcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
77c645df 428
a2ed6e69
SH
429if (!empty($search)) {
430 $fullname = $DB->sql_fullname('u.firstname', 'u.lastname');
431 $wheres[] = "(". $DB->sql_like($fullname, ':search1', false, false) .
432 " OR ". $DB->sql_like('email', ':search2', false, false) .
433 " OR ". $DB->sql_like('idnumber', ':search3', false, false) .") ";
434 $params['search1'] = "%$search%";
435 $params['search2'] = "%$search%";
436 $params['search3'] = "%$search%";
437}
77c645df 438
a2ed6e69
SH
439list($twhere, $tparams) = $table->get_sql_where();
440if ($twhere) {
441 $wheres[] = $twhere;
442 $params = array_merge($params, $tparams);
443}
77c645df 444
a2ed6e69
SH
445$from = implode("\n", $joins);
446if ($wheres) {
447 $where = "WHERE " . implode(" AND ", $wheres);
448} else {
449 $where = "";
450}
77c645df 451
a2ed6e69
SH
452if ($table->get_sql_sort()) {
453 $sort = ' ORDER BY '.$table->get_sql_sort();
454} else {
455 $sort = '';
456}
77c645df 457
a2ed6e69 458$matchcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
77c645df 459
a2ed6e69
SH
460$table->initialbars(true);
461$table->pagesize($perpage, $matchcount);
77c645df 462
a2ed6e69
SH
463// List of users at the current visible page - paging makes it relatively short.
464$userlist = $DB->get_recordset_sql("$select $from $where $sort", $params, $table->get_page_start(), $table->get_page_size());
3e219038 465
a2ed6e69
SH
466// If there are multiple Roles in the course, then show a drop down menu for switching.
467if (count($rolenames) > 1) {
468 echo '<div class="rolesform">';
32bd11cb
BB
469 echo $OUTPUT->single_select($rolenamesurl, 'roleid', $rolenames, $roleid, null,
470 'rolesform', array('label' => get_string('currentrole', 'role')));
a2ed6e69 471 echo '</div>';
5f9e296a 472
a2ed6e69
SH
473} else if (count($rolenames) == 1) {
474 // When all users with the same role - print its name.
475 echo '<div class="rolesform">';
476 echo get_string('role').get_string('labelsep', 'langconfig');
477 $rolename = reset($rolenames);
478 echo $rolename;
479 echo '</div>';
480}
3e219038 481
7057c3ef
DP
482$editlink = '';
483if ($course->id != SITEID && has_capability('moodle/course:enrolreview', $context)) {
484 $editlink = new moodle_url('/enrol/users.php', array('id' => $course->id));
485}
486
a2ed6e69
SH
487if ($roleid > 0) {
488 $a = new stdClass();
489 $a->number = $totalcount;
490 $a->role = $rolenames[$roleid];
491 $heading = format_string(get_string('xuserswiththerole', 'role', $a));
f642a21a 492
9488b6f3 493 if ($currentgroup and !empty($group)) {
a2ed6e69
SH
494 $a->group = $group->name;
495 $heading .= ' ' . format_string(get_string('ingroup', 'role', $a));
496 }
f642a21a 497
9488b6f3 498 if ($accesssince && !empty($timeoptions[$accesssince])) {
a2ed6e69
SH
499 $a->timeperiod = $timeoptions[$accesssince];
500 $heading .= ' ' . format_string(get_string('inactiveformorethan', 'role', $a));
501 }
f642a21a 502
a2ed6e69 503 $heading .= ": $a->number";
b85b25eb 504
7057c3ef
DP
505 if (!empty($editlink)) {
506 $editlink->param('role', $roleid);
507 $heading .= $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit')));
a2ed6e69
SH
508 }
509 echo $OUTPUT->heading($heading, 3);
510} else {
a2ed6e69
SH
511 if ($course->id == SITEID and $roleid < 0) {
512 $strallparticipants = get_string('allsiteusers', 'role');
513 } else {
514 $strallparticipants = get_string('allparticipants');
515 }
7057c3ef
DP
516
517 if (!empty($editlink)) {
518 $editlink = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit')));
519 }
520
a2ed6e69
SH
521 if ($matchcount < $totalcount) {
522 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount.'/'.$totalcount . $editlink, 3);
523 } else {
524 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount . $editlink, 3);
525 }
526}
3e219038 527
77c645df 528
a2ed6e69
SH
529if ($bulkoperations) {
530 echo '<form action="action_redir.php" method="post" id="participantsform">';
531 echo '<div>';
532 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
533 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
534}
77c645df 535
4c7593ff
MN
536$countrysort = (strpos($sort, 'country') !== false);
537$timeformat = get_string('strftimedate');
77c645df 538
4c7593ff 539if ($userlist) {
77c645df 540
4c7593ff
MN
541 $usersprinted = array();
542 foreach ($userlist as $user) {
543 if (in_array($user->id, $usersprinted)) { // Prevent duplicates by r.hidden - MDL-13935.
544 continue;
a2ed6e69 545 }
4c7593ff 546 $usersprinted[] = $user->id; // Add new user to the array of users printed.
a2ed6e69 547
4c7593ff 548 context_helper::preload_from_record($user);
a2ed6e69 549
4c7593ff
MN
550 if ($user->lastaccess) {
551 $lastaccess = format_time(time() - $user->lastaccess, $datestring);
a2ed6e69 552 } else {
4c7593ff 553 $lastaccess = $strnever;
ee4a52c5 554 }
3997cb40 555
4c7593ff
MN
556 if (empty($user->country)) {
557 $country = '';
3997cb40 558
4c7593ff
MN
559 } else {
560 if ($countrysort) {
561 $country = '('.$user->country.') '.$countries[$user->country];
a2ed6e69 562 } else {
4c7593ff 563 $country = $countries[$user->country];
a2ed6e69 564 }
4c7593ff 565 }
77c645df 566
4c7593ff 567 $usercontext = context_user::instance($user->id);
77c645df 568
4c7593ff
MN
569 if ($piclink = ($USER->id == $user->id || has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext))) {
570 $profilelink = '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></strong>';
571 } else {
572 $profilelink = '<strong>'.fullname($user).'</strong>';
573 }
0be6f678 574
4c7593ff
MN
575 $data = array();
576 if ($bulkoperations) {
577 if ($selectall) {
578 $checked = 'checked="true"';
a2ed6e69 579 } else {
4c7593ff 580 $checked = '';
a2ed6e69 581 }
4c7593ff
MN
582 $data[] = '<input type="checkbox" class="usercheckbox" name="user'.$user->id.'" ' . $checked .'/>';
583 }
584 $data[] = $OUTPUT->user_picture($user, array('size' => 35, 'courseid' => $course->id));
585 $data[] = $profilelink;
664fe87f 586
4c7593ff
MN
587 foreach ($extrafields as $field) {
588 $data[] = $user->{$field};
eca3af25 589 }
576ad290 590
4c7593ff
MN
591 if (!isset($hiddenfields['city'])) {
592 $data[] = $user->city;
593 }
594 if (!isset($hiddenfields['country'])) {
595 $data[] = $country;
596 }
597 if (!isset($hiddenfields['lastaccess'])) {
598 $data[] = $lastaccess;
599 }
d88c5043 600
4c7593ff
MN
601 $table->add_data($data);
602 }
a2ed6e69 603}
b90e2f19 604
4c7593ff
MN
605$table->print_html();
606
8dd42b38
AH
607$perpageurl = clone($baseurl);
608$perpageurl->remove_params('perpage');
609if ($perpage == SHOW_ALL_PAGE_SIZE) {
610 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
611 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
612
613} else if ($matchcount > 0 && $perpage < $matchcount) {
614 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
615 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $matchcount)), array(), 'showall');
616}
617
a2ed6e69
SH
618if ($bulkoperations) {
619 echo '<br /><div class="buttons">';
5b7c500a
AH
620
621 if ($matchcount > 0 && $perpage < $matchcount) {
622 $perpageurl = clone($baseurl);
623 $perpageurl->remove_params('perpage');
624 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
625 $perpageurl->param('selectall', true);
626 $showalllink = $perpageurl;
627 } else {
628 $showalllink = false;
629 }
630
b27c8d81 631 echo html_writer::start_tag('div', array('class' => 'btn-group'));
5b7c500a
AH
632 if ($perpage < $matchcount) {
633 // Select all users, refresh page showing all users and mark them all selected.
634 $label = get_string('selectalluserswithcount', 'moodle', $matchcount);
b27c8d81
SB
635 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
636 'value' => $label, 'data-showallink' => $showalllink));
5b7c500a 637 // Select all users, mark all users on page as selected.
b27c8d81
SB
638 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
639 'value' => get_string('selectallusersonpage')));
5b7c500a 640 } else {
b27c8d81
SB
641 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
642 'value' => get_string('selectall')));
5b7c500a
AH
643 }
644
b27c8d81
SB
645 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checknone', 'class' => 'btn btn-secondary',
646 'value' => get_string('deselectall')));
647 echo html_writer::end_tag('div');
a2ed6e69
SH
648 $displaylist = array();
649 $displaylist['messageselect.php'] = get_string('messageselectadd');
650 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
651 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
652 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
653 }
654
655 echo $OUTPUT->help_icon('withselectedusers');
656 echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
657 echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
658
659 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
660 echo '<noscript style="display:inline">';
661 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
662 echo '</noscript>';
663 echo '</div></div>';
664 echo '</form>';
665
666 $module = array('name' => 'core_user', 'fullpath' => '/user/module.js');
667 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
668}
b90e2f19 669
a2ed6e69
SH
670// Show a search box if all participants don't fit on a single screen.
671if ($totalcount > $perpage) {
672 echo '<form action="index.php" class="searchform"><div><input type="hidden" name="id" value="'.$course->id.'" />';
673 echo '<label for="search">' . get_string('search', 'search') . ' </label>';
674 echo '<input type="text" id="search" name="search" value="'.s($search).'" />&nbsp;<input type="submit" value="'.get_string('search').'" /></div></form>'."\n";
675}
77c645df 676
a2ed6e69 677echo '</div>'; // Userlist.
f9903ed0 678
a2ed6e69 679echo $OUTPUT->footer();
77c645df 680
a2ed6e69
SH
681if ($userlist) {
682 $userlist->close();
683}
77c645df 684
a2ed6e69
SH
685/**
686 * Returns SQL that can be used to limit a query to a period where the user last accessed a course..
687 *
688 * @param string $accesssince
689 * @return string
690 */
0749caef 691function get_course_lastaccess_sql($accesssince='') {
31b71336 692 if (empty($accesssince)) {
693 return '';
694 }
a2ed6e69 695 if ($accesssince == -1) { // Never.
4f0c2d00 696 return 'ul.timeaccess = 0';
31b71336 697 } else {
4f0c2d00 698 return 'ul.timeaccess != 0 AND ul.timeaccess < '.$accesssince;
0749caef 699 }
700}
701
a2ed6e69
SH
702/**
703 * Returns SQL that can be used to limit a query to a period where the user last accessed the system.
704 *
705 * @param string $accesssince
706 * @return string
707 */
0749caef 708function get_user_lastaccess_sql($accesssince='') {
709 if (empty($accesssince)) {
710 return '';
711 }
a2ed6e69 712 if ($accesssince == -1) { // Never.
4f0c2d00 713 return 'u.lastaccess = 0';
0749caef 714 } else {
4f0c2d00 715 return 'u.lastaccess != 0 AND u.lastaccess < '.$accesssince;
31b71336 716 }
717}