MDL-59832 core: allow users with 'enrolreview' to see participants page
[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');
a78ed71c 29require_once($CFG->dirroot.'/enrol/locallib.php');
a2ed6e69 30
a2ed6e69
SH
31define('DEFAULT_PAGE_SIZE', 20);
32define('SHOW_ALL_PAGE_SIZE', 5000);
9651e491
JP
33define('USER_FILTER_ENROLMENT', 1);
34define('USER_FILTER_GROUP', 2);
35define('USER_FILTER_LAST_ACCESS', 3);
36define('USER_FILTER_ROLE', 4);
37define('USER_FILTER_STATUS', 5);
a2ed6e69
SH
38
39$page = optional_param('page', 0, PARAM_INT); // Which page to show.
40$perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
a2ed6e69
SH
41$contextid = optional_param('contextid', 0, PARAM_INT); // One of this or.
42$courseid = optional_param('id', 0, PARAM_INT); // This are required.
5b7c500a 43$selectall = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
a2ed6e69
SH
44
45$PAGE->set_url('/user/index.php', array(
46 'page' => $page,
47 'perpage' => $perpage,
a2ed6e69
SH
48 'contextid' => $contextid,
49 'id' => $courseid));
50
51if ($contextid) {
52 $context = context::instance_by_id($contextid, MUST_EXIST);
53 if ($context->contextlevel != CONTEXT_COURSE) {
54 print_error('invalidcontext');
55 }
56 $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
57} else {
58 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
59 $context = context_course::instance($course->id, MUST_EXIST);
60}
61// Not needed anymore.
62unset($contextid);
63unset($courseid);
f9903ed0 64
a2ed6e69 65require_login($course);
f9903ed0 66
a2ed6e69
SH
67$systemcontext = context_system::instance();
68$isfrontpage = ($course->id == SITEID);
f9903ed0 69
a2ed6e69 70$frontpagectx = context_course::instance(SITEID);
4f0c2d00 71
a2ed6e69
SH
72if ($isfrontpage) {
73 $PAGE->set_pagelayout('admin');
d4576789
MN
74 if (!has_any_capability(['moodle/site:viewparticipants', 'moodle/course:enrolreview'], $systemcontext)) {
75 // We know they do not have any of the capabilities, so lets throw an exception using the capability with the least access.
76 throw new required_capability_exception($systemcontext, 'moodle/site:viewparticipants', 'nopermissions', '');
77 }
a2ed6e69
SH
78} else {
79 $PAGE->set_pagelayout('incourse');
d4576789
MN
80 if (!has_any_capability(['moodle/course:viewparticipants', 'moodle/course:enrolreview'], $context)) {
81 // We know they do not have any of the capabilities, so lets throw an exception using the capability with the least access.
82 throw new required_capability_exception($context, 'moodle/course:viewparticipants', 'nopermissions', '');
83 }
a2ed6e69 84}
224aa44a 85
0ff203b6
JL
86// Trigger events.
87user_list_view($course, $context);
a2ed6e69
SH
88
89$bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
90
a2ed6e69
SH
91$PAGE->set_title("$course->shortname: ".get_string('participants'));
92$PAGE->set_heading($course->fullname);
93$PAGE->set_pagetype('course-view-' . $course->format);
94$PAGE->add_body_class('path-user'); // So we can style it independently.
95$PAGE->set_other_editing_capability('moodle/course:manageactivities');
2cb2ce61 96
a2ed6e69 97echo $OUTPUT->header();
4e1f6047 98echo $OUTPUT->heading(get_string('participants'));
caa8363f 99
9651e491
JP
100// Get the currently applied filters.
101$filtersapplied = optional_param_array('unified-filters', [], PARAM_TEXT);
a78ed71c 102
9651e491
JP
103// Default group ID.
104$groupid = false;
105$canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
106if ($course->groupmode != NOGROUPS) {
107 if ($canaccessallgroups) {
108 // If the user can see all groups, set default to 0.
109 $groupid = 0;
110 } else {
111 // Otherwise, get the user's default group.
112 $groupid = groups_get_course_group($course, true);
113 if ($course->groupmode == SEPARATEGROUPS && !$groupid) {
114 // The user is not in the group so show message and exit.
115 echo $OUTPUT->notification(get_string('notingroup'));
116 echo $OUTPUT->footer();
117 exit;
118 }
119 }
a2ed6e69 120}
9651e491
JP
121$hasgroupfilter = false;
122$lastaccess = 0;
123$searchkeywords = [];
124$roleid = 0;
125$enrolid = 0;
126$status = -1;
127foreach ($filtersapplied as $filter) {
128 $filtervalue = explode(':', $filter, 2);
129 $value = null;
130 if (count($filtervalue) == 2) {
131 $key = clean_param($filtervalue[0], PARAM_INT);
132 $value = clean_param($filtervalue[1], PARAM_INT);
133 } else {
134 // Search string.
135 $key = clean_param($filtervalue[0], PARAM_TEXT);
136 }
99cca847 137
9651e491
JP
138 switch ($key) {
139 case USER_FILTER_ENROLMENT:
140 $enrolid = $value;
141 break;
142 case USER_FILTER_GROUP:
143 $groupid = $value;
144 $hasgroupfilter = true;
145 break;
146 case USER_FILTER_LAST_ACCESS:
147 $lastaccess = $value;
148 break;
149 case USER_FILTER_ROLE:
150 $roleid = $value;
151 break;
152 case USER_FILTER_STATUS:
153 // We only accept active/suspended statuses.
154 if ($value == ENROL_USER_ACTIVE || $value == ENROL_USER_SUSPENDED) {
155 $status = $value;
156 }
157 break;
158 default:
159 // Search string.
160 if (!empty($key) && empty($value)) {
161 $searchkeywords[] = $key;
162 }
163 break;
164 }
a2ed6e69 165}
03d9401e 166
9651e491
JP
167// If course supports groups, but the user can't access all groups and there's no group filter set, apply a default group filter.
168if ($groupid !== false && !$canaccessallgroups && !$hasgroupfilter) {
169 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
24c3db91 170}
a2ed6e69 171
9651e491
JP
172// Manage enrolments.
173$manager = new course_enrolment_manager($PAGE, $course);
174$enrolbuttons = $manager->get_manual_enrol_buttons();
175$enrolrenderer = $PAGE->get_renderer('core_enrol');
176$enrolbuttonsout = '';
177foreach ($enrolbuttons as $enrolbutton) {
178 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
bc47b706 179}
9651e491 180echo html_writer::div($enrolbuttonsout, 'pull-right');
bc47b706 181
9651e491
JP
182// Render the unified filter.
183$renderer = $PAGE->get_renderer('core_user');
184echo $renderer->unified_filter($course, $context, $filtersapplied);
87a13824 185
9651e491 186echo '<div class="userlist">';
87a13824 187
9651e491
JP
188// Should use this variable so that we don't break stuff every time a variable is added or changed.
189$baseurl = new moodle_url('/user/index.php', array(
190 'contextid' => $context->id,
191 'id' => $course->id,
192 'perpage' => $perpage));
a2ed6e69 193
9651e491
JP
194$participanttable = new \core_user\participants_table($course->id, $groupid, $lastaccess, $roleid, $enrolid, $status,
195 $searchkeywords, $bulkoperations, $selectall);
bc47b706 196$participanttable->define_baseurl($baseurl);
77c645df 197
bc47b706
MN
198// Do this so we can get the total number of rows.
199ob_start();
200$participanttable->out($perpage, true);
201$participanttablehtml = ob_get_contents();
202ob_end_clean();
3e219038 203
a2ed6e69
SH
204if ($bulkoperations) {
205 echo '<form action="action_redir.php" method="post" id="participantsform">';
206 echo '<div>';
207 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
208 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
209}
77c645df 210
bc47b706 211echo $participanttablehtml;
4c7593ff 212
8dd42b38
AH
213$perpageurl = clone($baseurl);
214$perpageurl->remove_params('perpage');
bc47b706 215if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
8dd42b38
AH
216 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
217 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
218
bc47b706 219} else if ($participanttable->get_page_size() < $participanttable->totalrows) {
8dd42b38 220 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
bc47b706
MN
221 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $participanttable->totalrows)),
222 array(), 'showall');
8dd42b38
AH
223}
224
a2ed6e69
SH
225if ($bulkoperations) {
226 echo '<br /><div class="buttons">';
5b7c500a 227
bc47b706 228 if ($participanttable->get_page_size() < $participanttable->totalrows) {
5b7c500a
AH
229 $perpageurl = clone($baseurl);
230 $perpageurl->remove_params('perpage');
231 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
232 $perpageurl->param('selectall', true);
233 $showalllink = $perpageurl;
234 } else {
235 $showalllink = false;
236 }
237
b27c8d81 238 echo html_writer::start_tag('div', array('class' => 'btn-group'));
bc47b706 239 if ($participanttable->get_page_size() < $participanttable->totalrows) {
5b7c500a 240 // Select all users, refresh page showing all users and mark them all selected.
bc47b706 241 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
b27c8d81
SB
242 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
243 'value' => $label, 'data-showallink' => $showalllink));
5b7c500a 244 // Select all users, mark all users on page as selected.
b27c8d81
SB
245 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
246 'value' => get_string('selectallusersonpage')));
5b7c500a 247 } else {
b27c8d81
SB
248 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
249 'value' => get_string('selectall')));
5b7c500a
AH
250 }
251
b27c8d81
SB
252 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checknone', 'class' => 'btn btn-secondary',
253 'value' => get_string('deselectall')));
254 echo html_writer::end_tag('div');
a2ed6e69
SH
255 $displaylist = array();
256 $displaylist['messageselect.php'] = get_string('messageselectadd');
257 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
258 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
259 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
260 }
261
689ccae5
DW
262 $plugins = $manager->get_enrolment_plugins();
263 foreach ($plugins as $plugin) {
264 $bulkoperations = $plugin->get_bulk_operations($manager);
265
266 $pluginoptions = [];
267 foreach ($bulkoperations as $key => $bulkoperation) {
268 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
269 $url = new moodle_url('bulkchange.php', $params);
270 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
271 }
272 if (!empty($pluginoptions)) {
273 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
274 $displaylist[] = [$name => $pluginoptions];
275 }
276 }
277
a2ed6e69
SH
278 echo $OUTPUT->help_icon('withselectedusers');
279 echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
280 echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
281
282 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
283 echo '<noscript style="display:inline">';
284 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
285 echo '</noscript>';
286 echo '</div></div>';
287 echo '</form>';
288
289 $module = array('name' => 'core_user', 'fullpath' => '/user/module.js');
290 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
291}
b90e2f19 292
a2ed6e69 293echo '</div>'; // Userlist.
f9903ed0 294
a78ed71c
DW
295$enrolrenderer = $PAGE->get_renderer('core_enrol');
296echo '<div class="pull-right">';
297foreach ($enrolbuttons as $enrolbutton) {
298 echo $enrolrenderer->render($enrolbutton);
299}
300echo '</div>';
301
a2ed6e69 302echo $OUTPUT->footer();