MDL-62660 tool_dataprivacy: Add method for unit tests to run adhoc tasks
[moodle.git] / user / index.php
blobf36491cef554a9f8c2347925b7f87f74e662b9a5
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->add_body_class('path-user'); // So we can style it independently.
90 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
92 // Expand the users node in the settings navigation when it exists because those pages
93 // are related to this one.
94 $node = $PAGE->settingsnav->find('users', navigation_node::TYPE_CONTAINER);
95 if ($node) {
96 $node->force_open();
99 echo $OUTPUT->header();
100 echo $OUTPUT->heading(get_string('participants'));
102 // Get the currently applied filters.
103 $filtersapplied = optional_param_array('unified-filters', [], PARAM_NOTAGS);
104 $filterwassubmitted = optional_param('unified-filter-submitted', 0, PARAM_BOOL);
106 // If they passed a role make sure they can view that role.
107 if ($roleid) {
108 $viewableroles = get_profile_roles($context);
110 // Check if the user can view this role.
111 if (array_key_exists($roleid, $viewableroles)) {
112 $filtersapplied[] = USER_FILTER_ROLE . ':' . $roleid;
113 } else {
114 $roleid = 0;
118 // Default group ID.
119 $groupid = false;
120 $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
121 if ($course->groupmode != NOGROUPS) {
122 if ($canaccessallgroups) {
123 // Change the group if the user can access all groups and has specified group in the URL.
124 if ($groupparam) {
125 $groupid = $groupparam;
127 } else {
128 // Otherwise, get the user's default group.
129 $groupid = groups_get_course_group($course, true);
130 if ($course->groupmode == SEPARATEGROUPS && !$groupid) {
131 // The user is not in the group so show message and exit.
132 echo $OUTPUT->notification(get_string('notingroup'));
133 echo $OUTPUT->footer();
134 exit;
138 $hasgroupfilter = false;
139 $lastaccess = 0;
140 $searchkeywords = [];
141 $enrolid = 0;
142 $status = -1;
143 foreach ($filtersapplied as $filter) {
144 $filtervalue = explode(':', $filter, 2);
145 $value = null;
146 if (count($filtervalue) == 2) {
147 $key = clean_param($filtervalue[0], PARAM_INT);
148 $value = clean_param($filtervalue[1], PARAM_INT);
149 } else {
150 // Search string.
151 $key = USER_FILTER_STRING;
152 $value = 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 $searchkeywords[] = $value;
178 break;
181 // If course supports groups we may need to set a default.
182 if (!empty($groupid)) {
183 if ($canaccessallgroups) {
184 // User can access all groups, let them filter by whatever was selected.
185 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
186 } else if (!$filterwassubmitted && $course->groupmode == VISIBLEGROUPS) {
187 // If we are in a course with visible groups and the user has not submitted anything and does not have
188 // access to all groups, then set a default group.
189 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
190 } else if (!$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 if ($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);
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 = '';
210 foreach ($enrolbuttons as $enrolbutton) {
211 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
213 echo html_writer::div($enrolbuttonsout, 'pull-right');
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 // Render the unified filter.
222 $renderer = $PAGE->get_renderer('core_user');
223 echo $renderer->unified_filter($course, $context, $filtersapplied, $baseurl);
225 echo '<div class="userlist">';
227 // Add filters to the baseurl after creating unified_filter to avoid losing them.
228 foreach (array_unique($filtersapplied) as $filterix => $filter) {
229 $baseurl->param('unified-filters[' . $filterix . ']', $filter);
231 $participanttable = new \core_user\participants_table($course->id, $groupid, $lastaccess, $roleid, $enrolid, $status,
232 $searchkeywords, $bulkoperations, $selectall);
233 $participanttable->define_baseurl($baseurl);
235 // Do this so we can get the total number of rows.
236 ob_start();
237 $participanttable->out($perpage, true);
238 $participanttablehtml = ob_get_contents();
239 ob_end_clean();
241 echo html_writer::tag('p', get_string('participantscount', 'moodle', $participanttable->totalrows));
243 if ($bulkoperations) {
244 echo '<form action="action_redir.php" method="post" id="participantsform">';
245 echo '<div>';
246 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
247 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
250 echo $participanttablehtml;
252 $perpageurl = clone($baseurl);
253 $perpageurl->remove_params('perpage');
254 if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
255 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
256 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
258 } else if ($participanttable->get_page_size() < $participanttable->totalrows) {
259 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
260 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $participanttable->totalrows)),
261 array(), 'showall');
264 if ($bulkoperations) {
265 echo '<br /><div class="buttons">';
267 if ($participanttable->get_page_size() < $participanttable->totalrows) {
268 $perpageurl = clone($baseurl);
269 $perpageurl->remove_params('perpage');
270 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
271 $perpageurl->param('selectall', true);
272 $showalllink = $perpageurl;
273 } else {
274 $showalllink = false;
277 echo html_writer::start_tag('div', array('class' => 'btn-group'));
278 if ($participanttable->get_page_size() < $participanttable->totalrows) {
279 // Select all users, refresh page showing all users and mark them all selected.
280 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
281 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
282 'value' => $label, 'data-showallink' => $showalllink));
283 // Select all users, mark all users on page as selected.
284 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
285 'value' => get_string('selectallusersonpage')));
286 } else {
287 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkallonpage', 'class' => 'btn btn-secondary',
288 'value' => get_string('selectall')));
291 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checknone', 'class' => 'btn btn-secondary',
292 'value' => get_string('deselectall')));
293 echo html_writer::end_tag('div');
294 $displaylist = array();
295 $displaylist['#messageselect'] = get_string('messageselectadd');
296 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
297 $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
300 if ($context->id != $frontpagectx->id) {
301 $instances = $manager->get_enrolment_instances();
302 $plugins = $manager->get_enrolment_plugins(false);
303 foreach ($instances as $key => $instance) {
304 if (!isset($plugins[$instance->enrol])) {
305 // Weird, some broken stuff in plugin.
306 continue;
308 $plugin = $plugins[$instance->enrol];
309 $bulkoperations = $plugin->get_bulk_operations($manager);
311 $pluginoptions = [];
312 foreach ($bulkoperations as $key => $bulkoperation) {
313 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
314 $url = new moodle_url('bulkchange.php', $params);
315 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
317 if (!empty($pluginoptions)) {
318 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
319 $displaylist[] = [$name => $pluginoptions];
324 echo $OUTPUT->help_icon('withselectedusers');
325 echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
326 echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
328 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
329 echo '<noscript style="display:inline">';
330 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
331 echo '</noscript>';
332 echo '</div></div>';
333 echo '</form>';
335 $options = new stdClass();
336 $options->courseid = $course->id;
337 $options->noteStateNames = note_get_state_names();
338 $options->stateHelpIcon = $OUTPUT->help_icon('publishstate', 'notes');
339 $PAGE->requires->js_call_amd('core_user/participants', 'init', [$options]);
342 echo '</div>'; // Userlist.
344 $enrolrenderer = $PAGE->get_renderer('core_enrol');
345 echo '<div class="pull-right">';
346 foreach ($enrolbuttons as $enrolbutton) {
347 echo $enrolrenderer->render($enrolbutton);
349 echo '</div>';
351 if ($newcourse == 1) {
352 $str = get_string('proceedtocourse', 'enrol');
353 // Floated left so it goes under the enrol users button on mobile.
354 // The margin is to make it line up with the enrol users button when they are both on the same line.
355 $classes = 'm-y-1 pull-xs-left';
356 $url = course_get_url($course);
357 echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
360 echo $OUTPUT->footer();