forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
185 lines (159 loc) · 6.95 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
<?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 files tree view
* @return string
*/
public function user_files_tree() {
return $this->render(new user_files_tree);
}
/**
* Render user files tree
* @param user_files_tree $tree
*/
public function render_user_files_tree(user_files_tree $tree) {
if (empty($tree->dir['subdirs']) && empty($tree->dir['files'])) {
$html = $this->output->box(get_string('nofilesavailable', 'repository'));
} else {
$htmlid = 'user_files_tree_'.uniqid();
$module = array('name'=>'core_user', 'fullpath'=>'/user/module.js');
$this->page->requires->js_init_call('M.core_user.init_tree', array(false, $htmlid), false, $module);
$html = '<div id="'.$htmlid.'">';
$html .= $this->htmllize_tree($tree, $tree->dir);
$html .= '</div>';
}
return $html;
}
/**
* Internal function - creates htmls structure suitable for YUI tree.
* @param user_files_tree $tree
* @param array $dir
*/
protected function htmllize_tree($tree, $dir) {
global $CFG;
$yuiconfig = array();
$yuiconfig['type'] = 'html';
if (empty($dir['subdirs']) and empty($dir['files'])) {
return '';
}
$result = '<ul>';
foreach ($dir['subdirs'] as $subdir) {
$image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class'=>'icon'));
$result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
}
foreach ($dir['files'] as $file) {
$url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/user/private'.$file->get_filepath().$file->get_filename(), true);
$filename = $file->get_filename();
$image = $this->output->pix_icon(file_file_icon($file), $filename, 'moodle', array('class'=>'icon'));
$result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.html_writer::link($url, $filename).'</div></li>';
}
$result .= '</ul>';
return $result;
}
/**
* 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) {
global $OUTPUT;
$strall = get_string('all');
$alpha = explode(',', get_string('alphabet', 'langconfig'));
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 .= $OUTPUT->heading($heading.get_string('labelsep', 'langconfig').$usercount.'/'.$totalcount, 3);
// Bar of first initials.
$content .= html_writer::start_tag('div', array('class' => 'initialbar firstinitial'));
$content .= html_writer::label(get_string('firstname').' : ', null);
if (!empty($firstinitial)) {
$content .= html_writer::link($url.'&sifirst=', $strall);
} else {
$content .= html_writer::tag('strong', $strall);
}
foreach ($alpha as $letter) {
if ($letter == $firstinitial) {
$content .= html_writer::tag('strong', $letter);
} else {
$content .= html_writer::link($url.'&sifirst='.$letter, $letter);
}
}
$content .= html_writer::end_tag('div');
// Bar of last initials.
$content .= html_writer::start_tag('div', array('class' => 'initialbar lastinitial'));
$content .= html_writer::label(get_string('lastname').' : ', null);
if (!empty($lastinitial)) {
$content .= html_writer::link($url.'&silast=', $strall);
} else {
$content .= html_writer::tag('strong', $strall);
}
foreach ($alpha as $letter) {
if ($letter == $lastinitial) {
$content .= html_writer::tag('strong', $letter);
} else {
$content .= html_writer::link($url.'&silast='.$letter, $letter);
}
}
$content .= html_writer::end_tag('div');
$content .= html_writer::end_tag('div');
$content .= html_writer::tag('div', ' ');
$content .= html_writer::end_tag('form');
return $content;
}
}
/**
* User files tree
* @copyright 2010 Dongsheng Cai <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_files_tree implements renderable {
public $context;
public $dir;
/**
* Create user files tree object
*/
public function __construct() {
global $USER;
$this->context = context_user::instance($USER->id);
$fs = get_file_storage();
$this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);
}
}