Merge branch 'MDL-63375-38' of https://github.com/syxton/moodle into MOODLE_38_STABLE
[moodle.git] / user / index.php
blob6e6e5a8c01b1b046fbf675850c2a899901db23b5
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 define('DEFAULT_PAGE_SIZE', 20);
34 define('SHOW_ALL_PAGE_SIZE', 5000);
36 $page = optional_param('page', 0, PARAM_INT); // Which page to show.
37 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
38 $contextid = optional_param('contextid', 0, PARAM_INT); // One of this or.
39 $courseid = optional_param('id', 0, PARAM_INT); // This are required.
40 $newcourse = optional_param('newcourse', false, PARAM_BOOL);
41 $selectall = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
42 $roleid = optional_param('roleid', 0, PARAM_INT);
43 $groupparam = optional_param('group', 0, PARAM_INT);
45 $PAGE->set_url('/user/index.php', array(
46 'page' => $page,
47 'perpage' => $perpage,
48 'contextid' => $contextid,
49 'id' => $courseid,
50 'newcourse' => $newcourse));
52 if ($contextid) {
53 $context = context::instance_by_id($contextid, MUST_EXIST);
54 if ($context->contextlevel != CONTEXT_COURSE) {
55 print_error('invalidcontext');
57 $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
58 } else {
59 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
60 $context = context_course::instance($course->id, MUST_EXIST);
62 // Not needed anymore.
63 unset($contextid);
64 unset($courseid);
66 require_login($course);
68 $systemcontext = context_system::instance();
69 $isfrontpage = ($course->id == SITEID);
71 $frontpagectx = context_course::instance(SITEID);
73 if ($isfrontpage) {
74 $PAGE->set_pagelayout('admin');
75 course_require_view_participants($systemcontext);
76 } else {
77 $PAGE->set_pagelayout('incourse');
78 course_require_view_participants($context);
81 // Trigger events.
82 user_list_view($course, $context);
84 $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
86 $PAGE->set_title("$course->shortname: ".get_string('participants'));
87 $PAGE->set_heading($course->fullname);
88 $PAGE->set_pagetype('course-view-' . $course->format);
89 $PAGE->set_docs_path('enrol/users');
90 $PAGE->add_body_class('path-user'); // So we can style it independently.
91 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
93 // Expand the users node in the settings navigation when it exists because those pages
94 // are related to this one.
95 $node = $PAGE->settingsnav->find('users', navigation_node::TYPE_CONTAINER);
96 if ($node) {
97 $node->force_open();
100 echo $OUTPUT->header();
101 echo $OUTPUT->heading(get_string('participants'));
103 // Get the currently applied filters.
104 $filtersapplied = optional_param_array('unified-filters', [], PARAM_NOTAGS);
105 $filterwassubmitted = optional_param('unified-filter-submitted', 0, PARAM_BOOL);
107 // If they passed a role make sure they can view that role.
108 if ($roleid) {
109 $viewableroles = get_profile_roles($context);
111 // Check if the user can view this role.
112 if (array_key_exists($roleid, $viewableroles)) {
113 $filtersapplied[] = USER_FILTER_ROLE . ':' . $roleid;
114 } else {
115 $roleid = 0;
119 // Default group ID.
120 $groupid = false;
121 $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
122 if ($course->groupmode != NOGROUPS) {
123 if ($canaccessallgroups) {
124 // Change the group if the user can access all groups and has specified group in the URL.
125 if ($groupparam) {
126 $groupid = $groupparam;
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;
139 $hasgroupfilter = false;
140 $lastaccess = 0;
141 $searchkeywords = [];
142 $enrolid = 0;
143 $status = -1;
144 foreach ($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 = USER_FILTER_STRING;
153 $value = clean_param($filtervalue[0], PARAM_TEXT);
156 switch ($key) {
157 case USER_FILTER_ENROLMENT:
158 $enrolid = $value;
159 break;
160 case USER_FILTER_GROUP:
161 $groupid = $value;
162 $hasgroupfilter = true;
163 break;
164 case USER_FILTER_LAST_ACCESS:
165 $lastaccess = $value;
166 break;
167 case USER_FILTER_ROLE:
168 $roleid = $value;
169 break;
170 case USER_FILTER_STATUS:
171 // We only accept active/suspended statuses.
172 if ($value == ENROL_USER_ACTIVE || $value == ENROL_USER_SUSPENDED) {
173 $status = $value;
175 break;
176 default:
177 // Search string.
178 $searchkeywords[] = $value;
179 break;
182 // If course supports groups we may need to set a default.
183 if (!empty($groupid)) {
184 if ($canaccessallgroups) {
185 // User can access all groups, let them filter by whatever was selected.
186 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
187 } else if (!$filterwassubmitted && $course->groupmode == VISIBLEGROUPS) {
188 // If we are in a course with visible groups and the user has not submitted anything and does not have
189 // access to all groups, then set a default group.
190 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
191 } else if (!$hasgroupfilter && $course->groupmode != VISIBLEGROUPS) {
192 // The user can't access all groups and has not set a group filter in a course where the groups are not visible
193 // then apply a default group filter.
194 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
195 } else if (!$hasgroupfilter) { // No need for the group id to be set.
196 $groupid = false;
200 if ($groupid > 0 && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
201 $grouprenderer = $PAGE->get_renderer('core_group');
202 $groupdetailpage = new \core_group\output\group_details($groupid);
203 echo $grouprenderer->group_details($groupdetailpage);
206 // Manage enrolments.
207 $manager = new course_enrolment_manager($PAGE, $course);
208 $enrolbuttons = $manager->get_manual_enrol_buttons();
209 $enrolrenderer = $PAGE->get_renderer('core_enrol');
210 $enrolbuttonsout = '';
211 foreach ($enrolbuttons as $enrolbutton) {
212 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
214 echo html_writer::div($enrolbuttonsout, 'float-right');
216 // Should use this variable so that we don't break stuff every time a variable is added or changed.
217 $baseurl = new moodle_url('/user/index.php', array(
218 'contextid' => $context->id,
219 'id' => $course->id,
220 'perpage' => $perpage));
222 // Render the unified filter.
223 $renderer = $PAGE->get_renderer('core_user');
224 echo $renderer->unified_filter($course, $context, $filtersapplied, $baseurl);
226 echo '<div class="userlist">';
228 // Add filters to the baseurl after creating unified_filter to avoid losing them.
229 foreach (array_unique($filtersapplied) as $filterix => $filter) {
230 $baseurl->param('unified-filters[' . $filterix . ']', $filter);
232 $participanttable = new \core_user\participants_table($course->id, $groupid, $lastaccess, $roleid, $enrolid, $status,
233 $searchkeywords, $bulkoperations, $selectall);
234 $participanttable->define_baseurl($baseurl);
236 // Do this so we can get the total number of rows.
237 ob_start();
238 $participanttable->out($perpage, true);
239 $participanttablehtml = ob_get_contents();
240 ob_end_clean();
242 echo html_writer::tag('p', get_string('participantscount', 'moodle', $participanttable->totalrows));
244 if ($bulkoperations) {
245 echo '<form action="action_redir.php" method="post" id="participantsform">';
246 echo '<div>';
247 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
248 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
251 echo $participanttablehtml;
253 $perpageurl = clone($baseurl);
254 $perpageurl->remove_params('perpage');
255 if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
256 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
257 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
259 } else if ($participanttable->get_page_size() < $participanttable->totalrows) {
260 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
261 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $participanttable->totalrows)),
262 array(), 'showall');
265 if ($bulkoperations) {
266 echo '<br /><div class="buttons"><div class="form-inline">';
268 if ($participanttable->get_page_size() < $participanttable->totalrows) {
269 $perpageurl = clone($baseurl);
270 $perpageurl->remove_params('perpage');
271 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
272 $perpageurl->param('selectall', true);
273 $showalllink = $perpageurl;
274 } else {
275 $showalllink = false;
278 echo html_writer::start_tag('div', array('class' => 'btn-group'));
279 if ($participanttable->get_page_size() < $participanttable->totalrows) {
280 // Select all users, refresh page showing all users and mark them all selected.
281 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
282 echo html_writer::empty_tag('input', array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
283 'value' => $label, 'data-showallink' => $showalllink));
285 echo html_writer::end_tag('div');
286 $displaylist = array();
287 if (!empty($CFG->messaging) && has_all_capabilities(['moodle/site:sendmessage', 'moodle/course:bulkmessaging'], $context)) {
288 $displaylist['#messageselect'] = get_string('messageselectadd');
290 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
291 $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
294 $params = ['operation' => 'download_participants'];
296 $downloadoptions = [];
297 $formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
298 foreach ($formats as $format) {
299 if ($format->is_enabled()) {
300 $params = ['operation' => 'download_participants', 'dataformat' => $format->name];
301 $url = new moodle_url('bulkchange.php', $params);
302 $downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
306 if (!empty($downloadoptions)) {
307 $displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
310 if ($context->id != $frontpagectx->id) {
311 $instances = $manager->get_enrolment_instances();
312 $plugins = $manager->get_enrolment_plugins(false);
313 foreach ($instances as $key => $instance) {
314 if (!isset($plugins[$instance->enrol])) {
315 // Weird, some broken stuff in plugin.
316 continue;
318 $plugin = $plugins[$instance->enrol];
319 $bulkoperations = $plugin->get_bulk_operations($manager);
321 $pluginoptions = [];
322 foreach ($bulkoperations as $key => $bulkoperation) {
323 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
324 $url = new moodle_url('bulkchange.php', $params);
325 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
327 if (!empty($pluginoptions)) {
328 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
329 $displaylist[] = [$name => $pluginoptions];
334 $selectactionparams = array(
335 'id' => 'formactionid',
336 'class' => 'ml-2',
337 'data-action' => 'toggle',
338 'data-togglegroup' => 'participants-table',
339 'data-toggle' => 'action',
340 'disabled' => empty($selectall)
342 $label = html_writer::tag('label', get_string("withselectedusers"),
343 ['for' => 'formactionid', 'class' => 'col-form-label d-inline']);
344 $select = html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], $selectactionparams);
345 echo html_writer::tag('div', $label . $select);
347 echo '<input type="hidden" name="id" value="' . $course->id . '" />';
348 echo '</div></div></div>';
349 echo '</form>';
351 $options = new stdClass();
352 $options->courseid = $course->id;
353 $options->noteStateNames = note_get_state_names();
354 $options->stateHelpIcon = $OUTPUT->help_icon('publishstate', 'notes');
355 $PAGE->requires->js_call_amd('core_user/participants', 'init', [$options]);
358 echo '</div>'; // Userlist.
360 $enrolrenderer = $PAGE->get_renderer('core_enrol');
361 echo '<div class="float-right">';
362 // Need to re-generate the buttons to avoid having elements with duplicate ids on the page.
363 $enrolbuttons = $manager->get_manual_enrol_buttons();
364 foreach ($enrolbuttons as $enrolbutton) {
365 echo $enrolrenderer->render($enrolbutton);
367 echo '</div>';
369 if ($newcourse == 1) {
370 $str = get_string('proceedtocourse', 'enrol');
371 // The margin is to make it line up with the enrol users button when they are both on the same line.
372 $classes = 'my-1';
373 $url = course_get_url($course);
374 echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
377 echo $OUTPUT->footer();