MDL-59364 core_user: removed 'User details' option in participants page
[moodle.git] / user / index.php
1 <?php
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/>.
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  */
25 require_once('../config.php');
26 require_once($CFG->dirroot.'/user/lib.php');
27 require_once($CFG->libdir.'/tablelib.php');
28 require_once($CFG->libdir.'/filelib.php');
30 define('USER_SMALL_CLASS', 20);   // Below this is considered small.
31 define('USER_LARGE_CLASS', 200);  // Above this is considered large.
32 define('DEFAULT_PAGE_SIZE', 20);
33 define('SHOW_ALL_PAGE_SIZE', 5000);
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.
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.
42 $selectall    = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
44 $PAGE->set_url('/user/index.php', array(
45         'page' => $page,
46         'perpage' => $perpage,
47         'accesssince' => $accesssince,
48         'search' => $search,
49         'roleid' => $roleid,
50         'contextid' => $contextid,
51         'id' => $courseid));
53 if ($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.
64 unset($contextid);
65 unset($courseid);
67 require_login($course);
69 $systemcontext = context_system::instance();
70 $isfrontpage = ($course->id == SITEID);
72 $frontpagectx = context_course::instance(SITEID);
74 if ($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 }
82 $rolenamesurl = new moodle_url("$CFG->wwwroot/user/index.php?contextid=$context->id&sifirst=&silast=");
84 $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true);
85 if ($isfrontpage) {
86     $rolenames[0] = get_string('allsiteusers', 'role');
87 } else {
88     $rolenames[0] = get_string('allparticipants');
89 }
91 // Make sure other roles may not be selected by any means.
92 if (empty($rolenames[$roleid])) {
93     print_error('noparticipants');
94 }
96 // No roles to display yet?
97 // frontpage course is an exception, on the front page course we should display all users.
98 if (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 {
102         print_error('noparticipants');
103     }
106 // Trigger events.
107 user_list_view($course, $context);
109 $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
111 $countries = get_string_manager()->get_list_of_countries();
113 $strnever = get_string('never');
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');
127 // Check to see if groups are being used in this course
128 // and if so, set $currentgroup to reflect the current group.
130 $groupmode    = groups_get_course_groupmode($course);   // Groups are being used.
131 $currentgroup = groups_get_course_group($course, true);
133 if (!$currentgroup) {      // To make some other functions work better later.
134     $currentgroup  = null;
137 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
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');
145 echo $OUTPUT->header();
146 echo $OUTPUT->heading(get_string('participants'));
148 echo '<div class="userlist">';
150 if ($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;
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)));
167 // Setting up tags.
168 if ($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;
180 // Get the hidden field list.
181 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
182     $hiddenfields = array();  // Teachers and admins are allowed to see everything.
183 } else {
184     $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
187 if (isset($hiddenfields['lastaccess'])) {
188     // Do not allow access since filtering.
189     $accesssince = 0;
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();
198 // Print my course menus.
199 if ($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);
215 if ($groupmenu = groups_print_course_menu($course, $baseurl->out(), true)) {
216     $controlstable->data[0]->cells[] = $groupmenu;
219 if (!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));
233     }
235     $now = usergetmidnight(time());
236     $timeaccess = array();
237     $baseurl->remove_params('accesssince');
239     // Makes sense for this to go first.
240     $timeoptions[0] = get_string('selectperiod');
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);
246         }
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);
252         }
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);
258         }
259     }
260     // Try a year.
261     if (strtotime('-1 year', $now) >= $minlastaccess) {
262         $timeoptions[strtotime('-1 year', $now)] = get_string('lastyear');
263     }
265     if (!empty($lastaccess0exists)) {
266         $timeoptions[-1] = get_string('never');
267     }
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);
273     }
276 echo html_writer::table($controlstable);
278 if ($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);
288             $contentcell = new html_table_cell();
289             $contentcell->attributes['class'] = 'content';
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             }
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;
301             }
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);
307         }
308     }
311 // Define a table showing a list of users in the current role selection.
312 $tablecolumns = array();
313 $tableheaders = array();
314 if ($bulkoperations) {
315     $tablecolumns[] = 'select';
316     $tableheaders[] = get_string('select');
318 $tablecolumns[] = 'userpic';
319 $tablecolumns[] = 'fullname';
321 $extrafields = get_extra_user_fields($context);
322 $tableheaders[] = get_string('userpic');
323 $tableheaders[] = get_string('fullnameuser');
326 foreach ($extrafields as $field) {
327     $tablecolumns[] = $field;
328     $tableheaders[] = get_user_field_name($field);
330 if (!isset($hiddenfields['city'])) {
331     $tablecolumns[] = 'city';
332     $tableheaders[] = get_string('city');
334 if (!isset($hiddenfields['country'])) {
335     $tablecolumns[] = 'country';
336     $tableheaders[] = get_string('country');
338 if (!isset($hiddenfields['lastaccess'])) {
339     $tablecolumns[] = 'lastaccess';
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     }
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());
353 if (!isset($hiddenfields['lastaccess'])) {
354     $table->sortable(true, 'lastaccess', SORT_DESC);
355 } else {
356     $table->sortable(true, 'firstname', SORT_ASC);
359 $table->no_sorting('roles');
360 $table->no_sorting('groups');
361 $table->no_sorting('groupings');
362 $table->no_sorting('select');
364 $table->set_attribute('cellspacing', '0');
365 $table->set_attribute('id', 'participants');
366 $table->set_attribute('class', 'generaltable generalbox');
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();
378 list($esql, $params) = get_enrolled_sql($context, null, $currentgroup, true);
379 $joins = array("FROM {user} u");
380 $wheres = array();
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);
386 if ($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     }
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);
400     }
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;
411 // Limit list to users with some role only.
412 if ($roleid) {
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');
416     $wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $relatedctxsql)";
417     $params = array_merge($params, array('roleid' => $roleid), $relatedctxparams);
420 $from = implode("\n", $joins);
421 if ($wheres) {
422     $where = "WHERE " . implode(" AND ", $wheres);
423 } else {
424     $where = "";
427 $totalcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
429 if (!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%";
439 list($twhere, $tparams) = $table->get_sql_where();
440 if ($twhere) {
441     $wheres[] = $twhere;
442     $params = array_merge($params, $tparams);
445 $from = implode("\n", $joins);
446 if ($wheres) {
447     $where = "WHERE " . implode(" AND ", $wheres);
448 } else {
449     $where = "";
452 if ($table->get_sql_sort()) {
453     $sort = ' ORDER BY '.$table->get_sql_sort();
454 } else {
455     $sort = '';
458 $matchcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
460 $table->initialbars(true);
461 $table->pagesize($perpage, $matchcount);
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());
466 // If there are multiple Roles in the course, then show a drop down menu for switching.
467 if (count($rolenames) > 1) {
468     echo '<div class="rolesform">';
469     echo $OUTPUT->single_select($rolenamesurl, 'roleid', $rolenames, $roleid, null,
470         'rolesform', array('label' => get_string('currentrole', 'role')));
471     echo '</div>';
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>';
482 $editlink = '';
483 if ($course->id != SITEID && has_capability('moodle/course:enrolreview', $context)) {
484     $editlink = new moodle_url('/enrol/users.php', array('id' => $course->id));
487 if ($roleid > 0) {
488     $a = new stdClass();
489     $a->number = $totalcount;
490     $a->role = $rolenames[$roleid];
491     $heading = format_string(get_string('xuserswiththerole', 'role', $a));
493     if ($currentgroup and !empty($group)) {
494         $a->group = $group->name;
495         $heading .= ' ' . format_string(get_string('ingroup', 'role', $a));
496     }
498     if ($accesssince && !empty($timeoptions[$accesssince])) {
499         $a->timeperiod = $timeoptions[$accesssince];
500         $heading .= ' ' . format_string(get_string('inactiveformorethan', 'role', $a));
501     }
503     $heading .= ": $a->number";
505     if (!empty($editlink)) {
506         $editlink->param('role', $roleid);
507         $heading .= $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit')));
508     }
509     echo $OUTPUT->heading($heading, 3);
510 } else {
511     if ($course->id == SITEID and $roleid < 0) {
512         $strallparticipants = get_string('allsiteusers', 'role');
513     } else {
514         $strallparticipants = get_string('allparticipants');
515     }
517     if (!empty($editlink)) {
518         $editlink = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit')));
519     }
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     }
529 if ($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)).'" />';
536 $countrysort = (strpos($sort, 'country') !== false);
537 $timeformat = get_string('strftimedate');
539 if ($userlist) {
541     $usersprinted = array();
542     foreach ($userlist as $user) {
543         if (in_array($user->id, $usersprinted)) { // Prevent duplicates by r.hidden - MDL-13935.
544             continue;
545         }
546         $usersprinted[] = $user->id; // Add new user to the array of users printed.
548         context_helper::preload_from_record($user);
550         if ($user->lastaccess) {
551             $lastaccess = format_time(time() - $user->lastaccess, $datestring);
552         } else {
553             $lastaccess = $strnever;
554         }
556         if (empty($user->country)) {
557             $country = '';
559         } else {
560             if ($countrysort) {
561                 $country = '('.$user->country.') '.$countries[$user->country];
562             } else {
563                 $country = $countries[$user->country];
564             }
565         }
567         $usercontext = context_user::instance($user->id);
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         }
575         $data = array();
576         if ($bulkoperations) {
577             if ($selectall) {
578                 $checked = 'checked="true"';
579             } else {
580                 $checked = '';
581             }
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;
587         foreach ($extrafields as $field) {
588             $data[] = $user->{$field};
589         }
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         }
601         $table->add_data($data);
602     }
605 $table->print_html();
607 $perpageurl = clone($baseurl);
608 $perpageurl->remove_params('perpage');
609 if ($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');
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');
618 if ($bulkoperations) {
619     echo '<br /><div class="buttons">';
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     }
631     echo html_writer::start_tag('div', array('class' => 'btn-group'));
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);
635         echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
636                 'value' => $label, 'data-showallink' => $showalllink));
637         // Select all users, mark all users on page as selected.
638         echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
639         'value' => get_string('selectallusersonpage')));
640     } else {
641         echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
642         'value' => get_string('selectall')));
643     }
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');
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     }
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'));
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>';
666     $module = array('name' => 'core_user', 'fullpath' => '/user/module.js');
667     $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
670 // Show a search box if all participants don't fit on a single screen.
671 if ($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";
677 echo '</div>';  // Userlist.
679 echo $OUTPUT->footer();
681 if ($userlist) {
682     $userlist->close();
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  */
691 function get_course_lastaccess_sql($accesssince='') {
692     if (empty($accesssince)) {
693         return '';
694     }
695     if ($accesssince == -1) { // Never.
696         return 'ul.timeaccess = 0';
697     } else {
698         return 'ul.timeaccess != 0 AND ul.timeaccess < '.$accesssince;
699     }
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  */
708 function get_user_lastaccess_sql($accesssince='') {
709     if (empty($accesssince)) {
710         return '';
711     }
712     if ($accesssince == -1) { // Never.
713         return 'u.lastaccess = 0';
714     } else {
715         return 'u.lastaccess != 0 AND u.lastaccess < '.$accesssince;
716     }