Merge branch 'MDL-34610_24' of git://github.com/timhunt/moodle into MOODLE_24_STABLE
[moodle.git] / files / renderer.php
blobeda9047e62ef2d5ff74ea9df6b041b4aa1a1df42
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 ///////////////////////////////////////////////////////////////////////////
22 defined('MOODLE_INTERNAL') || die();
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
33 /**
34 * File browser render
36 * @copyright 2010 Dongsheng Cai
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 2.0
40 class core_files_renderer extends plugin_renderer_base {
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);
47 public function render_files_tree_viewer(files_tree_viewer $tree) {
48 $html = $this->output->heading_with_help(get_string('coursefiles'), 'courselegacyfiles', 'moodle');
50 $html .= $this->output->container_start('coursefilesbreadcrumb');
51 foreach($tree->path as $path) {
52 $html .= $path;
53 $html .= ' / ';
55 $html .= $this->output->container_end();
57 $html .= $this->output->box_start();
58 $table = new html_table();
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');
61 $table->width = '100%';
62 $table->data = array();
64 foreach ($tree->tree as $file) {
65 $filedate = $filesize = $filetype = '';
66 if ($file['filedate']) {
67 $filedate = userdate($file['filedate'], get_string('strftimedatetimeshort', 'langconfig'));
69 if (empty($file['isdir'])) {
70 if ($file['filesize']) {
71 $filesize = display_size($file['filesize']);
73 $fileicon = file_file_icon($file, 24);
74 $filetype = get_mimetype_description($file);
75 } else {
76 $fileicon = file_folder_icon(24);
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
86 $html .= html_writer::table($table);
87 $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
88 $html .= $this->output->box_end();
89 return $html;
92 /**
93 * Prints the file manager and initializes all necessary libraries
95 * <pre>
96 * $fm = new form_filemanager($options);
97 * $output = get_renderer('core', 'files');
98 * echo $output->render($fm);
99 * </pre>
101 * @param form_filemanager $fm File manager to render
102 * @return string HTML fragment
104 public function render_form_filemanager($fm) {
105 static $filemanagertemplateloaded;
106 $html = $this->fm_print_generallayout($fm);
107 $module = array(
108 'name'=>'form_filemanager',
109 'fullpath'=>'/lib/form/filemanager.js',
110 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'core_dndupload', 'panel', 'resize-plugin', 'dd-plugin'),
111 'strings' => array(
112 array('error', 'moodle'), array('info', 'moodle'), array('confirmdeletefile', 'repository'),
113 array('draftareanofiles', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
114 array('invalidjson', 'repository'), array('popupblockeddownload', 'repository'),
115 array('unknownoriginal', 'repository'), array('confirmdeletefolder', 'repository'),
116 array('confirmdeletefilewithhref', 'repository'), array('confirmrenamefolder', 'repository'),
117 array('confirmrenamefile', 'repository')
120 if (empty($filemanagertemplateloaded)) {
121 $filemanagertemplateloaded = true;
122 $this->page->requires->js_init_call('M.form_filemanager.set_templates',
123 array($this->filemanager_js_templates()), true, $module);
125 $this->page->requires->js_init_call('M.form_filemanager.init', array($fm->options), true, $module);
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>';
133 return $html;
137 * Returns html for displaying one file manager
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)
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)
158 * If browser supports Drag-and-drop, the body element will have class 'dndsupported',
159 * otherwise - 'dndnotsupported';
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;
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;
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';
177 * @param form_filemanager $fm
178 * @return string
180 private function fm_print_generallayout($fm) {
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');
188 $strdroptoupload = get_string('droptoupload', 'moodle');
189 $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
190 $restrictions = $this->fm_print_restrictions($fm);
191 $strdndnotsupported = get_string('dndnotsupported_insentence', 'moodle').$OUTPUT->help_icon('dndnotsupported');
192 $strdndenabledinbox = get_string('dndenabled_inbox', 'moodle');
193 $loading = get_string('loading', 'repository');
195 $html = '
196 <div id="filemanager-'.$client_id.'" class="filemanager fm-loading">
197 <div class="fp-restrictions">
198 '.$restrictions.'
199 <span class="dnduploadnotsupported-message"> - '.$strdndnotsupported.' </span>
200 </div>
201 <div class="fp-navbar">
202 <div class="filemanager-toolbar">
203 <div class="fp-toolbar">
204 <div class="{!}fp-btn-add"><a href="#"><img src="'.$this->pix_url('a/add_file').'" /> '.$straddfile.'</a></div>
205 <div class="{!}fp-btn-mkdir"><a href="#"><img src="'.$this->pix_url('a/create_folder').'" /> '.$strmakedir.'</a></div>
206 <div class="{!}fp-btn-download"><a href="#"><img src="'.$this->pix_url('a/download_all').'" /> '.$strdownload.'</a></div>
207 </div>
208 <div class="{!}fp-viewbar">
209 <a class="{!}fp-vb-icons" href="#"></a>
210 <a class="{!}fp-vb-details" href="#"></a>
211 <a class="{!}fp-vb-tree" href="#"></a>
212 </div>
213 </div>
214 <div class="fp-pathbar">
215 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
216 </div>
217 </div>
218 <div class="filemanager-loading mdl-align">'.$icon_progress.'</div>
219 <div class="filemanager-container" >
220 <div class="fm-content-wrapper">
221 <div class="fp-content"></div>
222 <div class="fm-empty-container">
223 <div class="dndupload-message">'.$strdndenabledinbox.'<br/><div class="dndupload-arrow"></div></div>
224 </div>
225 <div class="dndupload-target">'.$strdroptoupload.'<br/><div class="dndupload-arrow"></div></div>
226 <div class="dndupload-uploadinprogress">'.$icon_progress.'</div>
227 </div>
228 <div class="filemanager-updating">'.$icon_progress.'</div>
229 </div>
230 </div>';
231 return preg_replace('/\{\!\}/', '', $html);
235 * FileManager JS template for displaying one file in 'icon view' mode.
237 * Except for elements described in fp_js_template_iconfilename, this template may also
238 * contain element with class 'fp-contextmenu'. If context menu is available for this
239 * file, the top element will receive the additional class 'fp-hascontextmenu' and
240 * the element with class 'fp-contextmenu' will hold onclick event for displaying
241 * the context menu.
243 * @see fp_js_template_iconfilename()
244 * @return string
246 private function fm_js_template_iconfilename() {
247 $rv = '
248 <div class="fp-file">
249 <a href="#">
250 <div style="position:relative;">
251 <div class="{!}fp-thumbnail"></div>
252 <div class="fp-reficons1"></div>
253 <div class="fp-reficons2"></div>
254 </div>
255 <div class="fp-filename-field">
256 <div class="{!}fp-filename"></div>
257 </div>
258 </a>
259 <a class="{!}fp-contextmenu" href="#">'.$this->pix_icon('i/menu', 'â–¶').'</a>
260 </div>';
261 return preg_replace('/\{\!\}/', '', $rv);
265 * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
267 * Except for elements described in fp_js_template_listfilename, this template may also
268 * contain element with class 'fp-contextmenu'. If context menu is available for this
269 * file, the top element will receive the additional class 'fp-hascontextmenu' and
270 * the element with class 'fp-contextmenu' will hold onclick event for displaying
271 * the context menu.
273 * @todo MDL-32736 remove onclick="return false;"
274 * @see fp_js_template_listfilename()
275 * @return string
277 private function fm_js_template_listfilename() {
278 $rv = '
279 <span class="fp-filename-icon">
280 <a href="#">
281 <span class="{!}fp-icon"></span>
282 <span class="fp-reficons1"></span>
283 <span class="fp-reficons2"></span>
284 <span class="{!}fp-filename"></span>
285 </a>
286 <a class="{!}fp-contextmenu" href="#" onclick="return false;">'.$this->pix_icon('i/menu', 'â–¶').'</a>
287 </span>';
288 return preg_replace('/\{\!\}/', '', $rv);
292 * FileManager JS template for displaying 'Make new folder' dialog.
294 * Must be wrapped in an element, CSS for this element must define width and height of the window;
296 * Must have one input element with type="text" (for users to enter the new folder name);
298 * content of element with class 'fp-dlg-curpath' will be replaced with current path where
299 * new folder is about to be created;
300 * elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
302 * @return string
304 private function fm_js_template_mkdir() {
305 $rv = '
306 <div class="filemanager fp-mkdir-dlg">
307 <div class="fp-mkdir-dlg-text">
308 <label>' . get_string('newfoldername', 'repository') . '</label><br/>
309 <input type="text" />
310 </div>
311 <button class="{!}fp-dlg-butcreate">'.get_string('makeafolder').'</button>
312 <button class="{!}fp-dlg-butcancel">'.get_string('cancel').'</button>
313 </div>';
314 return preg_replace('/\{\!\}/', '', $rv);
318 * FileManager JS template for error/info message displayed as a separate popup window.
320 * @see fp_js_template_message()
321 * @return string
323 private function fm_js_template_message() {
324 return $this->fp_js_template_message();
328 * FileManager JS template for window with file information/actions.
330 * All content must be enclosed in one element, CSS for this class must define width and
331 * height of the window;
333 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
335 * Inside the window the elements with the following classnames must be present:
336 * 'fp-saveas', 'fp-author', 'fp-license', 'fp-path'. Inside each of them must be
337 * one input element (or select in case of fp-license and fp-path). They may also have labels.
338 * The elements will be assign with class 'uneditable' and input/select element will become
339 * disabled if they are not applicable for the particular file;
341 * There may be present elements with classes 'fp-original', 'fp-datemodified', 'fp-datecreated',
342 * 'fp-size', 'fp-dimensions', 'fp-reflist'. They will receive additional class 'fp-unknown' if
343 * information is unavailable. If there is information available, the content of embedded
344 * element with class 'fp-value' will be substituted with the value;
346 * The value of Original ('fp-original') is loaded in separate request. When it is applicable
347 * but not yet loaded the 'fp-original' element receives additional class 'fp-loading';
349 * The value of 'Aliases/Shortcuts' ('fp-reflist') is also loaded in separate request. When it
350 * is applicable but not yet loaded the 'fp-original' element receives additional class
351 * 'fp-loading'. The string explaining that XX references exist will replace content of element
352 * 'fp-refcount'. Inside '.fp-reflist .fp-value' each reference will be enclosed in <li>;
354 * Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
355 * 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
356 * events (there may be several elements with class 'fp-file-cancel');
358 * When confirm button is pressed and file is being selected, the top element receives
359 * additional class 'loading'. It is removed when response from server is received.
361 * When any of the input fields is changed, the top element receives class 'fp-changed';
362 * When current file can be set as main - top element receives class 'fp-cansetmain';
363 * When current file is folder/zip/file - top element receives respectfully class
364 * 'fp-folder'/'fp-zip'/'fp-file';
366 * @return string
368 private function fm_js_template_fileselectlayout() {
369 $strloading = get_string('loading', 'repository');
370 $icon_progress = $this->pix_icon('i/loading_small', $strloading).'';
371 $rv = '
372 <div class="filemanager fp-select">
373 <div class="fp-select-loading">
374 <img src="'.$this->pix_url('i/loading_small').'" />
375 </div>
376 <form>
377 <button class="{!}fp-file-download">'.get_string('download').'</button>
378 <button class="{!}fp-file-delete">'.get_string('delete').'</button>
379 <button class="{!}fp-file-setmain">'.get_string('setmainfile', 'repository').'</button>
380 <button class="{!}fp-file-zip">'.get_string('zip', 'editor').'</button>
381 <button class="{!}fp-file-unzip">'.get_string('unzip').'</button>
382 <div class="fp-hr"></div>
383 <table>
384 <tr class="{!}fp-saveas"><td class="mdl-right"><label>'.get_string('name', 'moodle').'</label>:</td>
385 <td class="mdl-left"><input type="text"/></td></tr>
386 <tr class="{!}fp-author"><td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
387 <td class="mdl-left"><input type="text"/></td></tr>
388 <tr class="{!}fp-license"><td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
389 <td class="mdl-left"><select></select></td></tr>
390 <tr class="{!}fp-path"><td class="mdl-right"><label>'.get_string('path', 'moodle').'</label>:</td>
391 <td class="mdl-left"><select></select></td></tr>
392 <tr class="{!}fp-original"><td class="mdl-right"><label>'.get_string('original', 'repository').'</label>:</td>
393 <td class="mdl-left"><span class="fp-originloading">'.$icon_progress.' '.$strloading.'</span><span class="fp-value"></span></td></tr>
394 <tr class="{!}fp-reflist"><td class="mdl-right"><label>'.get_string('referenceslist', 'repository').'</label>:</td>
395 <td class="mdl-left"><p class="{!}fp-refcount"></p><span class="fp-reflistloading">'.$icon_progress.' '.$strloading.'</span><ul class="fp-value"></ul></td></tr>
396 </table>
397 <div class="fp-select-buttons">
398 <button class="{!}fp-file-update">'.get_string('update', 'moodle').'</button>
399 <button class="{!}fp-file-cancel">'.get_string('cancel').'</button>
400 </div>
401 </form>
402 <div class="fp-info">
403 <div class="fp-hr"></div>
404 <p class="{!}fp-thumbnail"></p>
405 <div class="fp-fileinfo">
406 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"></span></div>
407 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"></span></div>
408 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"></span></div>
409 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"></span></div>
410 </div>
411 </div>
412 </div>';
413 return preg_replace('/\{\!\}/', '', $rv);
417 * FileManager JS template for popup confirm dialogue window.
419 * Must have one top element, CSS for this element must define width and height of the window;
421 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
422 * elements with classes 'fp-dlg-butconfirm' and 'fp-dlg-butcancel' will
423 * hold onclick events;
425 * @return string
427 private function fm_js_template_confirmdialog() {
428 $rv = '
429 <div class="filemanager fp-dlg">
430 <div class="{!}fp-dlg-text"></div>
431 <button class="{!}fp-dlg-butconfirm">'.get_string('ok').'</button>
432 <button class="{!}fp-dlg-butcancel">'.get_string('cancel').'</button>
433 </div>';
434 return preg_replace('/\{\!\}/', '', $rv);
438 * Returns all FileManager JavaScript templates as an array.
440 * @return array
442 public function filemanager_js_templates() {
443 $class_methods = get_class_methods($this);
444 $templates = array();
445 foreach ($class_methods as $method_name) {
446 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
447 $templates[$matches[1]] = $this->$method_name();
449 return $templates;
453 * Displays restrictions for the file manager
455 * @param form_filemanager $fm
456 * @return string
458 private function fm_print_restrictions($fm) {
459 $maxbytes = display_size($fm->options->maxbytes);
460 $strparam = (object) array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles,
461 'areasize' => display_size($fm->options->areamaxbytes));
462 $hasmaxfiles = !empty($fm->options->maxfiles) && $fm->options->maxfiles > 0;
463 $hasarealimit = !empty($fm->options->areamaxbytes) && $fm->options->areamaxbytes != -1;
464 if ($hasmaxfiles && $hasarealimit) {
465 $maxsize = get_string('maxsizeandattachmentsandareasize', 'moodle', $strparam);
466 } else if ($hasmaxfiles) {
467 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
468 } else if ($hasarealimit) {
469 $maxsize = get_string('maxsizeandareasize', 'moodle', $strparam);
470 } else {
471 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
473 // TODO MDL-32020 also should say about 'File types accepted'
474 return '<span>'. $maxsize . '</span>';
478 * Template for FilePicker with general layout (not QuickUpload).
480 * Must have one top element containing everything else (recommended <div class="file-picker">),
481 * CSS for this element must define width and height of the filepicker window. Or CSS must
482 * define min-width, max-width, min-height and max-height and in this case the filepicker
483 * window will be resizeable;
485 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
486 * can be changed or not;
487 * Inside element with class 'fp-viewbar' there are expected elements with classes
488 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
489 * between the view modes, the last clicked element will have the class 'checked';
491 * Element with class 'fp-repo' is a template for displaying one repository. Other repositories
492 * will be attached as siblings (classes first/last/even/odd will be added respectfully).
493 * The currently selected repostory will have class 'active'. Contents of element with class
494 * 'fp-repo-name' will be replaced with repository name, source of image with class
495 * 'fp-repo-icon' will be replaced with repository icon;
497 * Element with class 'fp-content' is obligatory and will hold the current contents;
499 * Element with class 'fp-paging' will contain page navigation (will be deprecated soon);
501 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
502 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
503 * Parent element will receive class 'empty' when there are no folders to be displayed;
504 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
506 * Element with class 'fp-toolbar' will have class 'empty' if all 'Back', 'Search', 'Refresh',
507 * 'Logout', 'Manage' and 'Help' are unavailable for this repo;
509 * Inside fp-toolbar there are expected elements with classes fp-tb-back, fp-tb-search,
510 * fp-tb-refresh, fp-tb-logout, fp-tb-manage and fp-tb-help. Each of them will have
511 * class 'enabled' or 'disabled' if particular repository has this functionality.
512 * Element with class 'fp-tb-search' must contain empty form inside, it's contents will
513 * be substituted with the search form returned by repository (in the most cases it
514 * is generated with template core_repository_renderer::repository_default_searchform);
515 * Other elements must have either <a> or <button> element inside, it will hold onclick
516 * event for corresponding action; labels for fp-tb-back and fp-tb-logout may be
517 * replaced with those specified by repository;
519 * @return string
521 private function fp_js_template_generallayout() {
522 $rv = '
523 <div class="file-picker fp-generallayout">
524 <div class="fp-repo-area">
525 <ul class="fp-list">
526 <li class="{!}fp-repo"><a href="#"><img class="{!}fp-repo-icon" width="16" height="16" />&nbsp;<span class="{!}fp-repo-name"></span></a></li>
527 </ul>
528 </div>
529 <div class="fp-repo-items">
530 <div class="fp-navbar">
531 <div>
532 <div class="{!}fp-toolbar">
533 <div class="{!}fp-tb-back"><a href="#">'.get_string('back', 'repository').'</a></div>
534 <div class="{!}fp-tb-search"><form></form></div>
535 <div class="{!}fp-tb-refresh"><a href="#"><img src="'.$this->pix_url('a/refresh').'" /></a></div>
536 <div class="{!}fp-tb-logout"><img src="'.$this->pix_url('a/logout').'" /><a href="#"></a></div>
537 <div class="{!}fp-tb-manage"><a href="#"><img src="'.$this->pix_url('a/setting').'" /> '.get_string('manageurl', 'repository').'</a></div>
538 <div class="{!}fp-tb-help"><a href="#"><img src="'.$this->pix_url('a/help').'" /> '.get_string('help').'</a></div>
539 <div class="{!}fp-tb-message"></div>
540 </div>
541 <div class="{!}fp-viewbar">
542 <a class="{!}fp-vb-icons" href="#"></a>
543 <a class="{!}fp-vb-details" href="#"></a>
544 <a class="{!}fp-vb-tree" href="#"></a>
545 </div>
546 <div class="fp-clear-left"></div>
547 </div>
548 <div class="fp-pathbar">
549 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
550 </div>
551 </div>
552 <div class="{!}fp-content"></div>
553 </div>
554 </div>';
555 return preg_replace('/\{\!\}/', '', $rv);
559 * FilePicker JS template for displaying one file in 'icon view' mode.
561 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
562 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
563 * an element will be replaced with an appropriate img;
565 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
566 * (unless min-width is set in css) and the content of an element will be replaced with filename
567 * supplied by repository;
569 * top element(s) will have class fp-folder if the element is a folder;
571 * List of files will have parent <div> element with class 'fp-iconview'
573 * @return string
575 private function fp_js_template_iconfilename() {
576 $rv = '
577 <a class="fp-file" href="#" >
578 <div style="position:relative;">
579 <div class="{!}fp-thumbnail"></div>
580 <div class="fp-reficons1"></div>
581 <div class="fp-reficons2"></div>
582 </div>
583 <div class="fp-filename-field">
584 <p class="{!}fp-filename"></p>
585 </div>
586 </a>';
587 return preg_replace('/\{\!\}/', '', $rv);
591 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
593 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
595 * content of element with class 'fp-filename' will be replaced with filename supplied by
596 * repository;
598 * top element(s) will have class fp-folder if the element is a folder;
600 * Note that tree view and table view are the YUI widgets and therefore there are no
601 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
602 * fp-tableview (respectfully).
604 * @return string
606 private function fp_js_template_listfilename() {
607 $rv = '
608 <span class="fp-filename-icon">
609 <a href="#">
610 <span class="{!}fp-icon"></span>
611 <span class="{!}fp-filename"></span>
612 </a>
613 </span>';
614 return preg_replace('/\{\!\}/', '', $rv);
618 * FilePicker JS template for displaying link/loading progress for fetching of the next page
620 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
622 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
623 * class 'loading' during loading of the next page (it is recommended that in this case the link
624 * becomes unavailable). Also must contain one element <a> or <button> that will hold
625 * onclick event for displaying of the next page. The event will be triggered automatically
626 * when user scrolls to this link.
628 * @return string
630 private function fp_js_template_nextpage() {
631 $rv = '
632 <div class="{!}fp-nextpage">
633 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
634 <div class="fp-nextpage-loading">
635 <img src="'.$this->pix_url('i/loading_small').'" />
636 </div>
637 </div>';
638 return preg_replace('/\{\!\}/', '', $rv);
642 * FilePicker JS template for window appearing to select a file.
644 * All content must be enclosed in one element, CSS for this class must define width and
645 * height of the window;
647 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
649 * Inside the window the elements with the following classnames must be present:
650 * 'fp-saveas', 'fp-linktype-2', 'fp-linktype-1', 'fp-linktype-4', 'fp-setauthor',
651 * 'fp-setlicense'. Inside each of them must have one input element (or select in case of
652 * fp-setlicense). They may also have labels.
653 * The elements will be assign with class 'uneditable' and input/select element will become
654 * disabled if they are not applicable for the particular file;
656 * There may be present elements with classes 'fp-datemodified', 'fp-datecreated', 'fp-size',
657 * 'fp-license', 'fp-author', 'fp-dimensions'. They will receive additional class 'fp-unknown'
658 * if information is unavailable. If there is information available, the content of embedded
659 * element with class 'fp-value' will be substituted with the value;
661 * Elements with classes 'fp-select-confirm' and 'fp-select-cancel' will hold corresponding
662 * onclick events;
664 * When confirm button is pressed and file is being selected, the top element receives
665 * additional class 'loading'. It is removed when response from server is received.
667 * @return string
669 private function fp_js_template_selectlayout() {
670 $rv = '
671 <div class="file-picker fp-select">
672 <div class="fp-select-loading">
673 <img src="'.$this->pix_url('i/loading_small').'" />
674 </div>
675 <form>
676 <table>
677 <tr class="{!}fp-linktype-2">
678 <td class="mdl-right"></td>
679 <td class="mdl-left"><input type="radio"/><label>&nbsp;'.get_string('makefileinternal', 'repository').'</label></td></tr>
680 <tr class="{!}fp-linktype-1">
681 <td class="mdl-right"></td>
682 <td class="mdl-left"><input type="radio"/><label>&nbsp;'.get_string('makefilelink', 'repository').'</label></td></tr>
683 <tr class="{!}fp-linktype-4">
684 <td class="mdl-right"></td>
685 <td class="mdl-left"><input type="radio"/><label>&nbsp;'.get_string('makefilereference', 'repository').'</label></td></tr>
686 <tr class="{!}fp-saveas">
687 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
688 <td class="mdl-left"><input type="text"/></td></tr>
689 <tr class="{!}fp-setauthor">
690 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
691 <td class="mdl-left"><input type="text" /></td></tr>
692 <tr class="{!}fp-setlicense">
693 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
694 <td class="mdl-left"><select></select></td></tr>
695 </table>
696 <div class="fp-select-buttons">
697 <button class="{!}fp-select-confirm">'.get_string('getfile', 'repository').'</button>
698 <button class="{!}fp-select-cancel">'.get_string('cancel').'</button>
699 </div>
700 </form>
701 <div class="fp-info">
702 <div class="fp-hr"></div>
703 <p class="{!}fp-thumbnail"></p>
704 <div class="fp-fileinfo">
705 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"></span></div>
706 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"></span></div>
707 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"></span></div>
708 <div class="{!}fp-license">'.get_string('license', 'moodle').': <span class="fp-value"></span></div>
709 <div class="{!}fp-author">'.get_string('author', 'repository').': <span class="fp-value"></span></div>
710 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"></span></div>
711 </div>
712 <div>
713 </div>';
714 return preg_replace('/\{\!\}/', '', $rv);
718 * FilePicker JS template for 'Upload file' repository
720 * Content to display when user chooses 'Upload file' repository (will be nested inside
721 * element with class 'fp-content').
723 * Must contain form (enctype="multipart/form-data" method="POST")
725 * The elements with the following classnames must be present:
726 * 'fp-file', 'fp-saveas', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
727 * one input element (or select in case of fp-setlicense). They may also have labels.
729 * Element with class 'fp-upload-btn' will hold onclick event for uploading the file;
731 * Please note that some fields may be hidden using CSS if this is part of quickupload form
733 * @return string
735 private function fp_js_template_uploadform() {
736 $rv = '
737 <div class="fp-upload-form mdl-align">
738 <div class="fp-content-center">
739 <form enctype="multipart/form-data" method="POST">
740 <table >
741 <tr class="{!}fp-file">
742 <td class="mdl-right"><label>'.get_string('attachment', 'repository').'</label>:</td>
743 <td class="mdl-left"><input type="file"/></td></tr>
744 <tr class="{!}fp-saveas">
745 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
746 <td class="mdl-left"><input type="text"/></td></tr>
747 <tr class="{!}fp-setauthor">
748 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
749 <td class="mdl-left"><input type="text"/></td></tr>
750 <tr class="{!}fp-setlicense">
751 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
752 <td class="mdl-left"><select></select></td></tr>
753 </table>
754 </form>
755 <div><button class="{!}fp-upload-btn">'.get_string('upload', 'repository').'</button></div>
756 </div>
757 </div> ';
758 return preg_replace('/\{\!\}/', '', $rv);
762 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
764 * @return string
766 private function fp_js_template_loading() {
767 return '
768 <div class="fp-content-loading">
769 <div class="fp-content-center">
770 <img src="'.$this->pix_url('i/loading_small').'" />
771 </div>
772 </div>';
776 * FilePicker JS template for error (inside element with class 'fp-content').
778 * must have element with class 'fp-error', its content will be replaced with error text
779 * and the error code will be assigned as additional class to this element
780 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
782 * @return string
784 private function fp_js_template_error() {
785 $rv = '
786 <div class="fp-content-error" ><div class="{!}fp-error"></div></div>';
787 return preg_replace('/\{\!\}/', '', $rv);
791 * FilePicker JS template for error/info message displayed as a separate popup window.
793 * Must be wrapped in one element, CSS for this element must define
794 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
795 * or 'fp-msg-info' depending on message type;
797 * content of element with class 'fp-msg-text' will be replaced with error/info text;
799 * element with class 'fp-msg-butok' will hold onclick event
801 * @return string
803 private function fp_js_template_message() {
804 $rv = '
805 <div class="file-picker fp-msg">
806 <p class="{!}fp-msg-text"></p>
807 <button class="{!}fp-msg-butok">'.get_string('ok').'</button>
808 </div>';
809 return preg_replace('/\{\!\}/', '', $rv);
813 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
815 * Must have one top element, CSS for this element must define width and height of the window;
817 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
818 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename',
819 * 'fp-dlg-butoverwriteall', 'fp-dlg-butrenameall' and 'fp-dlg-butcancel' will
820 * hold onclick events;
822 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
823 * (Note that it may have long text)
825 * @return string
827 private function fp_js_template_processexistingfile() {
828 $rv = '
829 <div class="file-picker fp-dlg">
830 <p class="{!}fp-dlg-text"></p>
831 <div class="fp-dlg-buttons">
832 <button class="{!}fp-dlg-butoverwrite">'.get_string('overwrite', 'repository').'</button>
833 <button class="{!}fp-dlg-butrename"></button>
834 <button class="{!}fp-dlg-butcancel">'.get_string('cancel').'</button>
835 </div>
836 </div>';
837 return preg_replace('/\{\!\}/', '', $rv);
841 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists (multiple-file version).
843 * Must have one top element, CSS for this element must define width and height of the window;
845 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
846 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename' and 'fp-dlg-butcancel' will
847 * hold onclick events;
849 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
850 * (Note that it may have long text)
852 * @return string
854 private function fp_js_template_processexistingfilemultiple() {
855 $rv = '
856 <div class="file-picker fp-dlg">
857 <p class="{!}fp-dlg-text"></p>
858 <a class="{!}fp-dlg-butoverwrite fp-panel-button" href="#">'.get_string('overwrite', 'repository').'</a>
859 <a class="{!}fp-dlg-butcancel fp-panel-button" href="#">'.get_string('cancel').'</a>
860 <a class="{!}fp-dlg-butrename fp-panel-button" href="#"></a>
861 <br/>
862 <a class="{!}fp-dlg-butoverwriteall fp-panel-button" href="#">'.get_string('overwriteall', 'repository').'</a>
863 <a class="{!}fp-dlg-butrenameall fp-panel-button" href="#">'.get_string('renameall', 'repository').'</a>
864 </div>';
865 return preg_replace('/\{\!\}/', '', $rv);
869 * FilePicker JS template for repository login form including templates for each element type
871 * Must contain one <form> element with templates for different input types inside:
872 * Elements with classes 'fp-login-popup', 'fp-login-textarea', 'fp-login-select' and
873 * 'fp-login-input' are templates for displaying respective login form elements. Inside
874 * there must be exactly one element with type <button>, <textarea>, <select> or <input>
875 * (i.e. fp-login-popup should have <button>, fp-login-textarea should have <textarea>, etc.);
876 * They may also contain the <label> element and it's content will be substituted with
877 * label;
879 * You can also define elements with classes 'fp-login-checkbox', 'fp-login-text'
880 * but if they are not found, 'fp-login-input' will be used;
882 * Element with class 'fp-login-radiogroup' will be used for group of radio inputs. Inside
883 * it should hava a template for one radio input (with class 'fp-login-radio');
885 * Element with class 'fp-login-submit' will hold on click mouse event (form submission). It
886 * will be removed if at least one popup element is present;
888 * @return string
890 private function fp_js_template_loginform() {
891 $rv = '
892 <div class="fp-login-form">
893 <div class="fp-content-center">
894 <form>
895 <table >
896 <tr class="{!}fp-login-popup">
897 <td colspan="2">
898 <label>'.get_string('popup', 'repository').'</label>
899 <p class="fp-popup"><button class="{!}fp-login-popup-but">'.get_string('login', 'repository').'</button></p></td></tr>
900 <tr class="{!}fp-login-textarea">
901 <td colspan="2"><p><textarea></textarea></p></td></tr>
902 <tr class="{!}fp-login-select">
903 <td align="right"><label></label></td>
904 <td align="left"><select></select></td></tr>
905 <tr class="{!}fp-login-input">
906 <td class="label"><label></label></td>
907 <td class="input"><input/></td></tr>
908 <tr class="{!}fp-login-radiogroup">
909 <td align="right" width="30%" valign="top"><label></label></td>
910 <td align="left" valign="top"><p class="{!}fp-login-radio"><input /> <label></label></p></td></tr>
911 </table>
912 <p><button class="{!}fp-login-submit">'.get_string('submit', 'repository').'</button></p>
913 </form>
914 </div>
915 </div>';
916 return preg_replace('/\{\!\}/', '', $rv);
920 * Returns all FilePicker JavaScript templates as an array.
922 * @return array
924 public function filepicker_js_templates() {
925 $class_methods = get_class_methods($this);
926 $templates = array();
927 foreach ($class_methods as $method_name) {
928 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
929 $templates[$matches[1]] = $this->$method_name();
931 return $templates;
935 * Returns HTML for default repository searchform to be passed to Filepicker
937 * This will be used as contents for search form defined in generallayout template
938 * (form with id {TOOLSEARCHID}).
939 * Default contents is one text input field with name="s"
941 public function repository_default_searchform() {
942 $searchinput = html_writer::label(get_string('searchrepo', 'repository'),
943 'reposearch', false, array('class' => 'accesshide'));
944 $searchinput .= html_writer::empty_tag('input', array('type' => 'text',
945 'id' => 'reposearch', 'name' => 's', 'value' => get_string('search', 'repository')));
946 $str = html_writer::tag('div', $searchinput, array('class' => "fp-def-search"));
948 return $str;
953 * Data structure representing a general moodle file tree viewer
955 * @copyright 2010 Dongsheng Cai
956 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
957 * @since Moodle 2.0
959 class files_tree_viewer implements renderable {
960 public $tree;
961 public $path;
962 public $context;
965 * Constructor of moodle_file_tree_viewer class
966 * @param file_info $file_info
967 * @param array $options
969 public function __construct(file_info $file_info, array $options = null) {
970 global $CFG;
972 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
973 $this->options = (array)$options;
974 $this->context = $options['context'];
976 $this->tree = array();
977 $children = $file_info->get_children();
978 $current_file_params = $file_info->get_params();
979 $parent_info = $file_info->get_parent();
980 $level = $parent_info;
981 $this->path = array();
982 while ($level) {
983 $params = $level->get_params();
984 $context = context::instance_by_id($params['contextid']);
985 // $this->context is current context
986 if ($context->id != $this->context->id or empty($params['filearea'])) {
987 break;
989 // unset unused parameters
990 unset($params['component']);
991 unset($params['filearea']);
992 unset($params['filename']);
993 unset($params['itemid']);
994 $url = new moodle_url('/files/index.php', $params);
995 $this->path[] = html_writer::link($url, $level->get_visible_name());
996 $level = $level->get_parent();
998 $this->path = array_reverse($this->path);
999 if ($current_file_params['filepath'] != '/') {
1000 $this->path[] = $file_info->get_visible_name();
1003 foreach ($children as $child) {
1004 $filedate = $child->get_timemodified();
1005 $filesize = $child->get_filesize();
1006 $mimetype = $child->get_mimetype();
1007 $params = $child->get_params();
1008 unset($params['component']);
1009 unset($params['filearea']);
1010 unset($params['filename']);
1011 unset($params['itemid']);
1012 $fileitem = array(
1013 'params' => $params,
1014 'filename' => $child->get_visible_name(),
1015 'mimetype' => $child->get_mimetype(),
1016 'filedate' => $filedate ? $filedate : '',
1017 'filesize' => $filesize ? $filesize : ''
1019 $url = new moodle_url('/files/index.php', $params);
1020 if ($child->is_directory()) {
1021 $fileitem['isdir'] = true;
1022 $fileitem['url'] = $url->out(false);
1023 } else {
1024 $fileitem['url'] = $child->get_url();
1026 $this->tree[] = $fileitem;