MDL-60515 groups: Display group information on the 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');
5fa2d502 27require_once($CFG->dirroot.'/course/lib.php');
a2ed6e69
SH
28require_once($CFG->libdir.'/tablelib.php');
29require_once($CFG->libdir.'/filelib.php');
a78ed71c 30require_once($CFG->dirroot.'/enrol/locallib.php');
a2ed6e69 31
a2ed6e69
SH
32define('DEFAULT_PAGE_SIZE', 20);
33define('SHOW_ALL_PAGE_SIZE', 5000);
9651e491
JP
34define('USER_FILTER_ENROLMENT', 1);
35define('USER_FILTER_GROUP', 2);
36define('USER_FILTER_LAST_ACCESS', 3);
37define('USER_FILTER_ROLE', 4);
38define('USER_FILTER_STATUS', 5);
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);
5b7c500a 45$selectall = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
fe214295 46$roleid = optional_param('roleid', 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
9651e491
JP
105// Get the currently applied filters.
106$filtersapplied = optional_param_array('unified-filters', [], PARAM_TEXT);
32e5109d 107$filterwassubmitted = optional_param('unified-filter-submitted', 0, PARAM_BOOL);
a78ed71c 108
fe214295
MN
109// If they passed a role make sure they can view that role.
110if ($roleid) {
111 $viewableroles = get_profile_roles($context);
112
113 // Check if the user can view this role.
114 if (array_key_exists($roleid, $viewableroles)) {
115 $filtersapplied[] = USER_FILTER_ROLE . ':' . $roleid;
116 } else {
117 $roleid = 0;
118 }
119}
120
9651e491
JP
121// Default group ID.
122$groupid = false;
123$canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
124if ($course->groupmode != NOGROUPS) {
125 if ($canaccessallgroups) {
126 // If the user can see all groups, set default to 0.
127 $groupid = 0;
128 } else {
129 // Otherwise, get the user's default group.
130 $groupid = groups_get_course_group($course, true);
131 if ($course->groupmode == SEPARATEGROUPS && !$groupid) {
132 // The user is not in the group so show message and exit.
133 echo $OUTPUT->notification(get_string('notingroup'));
134 echo $OUTPUT->footer();
135 exit;
136 }
137 }
a2ed6e69 138}
9651e491
JP
139$hasgroupfilter = false;
140$lastaccess = 0;
141$searchkeywords = [];
9651e491
JP
142$enrolid = 0;
143$status = -1;
144foreach ($filtersapplied as $filter) {
145 $filtervalue = explode(':', $filter, 2);
146 $value = null;
147 if (count($filtervalue) == 2) {
148 $key = clean_param($filtervalue[0], PARAM_INT);
149 $value = clean_param($filtervalue[1], PARAM_INT);
150 } else {
151 // Search string.
152 $key = clean_param($filtervalue[0], PARAM_TEXT);
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.
177 if (!empty($key) && empty($value)) {
178 $searchkeywords[] = $key;
179 }
180 break;
181 }
a2ed6e69 182}
03d9401e 183
32e5109d
MN
184// If course supports groups we may need to set a default.
185if ($groupid !== false) {
186 // If we are in a course with visible groups and the user has not submitted anything and does not have
187 // access to all groups, then set a default group. This is the same behaviour in 3.3.
188 if (!$canaccessallgroups && !$filterwassubmitted && $course->groupmode == VISIBLEGROUPS) {
189 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
190 } else if (!$canaccessallgroups && !$hasgroupfilter && $course->groupmode != VISIBLEGROUPS) {
191 // The user can't access all groups and has not set a group filter in a course where the groups are not visible
192 // then apply a default group filter.
193 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
194 } else if (!$hasgroupfilter) { // No need for the group id to be set.
195 $groupid = false;
196 }
24c3db91 197}
a2ed6e69 198
349b705d
AG
199if ($groupid && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
200 $grouprenderer = $PAGE->get_renderer('core_group');
201 $groupdetailpage = new \core_group\output\group_details($groupid);
202 echo $grouprenderer->group_details($groupdetailpage);
203}
204
9651e491
JP
205// Manage enrolments.
206$manager = new course_enrolment_manager($PAGE, $course);
207$enrolbuttons = $manager->get_manual_enrol_buttons();
208$enrolrenderer = $PAGE->get_renderer('core_enrol');
209$enrolbuttonsout = '';
210foreach ($enrolbuttons as $enrolbutton) {
211 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
bc47b706 212}
9651e491 213echo html_writer::div($enrolbuttonsout, 'pull-right');
bc47b706 214
9651e491
JP
215// Render the unified filter.
216$renderer = $PAGE->get_renderer('core_user');
217echo $renderer->unified_filter($course, $context, $filtersapplied);
87a13824 218
9651e491 219echo '<div class="userlist">';
87a13824 220
9651e491
JP
221// Should use this variable so that we don't break stuff every time a variable is added or changed.
222$baseurl = new moodle_url('/user/index.php', array(
223 'contextid' => $context->id,
224 'id' => $course->id,
225 'perpage' => $perpage));
a2ed6e69 226
9651e491
JP
227$participanttable = new \core_user\participants_table($course->id, $groupid, $lastaccess, $roleid, $enrolid, $status,
228 $searchkeywords, $bulkoperations, $selectall);
bc47b706 229$participanttable->define_baseurl($baseurl);
77c645df 230
bc47b706
MN
231// Do this so we can get the total number of rows.
232ob_start();
233$participanttable->out($perpage, true);
234$participanttablehtml = ob_get_contents();
235ob_end_clean();
3e219038 236
a2ed6e69
SH
237if ($bulkoperations) {
238 echo '<form action="action_redir.php" method="post" id="participantsform">';
239 echo '<div>';
240 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
241 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
242}
77c645df 243
bc47b706 244echo $participanttablehtml;
4c7593ff 245
8dd42b38
AH
246$perpageurl = clone($baseurl);
247$perpageurl->remove_params('perpage');
bc47b706 248if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
8dd42b38
AH
249 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
250 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
251
bc47b706 252} else if ($participanttable->get_page_size() < $participanttable->totalrows) {
8dd42b38 253 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
bc47b706
MN
254 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $participanttable->totalrows)),
255 array(), 'showall');
8dd42b38
AH
256}
257
a2ed6e69
SH
258if ($bulkoperations) {
259 echo '<br /><div class="buttons">';
5b7c500a 260
bc47b706 261 if ($participanttable->get_page_size() < $participanttable->totalrows) {
5b7c500a
AH
262 $perpageurl = clone($baseurl);
263 $perpageurl->remove_params('perpage');
264 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
265 $perpageurl->param('selectall', true);
266 $showalllink = $perpageurl;
267 } else {
268 $showalllink = false;
269 }
270
b27c8d81 271 echo html_writer::start_tag('div', array('class' => 'btn-group'));
bc47b706 272 if ($participanttable->get_page_size() < $participanttable->totalrows) {
5b7c500a 273 // Select all users, refresh page showing all users and mark them all selected.
bc47b706 274 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
b27c8d81
SB
275 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
276 'value' => $label, 'data-showallink' => $showalllink));
5b7c500a 277 // Select all users, mark all users on page as selected.
b27c8d81
SB
278 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
279 'value' => get_string('selectallusersonpage')));
5b7c500a 280 } else {
b27c8d81
SB
281 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
282 'value' => get_string('selectall')));
5b7c500a
AH
283 }
284
b27c8d81
SB
285 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checknone', 'class' => 'btn btn-secondary',
286 'value' => get_string('deselectall')));
287 echo html_writer::end_tag('div');
a2ed6e69
SH
288 $displaylist = array();
289 $displaylist['messageselect.php'] = get_string('messageselectadd');
290 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
291 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
292 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
293 }
294
ca560952 295 if ($context->id != $frontpagectx->id) {
f4ddc4ec
DW
296 $instances = $manager->get_enrolment_instances();
297 $plugins = $manager->get_enrolment_plugins(false);
298 foreach ($instances as $key => $instance) {
299 if (!isset($plugins[$instance->enrol])) {
300 // Weird, some broken stuff in plugin.
301 continue;
302 }
303 $plugin = $plugins[$instance->enrol];
ca560952
DW
304 $bulkoperations = $plugin->get_bulk_operations($manager);
305
306 $pluginoptions = [];
307 foreach ($bulkoperations as $key => $bulkoperation) {
308 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
309 $url = new moodle_url('bulkchange.php', $params);
310 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
311 }
312 if (!empty($pluginoptions)) {
313 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
314 $displaylist[] = [$name => $pluginoptions];
315 }
689ccae5
DW
316 }
317 }
318
a2ed6e69
SH
319 echo $OUTPUT->help_icon('withselectedusers');
320 echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
321 echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
322
323 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
324 echo '<noscript style="display:inline">';
325 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
326 echo '</noscript>';
327 echo '</div></div>';
328 echo '</form>';
329
330 $module = array('name' => 'core_user', 'fullpath' => '/user/module.js');
331 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
332}
b90e2f19 333
a2ed6e69 334echo '</div>'; // Userlist.
f9903ed0 335
a78ed71c
DW
336$enrolrenderer = $PAGE->get_renderer('core_enrol');
337echo '<div class="pull-right">';
338foreach ($enrolbuttons as $enrolbutton) {
339 echo $enrolrenderer->render($enrolbutton);
340}
341echo '</div>';
342
5fa2d502
DW
343if ($newcourse == 1) {
344 $str = get_string('proceedtocourse', 'enrol');
345 // Floated left so it goes under the enrol users button on mobile.
346 // The margin is to make it line up with the enrol users button when they are both on the same line.
347 $classes = 'm-y-1 pull-xs-left';
348 $url = course_get_url($course);
349 echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
350}
351
a2ed6e69 352echo $OUTPUT->footer();