Merge branch 'MDL-57698-master' of https://github.com/xow/moodle
[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
27defined('MOODLE_INTERNAL') || die;
28
29global $CFG;
30
31require_once($CFG->libdir . '/tablelib.php');
32require_once($CFG->dirroot . '/user/lib.php');
33
34/**
35 * Class for the displaying the participants table.
36 *
37 * @package core_user
38 * @copyright 2017 Mark Nelson <markn@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class participants_table extends \table_sql {
42
43 /**
44 * @var int $courseid The course id
45 */
46 protected $courseid;
47
48 /**
49 * @var int|false False if groups not used, int if groups used, 0 for all groups.
50 */
51 protected $currentgroup;
52
53 /**
54 * @var int $accesssince The time the user last accessed the site
55 */
56 protected $accesssince;
57
58 /**
59 * @var int $roleid The role we are including, 0 means all enrolled users
60 */
61 protected $roleid;
62
63 /**
64 * @var string $search The string being searched.
65 */
66 protected $search;
67
68 /**
69 * @var bool $selectall Has the user selected all users on the page?
70 */
71 protected $selectall;
72
73 /**
74 * @var string[] The list of countries.
75 */
76 protected $countries;
77
2fa35b8d 78 /**
f3ecea3a 79 * @var \stdClass[] The list of groups with membership info for the course.
2fa35b8d
DW
80 */
81 protected $groups;
82
bc47b706
MN
83 /**
84 * @var string[] Extra fields to display.
85 */
86 protected $extrafields;
87
2fa35b8d 88 /**
f3ecea3a 89 * @var \stdClass The course details.
2fa35b8d
DW
90 */
91 protected $course;
92
93 /**
f3ecea3a 94 * @var \context The course context.
2fa35b8d
DW
95 */
96 protected $context;
97
73d0d562
DW
98 /**
99 * @var \stdClass[] List of roles indexed by roleid.
100 */
101 protected $allroles;
102
103 /**
104 * @var \stdClass[] Assignable roles in this course.
105 */
106 protected $assignableroles;
107
bc47b706
MN
108 /**
109 * Sets up the table.
110 *
111 * @param int $courseid
112 * @param int|false $currentgroup False if groups not used, int if groups used, 0 for all groups.
113 * @param int $accesssince The time the user last accessed the site
114 * @param int $roleid The role we are including, 0 means all enrolled users
115 * @param string $search The string being searched
116 * @param bool $bulkoperations Is the user allowed to perform bulk operations?
117 * @param bool $selectall Has the user selected all users on the page?
118 */
119 public function __construct($courseid, $currentgroup, $accesssince, $roleid, $search,
120 $bulkoperations, $selectall) {
121 global $CFG;
122
123 parent::__construct('user-index-participants-' . $courseid);
124
125 // Get the context.
2fa35b8d 126 $this->course = get_course($courseid);
bc47b706
MN
127 $context = \context_course::instance($courseid, MUST_EXIST);
128
129 // Define the headers and columns.
130 $headers = [];
131 $columns = [];
132
133 if ($bulkoperations) {
134 $headers[] = get_string('select');
135 $columns[] = 'select';
136 }
137
138 $headers[] = get_string('fullname');
139 $columns[] = 'fullname';
140
141 $extrafields = get_extra_user_fields($context);
142 foreach ($extrafields as $field) {
143 $headers[] = get_user_field_name($field);
144 $columns[] = $field;
145 }
146
5d0b4765
DW
147 $headers[] = get_string('roles');
148 $columns[] = 'roles';
149
2fa35b8d 150 // Load and cache the course groupinfo.
2fa35b8d
DW
151 // Add column for groups.
152 $headers[] = get_string('groups');
153 $columns[] = 'groups';
154
bc47b706
MN
155 // Get the list of fields we have to hide.
156 $hiddenfields = array();
157 if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
158 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
159 }
160
161 // Do not show the columns if it exists in the hiddenfields array.
bc47b706
MN
162 if (!isset($hiddenfields['lastaccess'])) {
163 if ($courseid == SITEID) {
164 $headers[] = get_string('lastsiteaccess');
165 } else {
166 $headers[] = get_string('lastcourseaccess');
167 }
168 $columns[] = 'lastaccess';
169 }
170
171 $this->define_columns($columns);
172 $this->define_headers($headers);
173
f7cd3bc0
JP
174 // Make this table sorted by first name by default.
175 $this->sortable(true, 'firstname');
176
bc47b706
MN
177 $this->no_sorting('select');
178
179 $this->set_attribute('id', 'participants');
180
181 // Set the variables we need to use later.
bc47b706
MN
182 $this->currentgroup = $currentgroup;
183 $this->accesssince = $accesssince;
184 $this->roleid = $roleid;
185 $this->search = $search;
186 $this->selectall = $selectall;
187 $this->countries = get_string_manager()->get_list_of_countries();
188 $this->extrafields = $extrafields;
f3ecea3a
DW
189 $this->context = $context;
190 $this->groups = groups_get_all_groups($courseid, 0, 0, 'g.*', true);
5d0b4765
DW
191 $this->allroles = role_fix_names(get_all_roles($this->context), $this->context);
192 $this->assignableroles = get_assignable_roles($this->context, ROLENAME_ALIAS, false);
bc47b706
MN
193 }
194
195 /**
196 * Generate the select column.
197 *
198 * @param \stdClass $data
199 * @return string
200 */
201 public function col_select($data) {
202 if ($this->selectall) {
203 $checked = 'checked="true"';
204 } else {
205 $checked = '';
206 }
207 return '<input type="checkbox" class="usercheckbox" name="user' . $data->id . '" ' . $checked . '/>';
208 }
209
210 /**
211 * Generate the fullname column.
212 *
213 * @param \stdClass $data
214 * @return string
215 */
216 public function col_fullname($data) {
217 global $OUTPUT;
218
c5d59db9 219 return $OUTPUT->user_picture($data, array('size' => 35, 'courseid' => $this->course->id, 'includefullname' => true));
bc47b706
MN
220 }
221
5d0b4765
DW
222 /**
223 * User roles column.
224 *
225 * @param \stdClass $data
226 * @return string
227 */
228 public function col_roles($data) {
229 global $OUTPUT;
230
231 $roles = get_user_roles($this->context, $data->id, true, 'c.contextlevel DESC, r.sortorder ASC');
232 $getrole = function($role) {
233 return $role->roleid;
234 };
73d0d562 235 $ids = array_values(array_unique(array_map($getrole, $roles)));
5d0b4765
DW
236
237 $editable = new \core_user\output\user_roles_editable($this->course,
238 $this->context,
239 $data,
240 $this->allroles,
241 $this->assignableroles,
242 $ids);
243
244 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
245 }
246
2fa35b8d
DW
247 /**
248 * Generate the groups column.
249 *
f3ecea3a 250 * @param \stdClass $data
2fa35b8d
DW
251 * @return string
252 */
f3ecea3a 253 public function col_groups($data) {
2fa35b8d
DW
254 global $OUTPUT;
255
256 $usergroups = [];
257 foreach ($this->groups as $coursegroup) {
f3ecea3a 258 if (isset($coursegroup->members[$data->id])) {
2fa35b8d
DW
259 $usergroups[] = $coursegroup->id;
260 }
261 }
f3ecea3a 262 $editable = new \core_group\output\user_groups_editable($this->course, $this->context, $data, $this->groups, $usergroups);
2fa35b8d
DW
263 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
264 }
265
bc47b706
MN
266 /**
267 * Generate the country column.
268 *
269 * @param \stdClass $data
270 * @return string
271 */
272 public function col_country($data) {
273 if (!empty($this->countries[$data->country])) {
274 return $this->countries[$data->country];
275 }
276 return '';
277 }
278
279 /**
280 * Generate the last access column.
281 *
282 * @param \stdClass $data
283 * @return string
284 */
285 public function col_lastaccess($data) {
286 if ($data->lastaccess) {
287 return format_time(time() - $data->lastaccess);
288 }
289
290 return get_string('never');
291 }
292
293 /**
294 * This function is used for the extra user fields.
295 *
296 * These are being dynamically added to the table so there are no functions 'col_<userfieldname>' as
297 * the list has the potential to increase in the future and we don't want to have to remember to add
298 * a new method to this class. We also don't want to pollute this class with unnecessary methods.
299 *
300 * @param string $colname The column name
301 * @param \stdClass $data
302 * @return string
303 */
304 public function other_cols($colname, $data) {
305 // Do not process if it is not a part of the extra fields.
306 if (!in_array($colname, $this->extrafields)) {
307 return '';
308 }
309
310 return s($data->{$colname});
311 }
312
313 /**
314 * Query the database for results to display in the table.
315 *
316 * @param int $pagesize size of page for paginated displayed table.
317 * @param bool $useinitialsbar do you want to use the initials bar.
318 */
319 public function query_db($pagesize, $useinitialsbar = true) {
320 list($twhere, $tparams) = $this->get_sql_where();
321
f3ecea3a 322 $total = user_get_total_participants($this->course->id, $this->currentgroup, $this->accesssince,
bc47b706
MN
323 $this->roleid, $this->search, $twhere, $tparams);
324
325 $this->pagesize($pagesize, $total);
326
327 $sort = $this->get_sql_sort();
328 if ($sort) {
329 $sort = 'ORDER BY ' . $sort;
330 }
331
f3ecea3a 332 $this->rawdata = user_get_participants($this->course->id, $this->currentgroup, $this->accesssince,
bc47b706
MN
333 $this->roleid, $this->search, $twhere, $tparams, $sort, $this->get_page_start(),
334 $this->get_page_size());
335
336 // Set initial bars.
337 if ($useinitialsbar) {
338 $this->initialbars(true);
339 }
340 }
341}
342