MDL-32773 Add a help icon for button 'Set main file' in filemanager
[moodle.git] / files / renderer.php
CommitLineData
64f93798
PS
1<?php
2///////////////////////////////////////////////////////////////////////////
3// //
4// This file is part of Moodle - http://moodle.org/ //
5// Moodle - Modular Object-Oriented Dynamic Learning Environment //
6// //
7// Moodle is free software: you can redistribute it and/or modify //
8// it under the terms of the GNU General Public License as published by //
9// the Free Software Foundation, either version 3 of the License, or //
10// (at your option) any later version. //
11// //
12// Moodle is distributed in the hope that it will be useful, //
13// but WITHOUT ANY WARRANTY; without even the implied warranty of //
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15// GNU General Public License for more details. //
16// //
17// You should have received a copy of the GNU General Public License //
18// along with Moodle. If not, see <http://www.gnu.org/licenses/>. //
19// //
20///////////////////////////////////////////////////////////////////////////
21
22defined('MOODLE_INTERNAL') || die();
23
24/**
25 * Rendering of files viewer related widgets.
26 * @package core
27 * @subpackage file
28 * @copyright 2010 Dongsheng Cai
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 * @since Moodle 2.0
31 */
32
33/**
e921afa8 34 * File browser render
64f93798
PS
35 *
36 * @copyright 2010 Dongsheng Cai
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 2.0
39 */
40class core_files_renderer extends plugin_renderer_base {
41
42 public function files_tree_viewer(file_info $file_info, array $options = null) {
43 $tree = new files_tree_viewer($file_info, $options);
44 return $this->render($tree);
45 }
46
47 public function render_files_tree_viewer(files_tree_viewer $tree) {
e0873f13 48 $html = $this->output->heading_with_help(get_string('coursefiles'), 'courselegacyfiles', 'moodle');
0b259d91
DC
49
50 $html .= $this->output->container_start('coursefilesbreadcrumb');
64f93798
PS
51 foreach($tree->path as $path) {
52 $html .= $path;
53 $html .= ' / ';
54 }
0b259d91 55 $html .= $this->output->container_end();
64f93798 56
0b259d91
DC
57 $html .= $this->output->box_start();
58 $table = new html_table();
559276b1
MG
59 $table->head = array(get_string('name'), get_string('lastmodified'), get_string('size', 'repository'), get_string('type', 'repository'));
60 $table->align = array('left', 'left', 'left', 'left');
0b259d91
DC
61 $table->width = '100%';
62 $table->data = array();
63
64 foreach ($tree->tree as $file) {
559276b1
MG
65 $filedate = $filesize = $filetype = '';
66 if ($file['filedate']) {
67 $filedate = userdate($file['filedate'], get_string('strftimedatetimeshort', 'langconfig'));
68 }
69 if (empty($file['isdir'])) {
70 if ($file['filesize']) {
71 $filesize = display_size($file['filesize']);
72 }
73 $fileicon = file_file_icon($file, 24);
74 $filetype = get_mimetype_description($file);
0b259d91 75 } else {
559276b1 76 $fileicon = file_folder_icon(24);
64f93798 77 }
559276b1
MG
78 $table->data[] = array(
79 html_writer::link($file['url'], $this->output->pix_icon($fileicon, get_string('icon')) . ' ' . $file['filename']),
80 $filedate,
81 $filesize,
82 $filetype
83 );
64f93798 84 }
0b259d91
DC
85
86 $html .= html_writer::table($table);
e921afa8 87 $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
0b259d91 88 $html .= $this->output->box_end();
64f93798
PS
89 return $html;
90 }
906e7d89
MG
91
92 /**
93 * Prints the file manager and initializes all necessary libraries
94 *
95 * <pre>
96 * $fm = new form_filemanager($options);
97 * $output = get_renderer('core', 'files');
98 * echo $output->render($fm);
99 * </pre>
100 *
101 * @param form_filemanager $fm File manager to render
102 * @return string HTML fragment
103 */
d1d18691 104 public function render_form_filemanager($fm) {
906e7d89 105 static $filemanagertemplateloaded;
d1d18691 106 $html = $this->fm_print_generallayout($fm);
906e7d89
MG
107 $module = array(
108 'name'=>'form_filemanager',
109 'fullpath'=>'/lib/form/filemanager.js',
d61c96b6 110 'requires' => array('moodle-core-notification-dialogue', 'core_filepicker', 'base', 'io-base', 'node', 'json', 'core_dndupload', 'panel', 'resize-plugin', 'dd-plugin'),
4325db53
MG
111 'strings' => array(
112 array('error', 'moodle'), array('info', 'moodle'), array('confirmdeletefile', 'repository'),
113 array('draftareanofiles', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
9a62f779
MG
114 array('invalidjson', 'repository'), array('popupblockeddownload', 'repository'),
115 array('unknownoriginal', 'repository'), array('confirmdeletefolder', 'repository'),
116 array('confirmdeletefilewithhref', 'repository'), array('confirmrenamefolder', 'repository'),
7f60308a 117 array('confirmrenamefile', 'repository'), array('newfolder', 'repository'), array('edit', 'moodle')
906e7d89
MG
118 )
119 );
120 if (empty($filemanagertemplateloaded)) {
121 $filemanagertemplateloaded = true;
122 $this->page->requires->js_init_call('M.form_filemanager.set_templates',
e709ddd2 123 array($this->filemanager_js_templates()), true, $module);
906e7d89
MG
124 }
125 $this->page->requires->js_init_call('M.form_filemanager.init', array($fm->options), true, $module);
126
127 // non javascript file manager
128 $html .= '<noscript>';
129 $html .= "<div><object type='text/html' data='".$fm->get_nonjsurl()."' height='160' width='600' style='border:1px solid #000'></object></div>";
130 $html .= '</noscript>';
131
132
133 return $html;
134 }
135
136 /**
137 * Returns html for displaying one file manager
138 *
139 * The main element in HTML must have id="filemanager-{$client_id}" and
140 * class="filemanager fm-loading";
141 * After all necessary code on the page (both html and javascript) is loaded,
142 * the class fm-loading will be removed and added class fm-loaded;
143 * The main element (class=filemanager) will be assigned the following classes:
144 * 'fm-maxfiles' - when filemanager has maximum allowed number of files;
145 * 'fm-nofiles' - when filemanager has no files at all (although there might be folders);
146 * 'fm-noitems' - when current view (folder) has no items - neither files nor folders;
147 * 'fm-updating' - when current view is being updated (usually means that loading icon is to be displayed);
148 * 'fm-nomkdir' - when 'Make folder' action is unavailable (empty($fm->options->subdirs) == true)
149 *
150 * Element with class 'filemanager-container' will be holding evens for dnd upload (dragover, etc.).
151 * It will have class:
152 * 'dndupload-ready' - when a file is being dragged over the browser
153 * 'dndupload-over' - when file is being dragged over this filepicker (additional to 'dndupload-ready')
154 * 'dndupload-uploading' - during the upload process (note that after dnd upload process is
155 * over, the file manager will refresh the files list and therefore will have for a while class
156 * fm-updating. Both waiting processes should look similar so the images don't jump for user)
157 *
158 * If browser supports Drag-and-drop, the body element will have class 'dndsupported',
159 * otherwise - 'dndnotsupported';
160 *
e709ddd2
MG
161 * Element with class 'fp-content' will be populated with files list;
162 * Element with class 'fp-btn-add' will hold onclick event for adding a file (opening filepicker);
163 * Element with class 'fp-btn-mkdir' will hold onclick event for adding new folder;
164 * Element with class 'fp-btn-download' will hold onclick event for download action;
165 *
166 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
167 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
168 * Parent element will receive class 'empty' when there are no folders to be displayed;
169 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
170 *
171 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
172 * can be changed or not;
173 * Inside element with class 'fp-viewbar' there are expected elements with classes
174 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
175 * between the view modes, the last clicked element will have the class 'checked';
906e7d89
MG
176 *
177 * @param form_filemanager $fm
178 * @return string
179 */
d1d18691 180 private function fm_print_generallayout($fm) {
906e7d89
MG
181 global $OUTPUT;
182 $options = $fm->options;
183 $client_id = $options->client_id;
184 $straddfile = get_string('addfile', 'repository');
185 $strmakedir = get_string('makeafolder', 'moodle');
186 $strdownload = get_string('downloadfolder', 'repository');
187 $strloading = get_string('loading', 'repository');
906e7d89
MG
188 $strdroptoupload = get_string('droptoupload', 'moodle');
189 $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
d1d18691 190 $restrictions = $this->fm_print_restrictions($fm);
a8b418e8 191 $strdndnotsupported = get_string('dndnotsupported_insentence', 'moodle').$OUTPUT->help_icon('dndnotsupported');
906e7d89
MG
192 $strdndenabledinbox = get_string('dndenabled_inbox', 'moodle');
193 $loading = get_string('loading', 'repository');
194
e709ddd2 195 $html = '
ecad7393 196<div id="filemanager-'.$client_id.'" class="filemanager fm-loading">
420e7bb2 197 <div class="fp-restrictions">
e709ddd2 198 '.$restrictions.'
a8b418e8 199 <span class="dnduploadnotsupported-message"> - '.$strdndnotsupported.' </span>
420e7bb2
BR
200 </div>
201 <div class="fp-navbar">
202 <div class="filemanager-toolbar">
420e7bb2 203 <div class="fp-toolbar">
2eaaae00
JF
204 <div class="{!}fp-btn-add"><a role="button" href="#"><img src="'.$this->pix_url('a/add_file').'" /> '.$straddfile.'</a></div>
205 <div class="{!}fp-btn-mkdir"><a role="button" href="#"><img src="'.$this->pix_url('a/create_folder').'" /> '.$strmakedir.'</a></div>
206 <div class="{!}fp-btn-download"><a role="button" href="#"><img src="'.$this->pix_url('a/download_all').'" /> '.$strdownload.'</a></div>
420e7bb2 207 </div>
b4562710 208 <div class="{!}fp-viewbar">
5bc49369
JF
209 <a title="'. get_string('displayicons', 'repository') .'" class="{!}fp-vb-icons" href="#"></a>
210 <a title="'. get_string('displaydetails', 'repository') .'" class="{!}fp-vb-details" href="#"></a>
211 <a title="'. get_string('displaytree', 'repository') .'" class="{!}fp-vb-tree" href="#"></a>
b4562710 212 </div>
420e7bb2
BR
213 </div>
214 <div class="fp-pathbar">
0f0d10c5 215 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
e709ddd2 216 </div>
906e7d89 217 </div>
0f0d10c5 218 <div class="filemanager-loading mdl-align">'.$icon_progress.'</div>
ecad7393 219 <div class="filemanager-container" >
e709ddd2 220 <div class="fm-content-wrapper">
420e7bb2 221 <div class="fp-content"></div>
19ef00b9 222 <div class="fm-empty-container">
1d747df1 223 <div class="dndupload-message">'.$strdndenabledinbox.'<br/><div class="dndupload-arrow"></div></div>
e709ddd2 224 </div>
1d747df1 225 <div class="dndupload-target">'.$strdroptoupload.'<br/><div class="dndupload-arrow"></div></div>
ead25632 226 <div class="dndupload-progressbars"></div>
e709ddd2 227 <div class="dndupload-uploadinprogress">'.$icon_progress.'</div>
906e7d89 228 </div>
e709ddd2 229 <div class="filemanager-updating">'.$icon_progress.'</div>
906e7d89 230 </div>
420e7bb2 231</div>';
e709ddd2
MG
232 return preg_replace('/\{\!\}/', '', $html);
233 }
234
235 /**
236 * FileManager JS template for displaying one file in 'icon view' mode.
237 *
23b83009
MG
238 * Except for elements described in fp_js_template_iconfilename, this template may also
239 * contain element with class 'fp-contextmenu'. If context menu is available for this
240 * file, the top element will receive the additional class 'fp-hascontextmenu' and
241 * the element with class 'fp-contextmenu' will hold onclick event for displaying
242 * the context menu.
243 *
e709ddd2
MG
244 * @see fp_js_template_iconfilename()
245 * @return string
246 */
247 private function fm_js_template_iconfilename() {
420e7bb2
BR
248 $rv = '
249<div class="fp-file">
250 <a href="#">
9a62f779 251 <div style="position:relative;">
0f0d10c5
BR
252 <div class="{!}fp-thumbnail"></div>
253 <div class="fp-reficons1"></div>
254 <div class="fp-reficons2"></div>
255 </div>
256 <div class="fp-filename-field">
257 <div class="{!}fp-filename"></div>
9a62f779 258 </div>
0f0d10c5 259 </a>
23b83009
MG
260 <a class="{!}fp-contextmenu" href="#">'.$this->pix_icon('i/menu', '▶').'</a>
261</div>';
262 return preg_replace('/\{\!\}/', '', $rv);
e709ddd2
MG
263 }
264
265 /**
266 * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
267 *
23b83009
MG
268 * Except for elements described in fp_js_template_listfilename, this template may also
269 * contain element with class 'fp-contextmenu'. If context menu is available for this
270 * file, the top element will receive the additional class 'fp-hascontextmenu' and
271 * the element with class 'fp-contextmenu' will hold onclick event for displaying
272 * the context menu.
273 *
274 * @todo MDL-32736 remove onclick="return false;"
e709ddd2
MG
275 * @see fp_js_template_listfilename()
276 * @return string
277 */
278 private function fm_js_template_listfilename() {
420e7bb2 279 $rv = '
a9352f1f 280<span class="fp-filename-icon">
0f0d10c5 281 <a href="#">
420e7bb2 282 <span class="{!}fp-icon"></span>
99f374c4
RT
283 <span class="fp-reficons1"></span>
284 <span class="fp-reficons2"></span>
420e7bb2 285 <span class="{!}fp-filename"></span>
0f0d10c5 286 </a>
420e7bb2
BR
287 <a class="{!}fp-contextmenu" href="#" onclick="return false;">'.$this->pix_icon('i/menu', '▶').'</a>
288</span>';
23b83009 289 return preg_replace('/\{\!\}/', '', $rv);
e709ddd2
MG
290 }
291
694beb54
MG
292 /**
293 * FileManager JS template for displaying 'Make new folder' dialog.
294 *
295 * Must be wrapped in an element, CSS for this element must define width and height of the window;
296 *
297 * Must have one input element with type="text" (for users to enter the new folder name);
298 *
299 * content of element with class 'fp-dlg-curpath' will be replaced with current path where
300 * new folder is about to be created;
301 * elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
302 *
303 * @return string
304 */
305 private function fm_js_template_mkdir() {
420e7bb2 306 $rv = '
1641bee2 307<div class="filemanager fp-mkdir-dlg" role="dialog" aria-live="assertive" aria-labelledby="fp-mkdir-dlg-title">
e06bfd3d 308 <div class="fp-mkdir-dlg-text">
1641bee2 309 <label id="fp-mkdir-dlg-title">' . get_string('newfoldername', 'repository') . '</label><br/>
e06bfd3d
MN
310 <input type="text" />
311 </div>
af9c48cb
BR
312 <button class="{!}fp-dlg-butcreate">'.get_string('makeafolder').'</button>
313 <button class="{!}fp-dlg-butcancel">'.get_string('cancel').'</button>
694beb54
MG
314</div>';
315 return preg_replace('/\{\!\}/', '', $rv);
316 }
317
318 /**
319 * FileManager JS template for error/info message displayed as a separate popup window.
320 *
321 * @see fp_js_template_message()
322 * @return string
323 */
324 private function fm_js_template_message() {
325 return $this->fp_js_template_message();
326 }
327
e709ddd2
MG
328 /**
329 * FileManager JS template for window with file information/actions.
330 *
55089a9d
MG
331 * All content must be enclosed in one element, CSS for this class must define width and
332 * height of the window;
e709ddd2
MG
333 *
334 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
335 *
336 * Inside the window the elements with the following classnames must be present:
337 * 'fp-saveas', 'fp-author', 'fp-license', 'fp-path'. Inside each of them must be
338 * one input element (or select in case of fp-license and fp-path). They may also have labels.
339 * The elements will be assign with class 'uneditable' and input/select element will become
340 * disabled if they are not applicable for the particular file;
341 *
e0ff0867 342 * There may be present elements with classes 'fp-original', 'fp-datemodified', 'fp-datecreated',
9dbdf31f
MG
343 * 'fp-size', 'fp-dimensions', 'fp-reflist'. They will receive additional class 'fp-unknown' if
344 * information is unavailable. If there is information available, the content of embedded
345 * element with class 'fp-value' will be substituted with the value;
e709ddd2 346 *
9a62f779
MG
347 * The value of Original ('fp-original') is loaded in separate request. When it is applicable
348 * but not yet loaded the 'fp-original' element receives additional class 'fp-loading';
349 *
9dbdf31f
MG
350 * The value of 'Aliases/Shortcuts' ('fp-reflist') is also loaded in separate request. When it
351 * is applicable but not yet loaded the 'fp-original' element receives additional class
352 * 'fp-loading'. The string explaining that XX references exist will replace content of element
353 * 'fp-refcount'. Inside '.fp-reflist .fp-value' each reference will be enclosed in <li>;
354 *
e709ddd2
MG
355 * Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
356 * 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
357 * events (there may be several elements with class 'fp-file-cancel');
358 *
359 * When confirm button is pressed and file is being selected, the top element receives
360 * additional class 'loading'. It is removed when response from server is received.
361 *
362 * When any of the input fields is changed, the top element receives class 'fp-changed';
363 * When current file can be set as main - top element receives class 'fp-cansetmain';
364 * When current file is folder/zip/file - top element receives respectfully class
365 * 'fp-folder'/'fp-zip'/'fp-file';
366 *
367 * @return string
368 */
369 private function fm_js_template_fileselectlayout() {
0fb7a8a3 370 global $OUTPUT;
9a62f779
MG
371 $strloading = get_string('loading', 'repository');
372 $icon_progress = $this->pix_icon('i/loading_small', $strloading).'';
420e7bb2
BR
373 $rv = '
374<div class="filemanager fp-select">
375 <div class="fp-select-loading">
19ef00b9 376 <img src="'.$this->pix_url('i/loading_small').'" />
420e7bb2
BR
377 </div>
378 <form>
af9c48cb
BR
379 <button class="{!}fp-file-download">'.get_string('download').'</button>
380 <button class="{!}fp-file-delete">'.get_string('delete').'</button>
0fb7a8a3 381 <button class="{!}fp-file-setmain">'.get_string('setmainfile', 'repository').'</button><span class="fp-file-setmain-help">'.$OUTPUT->help_icon('setmainfile', 'repository').'</span>
af9c48cb
BR
382 <button class="{!}fp-file-zip">'.get_string('zip', 'editor').'</button>
383 <button class="{!}fp-file-unzip">'.get_string('unzip').'</button>
420e7bb2
BR
384 <div class="fp-hr"></div>
385 <table>
386 <tr class="{!}fp-saveas"><td class="mdl-right"><label>'.get_string('name', 'moodle').'</label>:</td>
387 <td class="mdl-left"><input type="text"/></td></tr>
388 <tr class="{!}fp-author"><td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
6077e381 389 <td class="mdl-left"><input type="text"/></td></tr>
420e7bb2
BR
390 <tr class="{!}fp-license"><td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
391 <td class="mdl-left"><select></select></td></tr>
392 <tr class="{!}fp-path"><td class="mdl-right"><label>'.get_string('path', 'moodle').'</label>:</td>
393 <td class="mdl-left"><select></select></td></tr>
394 <tr class="{!}fp-original"><td class="mdl-right"><label>'.get_string('original', 'repository').'</label>:</td>
6077e381 395 <td class="mdl-left"><span class="fp-originloading">'.$icon_progress.' '.$strloading.'</span><span class="fp-value"></span></td></tr>
9dbdf31f 396 <tr class="{!}fp-reflist"><td class="mdl-right"><label>'.get_string('referenceslist', 'repository').'</label>:</td>
6077e381 397 <td class="mdl-left"><p class="{!}fp-refcount"></p><span class="fp-reflistloading">'.$icon_progress.' '.$strloading.'</span><ul class="fp-value"></ul></td></tr>
420e7bb2 398 </table>
af9c48cb
BR
399 <div class="fp-select-buttons">
400 <button class="{!}fp-file-update">'.get_string('update', 'moodle').'</button>
401 <button class="{!}fp-file-cancel">'.get_string('cancel').'</button>
402 </div>
4214bc63 403 </form>
c0739778 404 <div class="fp-info clearfix">
af9c48cb
BR
405 <div class="fp-hr"></div>
406 <p class="{!}fp-thumbnail"></p>
407 <div class="fp-fileinfo">
408 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"></span></div>
409 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"></span></div>
410 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"></span></div>
411 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"></span></div>
412 </div>
420e7bb2 413 </div>
e709ddd2
MG
414</div>';
415 return preg_replace('/\{\!\}/', '', $rv);
416 }
417
9a62f779
MG
418 /**
419 * FileManager JS template for popup confirm dialogue window.
420 *
421 * Must have one top element, CSS for this element must define width and height of the window;
422 *
423 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
424 * elements with classes 'fp-dlg-butconfirm' and 'fp-dlg-butcancel' will
425 * hold onclick events;
426 *
427 * @return string
428 */
429 private function fm_js_template_confirmdialog() {
420e7bb2
BR
430 $rv = '
431<div class="filemanager fp-dlg">
432 <div class="{!}fp-dlg-text"></div>
13429b95 433 <button class="{!}fp-dlg-butconfirm">'.get_string('ok').'</button>
af9c48cb 434 <button class="{!}fp-dlg-butcancel">'.get_string('cancel').'</button>
9a62f779
MG
435</div>';
436 return preg_replace('/\{\!\}/', '', $rv);
437 }
438
e709ddd2
MG
439 /**
440 * Returns all FileManager JavaScript templates as an array.
441 *
442 * @return array
443 */
444 public function filemanager_js_templates() {
445 $class_methods = get_class_methods($this);
446 $templates = array();
447 foreach ($class_methods as $method_name) {
448 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
449 $templates[$matches[1]] = $this->$method_name();
450 }
451 return $templates;
906e7d89
MG
452 }
453
454 /**
455 * Displays restrictions for the file manager
456 *
457 * @param form_filemanager $fm
458 * @return string
459 */
d1d18691 460 private function fm_print_restrictions($fm) {
906e7d89 461 $maxbytes = display_size($fm->options->maxbytes);
21e3ea77
FM
462 $strparam = (object) array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles,
463 'areasize' => display_size($fm->options->areamaxbytes));
464 $hasmaxfiles = !empty($fm->options->maxfiles) && $fm->options->maxfiles > 0;
465 $hasarealimit = !empty($fm->options->areamaxbytes) && $fm->options->areamaxbytes != -1;
466 if ($hasmaxfiles && $hasarealimit) {
467 $maxsize = get_string('maxsizeandattachmentsandareasize', 'moodle', $strparam);
468 } else if ($hasmaxfiles) {
906e7d89 469 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
21e3ea77
FM
470 } else if ($hasarealimit) {
471 $maxsize = get_string('maxsizeandareasize', 'moodle', $strparam);
472 } else {
473 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
906e7d89
MG
474 }
475 // TODO MDL-32020 also should say about 'File types accepted'
21e3ea77 476 return '<span>'. $maxsize . '</span>';
906e7d89 477 }
64f93798 478
d1d18691
MG
479 /**
480 * Template for FilePicker with general layout (not QuickUpload).
481 *
482 * Must have one top element containing everything else (recommended <div class="file-picker">),
483 * CSS for this element must define width and height of the filepicker window. Or CSS must
484 * define min-width, max-width, min-height and max-height and in this case the filepicker
485 * window will be resizeable;
486 *
487 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
488 * can be changed or not;
489 * Inside element with class 'fp-viewbar' there are expected elements with classes
490 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
491 * between the view modes, the last clicked element will have the class 'checked';
492 *
493 * Element with class 'fp-repo' is a template for displaying one repository. Other repositories
494 * will be attached as siblings (classes first/last/even/odd will be added respectfully).
495 * The currently selected repostory will have class 'active'. Contents of element with class
496 * 'fp-repo-name' will be replaced with repository name, source of image with class
497 * 'fp-repo-icon' will be replaced with repository icon;
498 *
499 * Element with class 'fp-content' is obligatory and will hold the current contents;
500 *
501 * Element with class 'fp-paging' will contain page navigation (will be deprecated soon);
502 *
e709ddd2 503 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
d1d18691 504 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
d1d18691 505 * Parent element will receive class 'empty' when there are no folders to be displayed;
e709ddd2 506 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
d1d18691
MG
507 *
508 * Element with class 'fp-toolbar' will have class 'empty' if all 'Back', 'Search', 'Refresh',
509 * 'Logout', 'Manage' and 'Help' are unavailable for this repo;
510 *
511 * Inside fp-toolbar there are expected elements with classes fp-tb-back, fp-tb-search,
512 * fp-tb-refresh, fp-tb-logout, fp-tb-manage and fp-tb-help. Each of them will have
513 * class 'enabled' or 'disabled' if particular repository has this functionality.
514 * Element with class 'fp-tb-search' must contain empty form inside, it's contents will
515 * be substituted with the search form returned by repository (in the most cases it
516 * is generated with template core_repository_renderer::repository_default_searchform);
517 * Other elements must have either <a> or <button> element inside, it will hold onclick
518 * event for corresponding action; labels for fp-tb-back and fp-tb-logout may be
519 * replaced with those specified by repository;
520 *
521 * @return string
522 */
523 private function fp_js_template_generallayout() {
524 $rv = '
2eaaae00 525<div tabindex="0" class="file-picker fp-generallayout" role="dialog" aria-live="assertive">
e88d5641
BR
526 <div class="fp-repo-area">
527 <ul class="fp-list">
2eaaae00 528 <li class="{!}fp-repo"><a href="#"><img class="{!}fp-repo-icon" alt=" " width="16" height="16" />&nbsp;<span class="{!}fp-repo-name"></span></a></li>
e88d5641 529 </ul>
d1d18691 530 </div>
ee4ab975 531 <div class="fp-repo-items" tabindex="0">
0f0d10c5 532 <div class="fp-navbar">
420e7bb2 533 <div>
420e7bb2
BR
534 <div class="{!}fp-toolbar">
535 <div class="{!}fp-tb-back"><a href="#">'.get_string('back', 'repository').'</a></div>
6077e381 536 <div class="{!}fp-tb-search"><form></form></div>
5bc49369
JF
537 <div class="{!}fp-tb-refresh"><a href="#"><img alt="'. get_string('refresh', 'repository') .'" src="'.$this->pix_url('a/refresh').'" /></a></div>
538 <div class="{!}fp-tb-logout"><img alt="'. get_string('logout', 'repository') .'" src="'.$this->pix_url('a/logout').'" /><a href="#"></a></div>
539 <div class="{!}fp-tb-manage"><a href="#"><img alt="'. get_string('settings', 'repository') .'" src="'.$this->pix_url('a/setting').'" /> '.get_string('manageurl', 'repository').'</a></div>
540 <div class="{!}fp-tb-help"><a href="#"><img alt="'. get_string('help', 'repository') .'" src="'.$this->pix_url('a/help').'" /> '.get_string('help').'</a></div>
a3433213 541 <div class="{!}fp-tb-message"></div>
420e7bb2 542 </div>
b4562710 543 <div class="{!}fp-viewbar">
5bc49369
JF
544 <a title="'. get_string('displayicons', 'repository') .'" class="{!}fp-vb-icons" href="#"></a>
545 <a title="'. get_string('displaydetails', 'repository') .'" class="{!}fp-vb-details" href="#"></a>
546 <a title="'. get_string('displaytree', 'repository') .'" class="{!}fp-vb-tree" href="#"></a>
b4562710 547 </div>
6077e381 548 <div class="fp-clear-left"></div>
e88d5641
BR
549 </div>
550 <div class="fp-pathbar">
0f0d10c5
BR
551 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
552 </div>
d1d18691 553 </div>
e88d5641 554 <div class="{!}fp-content"></div>
d1d18691 555 </div>
d1d18691
MG
556</div>';
557 return preg_replace('/\{\!\}/', '', $rv);
558 }
559
560 /**
e709ddd2 561 * FilePicker JS template for displaying one file in 'icon view' mode.
d1d18691
MG
562 *
563 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
564 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
565 * an element will be replaced with an appropriate img;
566 *
567 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
568 * (unless min-width is set in css) and the content of an element will be replaced with filename
569 * supplied by repository;
570 *
e709ddd2
MG
571 * top element(s) will have class fp-folder if the element is a folder;
572 *
573 * List of files will have parent <div> element with class 'fp-iconview'
574 *
d1d18691
MG
575 * @return string
576 */
e709ddd2 577 private function fp_js_template_iconfilename() {
420e7bb2
BR
578 $rv = '
579<a class="fp-file" href="#" >
1778f310
MG
580 <div style="position:relative;">
581 <div class="{!}fp-thumbnail"></div>
582 <div class="fp-reficons1"></div>
583 <div class="fp-reficons2"></div>
584 </div>
0f0d10c5
BR
585 <div class="fp-filename-field">
586 <p class="{!}fp-filename"></p>
587 </div>
e88d5641 588</a>';
d1d18691
MG
589 return preg_replace('/\{\!\}/', '', $rv);
590 }
591
592 /**
593 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
594 *
595 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
596 *
597 * content of element with class 'fp-filename' will be replaced with filename supplied by
598 * repository;
599 *
e709ddd2
MG
600 * top element(s) will have class fp-folder if the element is a folder;
601 *
d1d18691
MG
602 * Note that tree view and table view are the YUI widgets and therefore there are no
603 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
604 * fp-tableview (respectfully).
605 *
606 * @return string
607 */
608 private function fp_js_template_listfilename() {
a9352f1f
BR
609 $rv = '
610<span class="fp-filename-icon">
611 <a href="#">
612 <span class="{!}fp-icon"></span>
613 <span class="{!}fp-filename"></span>
614 </a>
615</span>';
d1d18691
MG
616 return preg_replace('/\{\!\}/', '', $rv);
617 }
618
619 /**
620 * FilePicker JS template for displaying link/loading progress for fetching of the next page
621 *
622 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
623 *
624 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
625 * class 'loading' during loading of the next page (it is recommended that in this case the link
626 * becomes unavailable). Also must contain one element <a> or <button> that will hold
627 * onclick event for displaying of the next page. The event will be triggered automatically
628 * when user scrolls to this link.
629 *
630 * @return string
631 */
632 private function fp_js_template_nextpage() {
420e7bb2
BR
633 $rv = '
634<div class="{!}fp-nextpage">
635 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
636 <div class="fp-nextpage-loading">
19ef00b9 637 <img src="'.$this->pix_url('i/loading_small').'" />
420e7bb2 638 </div>
d1d18691
MG
639</div>';
640 return preg_replace('/\{\!\}/', '', $rv);
641 }
642
643 /**
e709ddd2 644 * FilePicker JS template for window appearing to select a file.
d1d18691 645 *
55089a9d
MG
646 * All content must be enclosed in one element, CSS for this class must define width and
647 * height of the window;
d1d18691
MG
648 *
649 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
650 *
651 * Inside the window the elements with the following classnames must be present:
55089a9d
MG
652 * 'fp-saveas', 'fp-linktype-2', 'fp-linktype-1', 'fp-linktype-4', 'fp-setauthor',
653 * 'fp-setlicense'. Inside each of them must have one input element (or select in case of
654 * fp-setlicense). They may also have labels.
d1d18691
MG
655 * The elements will be assign with class 'uneditable' and input/select element will become
656 * disabled if they are not applicable for the particular file;
657 *
658 * There may be present elements with classes 'fp-datemodified', 'fp-datecreated', 'fp-size',
e709ddd2
MG
659 * 'fp-license', 'fp-author', 'fp-dimensions'. They will receive additional class 'fp-unknown'
660 * if information is unavailable. If there is information available, the content of embedded
661 * element with class 'fp-value' will be substituted with the value;
d1d18691
MG
662 *
663 * Elements with classes 'fp-select-confirm' and 'fp-select-cancel' will hold corresponding
664 * onclick events;
665 *
666 * When confirm button is pressed and file is being selected, the top element receives
667 * additional class 'loading'. It is removed when response from server is received.
668 *
669 * @return string
670 */
671 private function fp_js_template_selectlayout() {
420e7bb2
BR
672 $rv = '
673<div class="file-picker fp-select">
674 <div class="fp-select-loading">
19ef00b9 675 <img src="'.$this->pix_url('i/loading_small').'" />
420e7bb2
BR
676 </div>
677 <form>
420e7bb2 678 <table>
420e7bb2 679 <tr class="{!}fp-linktype-2">
19ef00b9 680 <td class="mdl-right"></td>
0f0d10c5 681 <td class="mdl-left"><input type="radio"/><label>&nbsp;'.get_string('makefileinternal', 'repository').'</label></td></tr>
420e7bb2 682 <tr class="{!}fp-linktype-1">
19ef00b9 683 <td class="mdl-right"></td>
0f0d10c5 684 <td class="mdl-left"><input type="radio"/><label>&nbsp;'.get_string('makefilelink', 'repository').'</label></td></tr>
420e7bb2 685 <tr class="{!}fp-linktype-4">
19ef00b9 686 <td class="mdl-right"></td>
0f0d10c5 687 <td class="mdl-left"><input type="radio"/><label>&nbsp;'.get_string('makefilereference', 'repository').'</label></td></tr>
420e7bb2
BR
688 <tr class="{!}fp-saveas">
689 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
690 <td class="mdl-left"><input type="text"/></td></tr>
691 <tr class="{!}fp-setauthor">
692 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
693 <td class="mdl-left"><input type="text" /></td></tr>
694 <tr class="{!}fp-setlicense">
695 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
696 <td class="mdl-left"><select></select></td></tr>
697 </table>
af9c48cb
BR
698 <div class="fp-select-buttons">
699 <button class="{!}fp-select-confirm">'.get_string('getfile', 'repository').'</button>
700 <button class="{!}fp-select-cancel">'.get_string('cancel').'</button>
701 </div>
420e7bb2 702 </form>
c0739778 703 <div class="fp-info clearfix">
af9c48cb
BR
704 <div class="fp-hr"></div>
705 <p class="{!}fp-thumbnail"></p>
706 <div class="fp-fileinfo">
707 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"></span></div>
708 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"></span></div>
709 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"></span></div>
710 <div class="{!}fp-license">'.get_string('license', 'moodle').': <span class="fp-value"></span></div>
711 <div class="{!}fp-author">'.get_string('author', 'repository').': <span class="fp-value"></span></div>
712 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"></span></div>
713 </div>
714 <div>
d1d18691
MG
715</div>';
716 return preg_replace('/\{\!\}/', '', $rv);
717 }
718
719 /**
720 * FilePicker JS template for 'Upload file' repository
721 *
722 * Content to display when user chooses 'Upload file' repository (will be nested inside
723 * element with class 'fp-content').
724 *
725 * Must contain form (enctype="multipart/form-data" method="POST")
726 *
727 * The elements with the following classnames must be present:
728 * 'fp-file', 'fp-saveas', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
729 * one input element (or select in case of fp-setlicense). They may also have labels.
730 *
731 * Element with class 'fp-upload-btn' will hold onclick event for uploading the file;
732 *
733 * Please note that some fields may be hidden using CSS if this is part of quickupload form
734 *
735 * @return string
736 */
737 private function fp_js_template_uploadform() {
420e7bb2
BR
738 $rv = '
739<div class="fp-upload-form mdl-align">
0f0d10c5 740 <div class="fp-content-center">
420e7bb2
BR
741 <form enctype="multipart/form-data" method="POST">
742 <table >
743 <tr class="{!}fp-file">
744 <td class="mdl-right"><label>'.get_string('attachment', 'repository').'</label>:</td>
745 <td class="mdl-left"><input type="file"/></td></tr>
746 <tr class="{!}fp-saveas">
747 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
748 <td class="mdl-left"><input type="text"/></td></tr>
749 <tr class="{!}fp-setauthor">
750 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
751 <td class="mdl-left"><input type="text"/></td></tr>
752 <tr class="{!}fp-setlicense">
753 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
6077e381 754 <td class="mdl-left"><select></select></td></tr>
420e7bb2
BR
755 </table>
756 </form>
757 <div><button class="{!}fp-upload-btn">'.get_string('upload', 'repository').'</button></div>
758 </div>
d1d18691
MG
759</div> ';
760 return preg_replace('/\{\!\}/', '', $rv);
761 }
762
763 /**
764 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
765 *
766 * @return string
767 */
768 private function fp_js_template_loading() {
420e7bb2 769 return '
0f0d10c5
BR
770<div class="fp-content-loading">
771 <div class="fp-content-center">
19ef00b9 772 <img src="'.$this->pix_url('i/loading_small').'" />
0f0d10c5 773 </div>
d1d18691
MG
774</div>';
775 }
776
777 /**
778 * FilePicker JS template for error (inside element with class 'fp-content').
779 *
780 * must have element with class 'fp-error', its content will be replaced with error text
781 * and the error code will be assigned as additional class to this element
782 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
783 *
784 * @return string
785 */
786 private function fp_js_template_error() {
420e7bb2 787 $rv = '
6077e381 788<div class="fp-content-error" ><div class="{!}fp-error"></div></div>';
d1d18691
MG
789 return preg_replace('/\{\!\}/', '', $rv);
790 }
791
792 /**
793 * FilePicker JS template for error/info message displayed as a separate popup window.
794 *
9a62f779 795 * Must be wrapped in one element, CSS for this element must define
d1d18691
MG
796 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
797 * or 'fp-msg-info' depending on message type;
798 *
799 * content of element with class 'fp-msg-text' will be replaced with error/info text;
800 *
801 * element with class 'fp-msg-butok' will hold onclick event
802 *
803 * @return string
804 */
805 private function fp_js_template_message() {
420e7bb2 806 $rv = '
f26cc2be
RW
807<div class="file-picker fp-msg" role="alertdialog" aria-live="assertive" aria-labelledby="fp-msg-labelledby">
808 <p class="{!}fp-msg-text" id="fp-msg-labelledby"></p>
af9c48cb 809 <button class="{!}fp-msg-butok">'.get_string('ok').'</button>
420e7bb2 810</div>';
d1d18691
MG
811 return preg_replace('/\{\!\}/', '', $rv);
812 }
813
814 /**
815 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
816 *
9a62f779 817 * Must have one top element, CSS for this element must define width and height of the window;
d1d18691
MG
818 *
819 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
d0b2d445
DS
820 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename',
821 * 'fp-dlg-butoverwriteall', 'fp-dlg-butrenameall' and 'fp-dlg-butcancel' will
d1d18691
MG
822 * hold onclick events;
823 *
824 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
825 * (Note that it may have long text)
826 *
827 * @return string
828 */
829 private function fp_js_template_processexistingfile() {
420e7bb2
BR
830 $rv = '
831<div class="file-picker fp-dlg">
832 <p class="{!}fp-dlg-text"></p>
af9c48cb
BR
833 <div class="fp-dlg-buttons">
834 <button class="{!}fp-dlg-butoverwrite">'.get_string('overwrite', 'repository').'</button>
835 <button class="{!}fp-dlg-butrename"></button>
836 <button class="{!}fp-dlg-butcancel">'.get_string('cancel').'</button>
837 </div>
d1d18691
MG
838</div>';
839 return preg_replace('/\{\!\}/', '', $rv);
840 }
0f0d10c5 841
d0b2d445
DS
842 /**
843 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists (multiple-file version).
844 *
845 * Must have one top element, CSS for this element must define width and height of the window;
846 *
847 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
848 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename' and 'fp-dlg-butcancel' will
849 * hold onclick events;
850 *
851 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
852 * (Note that it may have long text)
853 *
854 * @return string
855 */
856 private function fp_js_template_processexistingfilemultiple() {
857 $rv = '
858<div class="file-picker fp-dlg">
859 <p class="{!}fp-dlg-text"></p>
860 <a class="{!}fp-dlg-butoverwrite fp-panel-button" href="#">'.get_string('overwrite', 'repository').'</a>
d0b2d445
DS
861 <a class="{!}fp-dlg-butcancel fp-panel-button" href="#">'.get_string('cancel').'</a>
862 <a class="{!}fp-dlg-butrename fp-panel-button" href="#"></a>
b8a22d22
DS
863 <br/>
864 <a class="{!}fp-dlg-butoverwriteall fp-panel-button" href="#">'.get_string('overwriteall', 'repository').'</a>
d0b2d445
DS
865 <a class="{!}fp-dlg-butrenameall fp-panel-button" href="#">'.get_string('renameall', 'repository').'</a>
866</div>';
867 return preg_replace('/\{\!\}/', '', $rv);
868 }
869
d1d18691
MG
870 /**
871 * FilePicker JS template for repository login form including templates for each element type
872 *
873 * Must contain one <form> element with templates for different input types inside:
874 * Elements with classes 'fp-login-popup', 'fp-login-textarea', 'fp-login-select' and
875 * 'fp-login-input' are templates for displaying respective login form elements. Inside
876 * there must be exactly one element with type <button>, <textarea>, <select> or <input>
877 * (i.e. fp-login-popup should have <button>, fp-login-textarea should have <textarea>, etc.);
878 * They may also contain the <label> element and it's content will be substituted with
879 * label;
880 *
881 * You can also define elements with classes 'fp-login-checkbox', 'fp-login-text'
882 * but if they are not found, 'fp-login-input' will be used;
883 *
884 * Element with class 'fp-login-radiogroup' will be used for group of radio inputs. Inside
885 * it should hava a template for one radio input (with class 'fp-login-radio');
886 *
887 * Element with class 'fp-login-submit' will hold on click mouse event (form submission). It
888 * will be removed if at least one popup element is present;
889 *
890 * @return string
891 */
892 private function fp_js_template_loginform() {
893 $rv = '
894<div class="fp-login-form">
420e7bb2
BR
895 <div class="fp-content-center">
896 <form>
897 <table >
898 <tr class="{!}fp-login-popup">
899 <td colspan="2">
900 <label>'.get_string('popup', 'repository').'</label>
901 <p class="fp-popup"><button class="{!}fp-login-popup-but">'.get_string('login', 'repository').'</button></p></td></tr>
902 <tr class="{!}fp-login-textarea">
903 <td colspan="2"><p><textarea></textarea></p></td></tr>
904 <tr class="{!}fp-login-select">
905 <td align="right"><label></label></td>
906 <td align="left"><select></select></td></tr>
907 <tr class="{!}fp-login-input">
6077e381 908 <td class="label"><label></label></td>
420e7bb2
BR
909 <td class="input"><input/></td></tr>
910 <tr class="{!}fp-login-radiogroup">
6077e381
BR
911 <td align="right" width="30%" valign="top"><label></label></td>
912 <td align="left" valign="top"><p class="{!}fp-login-radio"><input /> <label></label></p></td></tr>
420e7bb2
BR
913 </table>
914 <p><button class="{!}fp-login-submit">'.get_string('submit', 'repository').'</button></p>
915 </form>
916 </div>
d1d18691
MG
917</div>';
918 return preg_replace('/\{\!\}/', '', $rv);
919 }
920
921 /**
922 * Returns all FilePicker JavaScript templates as an array.
923 *
924 * @return array
925 */
926 public function filepicker_js_templates() {
927 $class_methods = get_class_methods($this);
928 $templates = array();
929 foreach ($class_methods as $method_name) {
930 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
931 $templates[$matches[1]] = $this->$method_name();
932 }
933 return $templates;
934 }
935
936 /**
937 * Returns HTML for default repository searchform to be passed to Filepicker
938 *
939 * This will be used as contents for search form defined in generallayout template
940 * (form with id {TOOLSEARCHID}).
941 * Default contents is one text input field with name="s"
942 */
943 public function repository_default_searchform() {
4bde1152
RW
944 $searchinput = html_writer::label(get_string('searchrepo', 'repository'),
945 'reposearch', false, array('class' => 'accesshide'));
946 $searchinput .= html_writer::empty_tag('input', array('type' => 'text',
947 'id' => 'reposearch', 'name' => 's', 'value' => get_string('search', 'repository')));
948 $str = html_writer::tag('div', $searchinput, array('class' => "fp-def-search"));
949
d1d18691
MG
950 return $str;
951 }
952}
64f93798
PS
953
954/**
955 * Data structure representing a general moodle file tree viewer
956 *
957 * @copyright 2010 Dongsheng Cai
958 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
959 * @since Moodle 2.0
960 */
961class files_tree_viewer implements renderable {
962 public $tree;
963 public $path;
e921afa8 964 public $context;
64f93798
PS
965
966 /**
967 * Constructor of moodle_file_tree_viewer class
968 * @param file_info $file_info
969 * @param array $options
970 */
971 public function __construct(file_info $file_info, array $options = null) {
972 global $CFG;
973
974 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
64f93798 975 $this->options = (array)$options;
e921afa8
DC
976 $this->context = $options['context'];
977
64f93798
PS
978 $this->tree = array();
979 $children = $file_info->get_children();
0b259d91 980 $current_file_params = $file_info->get_params();
64f93798 981 $parent_info = $file_info->get_parent();
64f93798
PS
982 $level = $parent_info;
983 $this->path = array();
984 while ($level) {
985 $params = $level->get_params();
d197ea43 986 $context = context::instance_by_id($params['contextid']);
0b259d91
DC
987 // $this->context is current context
988 if ($context->id != $this->context->id or empty($params['filearea'])) {
64f93798
PS
989 break;
990 }
e921afa8
DC
991 // unset unused parameters
992 unset($params['component']);
993 unset($params['filearea']);
0b259d91 994 unset($params['filename']);
e921afa8 995 unset($params['itemid']);
64f93798 996 $url = new moodle_url('/files/index.php', $params);
e921afa8 997 $this->path[] = html_writer::link($url, $level->get_visible_name());
64f93798
PS
998 $level = $level->get_parent();
999 }
1000 $this->path = array_reverse($this->path);
0b259d91
DC
1001 if ($current_file_params['filepath'] != '/') {
1002 $this->path[] = $file_info->get_visible_name();
1003 }
64f93798
PS
1004
1005 foreach ($children as $child) {
1006 $filedate = $child->get_timemodified();
1007 $filesize = $child->get_filesize();
1008 $mimetype = $child->get_mimetype();
1009 $params = $child->get_params();
0b259d91
DC
1010 unset($params['component']);
1011 unset($params['filearea']);
1012 unset($params['filename']);
1013 unset($params['itemid']);
64f93798
PS
1014 $fileitem = array(
1015 'params' => $params,
1016 'filename' => $child->get_visible_name(),
559276b1
MG
1017 'mimetype' => $child->get_mimetype(),
1018 'filedate' => $filedate ? $filedate : '',
1019 'filesize' => $filesize ? $filesize : ''
64f93798 1020 );
e921afa8 1021 $url = new moodle_url('/files/index.php', $params);
64f93798
PS
1022 if ($child->is_directory()) {
1023 $fileitem['isdir'] = true;
1024 $fileitem['url'] = $url->out(false);
64f93798
PS
1025 } else {
1026 $fileitem['url'] = $child->get_url();
1027 }
1028 $this->tree[] = $fileitem;
1029 }
1030 }
1031}