MDL-60828 user: Reset current page when filtering/searching users
[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');
5fa2d502 27require_once($CFG->dirroot.'/course/lib.php');
b5b81de3 28require_once($CFG->dirroot.'/notes/lib.php');
a2ed6e69
SH
29require_once($CFG->libdir.'/tablelib.php');
30require_once($CFG->libdir.'/filelib.php');
a78ed71c 31require_once($CFG->dirroot.'/enrol/locallib.php');
a2ed6e69 32
a2ed6e69
SH
33define('DEFAULT_PAGE_SIZE', 20);
34define('SHOW_ALL_PAGE_SIZE', 5000);
a2ed6e69
SH
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.
a2ed6e69
SH
38$contextid = optional_param('contextid', 0, PARAM_INT); // One of this or.
39$courseid = optional_param('id', 0, PARAM_INT); // This are required.
5fa2d502 40$newcourse = optional_param('newcourse', false, PARAM_BOOL);
5b7c500a 41$selectall = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
fe214295 42$roleid = optional_param('roleid', 0, PARAM_INT);
9f33d5ac 43$groupparam = optional_param('group', 0, PARAM_INT);
a2ed6e69
SH
44
45$PAGE->set_url('/user/index.php', array(
46 'page' => $page,
47 'perpage' => $perpage,
a2ed6e69 48 'contextid' => $contextid,
5fa2d502
DW
49 'id' => $courseid,
50 'newcourse' => $newcourse));
a2ed6e69
SH
51
52if ($contextid) {
53 $context = context::instance_by_id($contextid, MUST_EXIST);
54 if ($context->contextlevel != CONTEXT_COURSE) {
55 print_error('invalidcontext');
56 }
57 $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
58} else {
59 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
60 $context = context_course::instance($course->id, MUST_EXIST);
61}
62// Not needed anymore.
63unset($contextid);
64unset($courseid);
f9903ed0 65
a2ed6e69 66require_login($course);
f9903ed0 67
a2ed6e69
SH
68$systemcontext = context_system::instance();
69$isfrontpage = ($course->id == SITEID);
f9903ed0 70
a2ed6e69 71$frontpagectx = context_course::instance(SITEID);
4f0c2d00 72
a2ed6e69
SH
73if ($isfrontpage) {
74 $PAGE->set_pagelayout('admin');
93b47710 75 course_require_view_participants($systemcontext);
a2ed6e69
SH
76} else {
77 $PAGE->set_pagelayout('incourse');
93b47710 78 course_require_view_participants($context);
a2ed6e69 79}
224aa44a 80
0ff203b6
JL
81// Trigger events.
82user_list_view($course, $context);
a2ed6e69
SH
83
84$bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
85
a2ed6e69
SH
86$PAGE->set_title("$course->shortname: ".get_string('participants'));
87$PAGE->set_heading($course->fullname);
88$PAGE->set_pagetype('course-view-' . $course->format);
89$PAGE->add_body_class('path-user'); // So we can style it independently.
90$PAGE->set_other_editing_capability('moodle/course:manageactivities');
2cb2ce61 91
b34fd2cc
DW
92// Expand the users node in the settings navigation when it exists because those pages
93// are related to this one.
94$node = $PAGE->settingsnav->find('users', navigation_node::TYPE_CONTAINER);
95if ($node) {
b0b6ff3c 96 $node->force_open();
b34fd2cc
DW
97}
98
a2ed6e69 99echo $OUTPUT->header();
4e1f6047 100echo $OUTPUT->heading(get_string('participants'));
caa8363f 101
9651e491 102// Get the currently applied filters.
7cf4331a 103$filtersapplied = optional_param_array('unified-filters', [], PARAM_NOTAGS);
32e5109d 104$filterwassubmitted = optional_param('unified-filter-submitted', 0, PARAM_BOOL);
a78ed71c 105
fe214295
MN
106// If they passed a role make sure they can view that role.
107if ($roleid) {
108 $viewableroles = get_profile_roles($context);
109
110 // Check if the user can view this role.
111 if (array_key_exists($roleid, $viewableroles)) {
112 $filtersapplied[] = USER_FILTER_ROLE . ':' . $roleid;
113 } else {
114 $roleid = 0;
115 }
116}
117
9651e491
JP
118// Default group ID.
119$groupid = false;
120$canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
121if ($course->groupmode != NOGROUPS) {
122 if ($canaccessallgroups) {
9f33d5ac
MN
123 // Change the group if the user can access all groups and has specified group in the URL.
124 if ($groupparam) {
125 $groupid = $groupparam;
126 }
9651e491
JP
127 } else {
128 // Otherwise, get the user's default group.
129 $groupid = groups_get_course_group($course, true);
130 if ($course->groupmode == SEPARATEGROUPS && !$groupid) {
131 // The user is not in the group so show message and exit.
132 echo $OUTPUT->notification(get_string('notingroup'));
133 echo $OUTPUT->footer();
134 exit;
135 }
136 }
a2ed6e69 137}
9651e491
JP
138$hasgroupfilter = false;
139$lastaccess = 0;
140$searchkeywords = [];
9651e491
JP
141$enrolid = 0;
142$status = -1;
143foreach ($filtersapplied as $filter) {
144 $filtervalue = explode(':', $filter, 2);
145 $value = null;
146 if (count($filtervalue) == 2) {
147 $key = clean_param($filtervalue[0], PARAM_INT);
148 $value = clean_param($filtervalue[1], PARAM_INT);
149 } else {
150 // Search string.
a997dc9d
AN
151 $key = USER_FILTER_STRING;
152 $value = clean_param($filtervalue[0], PARAM_TEXT);
9651e491 153 }
99cca847 154
9651e491
JP
155 switch ($key) {
156 case USER_FILTER_ENROLMENT:
157 $enrolid = $value;
158 break;
159 case USER_FILTER_GROUP:
160 $groupid = $value;
161 $hasgroupfilter = true;
162 break;
163 case USER_FILTER_LAST_ACCESS:
164 $lastaccess = $value;
165 break;
166 case USER_FILTER_ROLE:
167 $roleid = $value;
168 break;
169 case USER_FILTER_STATUS:
170 // We only accept active/suspended statuses.
171 if ($value == ENROL_USER_ACTIVE || $value == ENROL_USER_SUSPENDED) {
172 $status = $value;
173 }
174 break;
175 default:
176 // Search string.
a997dc9d 177 $searchkeywords[] = $value;
9651e491
JP
178 break;
179 }
a2ed6e69 180}
03d9401e 181
32e5109d
MN
182// If course supports groups we may need to set a default.
183if ($groupid !== false) {
9f33d5ac
MN
184 if ($canaccessallgroups) {
185 // User can access all groups, let them filter by whatever was selected.
186 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
187 } else if (!$filterwassubmitted && $course->groupmode == VISIBLEGROUPS) {
188 // If we are in a course with visible groups and the user has not submitted anything and does not have
189 // access to all groups, then set a default group.
32e5109d 190 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
9f33d5ac 191 } else if (!$hasgroupfilter && $course->groupmode != VISIBLEGROUPS) {
32e5109d
MN
192 // The user can't access all groups and has not set a group filter in a course where the groups are not visible
193 // then apply a default group filter.
194 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
195 } else if (!$hasgroupfilter) { // No need for the group id to be set.
196 $groupid = false;
197 }
24c3db91 198}
a2ed6e69 199
349b705d
AG
200if ($groupid && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
201 $grouprenderer = $PAGE->get_renderer('core_group');
202 $groupdetailpage = new \core_group\output\group_details($groupid);
203 echo $grouprenderer->group_details($groupdetailpage);
204}
205
9651e491
JP
206// Manage enrolments.
207$manager = new course_enrolment_manager($PAGE, $course);
208$enrolbuttons = $manager->get_manual_enrol_buttons();
209$enrolrenderer = $PAGE->get_renderer('core_enrol');
210$enrolbuttonsout = '';
211foreach ($enrolbuttons as $enrolbutton) {
212 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
bc47b706 213}
9651e491 214echo html_writer::div($enrolbuttonsout, 'pull-right');
bc47b706 215
9651e491
JP
216// Should use this variable so that we don't break stuff every time a variable is added or changed.
217$baseurl = new moodle_url('/user/index.php', array(
218 'contextid' => $context->id,
219 'id' => $course->id,
220 'perpage' => $perpage));
a2ed6e69 221
bb4a7923
SA
222// Render the unified filter.
223$renderer = $PAGE->get_renderer('core_user');
224echo $renderer->unified_filter($course, $context, $filtersapplied, $baseurl);
225
226echo '<div class="userlist">';
227
9651e491
JP
228$participanttable = new \core_user\participants_table($course->id, $groupid, $lastaccess, $roleid, $enrolid, $status,
229 $searchkeywords, $bulkoperations, $selectall);
bc47b706 230$participanttable->define_baseurl($baseurl);
77c645df 231
bc47b706
MN
232// Do this so we can get the total number of rows.
233ob_start();
234$participanttable->out($perpage, true);
235$participanttablehtml = ob_get_contents();
236ob_end_clean();
3e219038 237
a2ed6e69
SH
238if ($bulkoperations) {
239 echo '<form action="action_redir.php" method="post" id="participantsform">';
240 echo '<div>';
241 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
242 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
243}
77c645df 244
bc47b706 245echo $participanttablehtml;
4c7593ff 246
c8351261
MG
247$PAGE->requires->js_call_amd('core_user/name_page_filter', 'init');
248
8dd42b38
AH
249$perpageurl = clone($baseurl);
250$perpageurl->remove_params('perpage');
bc47b706 251if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
8dd42b38
AH
252 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
253 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
254
bc47b706 255} else if ($participanttable->get_page_size() < $participanttable->totalrows) {
8dd42b38 256 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
bc47b706
MN
257 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $participanttable->totalrows)),
258 array(), 'showall');
8dd42b38
AH
259}
260
a2ed6e69
SH
261if ($bulkoperations) {
262 echo '<br /><div class="buttons">';
5b7c500a 263
bc47b706 264 if ($participanttable->get_page_size() < $participanttable->totalrows) {
5b7c500a
AH
265 $perpageurl = clone($baseurl);
266 $perpageurl->remove_params('perpage');
267 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
268 $perpageurl->param('selectall', true);
269 $showalllink = $perpageurl;
270 } else {
271 $showalllink = false;
272 }
273
b27c8d81 274 echo html_writer::start_tag('div', array('class' => 'btn-group'));
bc47b706 275 if ($participanttable->get_page_size() < $participanttable->totalrows) {
5b7c500a 276 // Select all users, refresh page showing all users and mark them all selected.
bc47b706 277 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
b27c8d81
SB
278 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
279 'value' => $label, 'data-showallink' => $showalllink));
5b7c500a 280 // Select all users, mark all users on page as selected.
b27c8d81
SB
281 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
282 'value' => get_string('selectallusersonpage')));
5b7c500a 283 } else {
b27c8d81
SB
284 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
285 'value' => get_string('selectall')));
5b7c500a
AH
286 }
287
b27c8d81
SB
288 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checknone', 'class' => 'btn btn-secondary',
289 'value' => get_string('deselectall')));
290 echo html_writer::end_tag('div');
a2ed6e69 291 $displaylist = array();
b5b81de3 292 $displaylist['#messageselect'] = get_string('messageselectadd');
a2ed6e69 293 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
b5b81de3 294 $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
a2ed6e69
SH
295 }
296
ca560952 297 if ($context->id != $frontpagectx->id) {
f4ddc4ec
DW
298 $instances = $manager->get_enrolment_instances();
299 $plugins = $manager->get_enrolment_plugins(false);
300 foreach ($instances as $key => $instance) {
301 if (!isset($plugins[$instance->enrol])) {
302 // Weird, some broken stuff in plugin.
303 continue;
304 }
305 $plugin = $plugins[$instance->enrol];
ca560952
DW
306 $bulkoperations = $plugin->get_bulk_operations($manager);
307
308 $pluginoptions = [];
309 foreach ($bulkoperations as $key => $bulkoperation) {
310 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
311 $url = new moodle_url('bulkchange.php', $params);
312 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
313 }
314 if (!empty($pluginoptions)) {
315 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
316 $displaylist[] = [$name => $pluginoptions];
317 }
689ccae5
DW
318 }
319 }
320
a2ed6e69
SH
321 echo $OUTPUT->help_icon('withselectedusers');
322 echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
323 echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
324
325 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
326 echo '<noscript style="display:inline">';
327 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
328 echo '</noscript>';
329 echo '</div></div>';
330 echo '</form>';
331
b5b81de3
DW
332 $options = new stdClass();
333 $options->courseid = $course->id;
334 $options->noteStateNames = note_get_state_names();
335 $options->stateHelpIcon = $OUTPUT->help_icon('publishstate', 'notes');
336 $PAGE->requires->js_call_amd('core_user/participants', 'init', [$options]);
a2ed6e69 337}
b90e2f19 338
a2ed6e69 339echo '</div>'; // Userlist.
f9903ed0 340
a78ed71c
DW
341$enrolrenderer = $PAGE->get_renderer('core_enrol');
342echo '<div class="pull-right">';
343foreach ($enrolbuttons as $enrolbutton) {
344 echo $enrolrenderer->render($enrolbutton);
345}
346echo '</div>';
347
5fa2d502
DW
348if ($newcourse == 1) {
349 $str = get_string('proceedtocourse', 'enrol');
350 // Floated left so it goes under the enrol users button on mobile.
351 // The margin is to make it line up with the enrol users button when they are both on the same line.
352 $classes = 'm-y-1 pull-xs-left';
353 $url = course_get_url($course);
354 echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
355}
356
a2ed6e69 357echo $OUTPUT->footer();