weekly release 2.7dev
[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/**
02d1a0a5
MA
18 * Provides user rendering functionality such as printing private files tree and
19 * displaying a search utility
82af55d7
MD
20 *
21 * @package core_user
22 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
82af55d7
MD
26defined('MOODLE_INTERNAL') || die();
27
02d1a0a5
MA
28/**
29 * Provides user rendering functionality such as printing private files tree and
30 * displaying a search utility
31 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
82af55d7
MD
34class core_user_renderer extends plugin_renderer_base {
35
36 /**
37 * Prints user files tree view
38 * @return string
39 */
40 public function user_files_tree() {
41 return $this->render(new user_files_tree);
42 }
43
02d1a0a5
MA
44 /**
45 * Render user files tree
46 * @param user_files_tree $tree
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();
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>';
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
82af55d7
MD
66 */
67 protected function htmllize_tree($tree, $dir) {
68 global $CFG;
69 $yuiconfig = array();
70 $yuiconfig['type'] = 'html';
71
72 if (empty($dir['subdirs']) and empty($dir['files'])) {
73 return '';
74 }
75 $result = '<ul>';
76 foreach ($dir['subdirs'] as $subdir) {
559276b1 77 $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class'=>'icon'));
82af55d7
MD
78 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
79 }
80 foreach ($dir['files'] as $file) {
81 $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/user/private'.$file->get_filepath().$file->get_filename(), true);
82 $filename = $file->get_filename();
559276b1 83 $image = $this->output->pix_icon(file_file_icon($file), $filename, 'moodle', array('class'=>'icon'));
82af55d7
MD
84 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.html_writer::link($url, $filename).'</div></li>';
85 }
86 $result .= '</ul>';
87
88 return $result;
89 }
cbe8e5b3
MA
90
91 /**
92 * Prints user search utility that can search user by first initial of firstname and/or first initial of lastname
93 * Prints a header with a title and the number of users found within that subset
94 * @param string $url the url to return to, complete with any parameters needed for the return
cbe8e5b3
MA
95 * @param string $firstinitial the first initial of the firstname
96 * @param string $lastinitial the first initial of the lastname
97 * @param int $usercount the amount of users meeting the search criteria
2b267d31 98 * @param int $totalcount the amount of users of the set/subset being searched
cbe8e5b3
MA
99 * @param string $heading heading of the subset being searched, default is All Participants
100 * @return string html output
101 */
41877849 102 public function user_search($url, $firstinitial, $lastinitial, $usercount, $totalcount, $heading = null) {
cbe8e5b3
MA
103 global $OUTPUT;
104
105 $strall = get_string('all');
106 $alpha = explode(',', get_string('alphabet', 'langconfig'));
107
108 if (!isset($heading)) {
109 $heading = get_string('allparticipants');
110 }
111
112 $content = html_writer::start_tag('form', array('action' => new moodle_url($url)));
113 $content .= html_writer::start_tag('div');
114
41877849 115 // Search utility heading.
cbe8e5b3
MA
116 $content .= $OUTPUT->heading($heading.get_string('labelsep', 'langconfig').$usercount.'/'.$totalcount, 3);
117
41877849 118 // Bar of first initials.
cbe8e5b3
MA
119 $content .= html_writer::start_tag('div', array('class' => 'initialbar firstinitial'));
120 $content .= html_writer::label(get_string('firstname').' : ', null);
121
122 if (!empty($firstinitial)) {
123 $content .= html_writer::link($url.'&sifirst=', $strall);
124 } else {
125 $content .= html_writer::tag('strong', $strall);
126 }
127
128 foreach ($alpha as $letter) {
129 if ($letter == $firstinitial) {
130 $content .= html_writer::tag('strong', $letter);
131 } else {
132 $content .= html_writer::link($url.'&sifirst='.$letter, $letter);
133 }
134 }
135 $content .= html_writer::end_tag('div');
136
02d1a0a5 137 // Bar of last initials.
cbe8e5b3
MA
138 $content .= html_writer::start_tag('div', array('class' => 'initialbar lastinitial'));
139 $content .= html_writer::label(get_string('lastname').' : ', null);
140
141 if (!empty($lastinitial)) {
142 $content .= html_writer::link($url.'&silast=', $strall);
143 } else {
144 $content .= html_writer::tag('strong', $strall);
145 }
146
147 foreach ($alpha as $letter) {
148 if ($letter == $lastinitial) {
149 $content .= html_writer::tag('strong', $letter);
150 } else {
151 $content .= html_writer::link($url.'&silast='.$letter, $letter);
152 }
153 }
154 $content .= html_writer::end_tag('div');
155
cbe8e5b3
MA
156 $content .= html_writer::end_tag('div');
157 $content .= html_writer::tag('div', '&nbsp');
158 $content .= html_writer::end_tag('form');
159
160 return $content;
161 }
162
82af55d7
MD
163}
164
02d1a0a5
MA
165/**
166 * User files tree
167 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
168 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
169 */
82af55d7 170class user_files_tree implements renderable {
02d1a0a5 171
82af55d7 172 public $context;
02d1a0a5 173
82af55d7 174 public $dir;
02d1a0a5
MA
175
176 /**
177 * Create user files tree object
178 */
82af55d7
MD
179 public function __construct() {
180 global $USER;
43731030 181 $this->context = context_user::instance($USER->id);
82af55d7
MD
182 $fs = get_file_storage();
183 $this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);
184 }
185}