forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
363 lines (326 loc) · 16.6 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Provides user rendering functionality such as printing private files tree and displaying a search utility
*
* @package core_user
* @copyright 2010 Dongsheng Cai <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Provides user rendering functionality such as printing private files tree and displaying a search utility
* @copyright 2010 Dongsheng Cai <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_user_renderer extends plugin_renderer_base {
/**
* Prints user search utility that can search user by first initial of firstname and/or first initial of lastname
* Prints a header with a title and the number of users found within that subset
* @param string $url the url to return to, complete with any parameters needed for the return
* @param string $firstinitial the first initial of the firstname
* @param string $lastinitial the first initial of the lastname
* @param int $usercount the amount of users meeting the search criteria
* @param int $totalcount the amount of users of the set/subset being searched
* @param string $heading heading of the subset being searched, default is All Participants
* @return string html output
*/
public function user_search($url, $firstinitial, $lastinitial, $usercount, $totalcount, $heading = null) {
if ($firstinitial !== 'all') {
set_user_preference('ifirst', $firstinitial);
}
if ($lastinitial !== 'all') {
set_user_preference('ilast', $lastinitial);
}
if (!isset($heading)) {
$heading = get_string('allparticipants');
}
$content = html_writer::start_tag('form', array('action' => new moodle_url($url)));
$content .= html_writer::start_tag('div');
// Search utility heading.
$content .= $this->output->heading($heading.get_string('labelsep', 'langconfig').$usercount.'/'.$totalcount, 3);
// Initials bar.
$prefixfirst = 'sifirst';
$prefixlast = 'silast';
$content .= $this->output->initials_bar($firstinitial, 'firstinitial', get_string('firstname'), $prefixfirst, $url);
$content .= $this->output->initials_bar($lastinitial, 'lastinitial', get_string('lastname'), $prefixlast, $url);
$content .= html_writer::end_tag('div');
$content .= html_writer::tag('div', ' ');
$content .= html_writer::end_tag('form');
return $content;
}
/**
* Displays the list of tagged users
*
* @param array $userlist
* @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
* are displayed on the page and the per-page limit may be bigger
* @return string
*/
public function user_list($userlist, $exclusivemode) {
$tagfeed = new core_tag\output\tagfeed();
foreach ($userlist as $user) {
$userpicture = $this->output->user_picture($user, array('size' => $exclusivemode ? 100 : 35));
$fullname = fullname($user);
if (user_can_view_profile($user)) {
$profilelink = new moodle_url('/user/view.php', array('id' => $user->id));
$fullname = html_writer::link($profilelink, $fullname);
}
$tagfeed->add($userpicture, $fullname);
}
$items = $tagfeed->export_for_template($this->output);
if ($exclusivemode) {
$output = '<div><ul class="inline-list">';
foreach ($items['items'] as $item) {
$output .= '<li><div class="user-box">'. $item['img'] . $item['heading'] ."</div></li>\n";
}
$output .= "</ul></div>\n";
return $output;
}
return $this->output->render_from_template('core_tag/tagfeed', $items);
}
/**
* Renders the unified filter element for the course participants page.
* @deprecated since Moodle 3.9 MDL-68612 - Please use participants_filter() instead.
*
* @param stdClass $course The course object.
* @param context $context The context object.
* @param array $filtersapplied Array of currently applied filters.
* @param string|moodle_url $baseurl The url with params needed to call up this page.
* @return bool|string
*/
public function unified_filter($course, $context, $filtersapplied, $baseurl = null) {
global $CFG, $DB, $USER;
debugging('core_user_renderer->unified_filter() is deprecated. Please use participants_filter() instead.', DEBUG_DEVELOPER);
require_once($CFG->dirroot . '/enrol/locallib.php');
require_once($CFG->dirroot . '/lib/grouplib.php');
$manager = new course_enrolment_manager($this->page, $course);
$filteroptions = [];
// Filter options for role.
$roleseditable = has_capability('moodle/role:assign', $context);
$roles = get_viewable_roles($context);
if ($roleseditable) {
$roles += get_assignable_roles($context, ROLENAME_ALIAS);
}
$criteria = get_string('role');
$roleoptions = $this->format_filter_option(USER_FILTER_ROLE, $criteria, -1, get_string('noroles', 'role'));
foreach ($roles as $id => $role) {
$roleoptions += $this->format_filter_option(USER_FILTER_ROLE, $criteria, $id, $role);
}
$filteroptions += $roleoptions;
// Filter options for groups, if available.
if (has_capability('moodle/site:accessallgroups', $context) || $course->groupmode != SEPARATEGROUPS) {
// List all groups if the user can access all groups, or we are in visible group mode or no groups mode.
$groups = $manager->get_all_groups();
if (!empty($groups)) {
// Add 'No group' option, to enable filtering users without any group.
$nogroup[USERSWITHOUTGROUP] = (object)['name' => get_string('nogroup', 'group')];
$groups = $nogroup + $groups;
}
} else {
// Otherwise, just list the groups the user belongs to.
$groups = groups_get_all_groups($course->id, $USER->id);
}
$criteria = get_string('group');
$groupoptions = [];
foreach ($groups as $id => $group) {
$groupoptions += $this->format_filter_option(USER_FILTER_GROUP, $criteria, $id, $group->name);
}
$filteroptions += $groupoptions;
$canreviewenrol = has_capability('moodle/course:enrolreview', $context);
// Filter options for status.
if ($canreviewenrol) {
$criteria = get_string('status');
// Add statuses.
$filteroptions += $this->format_filter_option(USER_FILTER_STATUS, $criteria, ENROL_USER_ACTIVE, get_string('active'));
$filteroptions += $this->format_filter_option(USER_FILTER_STATUS, $criteria, ENROL_USER_SUSPENDED,
get_string('inactive'));
}
// Filter options for enrolment methods.
if ($canreviewenrol && $enrolmentmethods = $manager->get_enrolment_instance_names(true)) {
$criteria = get_string('enrolmentinstances', 'enrol');
$enroloptions = [];
foreach ($enrolmentmethods as $id => $enrolname) {
$enroloptions += $this->format_filter_option(USER_FILTER_ENROLMENT, $criteria, $id, $enrolname);
}
$filteroptions += $enroloptions;
}
$isfrontpage = ($course->id == SITEID);
// Get the list of fields we have to hide.
$hiddenfields = array();
if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
$hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
}
$haslastaccess = !isset($hiddenfields['lastaccess']);
// Filter options for last access.
if ($haslastaccess) {
// Get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
// We need to make it diferently for normal courses and site course.
if (!$isfrontpage) {
$params = ['courseid' => $course->id, 'timeaccess' => 0];
$select = 'courseid = :courseid AND timeaccess != :timeaccess';
$minlastaccess = $DB->get_field_select('user_lastaccess', 'MIN(timeaccess)', $select, $params);
$lastaccess0exists = $DB->record_exists('user_lastaccess', $params);
} else {
$params = ['lastaccess' => 0];
$select = 'lastaccess != :lastaccess';
$minlastaccess = $DB->get_field_select('user', 'MIN(lastaccess)', $select, $params);
$lastaccess0exists = $DB->record_exists('user', $params);
}
$now = usergetmidnight(time());
$timeoptions = [];
$criteria = get_string('usersnoaccesssince');
// Days.
for ($i = 1; $i < 7; $i++) {
$timestamp = strtotime('-' . $i . ' days', $now);
if ($timestamp < $minlastaccess) {
break;
}
$value = get_string('numdays', 'moodle', $i);
$timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
}
// Weeks.
for ($i = 1; $i < 10; $i++) {
$timestamp = strtotime('-'.$i.' weeks', $now);
if ($timestamp < $minlastaccess) {
break;
}
$value = get_string('numweeks', 'moodle', $i);
$timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
}
// Months.
for ($i = 2; $i < 12; $i++) {
$timestamp = strtotime('-'.$i.' months', $now);
if ($timestamp < $minlastaccess) {
break;
}
$value = get_string('nummonths', 'moodle', $i);
$timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
}
// Try a year.
$timestamp = strtotime('-1 year', $now);
if ($timestamp >= $minlastaccess) {
$value = get_string('numyear', 'moodle', 1);
$timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
}
if (!empty($lastaccess0exists)) {
$value = get_string('never', 'moodle');
$timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
}
if (count($timeoptions) > 1) {
$filteroptions += $timeoptions;
}
}
// Add missing applied filters to the filter options.
$filteroptions = $this->handle_missing_applied_filters($filtersapplied, $filteroptions);
$indexpage = new \core_user\output\unified_filter($filteroptions, $filtersapplied, $baseurl);
$context = $indexpage->export_for_template($this->output);
return $this->output->render_from_template('core_user/unified_filter', $context);
}
/**
* Render the data required for the participants filter on the course participants page.
*
* @param context $context The context of the course being displayed
* @param string $tableregionid The table to be updated by this filter
* @return string
*/
public function participants_filter(context $context, string $tableregionid): string {
$renderable = new \core_user\output\participants_filter($context, $tableregionid);
$templatecontext = $renderable->export_for_template($this->output);
return $this->output->render_from_template('core_user/participantsfilter', $templatecontext);
}
/**
* Returns a formatted filter option.
*
* @param int $filtertype The filter type (e.g. status, role, group, enrolment, last access).
* @param string $criteria The string label of the filter type.
* @param int $value The value for the filter option.
* @param string $label The string representation of the filter option's value.
* @return array The formatted option with the ['filtertype:value' => 'criteria: label'] format.
*/
protected function format_filter_option($filtertype, $criteria, $value, $label) {
$optionlabel = get_string('filteroption', 'moodle', (object)['criteria' => $criteria, 'value' => $label]);
$optionvalue = "$filtertype:$value";
return [$optionvalue => $optionlabel];
}
/**
* Handles cases when after reloading the applied filters are missing in the filter options.
*
* @param array $filtersapplied The applied filters.
* @param array $filteroptions The filter options.
* @return array The formatted options with the ['filtertype:value' => 'criteria: label'] format.
*/
private function handle_missing_applied_filters($filtersapplied, $filteroptions) {
global $DB;
foreach ($filtersapplied as $filter) {
if (!array_key_exists($filter, $filteroptions)) {
$filtervalue = explode(':', $filter);
if (count($filtervalue) !== 2) {
continue;
}
$key = $filtervalue[0];
$value = $filtervalue[1];
switch($key) {
case USER_FILTER_LAST_ACCESS:
$now = usergetmidnight(time());
$criteria = get_string('usersnoaccesssince');
// Days.
for ($i = 1; $i < 7; $i++) {
$timestamp = strtotime('-' . $i . ' days', $now);
if ($timestamp < $value) {
break;
}
$val = get_string('numdays', 'moodle', $i);
$filteroptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $val);
}
// Weeks.
for ($i = 1; $i < 10; $i++) {
$timestamp = strtotime('-'.$i.' weeks', $now);
if ($timestamp < $value) {
break;
}
$val = get_string('numweeks', 'moodle', $i);
$filteroptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $val);
}
// Months.
for ($i = 2; $i < 12; $i++) {
$timestamp = strtotime('-'.$i.' months', $now);
if ($timestamp < $value) {
break;
}
$val = get_string('nummonths', 'moodle', $i);
$filteroptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $val);
}
// Try a year.
$timestamp = strtotime('-1 year', $now);
if ($timestamp >= $value) {
$val = get_string('numyear', 'moodle', 1);
$filteroptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $val);
}
break;
case USER_FILTER_ROLE:
$criteria = get_string('role');
if ($role = $DB->get_record('role', array('id' => $value))) {
$role = role_get_name($role);
$filteroptions += $this->format_filter_option(USER_FILTER_ROLE, $criteria, $value, $role);
}
break;
}
}
}
return $filteroptions;
}
}