Merge branch 'MDL-59831_master' of git://github.com/markn86/moodle
[moodle.git] / user / renderer.php
CommitLineData
82af55d7 1<?php
82af55d7
MD
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
82af55d7 17/**
a2ed6e69 18 * Provides user rendering functionality such as printing private files tree and displaying a search utility
82af55d7
MD
19 *
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
23 */
24
82af55d7
MD
25defined('MOODLE_INTERNAL') || die();
26
02d1a0a5 27/**
a2ed6e69 28 * Provides user rendering functionality such as printing private files tree and displaying a search utility
02d1a0a5
MA
29 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
82af55d7
MD
32class core_user_renderer extends plugin_renderer_base {
33
34 /**
35 * Prints user files tree view
36 * @return string
37 */
38 public function user_files_tree() {
39 return $this->render(new user_files_tree);
40 }
41
02d1a0a5
MA
42 /**
43 * Render user files tree
a2ed6e69 44 *
02d1a0a5 45 * @param user_files_tree $tree
a2ed6e69 46 * @return string HTML
02d1a0a5 47 */
82af55d7 48 public function render_user_files_tree(user_files_tree $tree) {
d0257a09
DC
49 if (empty($tree->dir['subdirs']) && empty($tree->dir['files'])) {
50 $html = $this->output->box(get_string('nofilesavailable', 'repository'));
51 } else {
52 $htmlid = 'user_files_tree_'.uniqid();
a2ed6e69 53 $module = array('name' => 'core_user', 'fullpath' => '/user/module.js');
d0257a09
DC
54 $this->page->requires->js_init_call('M.core_user.init_tree', array(false, $htmlid), false, $module);
55 $html = '<div id="'.$htmlid.'">';
56 $html .= $this->htmllize_tree($tree, $tree->dir);
57 $html .= '</div>';
58 }
82af55d7
MD
59 return $html;
60 }
61
62 /**
63 * Internal function - creates htmls structure suitable for YUI tree.
02d1a0a5
MA
64 * @param user_files_tree $tree
65 * @param array $dir
a2ed6e69 66 * @return string HTML
82af55d7
MD
67 */
68 protected function htmllize_tree($tree, $dir) {
69 global $CFG;
70 $yuiconfig = array();
71 $yuiconfig['type'] = 'html';
72
73 if (empty($dir['subdirs']) and empty($dir['files'])) {
74 return '';
75 }
76 $result = '<ul>';
77 foreach ($dir['subdirs'] as $subdir) {
a2ed6e69
SH
78 $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class' => 'icon'));
79 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.
80 $this->htmllize_tree($tree, $subdir).'</li>';
82af55d7
MD
81 }
82 foreach ($dir['files'] as $file) {
a2ed6e69
SH
83 $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/user/private'.
84 $file->get_filepath().$file->get_filename(), true);
82af55d7 85 $filename = $file->get_filename();
a2ed6e69
SH
86 $image = $this->output->pix_icon(file_file_icon($file), $filename, 'moodle', array('class' => 'icon'));
87 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.html_writer::link($url, $filename).
88 '</div></li>';
82af55d7
MD
89 }
90 $result .= '</ul>';
91
92 return $result;
93 }
cbe8e5b3
MA
94
95 /**
96 * Prints user search utility that can search user by first initial of firstname and/or first initial of lastname
97 * Prints a header with a title and the number of users found within that subset
98 * @param string $url the url to return to, complete with any parameters needed for the return
cbe8e5b3
MA
99 * @param string $firstinitial the first initial of the firstname
100 * @param string $lastinitial the first initial of the lastname
101 * @param int $usercount the amount of users meeting the search criteria
2b267d31 102 * @param int $totalcount the amount of users of the set/subset being searched
cbe8e5b3
MA
103 * @param string $heading heading of the subset being searched, default is All Participants
104 * @return string html output
105 */
41877849 106 public function user_search($url, $firstinitial, $lastinitial, $usercount, $totalcount, $heading = null) {
cbe8e5b3
MA
107 global $OUTPUT;
108
8b844f70
IT
109 if ($firstinitial !== 'all') {
110 set_user_preference('ifirst', $firstinitial);
111 }
112 if ($lastinitial !== 'all') {
113 set_user_preference('ilast', $lastinitial);
114 }
cbe8e5b3
MA
115
116 if (!isset($heading)) {
117 $heading = get_string('allparticipants');
118 }
119
120 $content = html_writer::start_tag('form', array('action' => new moodle_url($url)));
121 $content .= html_writer::start_tag('div');
122
41877849 123 // Search utility heading.
cbe8e5b3
MA
124 $content .= $OUTPUT->heading($heading.get_string('labelsep', 'langconfig').$usercount.'/'.$totalcount, 3);
125
8b844f70
IT
126 // Initials bar.
127 $prefixfirst = 'sifirst';
128 $prefixlast = 'silast';
129 $content .= $OUTPUT->initials_bar($firstinitial, 'firstinitial', get_string('firstname'), $prefixfirst, $url);
130 $content .= $OUTPUT->initials_bar($lastinitial, 'lastinitial', get_string('lastname'), $prefixlast, $url);
cbe8e5b3 131
cbe8e5b3 132 $content .= html_writer::end_tag('div');
f836166a 133 $content .= html_writer::tag('div', '&nbsp;');
cbe8e5b3
MA
134 $content .= html_writer::end_tag('form');
135
136 return $content;
137 }
138
c4e868d5
MG
139 /**
140 * Displays the list of tagged users
141 *
142 * @param array $userlist
143 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
144 * are displayed on the page and the per-page limit may be bigger
145 * @return string
146 */
147 public function user_list($userlist, $exclusivemode) {
148 $tagfeed = new core_tag\output\tagfeed();
149 foreach ($userlist as $user) {
150 $userpicture = $this->output->user_picture($user, array('size' => $exclusivemode ? 100 : 35));
151 $fullname = fullname($user);
152 if (user_can_view_profile($user)) {
153 $profilelink = new moodle_url('/user/view.php', array('id' => $user->id));
154 $fullname = html_writer::link($profilelink, $fullname);
155 }
156 $tagfeed->add($userpicture, $fullname);
157 }
158
159 $items = $tagfeed->export_for_template($this->output);
160
161 if ($exclusivemode) {
162 $output = '<div><ul class="inline-list">';
163 foreach ($items['items'] as $item) {
164 $output .= '<li><div class="user-box">'. $item['img'] . $item['heading'] ."</div></li>\n";
165 }
166 $output .= "</ul></div>\n";
167 return $output;
168 }
169
170 return $this->output->render_from_template('core_tag/tagfeed', $items);
171 }
172
9651e491
JP
173 /**
174 * Renders the unified filter element for the course participants page.
175 *
176 * @param stdClass $course The course object.
177 * @param context $context The context object.
178 * @param array $filtersapplied Array of currently applied filters.
179 * @return bool|string
180 */
181 public function unified_filter($course, $context, $filtersapplied) {
182 global $CFG, $DB, $USER;
183
184 $filteroptions = [];
185 $isfrontpage = ($course->id == SITEID);
186
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));
191 }
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);
207 }
208 $now = usergetmidnight(time());
209 $timeoptions = [];
210 $criteria = get_string('usersnoaccesssince');
211
212 // Days.
213 for ($i = 1; $i < 7; $i++) {
214 $timestamp = strtotime('-' . $i . ' days', $now);
215 if ($timestamp >= $minlastaccess) {
216 $value = get_string('numdays', 'moodle', $i);
217 $timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
218 }
219 }
220 // Weeks.
221 for ($i = 1; $i < 10; $i++) {
222 $timestamp = strtotime('-'.$i.' weeks', $now);
223 if ($timestamp >= $minlastaccess) {
224 $value = get_string('numweeks', 'moodle', $i);
225 $timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
226 }
227 }
228 // Months.
229 for ($i = 2; $i < 12; $i++) {
230 $timestamp = strtotime('-'.$i.' months', $now);
231 if ($timestamp >= $minlastaccess) {
232 $value = get_string('nummonths', 'moodle', $i);
233 $timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
234 }
235 }
236 // Try a year.
237 $timestamp = strtotime('-'.$i.' year', $now);
238 if ($timestamp >= $minlastaccess) {
239 $value = get_string('lastyear', 'moodle');
240 $timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
241 }
242 if (!empty($lastaccess0exists)) {
243 $value = get_string('never', 'moodle');
244 $timeoptions += $this->format_filter_option(USER_FILTER_LAST_ACCESS, $criteria, $timestamp, $value);
245 }
246 if (count($timeoptions) > 1) {
247 $filteroptions += $timeoptions;
248 }
249 }
250
251 require_once($CFG->dirroot . '/enrol/locallib.php');
252 $manager = new course_enrolment_manager($this->page, $course);
253
254 $canreviewenrol = has_capability('moodle/course:enrolreview', $context);
255
256 // Filter options for enrolment methods.
257 if ($canreviewenrol && $enrolmentmethods = $manager->get_enrolment_instance_names(true)) {
258 $criteria = get_string('enrolmentinstances', 'enrol');
259 $enroloptions = [];
260 foreach ($enrolmentmethods as $id => $enrolname) {
261 $enroloptions += $this->format_filter_option(USER_FILTER_ENROLMENT, $criteria, $id, $enrolname);
262 }
263 $filteroptions += $enroloptions;
264 }
265
266 // Filter options for groups, if available.
267 if ($course->groupmode != NOGROUPS) {
580a5588
MN
268 if (has_capability('moodle/site:accessallgroups', $context) || $course->groupmode == VISIBLEGROUPS) {
269 // List all groups if the user can access all groups, or we are in visible group mode.
9651e491
JP
270 $groups = $manager->get_all_groups();
271 } else {
272 // Otherwise, just list the groups the user belongs to.
273 $groups = groups_get_all_groups($course->id, $USER->id);
274 }
275 $criteria = get_string('group');
276 $groupoptions = [];
277 foreach ($groups as $id => $group) {
278 $groupoptions += $this->format_filter_option(USER_FILTER_GROUP, $criteria, $id, $group->name);
279 }
280 $filteroptions += $groupoptions;
281 }
282
283 // Filter options for role.
555a43a9 284 $roleseditable = has_capability('moodle/role:assign', $context);
d29be320 285 $roles = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true);
555a43a9 286 if ($roleseditable) {
d29be320 287 $roles += get_assignable_roles($context, ROLENAME_ALIAS);
555a43a9 288 }
9651e491
JP
289 $criteria = get_string('role');
290 $roleoptions = [];
291 foreach ($roles as $id => $role) {
292 $roleoptions += $this->format_filter_option(USER_FILTER_ROLE, $criteria, $id, $role);
293 }
294 $filteroptions += $roleoptions;
295
296 // Filter options for status.
297 if ($canreviewenrol) {
298 $criteria = get_string('status');
299 // Add statuses.
300 $filteroptions += $this->format_filter_option(USER_FILTER_STATUS, $criteria, ENROL_USER_ACTIVE, get_string('active'));
301 $filteroptions += $this->format_filter_option(USER_FILTER_STATUS, $criteria, ENROL_USER_SUSPENDED,
302 get_string('inactive'));
303 }
304
305 $indexpage = new \core_user\output\unified_filter($filteroptions, $filtersapplied);
306 $context = $indexpage->export_for_template($this->output);
307
308 return $this->output->render_from_template('core_user/unified_filter', $context);
309 }
310
311 /**
312 * Returns a formatted filter option.
313 *
314 * @param int $filtertype The filter type (e.g. status, role, group, enrolment, last access).
315 * @param string $criteria The string label of the filter type.
316 * @param int $value The value for the filter option.
317 * @param string $label The string representation of the filter option's value.
318 * @return array The formatted option with the ['filtertype:value' => 'criteria: label'] format.
319 */
320 protected function format_filter_option($filtertype, $criteria, $value, $label) {
321 $optionlabel = get_string('filteroption', 'moodle', (object)['criteria' => $criteria, 'value' => $label]);
322 $optionvalue = "$filtertype:$value";
323 return [$optionvalue => $optionlabel];
324 }
82af55d7
MD
325}
326
02d1a0a5
MA
327/**
328 * User files tree
329 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
330 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
331 */
82af55d7 332class user_files_tree implements renderable {
02d1a0a5 333
a2ed6e69
SH
334 /**
335 * @var context_user $context
336 */
82af55d7 337 public $context;
02d1a0a5 338
a2ed6e69
SH
339 /**
340 * @var array $dir
341 */
82af55d7 342 public $dir;
02d1a0a5
MA
343
344 /**
345 * Create user files tree object
346 */
82af55d7
MD
347 public function __construct() {
348 global $USER;
43731030 349 $this->context = context_user::instance($USER->id);
82af55d7
MD
350 $fs = get_file_storage();
351 $this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);
352 }
353}