MDL-69017 user: Show all links should work for students
[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
8146335f
SL
33use core_table\local\filter\filter;
34use core_table\local\filter\integer_filter;
35use core_table\local\filter\string_filter;
8146335f 36
a2ed6e69
SH
37define('DEFAULT_PAGE_SIZE', 20);
38define('SHOW_ALL_PAGE_SIZE', 5000);
a2ed6e69
SH
39
40$page = optional_param('page', 0, PARAM_INT); // Which page to show.
41$perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
a2ed6e69
SH
42$contextid = optional_param('contextid', 0, PARAM_INT); // One of this or.
43$courseid = optional_param('id', 0, PARAM_INT); // This are required.
5fa2d502 44$newcourse = optional_param('newcourse', false, PARAM_BOOL);
fe214295 45$roleid = optional_param('roleid', 0, PARAM_INT);
d53dd31f 46$urlgroupid = optional_param('group', 0, PARAM_INT);
a2ed6e69
SH
47
48$PAGE->set_url('/user/index.php', array(
49 'page' => $page,
50 'perpage' => $perpage,
a2ed6e69 51 'contextid' => $contextid,
5fa2d502
DW
52 'id' => $courseid,
53 'newcourse' => $newcourse));
a2ed6e69
SH
54
55if ($contextid) {
56 $context = context::instance_by_id($contextid, MUST_EXIST);
57 if ($context->contextlevel != CONTEXT_COURSE) {
58 print_error('invalidcontext');
59 }
60 $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
61} else {
62 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
63 $context = context_course::instance($course->id, MUST_EXIST);
64}
65// Not needed anymore.
66unset($contextid);
67unset($courseid);
f9903ed0 68
a2ed6e69 69require_login($course);
f9903ed0 70
a2ed6e69
SH
71$systemcontext = context_system::instance();
72$isfrontpage = ($course->id == SITEID);
f9903ed0 73
a2ed6e69 74$frontpagectx = context_course::instance(SITEID);
4f0c2d00 75
a2ed6e69
SH
76if ($isfrontpage) {
77 $PAGE->set_pagelayout('admin');
93b47710 78 course_require_view_participants($systemcontext);
a2ed6e69
SH
79} else {
80 $PAGE->set_pagelayout('incourse');
93b47710 81 course_require_view_participants($context);
a2ed6e69 82}
224aa44a 83
0ff203b6
JL
84// Trigger events.
85user_list_view($course, $context);
a2ed6e69
SH
86
87$bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
88
a2ed6e69
SH
89$PAGE->set_title("$course->shortname: ".get_string('participants'));
90$PAGE->set_heading($course->fullname);
91$PAGE->set_pagetype('course-view-' . $course->format);
92$PAGE->add_body_class('path-user'); // So we can style it independently.
93$PAGE->set_other_editing_capability('moodle/course:manageactivities');
2cb2ce61 94
b34fd2cc
DW
95// Expand the users node in the settings navigation when it exists because those pages
96// are related to this one.
97$node = $PAGE->settingsnav->find('users', navigation_node::TYPE_CONTAINER);
98if ($node) {
b0b6ff3c 99 $node->force_open();
b34fd2cc
DW
100}
101
a2ed6e69 102echo $OUTPUT->header();
4e1f6047 103echo $OUTPUT->heading(get_string('participants'));
caa8363f 104
d53dd31f
MH
105$filterset = new \core_user\table\participants_filterset();
106$filterset->add_filter(new integer_filter('courseid', filter::JOINTYPE_DEFAULT, [(int)$course->id]));
fe214295 107
d53dd31f 108$participanttable = new \core_user\table\participants("user-index-participants-{$course->id}");
fe214295 109
9651e491 110$canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
d53dd31f 111$filtergroupids = $urlgroupid ? [$urlgroupid] : [];
8146335f 112
d53dd31f
MH
113// Force group filtering if user should only see a subset of groups' users.
114if ($course->groupmode == SEPARATEGROUPS && !$canaccessallgroups) {
115 $filtergroupids = array_keys(groups_get_all_groups($course->id, $USER->id));
99cca847 116
d53dd31f
MH
117 if (empty($filtergroupids)) {
118 // The user is not in a group so show message and exit.
119 echo $OUTPUT->notification(get_string('notingroup'));
120 echo $OUTPUT->footer();
121 exit();
9651e491 122 }
a2ed6e69 123}
d53dd31f
MH
124
125// Apply groups filter if included in URL or forced due to lack of capabilities.
126if (!empty($filtergroupids)) {
127 $filterset->add_filter(new integer_filter('groups', filter::JOINTYPE_DEFAULT, $filtergroupids));
24c3db91 128}
a2ed6e69 129
d53dd31f
MH
130// Display single group information if requested in the URL.
131if ($urlgroupid > 0 && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
349b705d 132 $grouprenderer = $PAGE->get_renderer('core_group');
d53dd31f 133 $groupdetailpage = new \core_group\output\group_details($urlgroupid);
349b705d
AG
134 echo $grouprenderer->group_details($groupdetailpage);
135}
136
d53dd31f
MH
137// Filter by role if passed via URL (used on profile page).
138if ($roleid) {
139 $viewableroles = get_profile_roles($context);
e0e7b19f 140
d53dd31f
MH
141 // Apply filter if the user can view this role.
142 if (array_key_exists($roleid, $viewableroles)) {
143 $filterset->add_filter(new integer_filter('roles', filter::JOINTYPE_DEFAULT, [$roleid]));
144 }
145}
e0e7b19f 146
9651e491
JP
147// Manage enrolments.
148$manager = new course_enrolment_manager($PAGE, $course);
149$enrolbuttons = $manager->get_manual_enrol_buttons();
150$enrolrenderer = $PAGE->get_renderer('core_enrol');
151$enrolbuttonsout = '';
152foreach ($enrolbuttons as $enrolbutton) {
153 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
bc47b706 154}
d53dd31f 155
6881ca38 156echo html_writer::div($enrolbuttonsout, 'd-flex justify-content-end', [
e0e7b19f
AN
157 'data-region' => 'wrapper',
158 'data-table-uniqueid' => $participanttable->uniqueid,
159]);
a2ed6e69 160
77ba77f1
AN
161// Render the user filters.
162$userrenderer = $PAGE->get_renderer('core_user');
163echo $userrenderer->participants_filter($context, $participanttable->uniqueid);
164
bb4a7923
SA
165echo '<div class="userlist">';
166
bc47b706
MN
167// Do this so we can get the total number of rows.
168ob_start();
e0e7b19f 169$participanttable->set_filterset($filterset);
bc47b706
MN
170$participanttable->out($perpage, true);
171$participanttablehtml = ob_get_contents();
172ob_end_clean();
3e219038 173
d3bfae8f
AN
174echo html_writer::start_tag('form', [
175 'action' => 'action_redir.php',
176 'method' => 'post',
177 'id' => 'participantsform',
178 'data-course-id' => $course->id,
179 'data-table-unique-id' => $participanttable->uniqueid,
180 'data-table-default-per-page' => ($perpage < DEFAULT_PAGE_SIZE) ? $perpage : DEFAULT_PAGE_SIZE,
181]);
182echo '<div>';
183echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
184echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
77c645df 185
ed23725b
AN
186echo html_writer::tag(
187 'p',
188 get_string('countparticipantsfound', 'core_user', $participanttable->totalrows),
189 [
190 'data-region' => 'participant-count',
191 ]
192);
193
bc47b706 194echo $participanttablehtml;
4c7593ff 195
d53dd31f
MH
196$perpageurl = new moodle_url('/user/index.php', [
197 'contextid' => $context->id,
198 'id' => $course->id,
199]);
50ba817f
AN
200$perpagesize = DEFAULT_PAGE_SIZE;
201$perpagevisible = false;
7f228675 202$perpagestring = '';
50ba817f 203
bc47b706 204if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
7f228675
AN
205 $perpageurl->param('perpage', $participanttable->totalrows);
206 $perpagesize = SHOW_ALL_PAGE_SIZE;
50ba817f 207 $perpagevisible = true;
7f228675 208 $perpagestring = get_string('showperpage', '', DEFAULT_PAGE_SIZE);
bc47b706 209} else if ($participanttable->get_page_size() < $participanttable->totalrows) {
8dd42b38 210 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
7f228675 211 $perpagesize = SHOW_ALL_PAGE_SIZE;
50ba817f 212 $perpagevisible = true;
7f228675 213 $perpagestring = get_string('showall', '', $participanttable->totalrows);
50ba817f
AN
214}
215
216$perpageclasses = '';
217if (!$perpagevisible) {
218 $perpageclasses = 'hidden';
8dd42b38 219}
50ba817f
AN
220echo $OUTPUT->container(html_writer::link(
221 $perpageurl,
7f228675 222 $perpagestring,
50ba817f
AN
223 [
224 'data-action' => 'showcount',
225 'data-target-page-size' => $perpagesize,
226 'class' => $perpageclasses,
227 ]
228), [], 'showall');
8dd42b38 229
d3bfae8f
AN
230$bulkoptions = (object) [
231 'uniqueid' => $participanttable->uniqueid,
232];
233
a2ed6e69 234if ($bulkoperations) {
8df785f9 235 echo '<br /><div class="buttons"><div class="form-inline">';
5b7c500a 236
b27c8d81 237 echo html_writer::start_tag('div', array('class' => 'btn-group'));
ed23725b 238
bc47b706 239 if ($participanttable->get_page_size() < $participanttable->totalrows) {
ed23725b 240 // Select all users, refresh table showing all users and mark them all selected.
bc47b706 241 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
ed23725b
AN
242 echo html_writer::empty_tag('input', [
243 'type' => 'button',
244 'id' => 'checkall',
245 'class' => 'btn btn-secondary',
246 'value' => $label,
247 'data-target-page-size' => $participanttable->totalrows,
248 ]);
5b7c500a 249 }
b27c8d81 250 echo html_writer::end_tag('div');
a2ed6e69 251 $displaylist = array();
51431068 252 if (!empty($CFG->messaging) && has_all_capabilities(['moodle/site:sendmessage', 'moodle/course:bulkmessaging'], $context)) {
713934f9
ND
253 $displaylist['#messageselect'] = get_string('messageselectadd');
254 }
a2ed6e69 255 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
b5b81de3 256 $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
a2ed6e69
SH
257 }
258
e5abe2af
LB
259 $params = ['operation' => 'download_participants'];
260
261 $downloadoptions = [];
262 $formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
263 foreach ($formats as $format) {
264 if ($format->is_enabled()) {
265 $params = ['operation' => 'download_participants', 'dataformat' => $format->name];
266 $url = new moodle_url('bulkchange.php', $params);
267 $downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
268 }
269 }
270
271 if (!empty($downloadoptions)) {
272 $displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
273 }
274
ca560952 275 if ($context->id != $frontpagectx->id) {
f4ddc4ec
DW
276 $instances = $manager->get_enrolment_instances();
277 $plugins = $manager->get_enrolment_plugins(false);
278 foreach ($instances as $key => $instance) {
279 if (!isset($plugins[$instance->enrol])) {
280 // Weird, some broken stuff in plugin.
281 continue;
282 }
283 $plugin = $plugins[$instance->enrol];
ca560952
DW
284 $bulkoperations = $plugin->get_bulk_operations($manager);
285
286 $pluginoptions = [];
287 foreach ($bulkoperations as $key => $bulkoperation) {
288 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
289 $url = new moodle_url('bulkchange.php', $params);
290 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
291 }
292 if (!empty($pluginoptions)) {
293 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
294 $displaylist[] = [$name => $pluginoptions];
295 }
689ccae5
DW
296 }
297 }
298
df92be9d
JP
299 $selectactionparams = array(
300 'id' => 'formactionid',
301 'class' => 'ml-2',
302 'data-action' => 'toggle',
303 'data-togglegroup' => 'participants-table',
304 'data-toggle' => 'action',
6754ceae 305 'disabled' => 'disabled'
df92be9d 306 );
8aecd857
SR
307 $label = html_writer::tag('label', get_string("withselectedusers"),
308 ['for' => 'formactionid', 'class' => 'col-form-label d-inline']);
309 $select = html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], $selectactionparams);
310 echo html_writer::tag('div', $label . $select);
311
312 echo '<input type="hidden" name="id" value="' . $course->id . '" />';
bae72dd0 313 echo '<div class="d-none" data-region="state-help-icon">' . $OUTPUT->help_icon('publishstate', 'notes') . '</div>';
8df785f9 314 echo '</div></div></div>';
a2ed6e69 315
d3bfae8f 316 $bulkoptions->noteStateNames = note_get_state_names();
a2ed6e69 317}
d3bfae8f 318echo '</form>';
b90e2f19 319
d3bfae8f 320$PAGE->requires->js_call_amd('core_user/participants', 'init', [$bulkoptions]);
a2ed6e69 321echo '</div>'; // Userlist.
f9903ed0 322
a78ed71c 323$enrolrenderer = $PAGE->get_renderer('core_enrol');
3dec4c6c
SR
324// Need to re-generate the buttons to avoid having elements with duplicate ids on the page.
325$enrolbuttons = $manager->get_manual_enrol_buttons();
84eb0a3e 326$enrolbuttonsout = '';
a78ed71c 327foreach ($enrolbuttons as $enrolbutton) {
84eb0a3e 328 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
a78ed71c 329}
84eb0a3e
AN
330echo html_writer::div($enrolbuttonsout, 'd-flex justify-content-end', [
331 'data-region' => 'wrapper',
332 'data-table-uniqueid' => $participanttable->uniqueid,
333]);
a78ed71c 334
5fa2d502
DW
335if ($newcourse == 1) {
336 $str = get_string('proceedtocourse', 'enrol');
5fa2d502 337 // The margin is to make it line up with the enrol users button when they are both on the same line.
d470c68b 338 $classes = 'my-1';
5fa2d502
DW
339 $url = course_get_url($course);
340 echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
341}
342
a2ed6e69 343echo $OUTPUT->footer();