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