Merge branch 'MDL-58716-master' of git://github.com/jleyva/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
24c3db91
DP
198if ($groupmenu = groups_print_course_menu($course, $baseurl->out(), true)) {
199 $controlstable->data[0]->cells[] = $groupmenu;
200}
a2ed6e69
SH
201
202if (!isset($hiddenfields['lastaccess'])) {
203 // Get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
204 // We need to make it diferently for normal courses and site course.
205 if (!$isfrontpage) {
206 $minlastaccess = $DB->get_field_sql('SELECT min(timeaccess)
207 FROM {user_lastaccess}
208 WHERE courseid = ?
209 AND timeaccess != 0', array($course->id));
210 $lastaccess0exists = $DB->record_exists('user_lastaccess', array('courseid' => $course->id, 'timeaccess' => 0));
211 } else {
212 $minlastaccess = $DB->get_field_sql('SELECT min(lastaccess)
213 FROM {user}
214 WHERE lastaccess != 0');
215 $lastaccess0exists = $DB->record_exists('user', array('lastaccess' => 0));
7da0af9f 216 }
87a13824 217
a2ed6e69
SH
218 $now = usergetmidnight(time());
219 $timeaccess = array();
220 $baseurl->remove_params('accesssince');
87a13824 221
a2ed6e69
SH
222 // Makes sense for this to go first.
223 $timeoptions[0] = get_string('selectperiod');
87a13824 224
a2ed6e69
SH
225 // Days.
226 for ($i = 1; $i < 7; $i++) {
227 if (strtotime('-'.$i.' days', $now) >= $minlastaccess) {
228 $timeoptions[strtotime('-'.$i.' days', $now)] = get_string('numdays', 'moodle', $i);
ca792680 229 }
a2ed6e69
SH
230 }
231 // Weeks.
232 for ($i = 1; $i < 10; $i++) {
233 if (strtotime('-'.$i.' weeks', $now) >= $minlastaccess) {
234 $timeoptions[strtotime('-'.$i.' weeks', $now)] = get_string('numweeks', 'moodle', $i);
5ff311f0 235 }
a2ed6e69
SH
236 }
237 // Months.
238 for ($i = 2; $i < 12; $i++) {
239 if (strtotime('-'.$i.' months', $now) >= $minlastaccess) {
240 $timeoptions[strtotime('-'.$i.' months', $now)] = get_string('nummonths', 'moodle', $i);
31b71336 241 }
a2ed6e69
SH
242 }
243 // Try a year.
244 if (strtotime('-1 year', $now) >= $minlastaccess) {
245 $timeoptions[strtotime('-1 year', $now)] = get_string('lastyear');
246 }
87a13824 247
a2ed6e69
SH
248 if (!empty($lastaccess0exists)) {
249 $timeoptions[-1] = get_string('never');
250 }
87a13824 251
a2ed6e69
SH
252 if (count($timeoptions) > 1) {
253 $select = new single_select($baseurl, 'accesssince', $timeoptions, $accesssince, null, 'timeoptions');
254 $select->set_label(get_string('usersnoaccesssince'));
255 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
77c645df 256 }
a2ed6e69 257}
87a13824 258
a2ed6e69
SH
259echo html_writer::table($controlstable);
260
a2ed6e69
SH
261// Define a table showing a list of users in the current role selection.
262$tablecolumns = array();
263$tableheaders = array();
4c7593ff 264if ($bulkoperations) {
a2ed6e69
SH
265 $tablecolumns[] = 'select';
266 $tableheaders[] = get_string('select');
267}
268$tablecolumns[] = 'userpic';
269$tablecolumns[] = 'fullname';
c238e1e2 270
a2ed6e69
SH
271$extrafields = get_extra_user_fields($context);
272$tableheaders[] = get_string('userpic');
273$tableheaders[] = get_string('fullnameuser');
77c645df 274
4c7593ff
MN
275
276foreach ($extrafields as $field) {
277 $tablecolumns[] = $field;
278 $tableheaders[] = get_user_field_name($field);
a2ed6e69 279}
4c7593ff 280if (!isset($hiddenfields['city'])) {
a2ed6e69
SH
281 $tablecolumns[] = 'city';
282 $tableheaders[] = get_string('city');
283}
4c7593ff 284if (!isset($hiddenfields['country'])) {
a2ed6e69
SH
285 $tablecolumns[] = 'country';
286 $tableheaders[] = get_string('country');
287}
288if (!isset($hiddenfields['lastaccess'])) {
289 $tablecolumns[] = 'lastaccess';
e89682d8
DC
290 if ($course->id == SITEID) {
291 // Exception case for viewing participants on site home.
292 $tableheaders[] = get_string('lastsiteaccess');
293 } else {
294 $tableheaders[] = get_string('lastcourseaccess');
295 }
a2ed6e69 296}
77c645df 297
a2ed6e69
SH
298$table = new flexible_table('user-index-participants-'.$course->id);
299$table->define_columns($tablecolumns);
300$table->define_headers($tableheaders);
301$table->define_baseurl($baseurl->out());
5ff311f0 302
28d27e85 303if (!isset($hiddenfields['lastaccess'])) {
304 $table->sortable(true, 'lastaccess', SORT_DESC);
a2ed6e69
SH
305} else {
306 $table->sortable(true, 'firstname', SORT_ASC);
307}
4f0c2d00 308
a2ed6e69
SH
309$table->no_sorting('roles');
310$table->no_sorting('groups');
311$table->no_sorting('groupings');
312$table->no_sorting('select');
313
314$table->set_attribute('cellspacing', '0');
315$table->set_attribute('id', 'participants');
316$table->set_attribute('class', 'generaltable generalbox');
317
318$table->set_control_variables(array(
319 TABLE_VAR_SORT => 'ssort',
320 TABLE_VAR_HIDE => 'shide',
321 TABLE_VAR_SHOW => 'sshow',
322 TABLE_VAR_IFIRST => 'sifirst',
323 TABLE_VAR_ILAST => 'silast',
324 TABLE_VAR_PAGE => 'spage'
325 ));
326$table->setup();
327
328list($esql, $params) = get_enrolled_sql($context, null, $currentgroup, true);
329$joins = array("FROM {user} u");
330$wheres = array();
331
368f0bf8
DC
332$userfields = array('username', 'email', 'city', 'country', 'lang', 'timezone', 'maildisplay');
333$mainuserfields = user_picture::fields('u', $userfields);
334$extrasql = get_extra_user_fields_sql($context, 'u', '', $userfields);
a2ed6e69
SH
335
336if ($isfrontpage) {
337 $select = "SELECT $mainuserfields, u.lastaccess$extrasql";
338 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // Everybody on the frontpage usually.
339 if ($accesssince) {
340 $wheres[] = get_user_lastaccess_sql($accesssince);
341 }
342
343} else {
344 $select = "SELECT $mainuserfields, COALESCE(ul.timeaccess, 0) AS lastaccess$extrasql";
345 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // Course enrolled users only.
346 $joins[] = "LEFT JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = :courseid)"; // Not everybody accessed course yet.
347 $params['courseid'] = $course->id;
348 if ($accesssince) {
349 $wheres[] = get_course_lastaccess_sql($accesssince);
165088f6 350 }
a2ed6e69 351}
5f9e296a 352
a2ed6e69
SH
353// Performance hacks - we preload user contexts together with accounts.
354$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
355$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
356$params['contextlevel'] = CONTEXT_USER;
357$select .= $ccselect;
358$joins[] = $ccjoin;
c0527567 359
4f0c2d00 360
a2ed6e69
SH
361// Limit list to users with some role only.
362if ($roleid) {
419d1abd
DW
363 // We want to query both the current context and parent contexts.
364 list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
365
a2ed6e69 366 $wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $relatedctxsql)";
419d1abd 367 $params = array_merge($params, array('roleid' => $roleid), $relatedctxparams);
a2ed6e69 368}
0be6f678 369
a2ed6e69
SH
370$from = implode("\n", $joins);
371if ($wheres) {
372 $where = "WHERE " . implode(" AND ", $wheres);
373} else {
374 $where = "";
375}
4f0c2d00 376
a2ed6e69 377$totalcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
77c645df 378
a2ed6e69
SH
379if (!empty($search)) {
380 $fullname = $DB->sql_fullname('u.firstname', 'u.lastname');
381 $wheres[] = "(". $DB->sql_like($fullname, ':search1', false, false) .
382 " OR ". $DB->sql_like('email', ':search2', false, false) .
383 " OR ". $DB->sql_like('idnumber', ':search3', false, false) .") ";
384 $params['search1'] = "%$search%";
385 $params['search2'] = "%$search%";
386 $params['search3'] = "%$search%";
387}
77c645df 388
a2ed6e69
SH
389list($twhere, $tparams) = $table->get_sql_where();
390if ($twhere) {
391 $wheres[] = $twhere;
392 $params = array_merge($params, $tparams);
393}
77c645df 394
a2ed6e69
SH
395$from = implode("\n", $joins);
396if ($wheres) {
397 $where = "WHERE " . implode(" AND ", $wheres);
398} else {
399 $where = "";
400}
77c645df 401
a2ed6e69
SH
402if ($table->get_sql_sort()) {
403 $sort = ' ORDER BY '.$table->get_sql_sort();
404} else {
405 $sort = '';
406}
77c645df 407
a2ed6e69 408$matchcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
77c645df 409
a2ed6e69
SH
410$table->initialbars(true);
411$table->pagesize($perpage, $matchcount);
77c645df 412
a2ed6e69
SH
413// List of users at the current visible page - paging makes it relatively short.
414$userlist = $DB->get_recordset_sql("$select $from $where $sort", $params, $table->get_page_start(), $table->get_page_size());
3e219038 415
a2ed6e69
SH
416// If there are multiple Roles in the course, then show a drop down menu for switching.
417if (count($rolenames) > 1) {
418 echo '<div class="rolesform">';
32bd11cb
BB
419 echo $OUTPUT->single_select($rolenamesurl, 'roleid', $rolenames, $roleid, null,
420 'rolesform', array('label' => get_string('currentrole', 'role')));
a2ed6e69 421 echo '</div>';
5f9e296a 422
a2ed6e69
SH
423} else if (count($rolenames) == 1) {
424 // When all users with the same role - print its name.
425 echo '<div class="rolesform">';
426 echo get_string('role').get_string('labelsep', 'langconfig');
427 $rolename = reset($rolenames);
428 echo $rolename;
429 echo '</div>';
430}
3e219038 431
7057c3ef
DP
432$editlink = '';
433if ($course->id != SITEID && has_capability('moodle/course:enrolreview', $context)) {
434 $editlink = new moodle_url('/enrol/users.php', array('id' => $course->id));
435}
436
a2ed6e69
SH
437if ($roleid > 0) {
438 $a = new stdClass();
439 $a->number = $totalcount;
440 $a->role = $rolenames[$roleid];
441 $heading = format_string(get_string('xuserswiththerole', 'role', $a));
f642a21a 442
e727e91c
MN
443 if ($currentgroup) {
444 if ($group = groups_get_group($currentgroup)) {
445 $a->group = $group->name;
446 $heading .= ' ' . format_string(get_string('ingroup', 'role', $a));
447 }
a2ed6e69 448 }
f642a21a 449
9488b6f3 450 if ($accesssince && !empty($timeoptions[$accesssince])) {
a2ed6e69
SH
451 $a->timeperiod = $timeoptions[$accesssince];
452 $heading .= ' ' . format_string(get_string('inactiveformorethan', 'role', $a));
453 }
f642a21a 454
a2ed6e69 455 $heading .= ": $a->number";
b85b25eb 456
7057c3ef
DP
457 if (!empty($editlink)) {
458 $editlink->param('role', $roleid);
459 $heading .= $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit')));
a2ed6e69
SH
460 }
461 echo $OUTPUT->heading($heading, 3);
462} else {
a2ed6e69
SH
463 if ($course->id == SITEID and $roleid < 0) {
464 $strallparticipants = get_string('allsiteusers', 'role');
465 } else {
466 $strallparticipants = get_string('allparticipants');
467 }
7057c3ef
DP
468
469 if (!empty($editlink)) {
470 $editlink = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit')));
471 }
472
a2ed6e69
SH
473 if ($matchcount < $totalcount) {
474 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount.'/'.$totalcount . $editlink, 3);
475 } else {
476 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount . $editlink, 3);
477 }
478}
3e219038 479
77c645df 480
a2ed6e69
SH
481if ($bulkoperations) {
482 echo '<form action="action_redir.php" method="post" id="participantsform">';
483 echo '<div>';
484 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
485 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
486}
77c645df 487
4c7593ff
MN
488$countrysort = (strpos($sort, 'country') !== false);
489$timeformat = get_string('strftimedate');
77c645df 490
4c7593ff 491if ($userlist) {
77c645df 492
4c7593ff
MN
493 $usersprinted = array();
494 foreach ($userlist as $user) {
495 if (in_array($user->id, $usersprinted)) { // Prevent duplicates by r.hidden - MDL-13935.
496 continue;
a2ed6e69 497 }
4c7593ff 498 $usersprinted[] = $user->id; // Add new user to the array of users printed.
a2ed6e69 499
4c7593ff 500 context_helper::preload_from_record($user);
a2ed6e69 501
4c7593ff
MN
502 if ($user->lastaccess) {
503 $lastaccess = format_time(time() - $user->lastaccess, $datestring);
a2ed6e69 504 } else {
4c7593ff 505 $lastaccess = $strnever;
ee4a52c5 506 }
3997cb40 507
4c7593ff
MN
508 if (empty($user->country)) {
509 $country = '';
3997cb40 510
4c7593ff
MN
511 } else {
512 if ($countrysort) {
513 $country = '('.$user->country.') '.$countries[$user->country];
a2ed6e69 514 } else {
4c7593ff 515 $country = $countries[$user->country];
a2ed6e69 516 }
4c7593ff 517 }
77c645df 518
4c7593ff 519 $usercontext = context_user::instance($user->id);
77c645df 520
4c7593ff
MN
521 if ($piclink = ($USER->id == $user->id || has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext))) {
522 $profilelink = '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></strong>';
523 } else {
524 $profilelink = '<strong>'.fullname($user).'</strong>';
525 }
0be6f678 526
4c7593ff
MN
527 $data = array();
528 if ($bulkoperations) {
529 if ($selectall) {
530 $checked = 'checked="true"';
a2ed6e69 531 } else {
4c7593ff 532 $checked = '';
a2ed6e69 533 }
4c7593ff
MN
534 $data[] = '<input type="checkbox" class="usercheckbox" name="user'.$user->id.'" ' . $checked .'/>';
535 }
536 $data[] = $OUTPUT->user_picture($user, array('size' => 35, 'courseid' => $course->id));
537 $data[] = $profilelink;
664fe87f 538
4c7593ff
MN
539 foreach ($extrafields as $field) {
540 $data[] = $user->{$field};
eca3af25 541 }
576ad290 542
4c7593ff
MN
543 if (!isset($hiddenfields['city'])) {
544 $data[] = $user->city;
545 }
546 if (!isset($hiddenfields['country'])) {
547 $data[] = $country;
548 }
549 if (!isset($hiddenfields['lastaccess'])) {
550 $data[] = $lastaccess;
551 }
d88c5043 552
4c7593ff
MN
553 $table->add_data($data);
554 }
a2ed6e69 555}
b90e2f19 556
4c7593ff
MN
557$table->print_html();
558
8dd42b38
AH
559$perpageurl = clone($baseurl);
560$perpageurl->remove_params('perpage');
561if ($perpage == SHOW_ALL_PAGE_SIZE) {
562 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
563 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
564
565} else if ($matchcount > 0 && $perpage < $matchcount) {
566 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
567 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $matchcount)), array(), 'showall');
568}
569
a2ed6e69
SH
570if ($bulkoperations) {
571 echo '<br /><div class="buttons">';
5b7c500a
AH
572
573 if ($matchcount > 0 && $perpage < $matchcount) {
574 $perpageurl = clone($baseurl);
575 $perpageurl->remove_params('perpage');
576 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
577 $perpageurl->param('selectall', true);
578 $showalllink = $perpageurl;
579 } else {
580 $showalllink = false;
581 }
582
b27c8d81 583 echo html_writer::start_tag('div', array('class' => 'btn-group'));
5b7c500a
AH
584 if ($perpage < $matchcount) {
585 // Select all users, refresh page showing all users and mark them all selected.
586 $label = get_string('selectalluserswithcount', 'moodle', $matchcount);
b27c8d81
SB
587 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
588 'value' => $label, 'data-showallink' => $showalllink));
5b7c500a 589 // Select all users, mark all users on page as selected.
b27c8d81
SB
590 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
591 'value' => get_string('selectallusersonpage')));
5b7c500a 592 } else {
b27c8d81
SB
593 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
594 'value' => get_string('selectall')));
5b7c500a
AH
595 }
596
b27c8d81
SB
597 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checknone', 'class' => 'btn btn-secondary',
598 'value' => get_string('deselectall')));
599 echo html_writer::end_tag('div');
a2ed6e69
SH
600 $displaylist = array();
601 $displaylist['messageselect.php'] = get_string('messageselectadd');
602 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
603 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
604 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
605 }
606
607 echo $OUTPUT->help_icon('withselectedusers');
608 echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
609 echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
610
611 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
612 echo '<noscript style="display:inline">';
613 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
614 echo '</noscript>';
615 echo '</div></div>';
616 echo '</form>';
617
618 $module = array('name' => 'core_user', 'fullpath' => '/user/module.js');
619 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
620}
b90e2f19 621
a2ed6e69
SH
622// Show a search box if all participants don't fit on a single screen.
623if ($totalcount > $perpage) {
624 echo '<form action="index.php" class="searchform"><div><input type="hidden" name="id" value="'.$course->id.'" />';
625 echo '<label for="search">' . get_string('search', 'search') . ' </label>';
626 echo '<input type="text" id="search" name="search" value="'.s($search).'" />&nbsp;<input type="submit" value="'.get_string('search').'" /></div></form>'."\n";
627}
77c645df 628
a2ed6e69 629echo '</div>'; // Userlist.
f9903ed0 630
a2ed6e69 631echo $OUTPUT->footer();
77c645df 632
a2ed6e69
SH
633if ($userlist) {
634 $userlist->close();
635}
77c645df 636
a2ed6e69
SH
637/**
638 * Returns SQL that can be used to limit a query to a period where the user last accessed a course..
639 *
640 * @param string $accesssince
641 * @return string
642 */
0749caef 643function get_course_lastaccess_sql($accesssince='') {
31b71336 644 if (empty($accesssince)) {
645 return '';
646 }
a2ed6e69 647 if ($accesssince == -1) { // Never.
4f0c2d00 648 return 'ul.timeaccess = 0';
31b71336 649 } else {
4f0c2d00 650 return 'ul.timeaccess != 0 AND ul.timeaccess < '.$accesssince;
0749caef 651 }
652}
653
a2ed6e69
SH
654/**
655 * Returns SQL that can be used to limit a query to a period where the user last accessed the system.
656 *
657 * @param string $accesssince
658 * @return string
659 */
0749caef 660function get_user_lastaccess_sql($accesssince='') {
661 if (empty($accesssince)) {
662 return '';
663 }
a2ed6e69 664 if ($accesssince == -1) { // Never.
4f0c2d00 665 return 'u.lastaccess = 0';
0749caef 666 } else {
4f0c2d00 667 return 'u.lastaccess != 0 AND u.lastaccess < '.$accesssince;
31b71336 668 }
669}