Merge branch 'MDL-59388-master' of git://github.com/lameze/moodle
[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');
a2ed6e69
SH
27require_once($CFG->libdir.'/tablelib.php');
28require_once($CFG->libdir.'/filelib.php');
a78ed71c 29require_once($CFG->dirroot.'/enrol/locallib.php');
a2ed6e69 30
a2ed6e69
SH
31define('DEFAULT_PAGE_SIZE', 20);
32define('SHOW_ALL_PAGE_SIZE', 5000);
9651e491
JP
33define('USER_FILTER_ENROLMENT', 1);
34define('USER_FILTER_GROUP', 2);
35define('USER_FILTER_LAST_ACCESS', 3);
36define('USER_FILTER_ROLE', 4);
37define('USER_FILTER_STATUS', 5);
a2ed6e69
SH
38
39$page = optional_param('page', 0, PARAM_INT); // Which page to show.
40$perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
a2ed6e69
SH
41$contextid = optional_param('contextid', 0, PARAM_INT); // One of this or.
42$courseid = optional_param('id', 0, PARAM_INT); // This are required.
5b7c500a 43$selectall = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
a2ed6e69
SH
44
45$PAGE->set_url('/user/index.php', array(
46 'page' => $page,
47 'perpage' => $perpage,
a2ed6e69
SH
48 'contextid' => $contextid,
49 'id' => $courseid));
50
51if ($contextid) {
52 $context = context::instance_by_id($contextid, MUST_EXIST);
53 if ($context->contextlevel != CONTEXT_COURSE) {
54 print_error('invalidcontext');
55 }
56 $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
57} else {
58 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
59 $context = context_course::instance($course->id, MUST_EXIST);
60}
61// Not needed anymore.
62unset($contextid);
63unset($courseid);
f9903ed0 64
a2ed6e69 65require_login($course);
f9903ed0 66
a2ed6e69
SH
67$systemcontext = context_system::instance();
68$isfrontpage = ($course->id == SITEID);
f9903ed0 69
a2ed6e69 70$frontpagectx = context_course::instance(SITEID);
4f0c2d00 71
a2ed6e69
SH
72if ($isfrontpage) {
73 $PAGE->set_pagelayout('admin');
74 require_capability('moodle/site:viewparticipants', $systemcontext);
75} else {
76 $PAGE->set_pagelayout('incourse');
77 require_capability('moodle/course:viewparticipants', $context);
78}
224aa44a 79
0ff203b6
JL
80// Trigger events.
81user_list_view($course, $context);
a2ed6e69
SH
82
83$bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
84
a2ed6e69
SH
85$PAGE->set_title("$course->shortname: ".get_string('participants'));
86$PAGE->set_heading($course->fullname);
87$PAGE->set_pagetype('course-view-' . $course->format);
88$PAGE->add_body_class('path-user'); // So we can style it independently.
89$PAGE->set_other_editing_capability('moodle/course:manageactivities');
2cb2ce61 90
a2ed6e69 91echo $OUTPUT->header();
4e1f6047 92echo $OUTPUT->heading(get_string('participants'));
caa8363f 93
9651e491
JP
94// Get the currently applied filters.
95$filtersapplied = optional_param_array('unified-filters', [], PARAM_TEXT);
a78ed71c 96
9651e491
JP
97// Default group ID.
98$groupid = false;
99$canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
100if ($course->groupmode != NOGROUPS) {
101 if ($canaccessallgroups) {
102 // If the user can see all groups, set default to 0.
103 $groupid = 0;
104 } else {
105 // Otherwise, get the user's default group.
106 $groupid = groups_get_course_group($course, true);
107 if ($course->groupmode == SEPARATEGROUPS && !$groupid) {
108 // The user is not in the group so show message and exit.
109 echo $OUTPUT->notification(get_string('notingroup'));
110 echo $OUTPUT->footer();
111 exit;
112 }
113 }
a2ed6e69 114}
9651e491
JP
115$hasgroupfilter = false;
116$lastaccess = 0;
117$searchkeywords = [];
118$roleid = 0;
119$enrolid = 0;
120$status = -1;
121foreach ($filtersapplied as $filter) {
122 $filtervalue = explode(':', $filter, 2);
123 $value = null;
124 if (count($filtervalue) == 2) {
125 $key = clean_param($filtervalue[0], PARAM_INT);
126 $value = clean_param($filtervalue[1], PARAM_INT);
127 } else {
128 // Search string.
129 $key = clean_param($filtervalue[0], PARAM_TEXT);
130 }
99cca847 131
9651e491
JP
132 switch ($key) {
133 case USER_FILTER_ENROLMENT:
134 $enrolid = $value;
135 break;
136 case USER_FILTER_GROUP:
137 $groupid = $value;
138 $hasgroupfilter = true;
139 break;
140 case USER_FILTER_LAST_ACCESS:
141 $lastaccess = $value;
142 break;
143 case USER_FILTER_ROLE:
144 $roleid = $value;
145 break;
146 case USER_FILTER_STATUS:
147 // We only accept active/suspended statuses.
148 if ($value == ENROL_USER_ACTIVE || $value == ENROL_USER_SUSPENDED) {
149 $status = $value;
150 }
151 break;
152 default:
153 // Search string.
154 if (!empty($key) && empty($value)) {
155 $searchkeywords[] = $key;
156 }
157 break;
158 }
a2ed6e69 159}
03d9401e 160
9651e491
JP
161// If course supports groups, but the user can't access all groups and there's no group filter set, apply a default group filter.
162if ($groupid !== false && !$canaccessallgroups && !$hasgroupfilter) {
163 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
24c3db91 164}
a2ed6e69 165
9651e491
JP
166// Manage enrolments.
167$manager = new course_enrolment_manager($PAGE, $course);
168$enrolbuttons = $manager->get_manual_enrol_buttons();
169$enrolrenderer = $PAGE->get_renderer('core_enrol');
170$enrolbuttonsout = '';
171foreach ($enrolbuttons as $enrolbutton) {
172 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
bc47b706 173}
9651e491 174echo html_writer::div($enrolbuttonsout, 'pull-right');
bc47b706 175
9651e491
JP
176// Render the unified filter.
177$renderer = $PAGE->get_renderer('core_user');
178echo $renderer->unified_filter($course, $context, $filtersapplied);
87a13824 179
9651e491 180echo '<div class="userlist">';
87a13824 181
9651e491
JP
182// Should use this variable so that we don't break stuff every time a variable is added or changed.
183$baseurl = new moodle_url('/user/index.php', array(
184 'contextid' => $context->id,
185 'id' => $course->id,
186 'perpage' => $perpage));
a2ed6e69 187
9651e491
JP
188$participanttable = new \core_user\participants_table($course->id, $groupid, $lastaccess, $roleid, $enrolid, $status,
189 $searchkeywords, $bulkoperations, $selectall);
bc47b706 190$participanttable->define_baseurl($baseurl);
77c645df 191
bc47b706
MN
192// Do this so we can get the total number of rows.
193ob_start();
194$participanttable->out($perpage, true);
195$participanttablehtml = ob_get_contents();
196ob_end_clean();
3e219038 197
a2ed6e69
SH
198if ($bulkoperations) {
199 echo '<form action="action_redir.php" method="post" id="participantsform">';
200 echo '<div>';
201 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
202 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
203}
77c645df 204
bc47b706 205echo $participanttablehtml;
4c7593ff 206
8dd42b38
AH
207$perpageurl = clone($baseurl);
208$perpageurl->remove_params('perpage');
bc47b706 209if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
8dd42b38
AH
210 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
211 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
212
bc47b706 213} else if ($participanttable->get_page_size() < $participanttable->totalrows) {
8dd42b38 214 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
bc47b706
MN
215 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $participanttable->totalrows)),
216 array(), 'showall');
8dd42b38
AH
217}
218
a2ed6e69
SH
219if ($bulkoperations) {
220 echo '<br /><div class="buttons">';
5b7c500a 221
bc47b706 222 if ($participanttable->get_page_size() < $participanttable->totalrows) {
5b7c500a
AH
223 $perpageurl = clone($baseurl);
224 $perpageurl->remove_params('perpage');
225 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
226 $perpageurl->param('selectall', true);
227 $showalllink = $perpageurl;
228 } else {
229 $showalllink = false;
230 }
231
b27c8d81 232 echo html_writer::start_tag('div', array('class' => 'btn-group'));
bc47b706 233 if ($participanttable->get_page_size() < $participanttable->totalrows) {
5b7c500a 234 // Select all users, refresh page showing all users and mark them all selected.
bc47b706 235 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
b27c8d81
SB
236 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
237 'value' => $label, 'data-showallink' => $showalllink));
5b7c500a 238 // Select all users, mark all users on page as selected.
b27c8d81
SB
239 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
240 'value' => get_string('selectallusersonpage')));
5b7c500a 241 } else {
b27c8d81
SB
242 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
243 'value' => get_string('selectall')));
5b7c500a
AH
244 }
245
b27c8d81
SB
246 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checknone', 'class' => 'btn btn-secondary',
247 'value' => get_string('deselectall')));
248 echo html_writer::end_tag('div');
a2ed6e69
SH
249 $displaylist = array();
250 $displaylist['messageselect.php'] = get_string('messageselectadd');
251 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
252 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
253 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
254 }
255
689ccae5
DW
256 $plugins = $manager->get_enrolment_plugins();
257 foreach ($plugins as $plugin) {
258 $bulkoperations = $plugin->get_bulk_operations($manager);
259
260 $pluginoptions = [];
261 foreach ($bulkoperations as $key => $bulkoperation) {
262 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
263 $url = new moodle_url('bulkchange.php', $params);
264 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
265 }
266 if (!empty($pluginoptions)) {
267 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
268 $displaylist[] = [$name => $pluginoptions];
269 }
270 }
271
a2ed6e69
SH
272 echo $OUTPUT->help_icon('withselectedusers');
273 echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
274 echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
275
276 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
277 echo '<noscript style="display:inline">';
278 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
279 echo '</noscript>';
280 echo '</div></div>';
281 echo '</form>';
282
283 $module = array('name' => 'core_user', 'fullpath' => '/user/module.js');
284 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
285}
b90e2f19 286
a2ed6e69 287echo '</div>'; // Userlist.
f9903ed0 288
a78ed71c
DW
289$enrolrenderer = $PAGE->get_renderer('core_enrol');
290echo '<div class="pull-right">';
291foreach ($enrolbuttons as $enrolbutton) {
292 echo $enrolrenderer->render($enrolbutton);
293}
294echo '</div>';
295
a2ed6e69 296echo $OUTPUT->footer();