MDL-68980 core_user: Show user participation group when groupmode set
[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 113// Force group filtering if user should only see a subset of groups' users.
20531b30
AN
114if ($course->groupmode != NOGROUPS && !$canaccessallgroups) {
115 if ($filtergroupids) {
116 $filtergroupids = array_intersect(
117 $filtergroupids,
118 array_keys(groups_get_all_groups($course->id, $USER->id))
119 );
120 } else {
121 $filtergroupids = array_keys(groups_get_all_groups($course->id, $USER->id));
122 }
99cca847 123
d53dd31f 124 if (empty($filtergroupids)) {
20531b30
AN
125 if ($course->groupmode == SEPARATEGROUPS) {
126 // The user is not in a group so show message and exit.
127 echo $OUTPUT->notification(get_string('notingroup'));
128 echo $OUTPUT->footer();
129 exit();
130 } else {
131 $filtergroupids = [(int) groups_get_course_group($course, true)];
132 }
9651e491 133 }
a2ed6e69 134}
d53dd31f
MH
135
136// Apply groups filter if included in URL or forced due to lack of capabilities.
137if (!empty($filtergroupids)) {
138 $filterset->add_filter(new integer_filter('groups', filter::JOINTYPE_DEFAULT, $filtergroupids));
24c3db91 139}
a2ed6e69 140
d53dd31f
MH
141// Display single group information if requested in the URL.
142if ($urlgroupid > 0 && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
349b705d 143 $grouprenderer = $PAGE->get_renderer('core_group');
d53dd31f 144 $groupdetailpage = new \core_group\output\group_details($urlgroupid);
349b705d
AG
145 echo $grouprenderer->group_details($groupdetailpage);
146}
147
d53dd31f
MH
148// Filter by role if passed via URL (used on profile page).
149if ($roleid) {
150 $viewableroles = get_profile_roles($context);
e0e7b19f 151
d53dd31f
MH
152 // Apply filter if the user can view this role.
153 if (array_key_exists($roleid, $viewableroles)) {
154 $filterset->add_filter(new integer_filter('roles', filter::JOINTYPE_DEFAULT, [$roleid]));
155 }
156}
e0e7b19f 157
9651e491
JP
158// Manage enrolments.
159$manager = new course_enrolment_manager($PAGE, $course);
160$enrolbuttons = $manager->get_manual_enrol_buttons();
161$enrolrenderer = $PAGE->get_renderer('core_enrol');
162$enrolbuttonsout = '';
163foreach ($enrolbuttons as $enrolbutton) {
164 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
bc47b706 165}
d53dd31f 166
6881ca38 167echo html_writer::div($enrolbuttonsout, 'd-flex justify-content-end', [
e0e7b19f
AN
168 'data-region' => 'wrapper',
169 'data-table-uniqueid' => $participanttable->uniqueid,
170]);
a2ed6e69 171
77ba77f1
AN
172// Render the user filters.
173$userrenderer = $PAGE->get_renderer('core_user');
174echo $userrenderer->participants_filter($context, $participanttable->uniqueid);
175
bb4a7923
SA
176echo '<div class="userlist">';
177
bc47b706
MN
178// Do this so we can get the total number of rows.
179ob_start();
e0e7b19f 180$participanttable->set_filterset($filterset);
bc47b706
MN
181$participanttable->out($perpage, true);
182$participanttablehtml = ob_get_contents();
183ob_end_clean();
3e219038 184
a2ed6e69 185if ($bulkoperations) {
bae72dd0
AN
186 echo html_writer::start_tag('form', [
187 'action' => 'action_redir.php',
188 'method' => 'post',
189 'id' => 'participantsform',
190 'data-course-id' => $course->id,
191 'data-table-unique-id' => $participanttable->uniqueid,
07c91064 192 'data-table-default-per-page' => ($perpage < DEFAULT_PAGE_SIZE) ? $perpage : DEFAULT_PAGE_SIZE,
bae72dd0 193 ]);
a2ed6e69
SH
194 echo '<div>';
195 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
196 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
197}
77c645df 198
ed23725b
AN
199echo html_writer::tag(
200 'p',
201 get_string('countparticipantsfound', 'core_user', $participanttable->totalrows),
202 [
203 'data-region' => 'participant-count',
204 ]
205);
206
bc47b706 207echo $participanttablehtml;
4c7593ff 208
d53dd31f
MH
209$perpageurl = new moodle_url('/user/index.php', [
210 'contextid' => $context->id,
211 'id' => $course->id,
212]);
50ba817f
AN
213$perpagesize = DEFAULT_PAGE_SIZE;
214$perpagevisible = false;
7f228675 215$perpagestring = '';
50ba817f 216
bc47b706 217if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
7f228675
AN
218 $perpageurl->param('perpage', $participanttable->totalrows);
219 $perpagesize = SHOW_ALL_PAGE_SIZE;
50ba817f 220 $perpagevisible = true;
7f228675 221 $perpagestring = get_string('showperpage', '', DEFAULT_PAGE_SIZE);
bc47b706 222} else if ($participanttable->get_page_size() < $participanttable->totalrows) {
8dd42b38 223 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
7f228675 224 $perpagesize = SHOW_ALL_PAGE_SIZE;
50ba817f 225 $perpagevisible = true;
7f228675 226 $perpagestring = get_string('showall', '', $participanttable->totalrows);
50ba817f
AN
227}
228
229$perpageclasses = '';
230if (!$perpagevisible) {
231 $perpageclasses = 'hidden';
8dd42b38 232}
50ba817f
AN
233echo $OUTPUT->container(html_writer::link(
234 $perpageurl,
7f228675 235 $perpagestring,
50ba817f
AN
236 [
237 'data-action' => 'showcount',
238 'data-target-page-size' => $perpagesize,
239 'class' => $perpageclasses,
240 ]
241), [], 'showall');
8dd42b38 242
a2ed6e69 243if ($bulkoperations) {
8df785f9 244 echo '<br /><div class="buttons"><div class="form-inline">';
5b7c500a 245
b27c8d81 246 echo html_writer::start_tag('div', array('class' => 'btn-group'));
ed23725b 247
bc47b706 248 if ($participanttable->get_page_size() < $participanttable->totalrows) {
ed23725b 249 // Select all users, refresh table showing all users and mark them all selected.
bc47b706 250 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
ed23725b
AN
251 echo html_writer::empty_tag('input', [
252 'type' => 'button',
253 'id' => 'checkall',
254 'class' => 'btn btn-secondary',
255 'value' => $label,
256 'data-target-page-size' => $participanttable->totalrows,
257 ]);
5b7c500a 258 }
b27c8d81 259 echo html_writer::end_tag('div');
a2ed6e69 260 $displaylist = array();
51431068 261 if (!empty($CFG->messaging) && has_all_capabilities(['moodle/site:sendmessage', 'moodle/course:bulkmessaging'], $context)) {
713934f9
ND
262 $displaylist['#messageselect'] = get_string('messageselectadd');
263 }
a2ed6e69 264 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
b5b81de3 265 $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
a2ed6e69
SH
266 }
267
e5abe2af
LB
268 $params = ['operation' => 'download_participants'];
269
270 $downloadoptions = [];
271 $formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
272 foreach ($formats as $format) {
273 if ($format->is_enabled()) {
274 $params = ['operation' => 'download_participants', 'dataformat' => $format->name];
275 $url = new moodle_url('bulkchange.php', $params);
276 $downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
277 }
278 }
279
280 if (!empty($downloadoptions)) {
281 $displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
282 }
283
ca560952 284 if ($context->id != $frontpagectx->id) {
f4ddc4ec
DW
285 $instances = $manager->get_enrolment_instances();
286 $plugins = $manager->get_enrolment_plugins(false);
287 foreach ($instances as $key => $instance) {
288 if (!isset($plugins[$instance->enrol])) {
289 // Weird, some broken stuff in plugin.
290 continue;
291 }
292 $plugin = $plugins[$instance->enrol];
ca560952
DW
293 $bulkoperations = $plugin->get_bulk_operations($manager);
294
295 $pluginoptions = [];
296 foreach ($bulkoperations as $key => $bulkoperation) {
297 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
298 $url = new moodle_url('bulkchange.php', $params);
299 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
300 }
301 if (!empty($pluginoptions)) {
302 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
303 $displaylist[] = [$name => $pluginoptions];
304 }
689ccae5
DW
305 }
306 }
307
df92be9d
JP
308 $selectactionparams = array(
309 'id' => 'formactionid',
310 'class' => 'ml-2',
311 'data-action' => 'toggle',
312 'data-togglegroup' => 'participants-table',
313 'data-toggle' => 'action',
6754ceae 314 'disabled' => 'disabled'
df92be9d 315 );
8aecd857
SR
316 $label = html_writer::tag('label', get_string("withselectedusers"),
317 ['for' => 'formactionid', 'class' => 'col-form-label d-inline']);
318 $select = html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], $selectactionparams);
319 echo html_writer::tag('div', $label . $select);
320
321 echo '<input type="hidden" name="id" value="' . $course->id . '" />';
bae72dd0 322 echo '<div class="d-none" data-region="state-help-icon">' . $OUTPUT->help_icon('publishstate', 'notes') . '</div>';
8df785f9 323 echo '</div></div></div>';
a2ed6e69
SH
324 echo '</form>';
325
bae72dd0
AN
326 $options = (object) [
327 'uniqueid' => $participanttable->uniqueid,
328 'noteStateNames' => note_get_state_names(),
329 ];
b5b81de3 330 $PAGE->requires->js_call_amd('core_user/participants', 'init', [$options]);
a2ed6e69 331}
b90e2f19 332
a2ed6e69 333echo '</div>'; // Userlist.
f9903ed0 334
a78ed71c 335$enrolrenderer = $PAGE->get_renderer('core_enrol');
3dec4c6c
SR
336// Need to re-generate the buttons to avoid having elements with duplicate ids on the page.
337$enrolbuttons = $manager->get_manual_enrol_buttons();
84eb0a3e 338$enrolbuttonsout = '';
a78ed71c 339foreach ($enrolbuttons as $enrolbutton) {
84eb0a3e 340 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
a78ed71c 341}
84eb0a3e
AN
342echo html_writer::div($enrolbuttonsout, 'd-flex justify-content-end', [
343 'data-region' => 'wrapper',
344 'data-table-uniqueid' => $participanttable->uniqueid,
345]);
a78ed71c 346
5fa2d502
DW
347if ($newcourse == 1) {
348 $str = get_string('proceedtocourse', 'enrol');
5fa2d502 349 // The margin is to make it line up with the enrol users button when they are both on the same line.
d470c68b 350 $classes = 'my-1';
5fa2d502
DW
351 $url = course_get_url($course);
352 echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
353}
354
a2ed6e69 355echo $OUTPUT->footer();