MDL-67386 theme_boost: update latest CSS changes
[moodle.git] / user / index.php
blob3078724573da406e21441853e3ed26ec7cfe9e73
1 <?php
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.
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/>.
17 /**
18 * Lists all the users within a given course.
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
25 require_once('../config.php');
26 require_once($CFG->dirroot.'/user/lib.php');
27 require_once($CFG->dirroot.'/course/lib.php');
28 require_once($CFG->dirroot.'/notes/lib.php');
29 require_once($CFG->libdir.'/tablelib.php');
30 require_once($CFG->libdir.'/filelib.php');
31 require_once($CFG->dirroot.'/enrol/locallib.php');
33 use core_table\local\filter\filter;
34 use core_table\local\filter\integer_filter;
35 use core_table\local\filter\string_filter;
37 define('DEFAULT_PAGE_SIZE', 20);
38 define('SHOW_ALL_PAGE_SIZE', 5000);
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.
42 $contextid = optional_param('contextid', 0, PARAM_INT); // One of this or.
43 $courseid = optional_param('id', 0, PARAM_INT); // This are required.
44 $newcourse = optional_param('newcourse', false, PARAM_BOOL);
45 $roleid = optional_param('roleid', 0, PARAM_INT);
46 $groupparam = optional_param('group', 0, PARAM_INT);
48 $PAGE->set_url('/user/index.php', array(
49 'page' => $page,
50 'perpage' => $perpage,
51 'contextid' => $contextid,
52 'id' => $courseid,
53 'newcourse' => $newcourse));
55 if ($contextid) {
56 $context = context::instance_by_id($contextid, MUST_EXIST);
57 if ($context->contextlevel != CONTEXT_COURSE) {
58 print_error('invalidcontext');
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);
65 // Not needed anymore.
66 unset($contextid);
67 unset($courseid);
69 require_login($course);
71 $systemcontext = context_system::instance();
72 $isfrontpage = ($course->id == SITEID);
74 $frontpagectx = context_course::instance(SITEID);
76 if ($isfrontpage) {
77 $PAGE->set_pagelayout('admin');
78 course_require_view_participants($systemcontext);
79 } else {
80 $PAGE->set_pagelayout('incourse');
81 course_require_view_participants($context);
84 // Trigger events.
85 user_list_view($course, $context);
87 $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
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');
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);
98 if ($node) {
99 $node->force_open();
102 echo $OUTPUT->header();
103 echo $OUTPUT->heading(get_string('participants'));
105 // Get the currently applied filters.
106 $filtersapplied = optional_param_array('unified-filters', [], PARAM_NOTAGS);
107 $filterwassubmitted = optional_param('unified-filter-submitted', 0, PARAM_BOOL);
109 // If they passed a role make sure they can view that role.
110 if ($roleid) {
111 $viewableroles = get_profile_roles($context);
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;
121 // Default group ID.
122 $groupid = false;
123 $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
124 if ($course->groupmode != NOGROUPS) {
125 if ($canaccessallgroups) {
126 // Change the group if the user can access all groups and has specified group in the URL.
127 if ($groupparam) {
128 $groupid = $groupparam;
130 } else {
131 // Otherwise, get the user's default group.
132 $groupid = groups_get_course_group($course, true);
133 if ($course->groupmode == SEPARATEGROUPS && !$groupid) {
134 // The user is not in the group so show message and exit.
135 echo $OUTPUT->notification(get_string('notingroup'));
136 echo $OUTPUT->footer();
137 exit;
141 $hasgroupfilter = false;
142 $lastaccess = 0;
143 $searchkeywords = [];
144 $enrolid = 0;
146 $filterset = new \core_user\table\participants_filterset();
147 $filterset->add_filter(new integer_filter('courseid', filter::JOINTYPE_DEFAULT, [(int)$course->id]));
148 $enrolfilter = new integer_filter('enrolments');
149 $groupfilter = new integer_filter('groups');
150 $keywordfilter = new string_filter('keywords');
151 $lastaccessfilter = new integer_filter('accesssince');
152 $rolefilter = new integer_filter('roles');
153 $statusfilter = new integer_filter('status');
155 foreach ($filtersapplied as $filter) {
156 $filtervalue = explode(':', $filter, 2);
157 $value = null;
158 if (count($filtervalue) == 2) {
159 $key = clean_param($filtervalue[0], PARAM_INT);
160 $value = clean_param($filtervalue[1], PARAM_INT);
161 } else {
162 // Search string.
163 $key = USER_FILTER_STRING;
164 $value = clean_param($filtervalue[0], PARAM_TEXT);
167 switch ($key) {
168 case USER_FILTER_ENROLMENT:
169 $enrolid = $value;
170 $enrolfilter->add_filter_value($value);
171 break;
172 case USER_FILTER_GROUP:
173 $groupid = $value;
174 $groupfilter->add_filter_value($value);
175 $hasgroupfilter = true;
176 break;
177 case USER_FILTER_LAST_ACCESS:
178 $lastaccess = $value;
179 $lastaccessfilter->add_filter_value($value);
180 break;
181 case USER_FILTER_ROLE:
182 $roleid = $value;
183 $rolefilter->add_filter_value($value);
184 break;
185 case USER_FILTER_STATUS:
186 // We only accept active/suspended statuses.
187 if ($value == ENROL_USER_ACTIVE || $value == ENROL_USER_SUSPENDED) {
188 $status = $value;
189 $statusfilter->add_filter_value($value);
191 break;
192 default:
193 // Search string.
194 $searchkeywords[] = $value;
195 $keywordfilter->add_filter_value($value);
196 break;
199 // If course supports groups we may need to set a default.
200 if (!empty($groupid)) {
201 if ($canaccessallgroups) {
202 // User can access all groups, let them filter by whatever was selected.
203 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
204 $groupfilter->add_filter_value((int)$groupid);
205 } else if (!$filterwassubmitted && $course->groupmode == VISIBLEGROUPS) {
206 // If we are in a course with visible groups and the user has not submitted anything and does not have
207 // access to all groups, then set a default group.
208 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
209 $groupfilter->add_filter_value((int)$groupid);
210 } else if (!$hasgroupfilter && $course->groupmode != VISIBLEGROUPS) {
211 // The user can't access all groups and has not set a group filter in a course where the groups are not visible
212 // then apply a default group filter.
213 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
214 $groupfilter->add_filter_value((int)$groupid);
215 } else if (!$hasgroupfilter) { // No need for the group id to be set.
216 $groupid = false;
220 if ($groupid > 0 && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
221 $grouprenderer = $PAGE->get_renderer('core_group');
222 $groupdetailpage = new \core_group\output\group_details($groupid);
223 echo $grouprenderer->group_details($groupdetailpage);
226 // Should use this variable so that we don't break stuff every time a variable is added or changed.
227 $baseurl = new moodle_url('/user/index.php', array(
228 'contextid' => $context->id,
229 'id' => $course->id,
230 'perpage' => $perpage));
232 $participanttable = new \core_user\table\participants("user-index-participants-{$course->id}");
233 $participanttable->define_baseurl($baseurl);
235 // Manage enrolments.
236 $manager = new course_enrolment_manager($PAGE, $course);
237 $enrolbuttons = $manager->get_manual_enrol_buttons();
238 $enrolrenderer = $PAGE->get_renderer('core_enrol');
239 $enrolbuttonsout = '';
240 foreach ($enrolbuttons as $enrolbutton) {
241 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
243 echo html_writer::div($enrolbuttonsout, 'float-right', [
244 'data-region' => 'wrapper',
245 'data-table-uniqueid' => $participanttable->uniqueid,
248 // Render the unified filter.
249 $renderer = $PAGE->get_renderer('core_user');
250 echo $renderer->unified_filter($course, $context, $filtersapplied, $baseurl);
252 echo '<div class="userlist">';
254 // Add filters to the baseurl after creating unified_filter to avoid losing them.
255 foreach (array_unique($filtersapplied) as $filterix => $filter) {
256 $baseurl->param('unified-filters[' . $filterix . ']', $filter);
259 if (count($groupfilter)) {
260 $filterset->add_filter($groupfilter);
263 if (count($lastaccessfilter)) {
264 $filterset->add_filter($lastaccessfilter);
267 if (count($rolefilter)) {
268 $filterset->add_filter($rolefilter);
271 if (count($enrolfilter)) {
272 $filterset->add_filter($enrolfilter);
275 if (count($statusfilter)) {
276 $filterset->add_filter($statusfilter);
279 if (count($keywordfilter)) {
280 $filterset->add_filter($keywordfilter);
283 // Do this so we can get the total number of rows.
284 ob_start();
285 $participanttable->set_filterset($filterset);
286 $participanttable->out($perpage, true);
287 $participanttablehtml = ob_get_contents();
288 ob_end_clean();
290 if ($bulkoperations) {
291 echo html_writer::start_tag('form', [
292 'action' => 'action_redir.php',
293 'method' => 'post',
294 'id' => 'participantsform',
295 'data-course-id' => $course->id,
296 'data-table-unique-id' => $participanttable->uniqueid,
297 'data-table-default-per-page' => ($perpage < DEFAULT_PAGE_SIZE) ? $perpage : DEFAULT_PAGE_SIZE,
299 echo '<div>';
300 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
301 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
304 echo html_writer::tag(
305 'p',
306 get_string('countparticipantsfound', 'core_user', $participanttable->totalrows),
308 'data-region' => 'participant-count',
312 echo $participanttablehtml;
314 $perpageurl = clone($baseurl);
315 $perpageurl->remove_params('perpage');
316 $perpagesize = DEFAULT_PAGE_SIZE;
317 $perpagevisible = false;
318 $perpagestring = '';
320 if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
321 $perpageurl->param('perpage', $participanttable->totalrows);
322 $perpagesize = SHOW_ALL_PAGE_SIZE;
323 $perpagevisible = true;
324 $perpagestring = get_string('showperpage', '', DEFAULT_PAGE_SIZE);
325 } else if ($participanttable->get_page_size() < $participanttable->totalrows) {
326 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
327 $perpagesize = SHOW_ALL_PAGE_SIZE;
328 $perpagevisible = true;
329 $perpagestring = get_string('showall', '', $participanttable->totalrows);
332 $perpageclasses = '';
333 if (!$perpagevisible) {
334 $perpageclasses = 'hidden';
336 echo $OUTPUT->container(html_writer::link(
337 $perpageurl,
338 $perpagestring,
340 'data-action' => 'showcount',
341 'data-target-page-size' => $perpagesize,
342 'class' => $perpageclasses,
344 ), [], 'showall');
346 if ($bulkoperations) {
347 echo '<br /><div class="buttons"><div class="form-inline">';
349 echo html_writer::start_tag('div', array('class' => 'btn-group'));
351 if ($participanttable->get_page_size() < $participanttable->totalrows) {
352 // Select all users, refresh table showing all users and mark them all selected.
353 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
354 echo html_writer::empty_tag('input', [
355 'type' => 'button',
356 'id' => 'checkall',
357 'class' => 'btn btn-secondary',
358 'value' => $label,
359 'data-target-page-size' => $participanttable->totalrows,
362 echo html_writer::end_tag('div');
363 $displaylist = array();
364 if (!empty($CFG->messaging)) {
365 $displaylist['#messageselect'] = get_string('messageselectadd');
367 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
368 $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
371 $params = ['operation' => 'download_participants'];
373 $downloadoptions = [];
374 $formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
375 foreach ($formats as $format) {
376 if ($format->is_enabled()) {
377 $params = ['operation' => 'download_participants', 'dataformat' => $format->name];
378 $url = new moodle_url('bulkchange.php', $params);
379 $downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
383 if (!empty($downloadoptions)) {
384 $displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
387 if ($context->id != $frontpagectx->id) {
388 $instances = $manager->get_enrolment_instances();
389 $plugins = $manager->get_enrolment_plugins(false);
390 foreach ($instances as $key => $instance) {
391 if (!isset($plugins[$instance->enrol])) {
392 // Weird, some broken stuff in plugin.
393 continue;
395 $plugin = $plugins[$instance->enrol];
396 $bulkoperations = $plugin->get_bulk_operations($manager);
398 $pluginoptions = [];
399 foreach ($bulkoperations as $key => $bulkoperation) {
400 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
401 $url = new moodle_url('bulkchange.php', $params);
402 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
404 if (!empty($pluginoptions)) {
405 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
406 $displaylist[] = [$name => $pluginoptions];
411 $selectactionparams = array(
412 'id' => 'formactionid',
413 'class' => 'ml-2',
414 'data-action' => 'toggle',
415 'data-togglegroup' => 'participants-table',
416 'data-toggle' => 'action',
417 'disabled' => 'disabled'
419 $label = html_writer::tag('label', get_string("withselectedusers"),
420 ['for' => 'formactionid', 'class' => 'col-form-label d-inline']);
421 $select = html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], $selectactionparams);
422 echo html_writer::tag('div', $label . $select);
424 echo '<input type="hidden" name="id" value="' . $course->id . '" />';
425 echo '<div class="d-none" data-region="state-help-icon">' . $OUTPUT->help_icon('publishstate', 'notes') . '</div>';
426 echo '</div></div></div>';
427 echo '</form>';
429 $options = (object) [
430 'uniqueid' => $participanttable->uniqueid,
431 'noteStateNames' => note_get_state_names(),
433 $PAGE->requires->js_call_amd('core_user/participants', 'init', [$options]);
436 echo '</div>'; // Userlist.
438 $enrolrenderer = $PAGE->get_renderer('core_enrol');
439 echo '<div class="float-right">';
440 // Need to re-generate the buttons to avoid having elements with duplicate ids on the page.
441 $enrolbuttons = $manager->get_manual_enrol_buttons();
442 foreach ($enrolbuttons as $enrolbutton) {
443 echo $enrolrenderer->render($enrolbutton);
445 echo '</div>';
447 if ($newcourse == 1) {
448 $str = get_string('proceedtocourse', 'enrol');
449 // The margin is to make it line up with the enrol users button when they are both on the same line.
450 $classes = 'my-1';
451 $url = course_get_url($course);
452 echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
455 echo $OUTPUT->footer();