MDL-59367 roles: Add roles to participants page.
[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
bc47b706
MN
98 /**
99 * Sets up the table.
100 *
101 * @param int $courseid
102 * @param int|false $currentgroup False if groups not used, int if groups used, 0 for all groups.
103 * @param int $accesssince The time the user last accessed the site
104 * @param int $roleid The role we are including, 0 means all enrolled users
105 * @param string $search The string being searched
106 * @param bool $bulkoperations Is the user allowed to perform bulk operations?
107 * @param bool $selectall Has the user selected all users on the page?
108 */
109 public function __construct($courseid, $currentgroup, $accesssince, $roleid, $search,
110 $bulkoperations, $selectall) {
111 global $CFG;
112
113 parent::__construct('user-index-participants-' . $courseid);
114
115 // Get the context.
2fa35b8d 116 $this->course = get_course($courseid);
bc47b706
MN
117 $context = \context_course::instance($courseid, MUST_EXIST);
118
119 // Define the headers and columns.
120 $headers = [];
121 $columns = [];
122
123 if ($bulkoperations) {
124 $headers[] = get_string('select');
125 $columns[] = 'select';
126 }
127
128 $headers[] = get_string('fullname');
129 $columns[] = 'fullname';
130
131 $extrafields = get_extra_user_fields($context);
132 foreach ($extrafields as $field) {
133 $headers[] = get_user_field_name($field);
134 $columns[] = $field;
135 }
136
5d0b4765
DW
137 $headers[] = get_string('roles');
138 $columns[] = 'roles';
139
2fa35b8d 140 // Load and cache the course groupinfo.
2fa35b8d
DW
141 // Add column for groups.
142 $headers[] = get_string('groups');
143 $columns[] = 'groups';
144
bc47b706
MN
145 // Get the list of fields we have to hide.
146 $hiddenfields = array();
147 if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
148 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
149 }
150
151 // Do not show the columns if it exists in the hiddenfields array.
152 if (!isset($hiddenfields['city'])) {
153 $headers[] = get_string('city');
154 $columns[] = 'city';
155 }
156 if (!isset($hiddenfields['country'])) {
157 $headers[] = get_string('country');
158 $columns[] = 'country';
159 }
160 if (!isset($hiddenfields['lastaccess'])) {
161 if ($courseid == SITEID) {
162 $headers[] = get_string('lastsiteaccess');
163 } else {
164 $headers[] = get_string('lastcourseaccess');
165 }
166 $columns[] = 'lastaccess';
167 }
168
169 $this->define_columns($columns);
170 $this->define_headers($headers);
171
172 $this->no_sorting('select');
173
174 $this->set_attribute('id', 'participants');
175
176 // Set the variables we need to use later.
bc47b706
MN
177 $this->currentgroup = $currentgroup;
178 $this->accesssince = $accesssince;
179 $this->roleid = $roleid;
180 $this->search = $search;
181 $this->selectall = $selectall;
182 $this->countries = get_string_manager()->get_list_of_countries();
183 $this->extrafields = $extrafields;
f3ecea3a
DW
184 $this->context = $context;
185 $this->groups = groups_get_all_groups($courseid, 0, 0, 'g.*', true);
5d0b4765
DW
186 $this->allroles = role_fix_names(get_all_roles($this->context), $this->context);
187 $this->assignableroles = get_assignable_roles($this->context, ROLENAME_ALIAS, false);
bc47b706
MN
188 }
189
190 /**
191 * Generate the select column.
192 *
193 * @param \stdClass $data
194 * @return string
195 */
196 public function col_select($data) {
197 if ($this->selectall) {
198 $checked = 'checked="true"';
199 } else {
200 $checked = '';
201 }
202 return '<input type="checkbox" class="usercheckbox" name="user' . $data->id . '" ' . $checked . '/>';
203 }
204
205 /**
206 * Generate the fullname column.
207 *
208 * @param \stdClass $data
209 * @return string
210 */
211 public function col_fullname($data) {
212 global $OUTPUT;
213
ed451192 214 return $OUTPUT->user_picture($data, array('size' => 35, 'courseid' => $this->course->id)) . ' ' . fullname($data);
bc47b706
MN
215 }
216
5d0b4765
DW
217 /**
218 * User roles column.
219 *
220 * @param \stdClass $data
221 * @return string
222 */
223 public function col_roles($data) {
224 global $OUTPUT;
225
226 $roles = get_user_roles($this->context, $data->id, true, 'c.contextlevel DESC, r.sortorder ASC');
227 $getrole = function($role) {
228 return $role->roleid;
229 };
230 $ids = array_values(array_map($getrole, $roles));
231
232 $editable = new \core_user\output\user_roles_editable($this->course,
233 $this->context,
234 $data,
235 $this->allroles,
236 $this->assignableroles,
237 $ids);
238
239 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
240 }
241
2fa35b8d
DW
242 /**
243 * Generate the groups column.
244 *
f3ecea3a 245 * @param \stdClass $data
2fa35b8d
DW
246 * @return string
247 */
f3ecea3a 248 public function col_groups($data) {
2fa35b8d
DW
249 global $OUTPUT;
250
251 $usergroups = [];
252 foreach ($this->groups as $coursegroup) {
f3ecea3a 253 if (isset($coursegroup->members[$data->id])) {
2fa35b8d
DW
254 $usergroups[] = $coursegroup->id;
255 }
256 }
f3ecea3a 257 $editable = new \core_group\output\user_groups_editable($this->course, $this->context, $data, $this->groups, $usergroups);
2fa35b8d
DW
258 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
259 }
260
bc47b706
MN
261 /**
262 * Generate the city column.
263 *
264 * @param \stdClass $data
265 * @return string
266 */
267 public function col_city($data) {
268 return $data->city;
269 }
270
271 /**
272 * Generate the country column.
273 *
274 * @param \stdClass $data
275 * @return string
276 */
277 public function col_country($data) {
278 if (!empty($this->countries[$data->country])) {
279 return $this->countries[$data->country];
280 }
281 return '';
282 }
283
284 /**
285 * Generate the last access column.
286 *
287 * @param \stdClass $data
288 * @return string
289 */
290 public function col_lastaccess($data) {
291 if ($data->lastaccess) {
292 return format_time(time() - $data->lastaccess);
293 }
294
295 return get_string('never');
296 }
297
298 /**
299 * This function is used for the extra user fields.
300 *
301 * These are being dynamically added to the table so there are no functions 'col_<userfieldname>' as
302 * the list has the potential to increase in the future and we don't want to have to remember to add
303 * a new method to this class. We also don't want to pollute this class with unnecessary methods.
304 *
305 * @param string $colname The column name
306 * @param \stdClass $data
307 * @return string
308 */
309 public function other_cols($colname, $data) {
310 // Do not process if it is not a part of the extra fields.
311 if (!in_array($colname, $this->extrafields)) {
312 return '';
313 }
314
315 return s($data->{$colname});
316 }
317
318 /**
319 * Query the database for results to display in the table.
320 *
321 * @param int $pagesize size of page for paginated displayed table.
322 * @param bool $useinitialsbar do you want to use the initials bar.
323 */
324 public function query_db($pagesize, $useinitialsbar = true) {
325 list($twhere, $tparams) = $this->get_sql_where();
326
f3ecea3a 327 $total = user_get_total_participants($this->course->id, $this->currentgroup, $this->accesssince,
bc47b706
MN
328 $this->roleid, $this->search, $twhere, $tparams);
329
330 $this->pagesize($pagesize, $total);
331
332 $sort = $this->get_sql_sort();
333 if ($sort) {
334 $sort = 'ORDER BY ' . $sort;
335 }
336
f3ecea3a 337 $this->rawdata = user_get_participants($this->course->id, $this->currentgroup, $this->accesssince,
bc47b706
MN
338 $this->roleid, $this->search, $twhere, $tparams, $sort, $this->get_page_start(),
339 $this->get_page_size());
340
341 // Set initial bars.
342 if ($useinitialsbar) {
343 $this->initialbars(true);
344 }
345 }
346}
347