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
78 /**
79 * @var string[] Extra fields to display.
80 */
81 protected $extrafields;
82
83 /**
84 * Sets up the table.
85 *
86 * @param int $courseid
87 * @param int|false $currentgroup False if groups not used, int if groups used, 0 for all groups.
88 * @param int $accesssince The time the user last accessed the site
89 * @param int $roleid The role we are including, 0 means all enrolled users
90 * @param string $search The string being searched
91 * @param bool $bulkoperations Is the user allowed to perform bulk operations?
92 * @param bool $selectall Has the user selected all users on the page?
93 */
94 public function __construct($courseid, $currentgroup, $accesssince, $roleid, $search,
95 $bulkoperations, $selectall) {
96 global $CFG;
97
98 parent::__construct('user-index-participants-' . $courseid);
99
100 // Get the context.
101 $context = \context_course::instance($courseid, MUST_EXIST);
102
103 // Define the headers and columns.
104 $headers = [];
105 $columns = [];
106
107 if ($bulkoperations) {
108 $headers[] = get_string('select');
109 $columns[] = 'select';
110 }
111
112 $headers[] = get_string('fullname');
113 $columns[] = 'fullname';
114
115 $extrafields = get_extra_user_fields($context);
116 foreach ($extrafields as $field) {
117 $headers[] = get_user_field_name($field);
118 $columns[] = $field;
119 }
120
121 // Get the list of fields we have to hide.
122 $hiddenfields = array();
123 if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
124 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
125 }
126
127 // Do not show the columns if it exists in the hiddenfields array.
128 if (!isset($hiddenfields['city'])) {
129 $headers[] = get_string('city');
130 $columns[] = 'city';
131 }
132 if (!isset($hiddenfields['country'])) {
133 $headers[] = get_string('country');
134 $columns[] = 'country';
135 }
136 if (!isset($hiddenfields['lastaccess'])) {
137 if ($courseid == SITEID) {
138 $headers[] = get_string('lastsiteaccess');
139 } else {
140 $headers[] = get_string('lastcourseaccess');
141 }
142 $columns[] = 'lastaccess';
143 }
144
145 $this->define_columns($columns);
146 $this->define_headers($headers);
147
148 $this->no_sorting('select');
149
150 $this->set_attribute('id', 'participants');
151
152 // Set the variables we need to use later.
153 $this->courseid = $courseid;
154 $this->currentgroup = $currentgroup;
155 $this->accesssince = $accesssince;
156 $this->roleid = $roleid;
157 $this->search = $search;
158 $this->selectall = $selectall;
159 $this->countries = get_string_manager()->get_list_of_countries();
160 $this->extrafields = $extrafields;
161 }
162
163 /**
164 * Generate the select column.
165 *
166 * @param \stdClass $data
167 * @return string
168 */
169 public function col_select($data) {
170 if ($this->selectall) {
171 $checked = 'checked="true"';
172 } else {
173 $checked = '';
174 }
175 return '<input type="checkbox" class="usercheckbox" name="user' . $data->id . '" ' . $checked . '/>';
176 }
177
178 /**
179 * Generate the fullname column.
180 *
181 * @param \stdClass $data
182 * @return string
183 */
184 public function col_fullname($data) {
185 global $OUTPUT;
186
187 return $OUTPUT->user_picture($data, array('size' => 35, 'courseid' => $this->courseid)) . ' ' . fullname($data);
188 }
189
190 /**
191 * Generate the city column.
192 *
193 * @param \stdClass $data
194 * @return string
195 */
196 public function col_city($data) {
197 return $data->city;
198 }
199
200 /**
201 * Generate the country column.
202 *
203 * @param \stdClass $data
204 * @return string
205 */
206 public function col_country($data) {
207 if (!empty($this->countries[$data->country])) {
208 return $this->countries[$data->country];
209 }
210 return '';
211 }
212
213 /**
214 * Generate the last access column.
215 *
216 * @param \stdClass $data
217 * @return string
218 */
219 public function col_lastaccess($data) {
220 if ($data->lastaccess) {
221 return format_time(time() - $data->lastaccess);
222 }
223
224 return get_string('never');
225 }
226
227 /**
228 * This function is used for the extra user fields.
229 *
230 * These are being dynamically added to the table so there are no functions 'col_<userfieldname>' as
231 * the list has the potential to increase in the future and we don't want to have to remember to add
232 * a new method to this class. We also don't want to pollute this class with unnecessary methods.
233 *
234 * @param string $colname The column name
235 * @param \stdClass $data
236 * @return string
237 */
238 public function other_cols($colname, $data) {
239 // Do not process if it is not a part of the extra fields.
240 if (!in_array($colname, $this->extrafields)) {
241 return '';
242 }
243
244 return s($data->{$colname});
245 }
246
247 /**
248 * Query the database for results to display in the table.
249 *
250 * @param int $pagesize size of page for paginated displayed table.
251 * @param bool $useinitialsbar do you want to use the initials bar.
252 */
253 public function query_db($pagesize, $useinitialsbar = true) {
254 list($twhere, $tparams) = $this->get_sql_where();
255
256 $total = user_get_total_participants($this->courseid, $this->currentgroup, $this->accesssince,
257 $this->roleid, $this->search, $twhere, $tparams);
258
259 $this->pagesize($pagesize, $total);
260
261 $sort = $this->get_sql_sort();
262 if ($sort) {
263 $sort = 'ORDER BY ' . $sort;
264 }
265
266 $this->rawdata = user_get_participants($this->courseid, $this->currentgroup, $this->accesssince,
267 $this->roleid, $this->search, $twhere, $tparams, $sort, $this->get_page_start(),
268 $this->get_page_size());
269
270 // Set initial bars.
271 if ($useinitialsbar) {
272 $this->initialbars(true);
273 }
274 }
275}
276