2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Contains the class used for the displaying the participants table.
21 * @copyright 2017 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 use core_user\output\status_field
;
31 defined('MOODLE_INTERNAL') ||
die;
35 require_once($CFG->libdir
. '/tablelib.php');
36 require_once($CFG->dirroot
. '/user/lib.php');
39 * Class for the displaying the participants table.
42 * @copyright 2017 Mark Nelson <markn@moodle.com>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class participants_table
extends \table_sql
{
48 * @var int $courseid The course id
53 * @var int|false False if groups not used, int if groups used, 0 for all groups.
55 protected $currentgroup;
58 * @var int $accesssince The time the user last accessed the site
60 protected $accesssince;
63 * @var int $roleid The role we are including, 0 means all enrolled users
68 * @var int $enrolid The applied filter for the user enrolment ID.
73 * @var int $status The applied filter for the user's enrolment status.
78 * @var string $search The string being searched.
83 * @var bool $selectall Has the user selected all users on the page?
88 * @var string[] The list of countries.
93 * @var \stdClass[] The list of groups with membership info for the course.
98 * @var string[] Extra fields to display.
100 protected $extrafields;
103 * @var \stdClass $course The course details.
108 * @var context $context The course context.
113 * @var \stdClass[] List of roles indexed by roleid.
118 * @var \stdClass[] List of roles indexed by roleid.
120 protected $allroleassignments;
123 * @var \stdClass[] Assignable roles in this course.
125 protected $assignableroles;
128 * @var \stdClass[] Profile roles in this course.
130 protected $profileroles;
132 /** @var \stdClass[] $viewableroles */
133 private $viewableroles;
138 * @param int $courseid
139 * @param int|false $currentgroup False if groups not used, int if groups used, 0 all groups, USERSWITHOUTGROUP for no group
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
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)
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?
148 public function __construct($courseid, $currentgroup, $accesssince, $roleid, $enrolid, $status, $search,
149 $bulkoperations, $selectall) {
152 parent
::__construct('user-index-participants-' . $courseid);
155 $this->course
= get_course($courseid);
156 $context = \context_course
::instance($courseid, MUST_EXIST
);
157 $this->context
= $context;
159 // Define the headers and columns.
163 if ($bulkoperations) {
164 $headers[] = get_string('select');
165 $columns[] = 'select';
168 $headers[] = get_string('fullname');
169 $columns[] = 'fullname';
171 $extrafields = get_extra_user_fields($context);
172 foreach ($extrafields as $field) {
173 $headers[] = get_user_field_name($field);
177 $headers[] = get_string('roles');
178 $columns[] = 'roles';
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
));
186 // Add column for groups if the user can view them.
187 $canseegroups = !isset($hiddenfields['groups']);
189 $headers[] = get_string('groups');
190 $columns[] = 'groups';
193 // Do not show the columns if it exists in the hiddenfields array.
194 if (!isset($hiddenfields['lastaccess'])) {
195 if ($courseid == SITEID
) {
196 $headers[] = get_string('lastsiteaccess');
198 $headers[] = get_string('lastcourseaccess');
200 $columns[] = 'lastaccess';
203 $canreviewenrol = has_capability('moodle/course:enrolreview', $context);
204 if ($canreviewenrol && $courseid != SITEID
) {
205 $columns[] = 'status';
206 $headers[] = get_string('participationstatus', 'enrol');
207 $this->no_sorting('status');
210 $this->define_columns($columns);
211 $this->define_headers($headers);
213 // Make this table sorted by first name by default.
214 $this->sortable(true, 'firstname');
216 $this->no_sorting('select');
217 $this->no_sorting('roles');
219 $this->no_sorting('groups');
222 $this->set_attribute('id', 'participants');
224 // Set the variables we need to use later.
225 $this->currentgroup
= $currentgroup;
226 $this->accesssince
= $accesssince;
227 $this->roleid
= $roleid;
228 $this->search
= $search;
229 $this->enrolid
= $enrolid;
230 $this->status
= $status;
231 $this->selectall
= $selectall;
232 $this->countries
= get_string_manager()->get_list_of_countries(true);
233 $this->extrafields
= $extrafields;
234 $this->context
= $context;
236 $this->groups
= groups_get_all_groups($courseid, 0, 0, 'g.*', true);
238 $this->allroles
= role_fix_names(get_all_roles($this->context
), $this->context
);
239 $this->assignableroles
= get_assignable_roles($this->context
, ROLENAME_ALIAS
, false);
240 $this->profileroles
= get_profile_roles($this->context
);
241 $this->viewableroles
= get_viewable_roles($this->context
);
245 * Render the participants table.
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
251 public function out($pagesize, $useinitialsbar, $downloadhelpbutton = '') {
254 parent
::out($pagesize, $useinitialsbar, $downloadhelpbutton);
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]);
263 * Generate the select column.
265 * @param \stdClass $data
268 public function col_select($data) {
269 if ($this->selectall
) {
270 $checked = 'checked="true"';
274 return '<input type="checkbox" class="usercheckbox" name="user' . $data->id
. '" ' . $checked . '/>';
278 * Generate the fullname column.
280 * @param \stdClass $data
283 public function col_fullname($data) {
286 return $OUTPUT->user_picture($data, array('size' => 35, 'courseid' => $this->course
->id
, 'includefullname' => true));
292 * @param \stdClass $data
295 public function col_roles($data) {
298 $roles = isset($this->allroleassignments
[$data->id
]) ?
$this->allroleassignments
[$data->id
] : [];
299 $editable = new \core_user\output\
user_roles_editable($this->course
,
303 $this->assignableroles
,
306 $this->viewableroles
);
308 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
312 * Generate the groups column.
314 * @param \stdClass $data
317 public function col_groups($data) {
321 foreach ($this->groups
as $coursegroup) {
322 if (isset($coursegroup->members
[$data->id
])) {
323 $usergroups[] = $coursegroup->id
;
326 $editable = new \core_group\output\
user_groups_editable($this->course
, $this->context
, $data, $this->groups
, $usergroups);
327 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
331 * Generate the country column.
333 * @param \stdClass $data
336 public function col_country($data) {
337 if (!empty($this->countries
[$data->country
])) {
338 return $this->countries
[$data->country
];
344 * Generate the last access column.
346 * @param \stdClass $data
349 public function col_lastaccess($data) {
350 if ($data->lastaccess
) {
351 return format_time(time() - $data->lastaccess
);
354 return get_string('never');
358 * Generate the status column.
360 * @param \stdClass $data The data object.
363 public function col_status($data) {
364 global $CFG, $OUTPUT, $PAGE;
366 $enrolstatusoutput = '';
367 $canreviewenrol = has_capability('moodle/course:enrolreview', $this->context
);
368 if ($canreviewenrol) {
369 $canviewfullnames = has_capability('moodle/site:viewfullnames', $this->context
);
370 $fullname = fullname($data, $canviewfullnames);
371 $coursename = format_string($this->course
->fullname
, true, array('context' => $this->context
));
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) {
376 $timestart = $ue->timestart
;
377 $timeend = $ue->timeend
;
378 $actions = $ue->enrolmentplugin
->get_user_enrolment_actions($manager, $ue);
379 $instancename = $ue->enrolmentinstancename
;
381 // Default status field label and value.
382 $status = get_string('participationactive', 'enrol');
383 $statusval = status_field
::STATUS_ACTIVE
;
384 switch ($ue->status
) {
385 case ENROL_USER_ACTIVE
:
386 $currentdate = new DateTime();
387 $now = $currentdate->getTimestamp();
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) {
392 $status = get_string('participationnotcurrent', 'enrol');
393 $statusval = status_field
::STATUS_NOT_CURRENT
;
396 case ENROL_USER_SUSPENDED
:
397 $status = get_string('participationsuspended', 'enrol');
398 $statusval = status_field
::STATUS_SUSPENDED
;
402 $statusfield = new status_field($instancename, $coursename, $fullname, $status, $timestart, $timeend, $actions);
403 $statusfielddata = $statusfield->set_status($statusval)->export_for_template($OUTPUT);
404 $enrolstatusoutput .= $OUTPUT->render_from_template('core_user/status_field', $statusfielddata);
407 return $enrolstatusoutput;
411 * This function is used for the extra user fields.
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.
417 * @param string $colname The column name
418 * @param \stdClass $data
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
)) {
427 return s($data->{$colname});
431 * Query the database for results to display in the table.
433 * @param int $pagesize size of page for paginated displayed table.
434 * @param bool $useinitialsbar do you want to use the initials bar.
436 public function query_db($pagesize, $useinitialsbar = true) {
437 list($twhere, $tparams) = $this->get_sql_where();
439 $total = user_get_total_participants($this->course
->id
, $this->currentgroup
, $this->accesssince
,
440 $this->roleid
, $this->enrolid
, $this->status
, $this->search
, $twhere, $tparams);
442 $this->pagesize($pagesize, $total);
444 $sort = $this->get_sql_sort();
446 $sort = 'ORDER BY ' . $sort;
449 $rawdata = user_get_participants($this->course
->id
, $this->currentgroup
, $this->accesssince
,
450 $this->roleid
, $this->enrolid
, $this->status
, $this->search
, $twhere, $tparams, $sort, $this->get_page_start(),
451 $this->get_page_size());
453 foreach ($rawdata as $user) {
454 $this->rawdata
[$user->id
] = $user;
458 if ($this->rawdata
) {
459 $this->allroleassignments
= get_users_roles($this->context
, array_keys($this->rawdata
),
460 true, 'c.contextlevel DESC, r.sortorder ASC');
462 $this->allroleassignments
= [];
466 if ($useinitialsbar) {
467 $this->initialbars(true);