weekly release 3.4dev
[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
2fa35b8d 137 // Load and cache the course groupinfo.
2fa35b8d
DW
138 // Add column for groups.
139 $headers[] = get_string('groups');
140 $columns[] = 'groups';
141
bc47b706
MN
142 // Get the list of fields we have to hide.
143 $hiddenfields = array();
144 if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
145 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
146 }
147
148 // Do not show the columns if it exists in the hiddenfields array.
149 if (!isset($hiddenfields['city'])) {
150 $headers[] = get_string('city');
151 $columns[] = 'city';
152 }
153 if (!isset($hiddenfields['country'])) {
154 $headers[] = get_string('country');
155 $columns[] = 'country';
156 }
157 if (!isset($hiddenfields['lastaccess'])) {
158 if ($courseid == SITEID) {
159 $headers[] = get_string('lastsiteaccess');
160 } else {
161 $headers[] = get_string('lastcourseaccess');
162 }
163 $columns[] = 'lastaccess';
164 }
165
166 $this->define_columns($columns);
167 $this->define_headers($headers);
168
169 $this->no_sorting('select');
170
171 $this->set_attribute('id', 'participants');
172
173 // Set the variables we need to use later.
bc47b706
MN
174 $this->currentgroup = $currentgroup;
175 $this->accesssince = $accesssince;
176 $this->roleid = $roleid;
177 $this->search = $search;
178 $this->selectall = $selectall;
179 $this->countries = get_string_manager()->get_list_of_countries();
180 $this->extrafields = $extrafields;
f3ecea3a
DW
181 $this->context = $context;
182 $this->groups = groups_get_all_groups($courseid, 0, 0, 'g.*', true);
bc47b706
MN
183 }
184
185 /**
186 * Generate the select column.
187 *
188 * @param \stdClass $data
189 * @return string
190 */
191 public function col_select($data) {
192 if ($this->selectall) {
193 $checked = 'checked="true"';
194 } else {
195 $checked = '';
196 }
197 return '<input type="checkbox" class="usercheckbox" name="user' . $data->id . '" ' . $checked . '/>';
198 }
199
200 /**
201 * Generate the fullname column.
202 *
203 * @param \stdClass $data
204 * @return string
205 */
206 public function col_fullname($data) {
207 global $OUTPUT;
208
ed451192 209 return $OUTPUT->user_picture($data, array('size' => 35, 'courseid' => $this->course->id)) . ' ' . fullname($data);
bc47b706
MN
210 }
211
2fa35b8d
DW
212 /**
213 * Generate the groups column.
214 *
f3ecea3a 215 * @param \stdClass $data
2fa35b8d
DW
216 * @return string
217 */
f3ecea3a 218 public function col_groups($data) {
2fa35b8d
DW
219 global $OUTPUT;
220
221 $usergroups = [];
222 foreach ($this->groups as $coursegroup) {
f3ecea3a 223 if (isset($coursegroup->members[$data->id])) {
2fa35b8d
DW
224 $usergroups[] = $coursegroup->id;
225 }
226 }
f3ecea3a 227 $editable = new \core_group\output\user_groups_editable($this->course, $this->context, $data, $this->groups, $usergroups);
2fa35b8d
DW
228 return $OUTPUT->render_from_template('core/inplace_editable', $editable->export_for_template($OUTPUT));
229 }
230
bc47b706
MN
231 /**
232 * Generate the city column.
233 *
234 * @param \stdClass $data
235 * @return string
236 */
237 public function col_city($data) {
238 return $data->city;
239 }
240
241 /**
242 * Generate the country column.
243 *
244 * @param \stdClass $data
245 * @return string
246 */
247 public function col_country($data) {
248 if (!empty($this->countries[$data->country])) {
249 return $this->countries[$data->country];
250 }
251 return '';
252 }
253
254 /**
255 * Generate the last access column.
256 *
257 * @param \stdClass $data
258 * @return string
259 */
260 public function col_lastaccess($data) {
261 if ($data->lastaccess) {
262 return format_time(time() - $data->lastaccess);
263 }
264
265 return get_string('never');
266 }
267
268 /**
269 * This function is used for the extra user fields.
270 *
271 * These are being dynamically added to the table so there are no functions 'col_<userfieldname>' as
272 * the list has the potential to increase in the future and we don't want to have to remember to add
273 * a new method to this class. We also don't want to pollute this class with unnecessary methods.
274 *
275 * @param string $colname The column name
276 * @param \stdClass $data
277 * @return string
278 */
279 public function other_cols($colname, $data) {
280 // Do not process if it is not a part of the extra fields.
281 if (!in_array($colname, $this->extrafields)) {
282 return '';
283 }
284
285 return s($data->{$colname});
286 }
287
288 /**
289 * Query the database for results to display in the table.
290 *
291 * @param int $pagesize size of page for paginated displayed table.
292 * @param bool $useinitialsbar do you want to use the initials bar.
293 */
294 public function query_db($pagesize, $useinitialsbar = true) {
295 list($twhere, $tparams) = $this->get_sql_where();
296
f3ecea3a 297 $total = user_get_total_participants($this->course->id, $this->currentgroup, $this->accesssince,
bc47b706
MN
298 $this->roleid, $this->search, $twhere, $tparams);
299
300 $this->pagesize($pagesize, $total);
301
302 $sort = $this->get_sql_sort();
303 if ($sort) {
304 $sort = 'ORDER BY ' . $sort;
305 }
306
f3ecea3a 307 $this->rawdata = user_get_participants($this->course->id, $this->currentgroup, $this->accesssince,
bc47b706
MN
308 $this->roleid, $this->search, $twhere, $tparams, $sort, $this->get_page_start(),
309 $this->get_page_size());
310
311 // Set initial bars.
312 if ($useinitialsbar) {
313 $this->initialbars(true);
314 }
315 }
316}
317