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