weekly back-to-dev release 3.6dev
[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
139 * @param int|false $currentgroup False if groups not used, int if groups used, 0 for all groups.
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 238 $this->allroles = role_fix_names(get_all_roles($this->context), $this->context);
0ae28fad 239 $this->allroleassignments = get_users_roles($this->context, [], true, 'c.contextlevel DESC, r.sortorder ASC');
5d0b4765 240 $this->assignableroles = get_assignable_roles($this->context, ROLENAME_ALIAS, false);
9df2fdec 241 $this->profileroles = get_profile_roles($this->context);
a63cd3e2 242 $this->viewableroles = get_viewable_roles($this->context);
bc47b706
MN
243 }
244
80d13bfe
JP
245 /**
246 * Render the participants table.
247 *
248 * @param int $pagesize Size of page for paginated displayed table.
249 * @param bool $useinitialsbar Whether to use the initials bar which will only be used if there is a fullname column defined.
250 * @param string $downloadhelpbutton
251 */
252 public function out($pagesize, $useinitialsbar, $downloadhelpbutton = '') {
253 global $PAGE;
254
255 parent::out($pagesize, $useinitialsbar, $downloadhelpbutton);
256
257 if (has_capability('moodle/course:enrolreview', $this->context)) {
258 $params = ['contextid' => $this->context->id, 'courseid' => $this->course->id];
259 $PAGE->requires->js_call_amd('core_user/status_field', 'init', [$params]);
260 }
261 }
262
bc47b706
MN
263 /**
264 * Generate the select column.
265 *
266 * @param \stdClass $data
267 * @return string
268 */
269 public function col_select($data) {
270 if ($this->selectall) {
271 $checked = 'checked="true"';
272 } else {
273 $checked = '';
274 }
275 return '<input type="checkbox" class="usercheckbox" name="user' . $data->id . '" ' . $checked . '/>';
276 }
277
278 /**
279 * Generate the fullname column.
280 *
281 * @param \stdClass $data
282 * @return string
283 */
284 public function col_fullname($data) {
285 global $OUTPUT;
286
c5d59db9 287 return $OUTPUT->user_picture($data, array('size' => 35, 'courseid' => $this->course->id, 'includefullname' => true));
bc47b706
MN
288 }
289
5d0b4765
DW
290 /**
291 * User roles column.
292 *
293 * @param \stdClass $data
294 * @return string
295 */
296 public function col_roles($data) {
297 global $OUTPUT;
298
0ae28fad 299 $roles = isset($this->allroleassignments[$data->id]) ? $this->allroleassignments[$data->id] : [];
5d0b4765
DW
300 $editable = new \core_user\output\user_roles_editable($this->course,
301 $this->context,
302 $data,
303 $this->allroles,
304 $this->assignableroles,
9df2fdec 305 $this->profileroles,
a63cd3e2
AH
306 $roles,
307 $this->viewableroles);
5d0b4765
DW
308
309 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
310 }
311
2fa35b8d
DW
312 /**
313 * Generate the groups column.
314 *
f3ecea3a 315 * @param \stdClass $data
2fa35b8d
DW
316 * @return string
317 */
f3ecea3a 318 public function col_groups($data) {
2fa35b8d
DW
319 global $OUTPUT;
320
321 $usergroups = [];
322 foreach ($this->groups as $coursegroup) {
f3ecea3a 323 if (isset($coursegroup->members[$data->id])) {
2fa35b8d
DW
324 $usergroups[] = $coursegroup->id;
325 }
326 }
f3ecea3a 327 $editable = new \core_group\output\user_groups_editable($this->course, $this->context, $data, $this->groups, $usergroups);
2fa35b8d
DW
328 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
329 }
330
bc47b706
MN
331 /**
332 * Generate the country column.
333 *
334 * @param \stdClass $data
335 * @return string
336 */
337 public function col_country($data) {
338 if (!empty($this->countries[$data->country])) {
339 return $this->countries[$data->country];
340 }
341 return '';
342 }
343
344 /**
345 * Generate the last access column.
346 *
347 * @param \stdClass $data
348 * @return string
349 */
350 public function col_lastaccess($data) {
351 if ($data->lastaccess) {
352 return format_time(time() - $data->lastaccess);
353 }
354
355 return get_string('never');
356 }
357
fb792967
JP
358 /**
359 * Generate the status column.
360 *
80d13bfe 361 * @param \stdClass $data The data object.
fb792967
JP
362 * @return string
363 */
364 public function col_status($data) {
365 global $CFG, $OUTPUT, $PAGE;
366
367 $enrolstatusoutput = '';
368 $canreviewenrol = has_capability('moodle/course:enrolreview', $this->context);
369 if ($canreviewenrol) {
370 $fullname = fullname($data);
80d13bfe 371 $coursename = $this->course->fullname;
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;
90ffcfa8
JP
378 $actions = $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue);
379 $instancename = $ue->enrolmentinstancename;
380
381 // Default status field label and value.
382 $status = get_string('participationactive', 'enrol');
383 $statusval = status_field::STATUS_ACTIVE;
fb792967
JP
384 switch ($ue->status) {
385 case ENROL_USER_ACTIVE:
386 $currentdate = new DateTime();
387 $now = $currentdate->getTimestamp();
3449816e
SA
388 $isexpired = $timestart > $now || ($timeend > 0 && $timeend < $now);
389 $enrolmentdisabled = $ue->enrolmentinstance->status == ENROL_INSTANCE_DISABLED;
390 // If user enrolment status has not yet started/already ended or the enrolment instance is disabled.
391 if ($isexpired || $enrolmentdisabled) {
fb792967 392 $status = get_string('participationnotcurrent', 'enrol');
90ffcfa8 393 $statusval = status_field::STATUS_NOT_CURRENT;
fb792967
JP
394 }
395 break;
396 case ENROL_USER_SUSPENDED:
397 $status = get_string('participationsuspended', 'enrol');
90ffcfa8 398 $statusval = status_field::STATUS_SUSPENDED;
fb792967
JP
399 break;
400 }
90ffcfa8
JP
401
402 $statusfield = new status_field($instancename, $coursename, $fullname, $status, $timestart, $timeend, $actions);
403 $statusfielddata = $statusfield->set_status($statusval)->export_for_template($OUTPUT);
80d13bfe 404 $enrolstatusoutput .= $OUTPUT->render_from_template('core_user/status_field', $statusfielddata);
fb792967
JP
405 }
406 }
407 return $enrolstatusoutput;
408 }
409
bc47b706
MN
410 /**
411 * This function is used for the extra user fields.
412 *
413 * These are being dynamically added to the table so there are no functions 'col_<userfieldname>' as
414 * the list has the potential to increase in the future and we don't want to have to remember to add
415 * a new method to this class. We also don't want to pollute this class with unnecessary methods.
416 *
417 * @param string $colname The column name
418 * @param \stdClass $data
419 * @return string
420 */
421 public function other_cols($colname, $data) {
422 // Do not process if it is not a part of the extra fields.
423 if (!in_array($colname, $this->extrafields)) {
424 return '';
425 }
426
427 return s($data->{$colname});
428 }
429
430 /**
431 * Query the database for results to display in the table.
432 *
433 * @param int $pagesize size of page for paginated displayed table.
434 * @param bool $useinitialsbar do you want to use the initials bar.
435 */
436 public function query_db($pagesize, $useinitialsbar = true) {
437 list($twhere, $tparams) = $this->get_sql_where();
438
f3ecea3a 439 $total = user_get_total_participants($this->course->id, $this->currentgroup, $this->accesssince,
9651e491 440 $this->roleid, $this->enrolid, $this->status, $this->search, $twhere, $tparams);
bc47b706
MN
441
442 $this->pagesize($pagesize, $total);
443
444 $sort = $this->get_sql_sort();
445 if ($sort) {
446 $sort = 'ORDER BY ' . $sort;
447 }
448
f3ecea3a 449 $this->rawdata = user_get_participants($this->course->id, $this->currentgroup, $this->accesssince,
9651e491 450 $this->roleid, $this->enrolid, $this->status, $this->search, $twhere, $tparams, $sort, $this->get_page_start(),
bc47b706
MN
451 $this->get_page_size());
452
453 // Set initial bars.
454 if ($useinitialsbar) {
455 $this->initialbars(true);
456 }
457 }
458}
459