Merge branch 'master-MDL-66805-search' of https://github.com/kristian-94/moodle
[moodle.git] / user / renderer.php
bloba1f22c1aa37a727f52f5d6f1907a0fb9589624f2
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.
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/>.
17 /**
18 * Provides user rendering functionality such as printing private files tree and displaying a search utility
20 * @package core_user
21 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Provides user rendering functionality such as printing private files tree and displaying a search utility
29 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class core_user_renderer extends plugin_renderer_base {
34 /**
35 * Prints user search utility that can search user by first initial of firstname and/or first initial of lastname
36 * Prints a header with a title and the number of users found within that subset
37 * @param string $url the url to return to, complete with any parameters needed for the return
38 * @param string $firstinitial the first initial of the firstname
39 * @param string $lastinitial the first initial of the lastname
40 * @param int $usercount the amount of users meeting the search criteria
41 * @param int $totalcount the amount of users of the set/subset being searched
42 * @param string $heading heading of the subset being searched, default is All Participants
43 * @return string html output
45 public function user_search($url, $firstinitial, $lastinitial, $usercount, $totalcount, $heading = null) {
46 global $OUTPUT;
48 if ($firstinitial !== 'all') {
49 set_user_preference('ifirst', $firstinitial);
51 if ($lastinitial !== 'all') {
52 set_user_preference('ilast', $lastinitial);
55 if (!isset($heading)) {
56 $heading = get_string('allparticipants');
59 $content = html_writer::start_tag('form', array('action' => new moodle_url($url)));
60 $content .= html_writer::start_tag('div');
62 // Search utility heading.
63 $content .= $OUTPUT->heading($heading.get_string('labelsep', 'langconfig').$usercount.'/'.$totalcount, 3);
65 // Initials bar.
66 $prefixfirst = 'sifirst';
67 $prefixlast = 'silast';
68 $content .= $OUTPUT->initials_bar($firstinitial, 'firstinitial', get_string('firstname'), $prefixfirst, $url);
69 $content .= $OUTPUT->initials_bar($lastinitial, 'lastinitial', get_string('lastname'), $prefixlast, $url);
71 $content .= html_writer::end_tag('div');
72 $content .= html_writer::tag('div', '&nbsp;');
73 $content .= html_writer::end_tag('form');
75 return $content;
78 /**
79 * Displays the list of tagged users
81 * @param array $userlist
82 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
83 * are displayed on the page and the per-page limit may be bigger
84 * @return string
86 public function user_list($userlist, $exclusivemode) {
87 $tagfeed = new core_tag\output\tagfeed();
88 foreach ($userlist as $user) {
89 $userpicture = $this->output->user_picture($user, array('size' => $exclusivemode ? 100 : 35));
90 $fullname = fullname($user);
91 if (user_can_view_profile($user)) {
92 $profilelink = new moodle_url('/user/view.php', array('id' => $user->id));
93 $fullname = html_writer::link($profilelink, $fullname);
95 $tagfeed->add($userpicture, $fullname);
98 $items = $tagfeed->export_for_template($this->output);
100 if ($exclusivemode) {
101 $output = '<div><ul class="inline-list">';
102 foreach ($items['items'] as $item) {
103 $output .= '<li><div class="user-box">'. $item['img'] . $item['heading'] ."</div></li>\n";
105 $output .= "</ul></div>\n";
106 return $output;
109 return $this->output->render_from_template('core_tag/tagfeed', $items);
113 * Renders the unified filter element for the course participants page.
115 * @param stdClass $course The course object.
116 * @param context $context The context object.
117 * @param array $filtersapplied Array of currently applied filters.
118 * @param string|moodle_url $baseurl The url with params needed to call up this page.
119 * @return bool|string
121 public function unified_filter($course, $context, $filtersapplied, $baseurl = null) {
122 global $CFG, $DB, $USER;
124 require_once($CFG->dirroot . '/enrol/locallib.php');
125 require_once($CFG->dirroot . '/lib/grouplib.php');
126 $manager = new course_enrolment_manager($this->page, $course);
128 $filteroptions = [];
130 // Filter options for role.
131 $roleseditable = has_capability('moodle/role:assign', $context);
132 $roles = get_viewable_roles($context);
133 if ($roleseditable) {
134 $roles += get_assignable_roles($context, ROLENAME_ALIAS);
137 $criteria = get_string('role');
138 $roleoptions = $this->format_filter_option(USER_FILTER_ROLE, $criteria, -1, get_string('noroles', 'role'));
139 foreach ($roles as $id => $role) {
140 $roleoptions += $this->format_filter_option(USER_FILTER_ROLE, $criteria, $id, $role);
142 $filteroptions += $roleoptions;
144 // Filter options for groups, if available.
145 if (has_capability('moodle/site:accessallgroups', $context) || $course->groupmode != SEPARATEGROUPS) {
146 // List all groups if the user can access all groups, or we are in visible group mode or no groups mode.
147 $groups = $manager->get_all_groups();
148 if (!empty($groups)) {
149 // Add 'No group' option, to enable filtering users without any group.
150 $nogroup[USERSWITHOUTGROUP] = (object)['name' => get_string('nogroup', 'group')];
151 $groups = $nogroup + $groups;
153 } else {
154 // Otherwise, just list the groups the user belongs to.
155 $groups = groups_get_all_groups($course->id, $USER->id);
157 $criteria = get_string('group');
158 $groupoptions = [];
159 foreach ($groups as $id => $group) {
160 $groupoptions += $this->format_filter_option(USER_FILTER_GROUP, $criteria, $id, $group->name);
162 $filteroptions += $groupoptions;
164 $canreviewenrol = has_capability('moodle/course:enrolreview', $context);
166 // Filter options for status.
167 if ($canreviewenrol) {
168 $criteria = get_string('status');
169 // Add statuses.
170 $filteroptions += $this->format_filter_option(USER_FILTER_STATUS, $criteria, ENROL_USER_ACTIVE, get_string('active'));
171 $filteroptions += $this->format_filter_option(USER_FILTER_STATUS, $criteria, ENROL_USER_SUSPENDED,
172 get_string('inactive'));
175 // Filter options for enrolment methods.
176 if ($canreviewenrol && $enrolmentmethods = $manager->get_enrolment_instance_names(true)) {
177 $criteria = get_string('enrolmentinstances', 'enrol');
178 $enroloptions = [];
179 foreach ($enrolmentmethods as $id => $enrolname) {
180 $enroloptions += $this->format_filter_option(USER_FILTER_ENROLMENT, $criteria, $id, $enrolname);
182 $filteroptions += $enroloptions;
185 $isfrontpage = ($course->id == SITEID);
187 // Get the list of fields we have to hide.
188 $hiddenfields = array();
189 if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
190 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
192 $haslastaccess = !isset($hiddenfields['lastaccess']);
193 // Filter options for last access.
194 if ($haslastaccess) {
195 // Get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
196 // We need to make it diferently for normal courses and site course.
197 if (!$isfrontpage) {
198 $params = ['courseid' => $course->id, 'timeaccess' => 0];
199 $select = 'courseid = :courseid AND timeaccess != :timeaccess';
200 $minlastaccess = $DB->get_field_select('user_lastaccess', 'MIN(timeaccess)', $select, $params);
201 $lastaccess0exists = $DB->record_exists('user_lastaccess', $params);
202 } else {
203 $params = ['lastaccess' => 0];
204 $select = 'lastaccess != :lastaccess';
205 $minlastaccess = $DB->get_field_select('user', 'MIN(lastaccess)', $select, $params);
206 $lastaccess0exists = $DB->record_exists('user', $params);
208 $now = usergetmidnight(time());
209 $timeoptions = [];
210 $criteria = get_string('usersnoaccesssince');
212 // Days.
213 for ($i = 1; $i < 7; $i++) {
214 $timestamp = strtotime('-' . $i . ' days', $now);
215 if ($timestamp < $minlastaccess) {
216 break;
218 $value = get_string('numdays', 'moodle', $i);
219 $timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
221 // Weeks.
222 for ($i = 1; $i < 10; $i++) {
223 $timestamp = strtotime('-'.$i.' weeks', $now);
224 if ($timestamp < $minlastaccess) {
225 break;
227 $value = get_string('numweeks', 'moodle', $i);
228 $timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
230 // Months.
231 for ($i = 2; $i < 12; $i++) {
232 $timestamp = strtotime('-'.$i.' months', $now);
233 if ($timestamp < $minlastaccess) {
234 break;
236 $value = get_string('nummonths', 'moodle', $i);
237 $timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
239 // Try a year.
240 $timestamp = strtotime('-1 year', $now);
241 if ($timestamp >= $minlastaccess) {
242 $value = get_string('numyear', 'moodle', 1);
243 $timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
245 if (!empty($lastaccess0exists)) {
246 $value = get_string('never', 'moodle');
247 $timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
249 if (count($timeoptions) > 1) {
250 $filteroptions += $timeoptions;
254 // Add missing applied filters to the filter options.
255 $filteroptions = $this->handle_missing_applied_filters($filtersapplied, $filteroptions);
257 $indexpage = new \core_user\output\unified_filter($filteroptions, $filtersapplied, $baseurl);
258 $context = $indexpage->export_for_template($this->output);
260 return $this->output->render_from_template('core_user/unified_filter', $context);
264 * Returns a formatted filter option.
266 * @param int $filtertype The filter type (e.g. status, role, group, enrolment, last access).
267 * @param string $criteria The string label of the filter type.
268 * @param int $value The value for the filter option.
269 * @param string $label The string representation of the filter option's value.
270 * @return array The formatted option with the ['filtertype:value' => 'criteria: label'] format.
272 protected function format_filter_option($filtertype, $criteria, $value, $label) {
273 $optionlabel = get_string('filteroption', 'moodle', (object)['criteria' => $criteria, 'value' => $label]);
274 $optionvalue = "$filtertype:$value";
275 return [$optionvalue => $optionlabel];
279 * Handles cases when after reloading the applied filters are missing in the filter options.
281 * @param array $filtersapplied The applied filters.
282 * @param array $filteroptions The filter options.
283 * @return array The formatted options with the ['filtertype:value' => 'criteria: label'] format.
285 private function handle_missing_applied_filters($filtersapplied, $filteroptions) {
286 global $DB;
288 foreach ($filtersapplied as $filter) {
289 if (!array_key_exists($filter, $filteroptions)) {
290 $filtervalue = explode(':', $filter);
291 if (count($filtervalue) !== 2) {
292 continue;
294 $key = $filtervalue[0];
295 $value = $filtervalue[1];
297 switch($key) {
298 case USER_FILTER_LAST_ACCESS:
299 $now = usergetmidnight(time());
300 $criteria = get_string('usersnoaccesssince');
301 // Days.
302 for ($i = 1; $i < 7; $i++) {
303 $timestamp = strtotime('-' . $i . ' days', $now);
304 if ($timestamp < $value) {
305 break;
307 $val = get_string('numdays', 'moodle', $i);
308 $filteroptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $val);
310 // Weeks.
311 for ($i = 1; $i < 10; $i++) {
312 $timestamp = strtotime('-'.$i.' weeks', $now);
313 if ($timestamp < $value) {
314 break;
316 $val = get_string('numweeks', 'moodle', $i);
317 $filteroptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $val);
319 // Months.
320 for ($i = 2; $i < 12; $i++) {
321 $timestamp = strtotime('-'.$i.' months', $now);
322 if ($timestamp < $value) {
323 break;
325 $val = get_string('nummonths', 'moodle', $i);
326 $filteroptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $val);
328 // Try a year.
329 $timestamp = strtotime('-1 year', $now);
330 if ($timestamp >= $value) {
331 $val = get_string('numyear', 'moodle', 1);
332 $filteroptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $val);
334 break;
335 case USER_FILTER_ROLE:
336 $criteria = get_string('role');
337 if ($role = $DB->get_record('role', array('id' => $value))) {
338 $role = role_get_name($role);
339 $filteroptions += $this->format_filter_option(USER_FILTER_ROLE, $criteria, $value, $role);
341 break;
345 return $filteroptions;