MDL-59366 core_user: Disable sorting for roles and groups columns
[moodle.git] / user / classes / participants_table.php
CommitLineData
bc47b706
MN
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.
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 * Contains the class used for the displaying the participants table.
19 *
20 * @package core_user
21 * @copyright 2017 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_user;
26
fb792967 27use context;
90ffcfa8 28use core_user\output\status_field;
fb792967 29use DateTime;
fb792967 30
bc47b706
MN
31defined('MOODLE_INTERNAL') || die;
32
33global $CFG;
34
35require_once($CFG->libdir . '/tablelib.php');
36require_once($CFG->dirroot . '/user/lib.php');
37
38/**
39 * Class for the displaying the participants table.
40 *
41 * @package core_user
42 * @copyright 2017 Mark Nelson <markn@moodle.com>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45class participants_table extends \table_sql {
46
47 /**
48 * @var int $courseid The course id
49 */
50 protected $courseid;
51
52 /**
53 * @var int|false False if groups not used, int if groups used, 0 for all groups.
54 */
55 protected $currentgroup;
56
57 /**
58 * @var int $accesssince The time the user last accessed the site
59 */
60 protected $accesssince;
61
62 /**
63 * @var int $roleid The role we are including, 0 means all enrolled users
64 */
65 protected $roleid;
66
9651e491
JP
67 /**
68 * @var int $enrolid The applied filter for the user enrolment ID.
69 */
70 protected $enrolid;
71
72 /**
73 * @var int $status The applied filter for the user's enrolment status.
74 */
75 protected $status;
76
bc47b706
MN
77 /**
78 * @var string $search The string being searched.
79 */
80 protected $search;
81
82 /**
83 * @var bool $selectall Has the user selected all users on the page?
84 */
85 protected $selectall;
86
87 /**
88 * @var string[] The list of countries.
89 */
90 protected $countries;
91
2fa35b8d 92 /**
f3ecea3a 93 * @var \stdClass[] The list of groups with membership info for the course.
2fa35b8d
DW
94 */
95 protected $groups;
96
bc47b706
MN
97 /**
98 * @var string[] Extra fields to display.
99 */
100 protected $extrafields;
101
2fa35b8d 102 /**
fb792967 103 * @var \stdClass $course The course details.
2fa35b8d
DW
104 */
105 protected $course;
106
107 /**
fb792967 108 * @var context $context The course context.
2fa35b8d
DW
109 */
110 protected $context;
111
73d0d562
DW
112 /**
113 * @var \stdClass[] List of roles indexed by roleid.
114 */
115 protected $allroles;
116
0ae28fad
DW
117 /**
118 * @var \stdClass[] List of roles indexed by roleid.
119 */
120 protected $allroleassignments;
121
73d0d562
DW
122 /**
123 * @var \stdClass[] Assignable roles in this course.
124 */
125 protected $assignableroles;
126
bc47b706
MN
127 /**
128 * Sets up the table.
129 *
130 * @param int $courseid
131 * @param int|false $currentgroup False if groups not used, int if groups used, 0 for all groups.
132 * @param int $accesssince The time the user last accessed the site
133 * @param int $roleid The role we are including, 0 means all enrolled users
9651e491
JP
134 * @param int $enrolid The applied filter for the user enrolment ID.
135 * @param int $status The applied filter for the user's enrolment status.
136 * @param string|array $search The search string(s)
bc47b706
MN
137 * @param bool $bulkoperations Is the user allowed to perform bulk operations?
138 * @param bool $selectall Has the user selected all users on the page?
139 */
9651e491 140 public function __construct($courseid, $currentgroup, $accesssince, $roleid, $enrolid, $status, $search,
bc47b706
MN
141 $bulkoperations, $selectall) {
142 global $CFG;
143
144 parent::__construct('user-index-participants-' . $courseid);
145
146 // Get the context.
2fa35b8d 147 $this->course = get_course($courseid);
bc47b706 148 $context = \context_course::instance($courseid, MUST_EXIST);
fb792967 149 $this->context = $context;
bc47b706
MN
150
151 // Define the headers and columns.
152 $headers = [];
153 $columns = [];
154
155 if ($bulkoperations) {
156 $headers[] = get_string('select');
157 $columns[] = 'select';
158 }
159
160 $headers[] = get_string('fullname');
161 $columns[] = 'fullname';
162
163 $extrafields = get_extra_user_fields($context);
164 foreach ($extrafields as $field) {
165 $headers[] = get_user_field_name($field);
166 $columns[] = $field;
167 }
168
5d0b4765
DW
169 $headers[] = get_string('roles');
170 $columns[] = 'roles';
171
2fa35b8d 172 // Load and cache the course groupinfo.
2fa35b8d
DW
173 // Add column for groups.
174 $headers[] = get_string('groups');
175 $columns[] = 'groups';
176
bc47b706
MN
177 // Get the list of fields we have to hide.
178 $hiddenfields = array();
179 if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
180 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
181 }
182
183 // Do not show the columns if it exists in the hiddenfields array.
bc47b706
MN
184 if (!isset($hiddenfields['lastaccess'])) {
185 if ($courseid == SITEID) {
186 $headers[] = get_string('lastsiteaccess');
187 } else {
188 $headers[] = get_string('lastcourseaccess');
189 }
190 $columns[] = 'lastaccess';
191 }
192
fb792967
JP
193 $canreviewenrol = has_capability('moodle/course:enrolreview', $context);
194 if ($canreviewenrol) {
195 $columns[] = 'status';
196 $headers[] = get_string('participationstatus', 'enrol');
197 $this->no_sorting('status');
198 };
199
bc47b706
MN
200 $this->define_columns($columns);
201 $this->define_headers($headers);
202
f7cd3bc0
JP
203 // Make this table sorted by first name by default.
204 $this->sortable(true, 'firstname');
205
bc47b706 206 $this->no_sorting('select');
cd3f250f
JP
207 $this->no_sorting('roles');
208 $this->no_sorting('groups');
bc47b706
MN
209
210 $this->set_attribute('id', 'participants');
211
212 // Set the variables we need to use later.
bc47b706
MN
213 $this->currentgroup = $currentgroup;
214 $this->accesssince = $accesssince;
215 $this->roleid = $roleid;
216 $this->search = $search;
9651e491
JP
217 $this->enrolid = $enrolid;
218 $this->status = $status;
bc47b706
MN
219 $this->selectall = $selectall;
220 $this->countries = get_string_manager()->get_list_of_countries();
221 $this->extrafields = $extrafields;
f3ecea3a
DW
222 $this->context = $context;
223 $this->groups = groups_get_all_groups($courseid, 0, 0, 'g.*', true);
5d0b4765 224 $this->allroles = role_fix_names(get_all_roles($this->context), $this->context);
0ae28fad 225 $this->allroleassignments = get_users_roles($this->context, [], true, 'c.contextlevel DESC, r.sortorder ASC');
5d0b4765 226 $this->assignableroles = get_assignable_roles($this->context, ROLENAME_ALIAS, false);
bc47b706
MN
227 }
228
80d13bfe
JP
229 /**
230 * Render the participants table.
231 *
232 * @param int $pagesize Size of page for paginated displayed table.
233 * @param bool $useinitialsbar Whether to use the initials bar which will only be used if there is a fullname column defined.
234 * @param string $downloadhelpbutton
235 */
236 public function out($pagesize, $useinitialsbar, $downloadhelpbutton = '') {
237 global $PAGE;
238
239 parent::out($pagesize, $useinitialsbar, $downloadhelpbutton);
240
241 if (has_capability('moodle/course:enrolreview', $this->context)) {
242 $params = ['contextid' => $this->context->id, 'courseid' => $this->course->id];
243 $PAGE->requires->js_call_amd('core_user/status_field', 'init', [$params]);
244 }
245 }
246
bc47b706
MN
247 /**
248 * Generate the select column.
249 *
250 * @param \stdClass $data
251 * @return string
252 */
253 public function col_select($data) {
254 if ($this->selectall) {
255 $checked = 'checked="true"';
256 } else {
257 $checked = '';
258 }
259 return '<input type="checkbox" class="usercheckbox" name="user' . $data->id . '" ' . $checked . '/>';
260 }
261
262 /**
263 * Generate the fullname column.
264 *
265 * @param \stdClass $data
266 * @return string
267 */
268 public function col_fullname($data) {
269 global $OUTPUT;
270
c5d59db9 271 return $OUTPUT->user_picture($data, array('size' => 35, 'courseid' => $this->course->id, 'includefullname' => true));
bc47b706
MN
272 }
273
5d0b4765
DW
274 /**
275 * User roles column.
276 *
277 * @param \stdClass $data
278 * @return string
279 */
280 public function col_roles($data) {
281 global $OUTPUT;
282
0ae28fad 283 $roles = isset($this->allroleassignments[$data->id]) ? $this->allroleassignments[$data->id] : [];
5d0b4765
DW
284 $getrole = function($role) {
285 return $role->roleid;
286 };
73d0d562 287 $ids = array_values(array_unique(array_map($getrole, $roles)));
5d0b4765
DW
288
289 $editable = new \core_user\output\user_roles_editable($this->course,
290 $this->context,
291 $data,
292 $this->allroles,
293 $this->assignableroles,
294 $ids);
295
296 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
297 }
298
2fa35b8d
DW
299 /**
300 * Generate the groups column.
301 *
f3ecea3a 302 * @param \stdClass $data
2fa35b8d
DW
303 * @return string
304 */
f3ecea3a 305 public function col_groups($data) {
2fa35b8d
DW
306 global $OUTPUT;
307
308 $usergroups = [];
309 foreach ($this->groups as $coursegroup) {
f3ecea3a 310 if (isset($coursegroup->members[$data->id])) {
2fa35b8d
DW
311 $usergroups[] = $coursegroup->id;
312 }
313 }
f3ecea3a 314 $editable = new \core_group\output\user_groups_editable($this->course, $this->context, $data, $this->groups, $usergroups);
2fa35b8d
DW
315 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
316 }
317
bc47b706
MN
318 /**
319 * Generate the country column.
320 *
321 * @param \stdClass $data
322 * @return string
323 */
324 public function col_country($data) {
325 if (!empty($this->countries[$data->country])) {
326 return $this->countries[$data->country];
327 }
328 return '';
329 }
330
331 /**
332 * Generate the last access column.
333 *
334 * @param \stdClass $data
335 * @return string
336 */
337 public function col_lastaccess($data) {
338 if ($data->lastaccess) {
339 return format_time(time() - $data->lastaccess);
340 }
341
342 return get_string('never');
343 }
344
fb792967
JP
345 /**
346 * Generate the status column.
347 *
80d13bfe 348 * @param \stdClass $data The data object.
fb792967
JP
349 * @return string
350 */
351 public function col_status($data) {
352 global $CFG, $OUTPUT, $PAGE;
353
354 $enrolstatusoutput = '';
355 $canreviewenrol = has_capability('moodle/course:enrolreview', $this->context);
356 if ($canreviewenrol) {
357 $fullname = fullname($data);
80d13bfe 358 $coursename = $this->course->fullname;
fb792967
JP
359 require_once($CFG->dirroot . '/enrol/locallib.php');
360 $manager = new \course_enrolment_manager($PAGE, $this->course);
361 $userenrolments = $manager->get_user_enrolments($data->id);
362 foreach ($userenrolments as $ue) {
fb792967
JP
363 $timestart = $ue->timestart;
364 $timeend = $ue->timeend;
90ffcfa8
JP
365 $actions = $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue);
366 $instancename = $ue->enrolmentinstancename;
367
368 // Default status field label and value.
369 $status = get_string('participationactive', 'enrol');
370 $statusval = status_field::STATUS_ACTIVE;
fb792967
JP
371 switch ($ue->status) {
372 case ENROL_USER_ACTIVE:
373 $currentdate = new DateTime();
374 $now = $currentdate->getTimestamp();
90ffcfa8
JP
375 // If user enrolment status has not yet started/already ended.
376 if ($timestart > $now || ($timeend > 0 && $timeend < $now)) {
fb792967 377 $status = get_string('participationnotcurrent', 'enrol');
90ffcfa8 378 $statusval = status_field::STATUS_NOT_CURRENT;
fb792967
JP
379 }
380 break;
381 case ENROL_USER_SUSPENDED:
382 $status = get_string('participationsuspended', 'enrol');
90ffcfa8 383 $statusval = status_field::STATUS_SUSPENDED;
fb792967
JP
384 break;
385 }
90ffcfa8
JP
386
387 $statusfield = new status_field($instancename, $coursename, $fullname, $status, $timestart, $timeend, $actions);
388 $statusfielddata = $statusfield->set_status($statusval)->export_for_template($OUTPUT);
80d13bfe 389 $enrolstatusoutput .= $OUTPUT->render_from_template('core_user/status_field', $statusfielddata);
fb792967
JP
390 }
391 }
392 return $enrolstatusoutput;
393 }
394
bc47b706
MN
395 /**
396 * This function is used for the extra user fields.
397 *
398 * These are being dynamically added to the table so there are no functions 'col_<userfieldname>' as
399 * the list has the potential to increase in the future and we don't want to have to remember to add
400 * a new method to this class. We also don't want to pollute this class with unnecessary methods.
401 *
402 * @param string $colname The column name
403 * @param \stdClass $data
404 * @return string
405 */
406 public function other_cols($colname, $data) {
407 // Do not process if it is not a part of the extra fields.
408 if (!in_array($colname, $this->extrafields)) {
409 return '';
410 }
411
412 return s($data->{$colname});
413 }
414
415 /**
416 * Query the database for results to display in the table.
417 *
418 * @param int $pagesize size of page for paginated displayed table.
419 * @param bool $useinitialsbar do you want to use the initials bar.
420 */
421 public function query_db($pagesize, $useinitialsbar = true) {
422 list($twhere, $tparams) = $this->get_sql_where();
423
f3ecea3a 424 $total = user_get_total_participants($this->course->id, $this->currentgroup, $this->accesssince,
9651e491 425 $this->roleid, $this->enrolid, $this->status, $this->search, $twhere, $tparams);
bc47b706
MN
426
427 $this->pagesize($pagesize, $total);
428
429 $sort = $this->get_sql_sort();
430 if ($sort) {
431 $sort = 'ORDER BY ' . $sort;
432 }
433
f3ecea3a 434 $this->rawdata = user_get_participants($this->course->id, $this->currentgroup, $this->accesssince,
9651e491 435 $this->roleid, $this->enrolid, $this->status, $this->search, $twhere, $tparams, $sort, $this->get_page_start(),
bc47b706
MN
436 $this->get_page_size());
437
438 // Set initial bars.
439 if ($useinitialsbar) {
440 $this->initialbars(true);
441 }
442 }
443}
444