MDL-68148 core_user: avoid multiple enrol buttons with the same id
[moodle.git] / user / index.php
blobdf921baa32c049d49baea57042c6fd9bf8dadce7
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;
36 use core_user\participants_table;
38 define('DEFAULT_PAGE_SIZE', 20);
39 define('SHOW_ALL_PAGE_SIZE', 5000);
41 $page = optional_param('page', 0, PARAM_INT); // Which page to show.
42 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
43 $contextid = optional_param('contextid', 0, PARAM_INT); // One of this or.
44 $courseid = optional_param('id', 0, PARAM_INT); // This are required.
45 $newcourse = optional_param('newcourse', false, PARAM_BOOL);
46 $selectall = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
47 $roleid = optional_param('roleid', 0, PARAM_INT);
48 $groupparam = optional_param('group', 0, PARAM_INT);
50 $PAGE->set_url('/user/index.php', array(
51 'page' => $page,
52 'perpage' => $perpage,
53 'contextid' => $contextid,
54 'id' => $courseid,
55 'newcourse' => $newcourse));
57 if ($contextid) {
58 $context = context::instance_by_id($contextid, MUST_EXIST);
59 if ($context->contextlevel != CONTEXT_COURSE) {
60 print_error('invalidcontext');
62 $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
63 } else {
64 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
65 $context = context_course::instance($course->id, MUST_EXIST);
67 // Not needed anymore.
68 unset($contextid);
69 unset($courseid);
71 require_login($course);
73 $systemcontext = context_system::instance();
74 $isfrontpage = ($course->id == SITEID);
76 $frontpagectx = context_course::instance(SITEID);
78 if ($isfrontpage) {
79 $PAGE->set_pagelayout('admin');
80 course_require_view_participants($systemcontext);
81 } else {
82 $PAGE->set_pagelayout('incourse');
83 course_require_view_participants($context);
86 // Trigger events.
87 user_list_view($course, $context);
89 $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
91 $PAGE->set_title("$course->shortname: ".get_string('participants'));
92 $PAGE->set_heading($course->fullname);
93 $PAGE->set_pagetype('course-view-' . $course->format);
94 $PAGE->add_body_class('path-user'); // So we can style it independently.
95 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
97 // Expand the users node in the settings navigation when it exists because those pages
98 // are related to this one.
99 $node = $PAGE->settingsnav->find('users', navigation_node::TYPE_CONTAINER);
100 if ($node) {
101 $node->force_open();
104 echo $OUTPUT->header();
105 echo $OUTPUT->heading(get_string('participants'));
107 // Get the currently applied filters.
108 $filtersapplied = optional_param_array('unified-filters', [], PARAM_NOTAGS);
109 $filterwassubmitted = optional_param('unified-filter-submitted', 0, PARAM_BOOL);
111 // If they passed a role make sure they can view that role.
112 if ($roleid) {
113 $viewableroles = get_profile_roles($context);
115 // Check if the user can view this role.
116 if (array_key_exists($roleid, $viewableroles)) {
117 $filtersapplied[] = USER_FILTER_ROLE . ':' . $roleid;
118 } else {
119 $roleid = 0;
123 // Default group ID.
124 $groupid = false;
125 $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
126 if ($course->groupmode != NOGROUPS) {
127 if ($canaccessallgroups) {
128 // Change the group if the user can access all groups and has specified group in the URL.
129 if ($groupparam) {
130 $groupid = $groupparam;
132 } else {
133 // Otherwise, get the user's default group.
134 $groupid = groups_get_course_group($course, true);
135 if ($course->groupmode == SEPARATEGROUPS && !$groupid) {
136 // The user is not in the group so show message and exit.
137 echo $OUTPUT->notification(get_string('notingroup'));
138 echo $OUTPUT->footer();
139 exit;
143 $hasgroupfilter = false;
144 $lastaccess = 0;
145 $searchkeywords = [];
146 $enrolid = 0;
148 $filterset = new \core_user\table\participants_filterset();
149 $filterset->add_filter(new integer_filter('courseid', filter::JOINTYPE_DEFAULT, [(int)$course->id]));
150 $enrolfilter = new integer_filter('enrolments');
151 $groupfilter = new integer_filter('groups');
152 $keywordfilter = new string_filter('keywords');
153 $lastaccessfilter = new integer_filter('accesssince');
154 $rolefilter = new integer_filter('roles');
155 $statusfilter = new integer_filter('status');
157 foreach ($filtersapplied as $filter) {
158 $filtervalue = explode(':', $filter, 2);
159 $value = null;
160 if (count($filtervalue) == 2) {
161 $key = clean_param($filtervalue[0], PARAM_INT);
162 $value = clean_param($filtervalue[1], PARAM_INT);
163 } else {
164 // Search string.
165 $key = USER_FILTER_STRING;
166 $value = clean_param($filtervalue[0], PARAM_TEXT);
169 switch ($key) {
170 case USER_FILTER_ENROLMENT:
171 $enrolid = $value;
172 $enrolfilter->add_filter_value($value);
173 break;
174 case USER_FILTER_GROUP:
175 $groupid = $value;
176 $groupfilter->add_filter_value($value);
177 $hasgroupfilter = true;
178 break;
179 case USER_FILTER_LAST_ACCESS:
180 $lastaccess = $value;
181 $lastaccessfilter->add_filter_value($value);
182 break;
183 case USER_FILTER_ROLE:
184 $roleid = $value;
185 $rolefilter->add_filter_value($value);
186 break;
187 case USER_FILTER_STATUS:
188 // We only accept active/suspended statuses.
189 if ($value == ENROL_USER_ACTIVE || $value == ENROL_USER_SUSPENDED) {
190 $status = $value;
191 $statusfilter->add_filter_value($value);
193 break;
194 default:
195 // Search string.
196 $searchkeywords[] = $value;
197 $keywordfilter->add_filter_value($value);
198 break;
201 // If course supports groups we may need to set a default.
202 if (!empty($groupid)) {
203 if ($canaccessallgroups) {
204 // User can access all groups, let them filter by whatever was selected.
205 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
206 $groupfilter->add_filter_value((int)$groupid);
207 } else if (!$filterwassubmitted && $course->groupmode == VISIBLEGROUPS) {
208 // If we are in a course with visible groups and the user has not submitted anything and does not have
209 // access to all groups, then set a default group.
210 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
211 $groupfilter->add_filter_value((int)$groupid);
212 } else if (!$hasgroupfilter && $course->groupmode != VISIBLEGROUPS) {
213 // The user can't access all groups and has not set a group filter in a course where the groups are not visible
214 // then apply a default group filter.
215 $filtersapplied[] = USER_FILTER_GROUP . ':' . $groupid;
216 $groupfilter->add_filter_value((int)$groupid);
217 } else if (!$hasgroupfilter) { // No need for the group id to be set.
218 $groupid = false;
222 if ($groupid > 0 && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
223 $grouprenderer = $PAGE->get_renderer('core_group');
224 $groupdetailpage = new \core_group\output\group_details($groupid);
225 echo $grouprenderer->group_details($groupdetailpage);
228 // Manage enrolments.
229 $manager = new course_enrolment_manager($PAGE, $course);
230 $enrolbuttons = $manager->get_manual_enrol_buttons();
231 $enrolrenderer = $PAGE->get_renderer('core_enrol');
232 $enrolbuttonsout = '';
233 foreach ($enrolbuttons as $enrolbutton) {
234 $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
236 echo html_writer::div($enrolbuttonsout, 'float-right');
238 // Should use this variable so that we don't break stuff every time a variable is added or changed.
239 $baseurl = new moodle_url('/user/index.php', array(
240 'contextid' => $context->id,
241 'id' => $course->id,
242 'perpage' => $perpage));
244 // Render the unified filter.
245 $renderer = $PAGE->get_renderer('core_user');
246 echo $renderer->unified_filter($course, $context, $filtersapplied, $baseurl);
248 echo '<div class="userlist">';
250 // Add filters to the baseurl after creating unified_filter to avoid losing them.
251 foreach (array_unique($filtersapplied) as $filterix => $filter) {
252 $baseurl->param('unified-filters[' . $filterix . ']', $filter);
255 if (count($groupfilter)) {
256 $filterset->add_filter($groupfilter);
259 if (count($lastaccessfilter)) {
260 $filterset->add_filter($lastaccessfilter);
263 if (count($rolefilter)) {
264 $filterset->add_filter($rolefilter);
267 if (count($enrolfilter)) {
268 $filterset->add_filter($enrolfilter);
271 if (count($statusfilter)) {
272 $filterset->add_filter($statusfilter);
275 if (count($keywordfilter)) {
276 $filterset->add_filter($keywordfilter);
279 $participanttable = new participants_table(participants_table::get_unique_id_from_argument($course->id));
280 $participanttable->set_selectall($selectall);
281 $participanttable->set_filterset($filterset);
282 $participanttable->define_baseurl($baseurl);
284 // Do this so we can get the total number of rows.
285 ob_start();
286 $participanttable->out($perpage, true);
287 $participanttablehtml = ob_get_contents();
288 ob_end_clean();
290 echo html_writer::tag('p', get_string('participantscount', 'moodle', $participanttable->totalrows));
292 if ($bulkoperations) {
293 echo '<form action="action_redir.php" method="post" id="participantsform">';
294 echo '<div>';
295 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
296 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
299 echo $participanttablehtml;
301 $perpageurl = clone($baseurl);
302 $perpageurl->remove_params('perpage');
303 if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
304 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
305 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
307 } else if ($participanttable->get_page_size() < $participanttable->totalrows) {
308 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
309 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $participanttable->totalrows)),
310 array(), 'showall');
313 if ($bulkoperations) {
314 echo '<br /><div class="buttons"><div class="form-inline">';
316 if ($participanttable->get_page_size() < $participanttable->totalrows) {
317 $perpageurl = clone($baseurl);
318 $perpageurl->remove_params('perpage');
319 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
320 $perpageurl->param('selectall', true);
321 $showalllink = $perpageurl;
322 } else {
323 $showalllink = false;
326 echo html_writer::start_tag('div', array('class' => 'btn-group'));
327 if ($participanttable->get_page_size() < $participanttable->totalrows) {
328 // Select all users, refresh page showing all users and mark them all selected.
329 $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
330 echo html_writer::tag('input', "", array('type' => 'button', 'id' => 'checkall', 'class' => 'btn btn-secondary',
331 'value' => $label, 'data-showallink' => $showalllink));
333 echo html_writer::end_tag('div');
334 $displaylist = array();
335 if (!empty($CFG->messaging)) {
336 $displaylist['#messageselect'] = get_string('messageselectadd');
338 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
339 $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
342 $params = ['operation' => 'download_participants'];
344 $downloadoptions = [];
345 $formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
346 foreach ($formats as $format) {
347 if ($format->is_enabled()) {
348 $params = ['operation' => 'download_participants', 'dataformat' => $format->name];
349 $url = new moodle_url('bulkchange.php', $params);
350 $downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
354 if (!empty($downloadoptions)) {
355 $displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
358 if ($context->id != $frontpagectx->id) {
359 $instances = $manager->get_enrolment_instances();
360 $plugins = $manager->get_enrolment_plugins(false);
361 foreach ($instances as $key => $instance) {
362 if (!isset($plugins[$instance->enrol])) {
363 // Weird, some broken stuff in plugin.
364 continue;
366 $plugin = $plugins[$instance->enrol];
367 $bulkoperations = $plugin->get_bulk_operations($manager);
369 $pluginoptions = [];
370 foreach ($bulkoperations as $key => $bulkoperation) {
371 $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
372 $url = new moodle_url('bulkchange.php', $params);
373 $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
375 if (!empty($pluginoptions)) {
376 $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
377 $displaylist[] = [$name => $pluginoptions];
382 $selectactionparams = array(
383 'id' => 'formactionid',
384 'class' => 'ml-2',
385 'data-action' => 'toggle',
386 'data-togglegroup' => 'participants-table',
387 'data-toggle' => 'action',
388 'disabled' => empty($selectall)
390 echo html_writer::tag('div', html_writer::tag('label', get_string("withselectedusers"),
391 array('for' => 'formactionid', 'class' => 'col-form-label d-inline')) .
392 html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), $selectactionparams));
394 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
395 echo '<noscript style="display:inline">';
396 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
397 echo '</noscript>';
398 echo '</div></div></div>';
399 echo '</form>';
401 $options = new stdClass();
402 $options->courseid = $course->id;
403 $options->noteStateNames = note_get_state_names();
404 $options->stateHelpIcon = $OUTPUT->help_icon('publishstate', 'notes');
405 $PAGE->requires->js_call_amd('core_user/participants', 'init', [$options]);
408 echo '</div>'; // Userlist.
410 $enrolrenderer = $PAGE->get_renderer('core_enrol');
411 echo '<div class="float-right">';
412 // Need to re-generate the buttons to avoid having elements with duplicate ids on the page.
413 $enrolbuttons = $manager->get_manual_enrol_buttons();
414 foreach ($enrolbuttons as $enrolbutton) {
415 echo $enrolrenderer->render($enrolbutton);
417 echo '</div>';
419 if ($newcourse == 1) {
420 $str = get_string('proceedtocourse', 'enrol');
421 // The margin is to make it line up with the enrol users button when they are both on the same line.
422 $classes = 'my-1';
423 $url = course_get_url($course);
424 echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
427 echo $OUTPUT->footer();