MDL-53451 competency: Improve performance by bypassing api::is_enabled()
[moodle.git] / user / renderer.php
blobc12f579d26689cd1418bd4c3064bd54950cde78c
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 files tree view
36 * @return string
38 public function user_files_tree() {
39 return $this->render(new user_files_tree);
42 /**
43 * Render user files tree
45 * @param user_files_tree $tree
46 * @return string HTML
48 public function render_user_files_tree(user_files_tree $tree) {
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();
53 $module = array('name' => 'core_user', 'fullpath' => '/user/module.js');
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>';
59 return $html;
62 /**
63 * Internal function - creates htmls structure suitable for YUI tree.
64 * @param user_files_tree $tree
65 * @param array $dir
66 * @return string HTML
68 protected function htmllize_tree($tree, $dir) {
69 global $CFG;
70 $yuiconfig = array();
71 $yuiconfig['type'] = 'html';
73 if (empty($dir['subdirs']) and empty($dir['files'])) {
74 return '';
76 $result = '<ul>';
77 foreach ($dir['subdirs'] as $subdir) {
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>';
82 foreach ($dir['files'] as $file) {
83 $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/user/private'.
84 $file->get_filepath().$file->get_filename(), true);
85 $filename = $file->get_filename();
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>';
90 $result .= '</ul>';
92 return $result;
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
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
102 * @param int $totalcount the amount of users of the set/subset being searched
103 * @param string $heading heading of the subset being searched, default is All Participants
104 * @return string html output
106 public function user_search($url, $firstinitial, $lastinitial, $usercount, $totalcount, $heading = null) {
107 global $OUTPUT;
109 $strall = get_string('all');
110 $alpha = explode(',', get_string('alphabet', 'langconfig'));
112 if (!isset($heading)) {
113 $heading = get_string('allparticipants');
116 $content = html_writer::start_tag('form', array('action' => new moodle_url($url)));
117 $content .= html_writer::start_tag('div');
119 // Search utility heading.
120 $content .= $OUTPUT->heading($heading.get_string('labelsep', 'langconfig').$usercount.'/'.$totalcount, 3);
122 // Bar of first initials.
123 $content .= html_writer::start_tag('div', array('class' => 'initialbar firstinitial'));
124 $content .= html_writer::label(get_string('firstname').' : ', null);
126 if (!empty($firstinitial)) {
127 $content .= html_writer::link($url.'&sifirst=', $strall);
128 } else {
129 $content .= html_writer::tag('strong', $strall);
132 foreach ($alpha as $letter) {
133 if ($letter == $firstinitial) {
134 $content .= html_writer::tag('strong', $letter);
135 } else {
136 $content .= html_writer::link($url.'&sifirst='.$letter, $letter);
139 $content .= html_writer::end_tag('div');
141 // Bar of last initials.
142 $content .= html_writer::start_tag('div', array('class' => 'initialbar lastinitial'));
143 $content .= html_writer::label(get_string('lastname').' : ', null);
145 if (!empty($lastinitial)) {
146 $content .= html_writer::link($url.'&silast=', $strall);
147 } else {
148 $content .= html_writer::tag('strong', $strall);
151 foreach ($alpha as $letter) {
152 if ($letter == $lastinitial) {
153 $content .= html_writer::tag('strong', $letter);
154 } else {
155 $content .= html_writer::link($url.'&silast='.$letter, $letter);
158 $content .= html_writer::end_tag('div');
160 $content .= html_writer::end_tag('div');
161 $content .= html_writer::tag('div', '&nbsp;');
162 $content .= html_writer::end_tag('form');
164 return $content;
168 * Displays the list of tagged users
170 * @param array $userlist
171 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
172 * are displayed on the page and the per-page limit may be bigger
173 * @return string
175 public function user_list($userlist, $exclusivemode) {
176 $tagfeed = new core_tag\output\tagfeed();
177 foreach ($userlist as $user) {
178 $userpicture = $this->output->user_picture($user, array('size' => $exclusivemode ? 100 : 35));
179 $fullname = fullname($user);
180 if (user_can_view_profile($user)) {
181 $profilelink = new moodle_url('/user/view.php', array('id' => $user->id));
182 $fullname = html_writer::link($profilelink, $fullname);
184 $tagfeed->add($userpicture, $fullname);
187 $items = $tagfeed->export_for_template($this->output);
189 if ($exclusivemode) {
190 $output = '<div><ul class="inline-list">';
191 foreach ($items['items'] as $item) {
192 $output .= '<li><div class="user-box">'. $item['img'] . $item['heading'] ."</div></li>\n";
194 $output .= "</ul></div>\n";
195 return $output;
198 return $this->output->render_from_template('core_tag/tagfeed', $items);
204 * User files tree
205 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
206 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
208 class user_files_tree implements renderable {
211 * @var context_user $context
213 public $context;
216 * @var array $dir
218 public $dir;
221 * Create user files tree object
223 public function __construct() {
224 global $USER;
225 $this->context = context_user::instance($USER->id);
226 $fs = get_file_storage();
227 $this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);