Moodle release 3.9beta
[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
a2ed6e69 174if ($bulkoperations) {
bae72dd0
AN
175 echo html_writer::start_tag('form', [
176 'action' => 'action_redir.php',
177 'method' => 'post',
178 'id' => 'participantsform',
179 'data-course-id' => $course->id,
180 'data-table-unique-id' => $participanttable->uniqueid,
07c91064 181 'data-table-default-per-page' => ($perpage < DEFAULT_PAGE_SIZE) ? $perpage : DEFAULT_PAGE_SIZE,
bae72dd0 182 ]);
a2ed6e69
SH
183 echo '<div>';
184 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
185 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
186}
77c645df 187
ed23725b
AN
188echo html_writer::tag(
189 'p',
190 get_string('countparticipantsfound', 'core_user', $participanttable->totalrows),
191 [
192 'data-region' => 'participant-count',
193 ]
194);
195
bc47b706 196echo $participanttablehtml;
4c7593ff 197
d53dd31f
MH
198$perpageurl = new moodle_url('/user/index.php', [
199 'contextid' => $context->id,
200 'id' => $course->id,
201]);
50ba817f
AN
202$perpagesize = DEFAULT_PAGE_SIZE;
203$perpagevisible = false;
7f228675 204$perpagestring = '';
50ba817f 205
bc47b706 206if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
7f228675
AN
207 $perpageurl->param('perpage', $participanttable->totalrows);
208 $perpagesize = SHOW_ALL_PAGE_SIZE;
50ba817f 209 $perpagevisible = true;
7f228675 210 $perpagestring = get_string('showperpage', '', DEFAULT_PAGE_SIZE);
bc47b706 211} else if ($participanttable->get_page_size() < $participanttable->totalrows) {
8dd42b38 212 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
7f228675 213 $perpagesize = SHOW_ALL_PAGE_SIZE;
50ba817f 214 $perpagevisible = true;
7f228675 215 $perpagestring = get_string('showall', '', $participanttable->totalrows);
50ba817f
AN
216}
217
218$perpageclasses = '';
219if (!$perpagevisible) {
220 $perpageclasses = 'hidden';
8dd42b38 221}
50ba817f
AN
222echo $OUTPUT->container(html_writer::link(
223 $perpageurl,
7f228675 224 $perpagestring,
50ba817f
AN
225 [
226 'data-action' => 'showcount',
227 'data-target-page-size' => $perpagesize,
228 'class' => $perpageclasses,
229 ]
230), [], 'showall');
8dd42b38 231
a2ed6e69 232if ($bulkoperations) {
8df785f9 233 echo '<br /><div class="buttons"><div class="form-inline">';
5b7c500a 234
b27c8d81 235 echo html_writer::start_tag('div', array('class' => 'btn-group'));
ed23725b 236
bc47b706 237 if ($participanttable->get_page_size() < $participanttable->totalrows) {
ed23725b 238 // Select all users, refresh table showing all users and mark them all selected.
bc47b706 239 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
ed23725b
AN
240 echo html_writer::empty_tag('input', [
241 'type' => 'button',
242 'id' => 'checkall',
243 'class' => 'btn btn-secondary',
244 'value' => $label,
245 'data-target-page-size' => $participanttable->totalrows,
246 ]);
5b7c500a 247 }
b27c8d81 248 echo html_writer::end_tag('div');
a2ed6e69 249 $displaylist = array();
51431068 250 if (!empty($CFG->messaging) && has_all_capabilities(['moodle/site:sendmessage', 'moodle/course:bulkmessaging'], $context)) {
713934f9
ND
251 $displaylist['#messageselect'] = get_string('messageselectadd');
252 }
a2ed6e69 253 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
b5b81de3 254 $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
a2ed6e69
SH
255 }
256
e5abe2af
LB
257 $params = ['operation' => 'download_participants'];
258
259 $downloadoptions = [];
260 $formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
261 foreach ($formats as $format) {
262 if ($format->is_enabled()) {
263 $params = ['operation' => 'download_participants', 'dataformat' => $format->name];
264 $url = new moodle_url('bulkchange.php', $params);
265 $downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
266 }
267 }
268
269 if (!empty($downloadoptions)) {
270 $displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
271 }
272
ca560952 273 if ($context->id != $frontpagectx->id) {
f4ddc4ec
DW
274 $instances = $manager->get_enrolment_instances();
275 $plugins = $manager->get_enrolment_plugins(false);
276 foreach ($instances as $key => $instance) {
277 if (!isset($plugins[$instance->enrol])) {
278 // Weird, some broken stuff in plugin.
279 continue;
280 }
281 $plugin = $plugins[$instance->enrol];
ca560952
DW
282 $bulkoperations = $plugin->get_bulk_operations($manager);
283
284 $pluginoptions = [];
285 foreach ($bulkoperations as $key => $bulkoperation) {
286 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
287 $url = new moodle_url('bulkchange.php', $params);
288 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
289 }
290 if (!empty($pluginoptions)) {
291 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
292 $displaylist[] = [$name => $pluginoptions];
293 }
689ccae5
DW
294 }
295 }
296
df92be9d
JP
297 $selectactionparams = array(
298 'id' => 'formactionid',
299 'class' => 'ml-2',
300 'data-action' => 'toggle',
301 'data-togglegroup' => 'participants-table',
302 'data-toggle' => 'action',
6754ceae 303 'disabled' => 'disabled'
df92be9d 304 );
8aecd857
SR
305 $label = html_writer::tag('label', get_string("withselectedusers"),
306 ['for' => 'formactionid', 'class' => 'col-form-label d-inline']);
307 $select = html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], $selectactionparams);
308 echo html_writer::tag('div', $label . $select);
309
310 echo '<input type="hidden" name="id" value="' . $course->id . '" />';
bae72dd0 311 echo '<div class="d-none" data-region="state-help-icon">' . $OUTPUT->help_icon('publishstate', 'notes') . '</div>';
8df785f9 312 echo '</div></div></div>';
a2ed6e69
SH
313 echo '</form>';
314
bae72dd0
AN
315 $options = (object) [
316 'uniqueid' => $participanttable->uniqueid,
317 'noteStateNames' => note_get_state_names(),
318 ];
b5b81de3 319 $PAGE->requires->js_call_amd('core_user/participants', 'init', [$options]);
a2ed6e69 320}
b90e2f19 321
a2ed6e69 322echo '</div>'; // Userlist.
f9903ed0 323
a78ed71c 324$enrolrenderer = $PAGE->get_renderer('core_enrol');
3dec4c6c
SR
325// Need to re-generate the buttons to avoid having elements with duplicate ids on the page.
326$enrolbuttons = $manager->get_manual_enrol_buttons();
84eb0a3e 327$enrolbuttonsout = '';
a78ed71c 328foreach ($enrolbuttons as $enrolbutton) {
84eb0a3e 329 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
a78ed71c 330}
84eb0a3e
AN
331echo html_writer::div($enrolbuttonsout, 'd-flex justify-content-end', [
332 'data-region' => 'wrapper',
333 'data-table-uniqueid' => $participanttable->uniqueid,
334]);
a78ed71c 335
5fa2d502
DW
336if ($newcourse == 1) {
337 $str = get_string('proceedtocourse', 'enrol');
5fa2d502 338 // The margin is to make it line up with the enrol users button when they are both on the same line.
d470c68b 339 $classes = 'my-1';
5fa2d502
DW
340 $url = course_get_url($course);
341 echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
342}
343
a2ed6e69 344echo $OUTPUT->footer();