Merge branch 'MDL-60489-master' of git://github.com/ryanwyllie/moodle
[moodle.git] / user / index.php
blobef2de01dac63a7cd12461c125505acecb95a03d1
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->libdir.'/tablelib.php');
29 require_once($CFG->libdir.'/filelib.php');
30 require_once($CFG->dirroot.'/enrol/locallib.php');
32 define('DEFAULT_PAGE_SIZE', 20);
33 define('SHOW_ALL_PAGE_SIZE', 5000);
34 define('USER_FILTER_ENROLMENT', 1);
35 define('USER_FILTER_GROUP', 2);
36 define('USER_FILTER_LAST_ACCESS', 3);
37 define('USER_FILTER_ROLE', 4);
38 define('USER_FILTER_STATUS', 5);
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 $selectall = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
46 $roleid = optional_param('roleid', 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_TEXT);
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 // 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;
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 = clean_param($filtervalue[0], PARAM_TEXT);
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;
174 break;
175 default:
176 // Search string.
177 if (!empty($key) && empty($value)) {
178 $searchkeywords[] = $key;
180 break;
184 // If course supports groups we may need to set a default.
185 if ($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;
199 // Manage enrolments.
200 $manager = new course_enrolment_manager($PAGE, $course);
201 $enrolbuttons = $manager->get_manual_enrol_buttons();
202 $enrolrenderer = $PAGE->get_renderer('core_enrol');
203 $enrolbuttonsout = '';
204 foreach ($enrolbuttons as $enrolbutton) {
205 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
207 echo html_writer::div($enrolbuttonsout, 'pull-right');
209 // Render the unified filter.
210 $renderer = $PAGE->get_renderer('core_user');
211 echo $renderer->unified_filter($course, $context, $filtersapplied);
213 echo '<div class="userlist">';
215 // Should use this variable so that we don't break stuff every time a variable is added or changed.
216 $baseurl = new moodle_url('/user/index.php', array(
217 'contextid' => $context->id,
218 'id' => $course->id,
219 'perpage' => $perpage));
221 $participanttable = new \core_user\participants_table($course->id, $groupid, $lastaccess, $roleid, $enrolid, $status,
222 $searchkeywords, $bulkoperations, $selectall);
223 $participanttable->define_baseurl($baseurl);
225 // Do this so we can get the total number of rows.
226 ob_start();
227 $participanttable->out($perpage, true);
228 $participanttablehtml = ob_get_contents();
229 ob_end_clean();
231 if ($bulkoperations) {
232 echo '<form action="action_redir.php" method="post" id="participantsform">';
233 echo '<div>';
234 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
235 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
238 echo $participanttablehtml;
240 $perpageurl = clone($baseurl);
241 $perpageurl->remove_params('perpage');
242 if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
243 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
244 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
246 } else if ($participanttable->get_page_size() < $participanttable->totalrows) {
247 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
248 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $participanttable->totalrows)),
249 array(), 'showall');
252 if ($bulkoperations) {
253 echo '<br /><div class="buttons">';
255 if ($participanttable->get_page_size() < $participanttable->totalrows) {
256 $perpageurl = clone($baseurl);
257 $perpageurl->remove_params('perpage');
258 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
259 $perpageurl->param('selectall', true);
260 $showalllink = $perpageurl;
261 } else {
262 $showalllink = false;
265 echo html_writer::start_tag('div', array('class' => 'btn-group'));
266 if ($participanttable->get_page_size() < $participanttable->totalrows) {
267 // Select all users, refresh page showing all users and mark them all selected.
268 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
269 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
270 'value' => $label, 'data-showallink' => $showalllink));
271 // Select all users, mark all users on page as selected.
272 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
273 'value' => get_string('selectallusersonpage')));
274 } else {
275 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
276 'value' => get_string('selectall')));
279 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checknone', 'class' => 'btn btn-secondary',
280 'value' => get_string('deselectall')));
281 echo html_writer::end_tag('div');
282 $displaylist = array();
283 $displaylist['messageselect.php'] = get_string('messageselectadd');
284 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
285 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
286 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
289 if ($context->id != $frontpagectx->id) {
290 $instances = $manager->get_enrolment_instances();
291 $plugins = $manager->get_enrolment_plugins(false);
292 foreach ($instances as $key => $instance) {
293 if (!isset($plugins[$instance->enrol])) {
294 // Weird, some broken stuff in plugin.
295 continue;
297 $plugin = $plugins[$instance->enrol];
298 $bulkoperations = $plugin->get_bulk_operations($manager);
300 $pluginoptions = [];
301 foreach ($bulkoperations as $key => $bulkoperation) {
302 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
303 $url = new moodle_url('bulkchange.php', $params);
304 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
306 if (!empty($pluginoptions)) {
307 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
308 $displaylist[] = [$name => $pluginoptions];
313 echo $OUTPUT->help_icon('withselectedusers');
314 echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
315 echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
317 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
318 echo '<noscript style="display:inline">';
319 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
320 echo '</noscript>';
321 echo '</div></div>';
322 echo '</form>';
324 $module = array('name' => 'core_user', 'fullpath' => '/user/module.js');
325 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
328 echo '</div>'; // Userlist.
330 $enrolrenderer = $PAGE->get_renderer('core_enrol');
331 echo '<div class="pull-right">';
332 foreach ($enrolbuttons as $enrolbutton) {
333 echo $enrolrenderer->render($enrolbutton);
335 echo '</div>';
337 if ($newcourse == 1) {
338 $str = get_string('proceedtocourse', 'enrol');
339 // Floated left so it goes under the enrol users button on mobile.
340 // The margin is to make it line up with the enrol users button when they are both on the same line.
341 $classes = 'm-y-1 pull-xs-left';
342 $url = course_get_url($course);
343 echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
346 echo $OUTPUT->footer();