weekly release 3.8dev
[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
9df2fdec
DW
127 /**
128 * @var \stdClass[] Profile roles in this course.
129 */
130 protected $profileroles;
131
a63cd3e2
AH
132 /** @var \stdClass[] $viewableroles */
133 private $viewableroles;
134
bc47b706
MN
135 /**
136 * Sets up the table.
137 *
138 * @param int $courseid
5290d060 139 * @param int|false $currentgroup False if groups not used, int if groups used, 0 all groups, USERSWITHOUTGROUP for no group
bc47b706
MN
140 * @param int $accesssince The time the user last accessed the site
141 * @param int $roleid The role we are including, 0 means all enrolled users
9651e491
JP
142 * @param int $enrolid The applied filter for the user enrolment ID.
143 * @param int $status The applied filter for the user's enrolment status.
144 * @param string|array $search The search string(s)
bc47b706
MN
145 * @param bool $bulkoperations Is the user allowed to perform bulk operations?
146 * @param bool $selectall Has the user selected all users on the page?
147 */
9651e491 148 public function __construct($courseid, $currentgroup, $accesssince, $roleid, $enrolid, $status, $search,
bc47b706
MN
149 $bulkoperations, $selectall) {
150 global $CFG;
151
152 parent::__construct('user-index-participants-' . $courseid);
153
154 // Get the context.
2fa35b8d 155 $this->course = get_course($courseid);
bc47b706 156 $context = \context_course::instance($courseid, MUST_EXIST);
fb792967 157 $this->context = $context;
bc47b706
MN
158
159 // Define the headers and columns.
160 $headers = [];
161 $columns = [];
162
163 if ($bulkoperations) {
164 $headers[] = get_string('select');
165 $columns[] = 'select';
166 }
167
168 $headers[] = get_string('fullname');
169 $columns[] = 'fullname';
170
171 $extrafields = get_extra_user_fields($context);
172 foreach ($extrafields as $field) {
173 $headers[] = get_user_field_name($field);
174 $columns[] = $field;
175 }
176
5d0b4765
DW
177 $headers[] = get_string('roles');
178 $columns[] = 'roles';
179
bc47b706
MN
180 // Get the list of fields we have to hide.
181 $hiddenfields = array();
182 if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
183 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
184 }
185
359e9d83
MN
186 // Add column for groups if the user can view them.
187 $canseegroups = !isset($hiddenfields['groups']);
188 if ($canseegroups) {
189 $headers[] = get_string('groups');
190 $columns[] = 'groups';
191 }
192
bc47b706 193 // Do not show the columns if it exists in the hiddenfields array.
bc47b706
MN
194 if (!isset($hiddenfields['lastaccess'])) {
195 if ($courseid == SITEID) {
196 $headers[] = get_string('lastsiteaccess');
197 } else {
198 $headers[] = get_string('lastcourseaccess');
199 }
200 $columns[] = 'lastaccess';
201 }
202
fb792967 203 $canreviewenrol = has_capability('moodle/course:enrolreview', $context);
ca560952 204 if ($canreviewenrol && $courseid != SITEID) {
fb792967
JP
205 $columns[] = 'status';
206 $headers[] = get_string('participationstatus', 'enrol');
207 $this->no_sorting('status');
208 };
209
bc47b706
MN
210 $this->define_columns($columns);
211 $this->define_headers($headers);
212
f7cd3bc0
JP
213 // Make this table sorted by first name by default.
214 $this->sortable(true, 'firstname');
215
bc47b706 216 $this->no_sorting('select');
cd3f250f 217 $this->no_sorting('roles');
359e9d83
MN
218 if ($canseegroups) {
219 $this->no_sorting('groups');
220 }
bc47b706
MN
221
222 $this->set_attribute('id', 'participants');
223
224 // Set the variables we need to use later.
bc47b706
MN
225 $this->currentgroup = $currentgroup;
226 $this->accesssince = $accesssince;
227 $this->roleid = $roleid;
228 $this->search = $search;
9651e491
JP
229 $this->enrolid = $enrolid;
230 $this->status = $status;
bc47b706 231 $this->selectall = $selectall;
0c98acef 232 $this->countries = get_string_manager()->get_list_of_countries(true);
bc47b706 233 $this->extrafields = $extrafields;
f3ecea3a 234 $this->context = $context;
359e9d83
MN
235 if ($canseegroups) {
236 $this->groups = groups_get_all_groups($courseid, 0, 0, 'g.*', true);
237 }
5d0b4765
DW
238 $this->allroles = role_fix_names(get_all_roles($this->context), $this->context);
239 $this->assignableroles = get_assignable_roles($this->context, ROLENAME_ALIAS, false);
9df2fdec 240 $this->profileroles = get_profile_roles($this->context);
a63cd3e2 241 $this->viewableroles = get_viewable_roles($this->context);
bc47b706
MN
242 }
243
80d13bfe
JP
244 /**
245 * Render the participants table.
246 *
247 * @param int $pagesize Size of page for paginated displayed table.
248 * @param bool $useinitialsbar Whether to use the initials bar which will only be used if there is a fullname column defined.
249 * @param string $downloadhelpbutton
250 */
251 public function out($pagesize, $useinitialsbar, $downloadhelpbutton = '') {
252 global $PAGE;
253
254 parent::out($pagesize, $useinitialsbar, $downloadhelpbutton);
255
256 if (has_capability('moodle/course:enrolreview', $this->context)) {
257 $params = ['contextid' => $this->context->id, 'courseid' => $this->course->id];
258 $PAGE->requires->js_call_amd('core_user/status_field', 'init', [$params]);
259 }
260 }
261
bc47b706
MN
262 /**
263 * Generate the select column.
264 *
265 * @param \stdClass $data
266 * @return string
267 */
268 public function col_select($data) {
269 if ($this->selectall) {
270 $checked = 'checked="true"';
271 } else {
272 $checked = '';
273 }
274 return '<input type="checkbox" class="usercheckbox" name="user' . $data->id . '" ' . $checked . '/>';
275 }
276
277 /**
278 * Generate the fullname column.
279 *
280 * @param \stdClass $data
281 * @return string
282 */
283 public function col_fullname($data) {
284 global $OUTPUT;
285
c5d59db9 286 return $OUTPUT->user_picture($data, array('size' => 35, 'courseid' => $this->course->id, 'includefullname' => true));
bc47b706
MN
287 }
288
5d0b4765
DW
289 /**
290 * User roles column.
291 *
292 * @param \stdClass $data
293 * @return string
294 */
295 public function col_roles($data) {
296 global $OUTPUT;
297
0ae28fad 298 $roles = isset($this->allroleassignments[$data->id]) ? $this->allroleassignments[$data->id] : [];
5d0b4765
DW
299 $editable = new \core_user\output\user_roles_editable($this->course,
300 $this->context,
301 $data,
302 $this->allroles,
303 $this->assignableroles,
9df2fdec 304 $this->profileroles,
a63cd3e2
AH
305 $roles,
306 $this->viewableroles);
5d0b4765
DW
307
308 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
309 }
310
2fa35b8d
DW
311 /**
312 * Generate the groups column.
313 *
f3ecea3a 314 * @param \stdClass $data
2fa35b8d
DW
315 * @return string
316 */
f3ecea3a 317 public function col_groups($data) {
2fa35b8d
DW
318 global $OUTPUT;
319
320 $usergroups = [];
321 foreach ($this->groups as $coursegroup) {
f3ecea3a 322 if (isset($coursegroup->members[$data->id])) {
2fa35b8d
DW
323 $usergroups[] = $coursegroup->id;
324 }
325 }
f3ecea3a 326 $editable = new \core_group\output\user_groups_editable($this->course, $this->context, $data, $this->groups, $usergroups);
2fa35b8d
DW
327 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
328 }
329
bc47b706
MN
330 /**
331 * Generate the country column.
332 *
333 * @param \stdClass $data
334 * @return string
335 */
336 public function col_country($data) {
337 if (!empty($this->countries[$data->country])) {
338 return $this->countries[$data->country];
339 }
340 return '';
341 }
342
343 /**
344 * Generate the last access column.
345 *
346 * @param \stdClass $data
347 * @return string
348 */
349 public function col_lastaccess($data) {
350 if ($data->lastaccess) {
351 return format_time(time() - $data->lastaccess);
352 }
353
354 return get_string('never');
355 }
356
fb792967
JP
357 /**
358 * Generate the status column.
359 *
80d13bfe 360 * @param \stdClass $data The data object.
fb792967
JP
361 * @return string
362 */
363 public function col_status($data) {
364 global $CFG, $OUTPUT, $PAGE;
365
366 $enrolstatusoutput = '';
367 $canreviewenrol = has_capability('moodle/course:enrolreview', $this->context);
368 if ($canreviewenrol) {
c157e137
MH
369 $canviewfullnames = has_capability('moodle/site:viewfullnames', $this->context);
370 $fullname = fullname($data, $canviewfullnames);
4a42d3de 371 $coursename = format_string($this->course->fullname, true, array('context' => $this->context));
fb792967
JP
372 require_once($CFG->dirroot . '/enrol/locallib.php');
373 $manager = new \course_enrolment_manager($PAGE, $this->course);
374 $userenrolments = $manager->get_user_enrolments($data->id);
375 foreach ($userenrolments as $ue) {
fb792967
JP
376 $timestart = $ue->timestart;
377 $timeend = $ue->timeend;
6676f202 378 $timeenrolled = $ue->timecreated;
90ffcfa8
JP
379 $actions = $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue);
380 $instancename = $ue->enrolmentinstancename;
381
382 // Default status field label and value.
383 $status = get_string('participationactive', 'enrol');
384 $statusval = status_field::STATUS_ACTIVE;
fb792967
JP
385 switch ($ue->status) {
386 case ENROL_USER_ACTIVE:
387 $currentdate = new DateTime();
388 $now = $currentdate->getTimestamp();
3449816e
SA
389 $isexpired = $timestart > $now || ($timeend > 0 && $timeend < $now);
390 $enrolmentdisabled = $ue->enrolmentinstance->status == ENROL_INSTANCE_DISABLED;
391 // If user enrolment status has not yet started/already ended or the enrolment instance is disabled.
392 if ($isexpired || $enrolmentdisabled) {
fb792967 393 $status = get_string('participationnotcurrent', 'enrol');
90ffcfa8 394 $statusval = status_field::STATUS_NOT_CURRENT;
fb792967
JP
395 }
396 break;
397 case ENROL_USER_SUSPENDED:
398 $status = get_string('participationsuspended', 'enrol');
90ffcfa8 399 $statusval = status_field::STATUS_SUSPENDED;
fb792967
JP
400 break;
401 }
90ffcfa8 402
26b34c30
AA
403 $statusfield = new status_field($instancename, $coursename, $fullname, $status, $timestart, $timeend,
404 $actions, $timeenrolled);
90ffcfa8 405 $statusfielddata = $statusfield->set_status($statusval)->export_for_template($OUTPUT);
80d13bfe 406 $enrolstatusoutput .= $OUTPUT->render_from_template('core_user/status_field', $statusfielddata);
fb792967
JP
407 }
408 }
409 return $enrolstatusoutput;
410 }
411
bc47b706
MN
412 /**
413 * This function is used for the extra user fields.
414 *
415 * These are being dynamically added to the table so there are no functions 'col_<userfieldname>' as
416 * the list has the potential to increase in the future and we don't want to have to remember to add
417 * a new method to this class. We also don't want to pollute this class with unnecessary methods.
418 *
419 * @param string $colname The column name
420 * @param \stdClass $data
421 * @return string
422 */
423 public function other_cols($colname, $data) {
424 // Do not process if it is not a part of the extra fields.
425 if (!in_array($colname, $this->extrafields)) {
426 return '';
427 }
428
429 return s($data->{$colname});
430 }
431
432 /**
433 * Query the database for results to display in the table.
434 *
435 * @param int $pagesize size of page for paginated displayed table.
436 * @param bool $useinitialsbar do you want to use the initials bar.
437 */
438 public function query_db($pagesize, $useinitialsbar = true) {
439 list($twhere, $tparams) = $this->get_sql_where();
440
f3ecea3a 441 $total = user_get_total_participants($this->course->id, $this->currentgroup, $this->accesssince,
9651e491 442 $this->roleid, $this->enrolid, $this->status, $this->search, $twhere, $tparams);
bc47b706
MN
443
444 $this->pagesize($pagesize, $total);
445
446 $sort = $this->get_sql_sort();
447 if ($sort) {
448 $sort = 'ORDER BY ' . $sort;
449 }
450
5359c517 451 $rawdata = user_get_participants($this->course->id, $this->currentgroup, $this->accesssince,
9651e491 452 $this->roleid, $this->enrolid, $this->status, $this->search, $twhere, $tparams, $sort, $this->get_page_start(),
bc47b706 453 $this->get_page_size());
5359c517
TH
454 $this->rawdata = [];
455 foreach ($rawdata as $user) {
456 $this->rawdata[$user->id] = $user;
457 }
458 $rawdata->close();
459
460 if ($this->rawdata) {
461 $this->allroleassignments = get_users_roles($this->context, array_keys($this->rawdata),
462 true, 'c.contextlevel DESC, r.sortorder ASC');
463 } else {
464 $this->allroleassignments = [];
465 }
bc47b706
MN
466
467 // Set initial bars.
468 if ($useinitialsbar) {
469 $this->initialbars(true);
470 }
471 }
472}
473