Merge branch 'MDL-35155-27' of git://github.com/danpoltawski/moodle into MOODLE_27_STABLE
[moodle.git] / files / renderer.php
blob719b363d633054649cfea0f572724d8517a465c0
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('moodle-core-notification-dialogue', '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'), array('newfolder', 'repository'), array('edit', 'moodle')
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');
194 $straddfiletext = get_string('addfiletext', 'repository');
195 $strcreatefolder = get_string('createfolder', 'repository');
196 $strdownloadallfiles = get_string('downloadallfiles', 'repository');
198 $html = '
199 <div id="filemanager-'.$client_id.'" class="filemanager fm-loading">
200 <div class="fp-restrictions">
201 '.$restrictions.'
202 <span class="dnduploadnotsupported-message"> - '.$strdndnotsupported.' </span>
203 </div>
204 <div class="fp-navbar">
205 <div class="filemanager-toolbar">
206 <div class="fp-toolbar">
207 <div class="fp-btn-add">
208 <a role="button" title="' . $straddfile . '" href="#">
209 <img src="' . $this->pix_url('a/add_file') . '" alt="' . $straddfiletext . '" />
210 </a>
211 </div>
212 <div class="fp-btn-mkdir">
213 <a role="button" title="' . $strmakedir . '" href="#">
214 <img src="' . $this->pix_url('a/create_folder') . '" alt="' . $strcreatefolder . '" />
215 </a>
216 </div>
217 <div class="fp-btn-download">
218 <a role="button" title="' . $strdownload . '" href="#">
219 <img src="' . $this->pix_url('a/download_all').'" alt="' . $strdownloadallfiles . '" />
220 </a>
221 </div>
222 <img class="fp-img-downloading" src="'.$this->pix_url('i/loading_small').'" alt="" />
223 </div>
224 <div class="fp-viewbar">
225 <a title="'. get_string('displayicons', 'repository') .'" class="fp-vb-icons" href="#">
226 <img alt="'. get_string('displayasicons', 'repository') .'" src="'. $this->pix_url('fp/view_icon_active', 'theme') .'" />
227 </a>
228 <a title="'. get_string('displaydetails', 'repository') .'" class="fp-vb-details" href="#">
229 <img alt="'. get_string('displayasdetails', 'repository') .'" src="'. $this->pix_url('fp/view_list_active', 'theme') .'" />
230 </a>
231 <a title="'. get_string('displaytree', 'repository') .'" class="fp-vb-tree" href="#">
232 <img alt="'. get_string('displayastree', 'repository') .'" src="'. $this->pix_url('fp/view_tree_active', 'theme') .'" />
233 </a>
234 </div>
235 </div>
236 <div class="fp-pathbar">
237 <span class="fp-path-folder"><a class="fp-path-folder-name" href="#"></a></span>
238 </div>
239 </div>
240 <div class="filemanager-loading mdl-align">'.$icon_progress.'</div>
241 <div class="filemanager-container" >
242 <div class="fm-content-wrapper">
243 <div class="fp-content"></div>
244 <div class="fm-empty-container">
245 <div class="dndupload-message">'.$strdndenabledinbox.'<br/><div class="dndupload-arrow"></div></div>
246 </div>
247 <div class="dndupload-target">'.$strdroptoupload.'<br/><div class="dndupload-arrow"></div></div>
248 <div class="dndupload-progressbars"></div>
249 <div class="dndupload-uploadinprogress">'.$icon_progress.'</div>
250 </div>
251 <div class="filemanager-updating">'.$icon_progress.'</div>
252 </div>
253 </div>';
254 return $html;
258 * FileManager JS template for displaying one file in 'icon view' mode.
260 * Except for elements described in fp_js_template_iconfilename, this template may also
261 * contain element with class 'fp-contextmenu'. If context menu is available for this
262 * file, the top element will receive the additional class 'fp-hascontextmenu' and
263 * the element with class 'fp-contextmenu' will hold onclick event for displaying
264 * the context menu.
266 * @see fp_js_template_iconfilename()
267 * @return string
269 private function fm_js_template_iconfilename() {
270 $rv = '
271 <div class="fp-file">
272 <a href="#">
273 <div style="position:relative;">
274 <div class="fp-thumbnail"></div>
275 <div class="fp-reficons1"></div>
276 <div class="fp-reficons2"></div>
277 </div>
278 <div class="fp-filename-field">
279 <div class="fp-filename"></div>
280 </div>
281 </a>
282 <a class="fp-contextmenu" href="#">'.$this->pix_icon('i/menu', 'â–¶').'</a>
283 </div>';
284 return $rv;
288 * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
290 * Except for elements described in fp_js_template_listfilename, this template may also
291 * contain element with class 'fp-contextmenu'. If context menu is available for this
292 * file, the top element will receive the additional class 'fp-hascontextmenu' and
293 * the element with class 'fp-contextmenu' will hold onclick event for displaying
294 * the context menu.
296 * @todo MDL-32736 remove onclick="return false;"
297 * @see fp_js_template_listfilename()
298 * @return string
300 private function fm_js_template_listfilename() {
301 $rv = '
302 <span class="fp-filename-icon">
303 <a href="#">
304 <span class="fp-icon"></span>
305 <span class="fp-reficons1"></span>
306 <span class="fp-reficons2"></span>
307 <span class="fp-filename"></span>
308 </a>
309 <a class="fp-contextmenu" href="#" onclick="return false;">'.$this->pix_icon('i/menu', 'â–¶').'</a>
310 </span>';
311 return $rv;
315 * FileManager JS template for displaying 'Make new folder' dialog.
317 * Must be wrapped in an element, CSS for this element must define width and height of the window;
319 * Must have one input element with type="text" (for users to enter the new folder name);
321 * content of element with class 'fp-dlg-curpath' will be replaced with current path where
322 * new folder is about to be created;
323 * elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
325 * @return string
327 private function fm_js_template_mkdir() {
328 $rv = '
329 <div class="filemanager fp-mkdir-dlg" role="dialog" aria-live="assertive" aria-labelledby="fp-mkdir-dlg-title">
330 <div class="fp-mkdir-dlg-text">
331 <label id="fp-mkdir-dlg-title">' . get_string('newfoldername', 'repository') . '</label><br/>
332 <input type="text" />
333 </div>
334 <button class="fp-dlg-butcreate btn-primary btn">'.get_string('makeafolder').'</button>
335 <button class="fp-dlg-butcancel btn-cancel btn">'.get_string('cancel').'</button>
336 </div>';
337 return $rv;
341 * FileManager JS template for error/info message displayed as a separate popup window.
343 * @see fp_js_template_message()
344 * @return string
346 private function fm_js_template_message() {
347 return $this->fp_js_template_message();
351 * FileManager JS template for window with file information/actions.
353 * All content must be enclosed in one element, CSS for this class must define width and
354 * height of the window;
356 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
358 * Inside the window the elements with the following classnames must be present:
359 * 'fp-saveas', 'fp-author', 'fp-license', 'fp-path'. Inside each of them must be
360 * one input element (or select in case of fp-license and fp-path). They may also have labels.
361 * The elements will be assign with class 'uneditable' and input/select element will become
362 * disabled if they are not applicable for the particular file;
364 * There may be present elements with classes 'fp-original', 'fp-datemodified', 'fp-datecreated',
365 * 'fp-size', 'fp-dimensions', 'fp-reflist'. They will receive additional class 'fp-unknown' if
366 * information is unavailable. If there is information available, the content of embedded
367 * element with class 'fp-value' will be substituted with the value;
369 * The value of Original ('fp-original') is loaded in separate request. When it is applicable
370 * but not yet loaded the 'fp-original' element receives additional class 'fp-loading';
372 * The value of 'Aliases/Shortcuts' ('fp-reflist') is also loaded in separate request. When it
373 * is applicable but not yet loaded the 'fp-original' element receives additional class
374 * 'fp-loading'. The string explaining that XX references exist will replace content of element
375 * 'fp-refcount'. Inside '.fp-reflist .fp-value' each reference will be enclosed in <li>;
377 * Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
378 * 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
379 * events (there may be several elements with class 'fp-file-cancel');
381 * When confirm button is pressed and file is being selected, the top element receives
382 * additional class 'loading'. It is removed when response from server is received.
384 * When any of the input fields is changed, the top element receives class 'fp-changed';
385 * When current file can be set as main - top element receives class 'fp-cansetmain';
386 * When current file is folder/zip/file - top element receives respectfully class
387 * 'fp-folder'/'fp-zip'/'fp-file';
389 * @return string
391 private function fm_js_template_fileselectlayout() {
392 global $OUTPUT;
393 $strloading = get_string('loading', 'repository');
394 $iconprogress = $this->pix_icon('i/loading_small', $strloading).'';
395 $rv = '
396 <div class="filemanager fp-select">
397 <div class="fp-select-loading">
398 <img src="'.$this->pix_url('i/loading_small').'" />
399 </div>
400 <form class="form-horizontal">
401 <button class="fp-file-download">'.get_string('download').'</button>
402 <button class="fp-file-delete">'.get_string('delete').'</button>
403 <button class="fp-file-setmain">'.get_string('setmainfile', 'repository').'</button>
404 <span class="fp-file-setmain-help">'.$OUTPUT->help_icon('setmainfile', 'repository').'</span>
405 <button class="fp-file-zip">'.get_string('zip', 'editor').'</button>
406 <button class="fp-file-unzip">'.get_string('unzip').'</button>
407 <div class="fp-hr"></div>
409 <div class="fp-forminset">
410 <div class="fp-saveas control-group clearfix">
411 <label class="control-label">'.get_string('name', 'repository').'</label>
412 <div class="controls">
413 <input type="text"/>
414 </div>
415 </div>
416 <div class="fp-author control-group clearfix">
417 <label class="control-label">'.get_string('author', 'repository').'</label>
418 <div class="controls">
419 <input type="text"/>
420 </div>
421 </div>
422 <div class="fp-license control-group clearfix">
423 <label class="control-label">'.get_string('chooselicense', 'repository').'</label>
424 <div class="controls">
425 <select></select>
426 </div>
427 </div>
428 <div class="fp-path control-group clearfix">
429 <label class="control-label">'.get_string('path', 'repository').'</label>
430 <div class="controls">
431 <select></select>
432 </div>
433 </div>
434 <div class="fp-original control-group clearfix">
435 <label class="control-label">'.get_string('original', 'repository').'</label>
436 <div class="controls">
437 <span class="fp-originloading">'.$iconprogress.' '.$strloading.'</span><span class="fp-value"></span>
438 </div>
439 </div>
440 <div class="fp-reflist control-group clearfix">
441 <label class="control-label">'.get_string('referenceslist', 'repository').'</label>
442 <div class="controls">
443 <p class="fp-refcount"></p>
444 <span class="fp-reflistloading">'.$iconprogress.' '.$strloading.'</span>
445 <ul class="fp-value"></ul>
446 </div>
447 </div>
448 </div>
449 <div class="fp-select-buttons">
450 <button class="fp-file-update btn-primary btn">'.get_string('update', 'moodle').'</button>
451 <button class="fp-file-cancel btn-cancel btn">'.get_string('cancel').'</button>
452 </div>
453 </form>
454 <div class="fp-info clearfix">
455 <div class="fp-hr"></div>
456 <p class="fp-thumbnail"></p>
457 <div class="fp-fileinfo">
458 <div class="fp-datemodified">'.get_string('lastmodified', 'repository').' <span class="fp-value"></span></div>
459 <div class="fp-datecreated">'.get_string('datecreated', 'repository').' <span class="fp-value"></span></div>
460 <div class="fp-size">'.get_string('size', 'repository').' <span class="fp-value"></span></div>
461 <div class="fp-dimensions">'.get_string('dimensions', 'repository').' <span class="fp-value"></span></div>
462 </div>
463 </div>
464 </div>';
465 return $rv;
469 * FileManager JS template for popup confirm dialogue window.
471 * Must have one top element, CSS for this element must define width and height of the window;
473 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
474 * elements with classes 'fp-dlg-butconfirm' and 'fp-dlg-butcancel' will
475 * hold onclick events;
477 * @return string
479 private function fm_js_template_confirmdialog() {
480 $rv = '
481 <div class="filemanager fp-dlg">
482 <div class="fp-dlg-text"></div>
483 <button class="fp-dlg-butconfirm btn-primary btn">'.get_string('ok').'</button>
484 <button class="fp-dlg-butcancel btn-cancel btn">'.get_string('cancel').'</button>
485 </div>';
486 return $rv;
490 * Returns all FileManager JavaScript templates as an array.
492 * @return array
494 public function filemanager_js_templates() {
495 $class_methods = get_class_methods($this);
496 $templates = array();
497 foreach ($class_methods as $method_name) {
498 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
499 $templates[$matches[1]] = $this->$method_name();
501 return $templates;
505 * Displays restrictions for the file manager
507 * @param form_filemanager $fm
508 * @return string
510 private function fm_print_restrictions($fm) {
511 $maxbytes = display_size($fm->options->maxbytes);
512 $strparam = (object) array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles,
513 'areasize' => display_size($fm->options->areamaxbytes));
514 $hasmaxfiles = !empty($fm->options->maxfiles) && $fm->options->maxfiles > 0;
515 $hasarealimit = !empty($fm->options->areamaxbytes) && $fm->options->areamaxbytes != -1;
516 if ($hasmaxfiles && $hasarealimit) {
517 $maxsize = get_string('maxsizeandattachmentsandareasize', 'moodle', $strparam);
518 } else if ($hasmaxfiles) {
519 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
520 } else if ($hasarealimit) {
521 $maxsize = get_string('maxsizeandareasize', 'moodle', $strparam);
522 } else {
523 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
525 // TODO MDL-32020 also should say about 'File types accepted'
526 return '<span>'. $maxsize . '</span>';
530 * Template for FilePicker with general layout (not QuickUpload).
532 * Must have one top element containing everything else (recommended <div class="file-picker">),
533 * CSS for this element must define width and height of the filepicker window. Or CSS must
534 * define min-width, max-width, min-height and max-height and in this case the filepicker
535 * window will be resizeable;
537 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
538 * can be changed or not;
539 * Inside element with class 'fp-viewbar' there are expected elements with classes
540 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
541 * between the view modes, the last clicked element will have the class 'checked';
543 * Element with class 'fp-repo' is a template for displaying one repository. Other repositories
544 * will be attached as siblings (classes first/last/even/odd will be added respectfully).
545 * The currently selected repostory will have class 'active'. Contents of element with class
546 * 'fp-repo-name' will be replaced with repository name, source of image with class
547 * 'fp-repo-icon' will be replaced with repository icon;
549 * Element with class 'fp-content' is obligatory and will hold the current contents;
551 * Element with class 'fp-paging' will contain page navigation (will be deprecated soon);
553 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
554 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
555 * Parent element will receive class 'empty' when there are no folders to be displayed;
556 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
558 * Element with class 'fp-toolbar' will have class 'empty' if all 'Back', 'Search', 'Refresh',
559 * 'Logout', 'Manage' and 'Help' are unavailable for this repo;
561 * Inside fp-toolbar there are expected elements with classes fp-tb-back, fp-tb-search,
562 * fp-tb-refresh, fp-tb-logout, fp-tb-manage and fp-tb-help. Each of them will have
563 * class 'enabled' or 'disabled' if particular repository has this functionality.
564 * Element with class 'fp-tb-search' must contain empty form inside, it's contents will
565 * be substituted with the search form returned by repository (in the most cases it
566 * is generated with template core_repository_renderer::repository_default_searchform);
567 * Other elements must have either <a> or <button> element inside, it will hold onclick
568 * event for corresponding action; labels for fp-tb-back and fp-tb-logout may be
569 * replaced with those specified by repository;
571 * @return string
573 private function fp_js_template_generallayout() {
574 $rv = '
575 <div tabindex="0" class="file-picker fp-generallayout" role="dialog" aria-live="assertive">
576 <div class="fp-repo-area">
577 <ul class="fp-list">
578 <li class="fp-repo">
579 <a href="#"><img class="fp-repo-icon" alt=" " width="16" height="16" />&nbsp;<span class="fp-repo-name"></span></a>
580 </li>
581 </ul>
582 </div>
583 <div class="fp-repo-items" tabindex="0">
584 <div class="fp-navbar">
585 <div>
586 <div class="fp-toolbar">
587 <div class="fp-tb-back">
588 <a href="#">'.get_string('back', 'repository').'</a>
589 </div>
590 <div class="fp-tb-search">
591 <form></form>
592 </div>
593 <div class="fp-tb-refresh">
594 <a title="'. get_string('refresh', 'repository') .'" href="#">
595 <img alt="" src="'.$this->pix_url('a/refresh').'" />
596 </a>
597 </div>
598 <div class="fp-tb-logout">
599 <a title="'. get_string('logout', 'repository') .'" href="#">
600 <img alt="" src="'.$this->pix_url('a/logout').'" />
601 </a>
602 </div>
603 <div class="fp-tb-manage">
604 <a title="'. get_string('settings', 'repository') .'" href="#">
605 <img alt="" src="'.$this->pix_url('a/setting').'" />
606 </a>
607 </div>
608 <div class="fp-tb-help">
609 <a title="'. get_string('help', 'repository') .'" href="#">
610 <img alt="" src="'.$this->pix_url('a/help').'" />
611 </a>
612 </div>
613 <div class="fp-tb-message"></div>
614 </div>
615 <div class="fp-viewbar">
616 <a role="button" title="'. get_string('displayicons', 'repository') .'" class="fp-vb-icons" href="#">
617 <img alt="" src="'. $this->pix_url('fp/view_icon_active', 'theme') .'" />
618 </a>
619 <a role="button" title="'. get_string('displaydetails', 'repository') .'" class="fp-vb-details" href="#">
620 <img alt="" src="'. $this->pix_url('fp/view_list_active', 'theme') .'" />
621 </a>
622 <a role="button" title="'. get_string('displaytree', 'repository') .'" class="fp-vb-tree" href="#">
623 <img alt="" src="'. $this->pix_url('fp/view_tree_active', 'theme') .'" />
624 </a>
625 </div>
626 <div class="fp-clear-left"></div>
627 </div>
628 <div class="fp-pathbar">
629 <span class="fp-path-folder"><a class="fp-path-folder-name" href="#"></a></span>
630 </div>
631 </div>
632 <div class="fp-content"></div>
633 </div>
634 </div>';
635 return $rv;
639 * FilePicker JS template for displaying one file in 'icon view' mode.
641 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
642 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
643 * an element will be replaced with an appropriate img;
645 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
646 * (unless min-width is set in css) and the content of an element will be replaced with filename
647 * supplied by repository;
649 * top element(s) will have class fp-folder if the element is a folder;
651 * List of files will have parent <div> element with class 'fp-iconview'
653 * @return string
655 private function fp_js_template_iconfilename() {
656 $rv = '
657 <a class="fp-file" href="#" >
658 <div style="position:relative;">
659 <div class="fp-thumbnail"></div>
660 <div class="fp-reficons1"></div>
661 <div class="fp-reficons2"></div>
662 </div>
663 <div class="fp-filename-field">
664 <p class="fp-filename"></p>
665 </div>
666 </a>';
667 return $rv;
671 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
673 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
675 * content of element with class 'fp-filename' will be replaced with filename supplied by
676 * repository;
678 * top element(s) will have class fp-folder if the element is a folder;
680 * Note that tree view and table view are the YUI widgets and therefore there are no
681 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
682 * fp-tableview (respectfully).
684 * @return string
686 private function fp_js_template_listfilename() {
687 $rv = '
688 <span class="fp-filename-icon">
689 <a href="#">
690 <span class="fp-icon"></span>
691 <span class="fp-filename"></span>
692 </a>
693 </span>';
694 return $rv;
698 * FilePicker JS template for displaying link/loading progress for fetching of the next page
700 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
702 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
703 * class 'loading' during loading of the next page (it is recommended that in this case the link
704 * becomes unavailable). Also must contain one element <a> or <button> that will hold
705 * onclick event for displaying of the next page. The event will be triggered automatically
706 * when user scrolls to this link.
708 * @return string
710 private function fp_js_template_nextpage() {
711 $rv = '
712 <div class="fp-nextpage">
713 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
714 <div class="fp-nextpage-loading">
715 <img src="'.$this->pix_url('i/loading_small').'" />
716 </div>
717 </div>';
718 return $rv;
722 * FilePicker JS template for window appearing to select a file.
724 * All content must be enclosed in one element, CSS for this class must define width and
725 * height of the window;
727 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
729 * Inside the window the elements with the following classnames must be present:
730 * 'fp-saveas', 'fp-linktype-2', 'fp-linktype-1', 'fp-linktype-4', 'fp-setauthor',
731 * 'fp-setlicense'. Inside each of them must have one input element (or select in case of
732 * fp-setlicense). They may also have labels.
733 * The elements will be assign with class 'uneditable' and input/select element will become
734 * disabled if they are not applicable for the particular file;
736 * There may be present elements with classes 'fp-datemodified', 'fp-datecreated', 'fp-size',
737 * 'fp-license', 'fp-author', 'fp-dimensions'. They will receive additional class 'fp-unknown'
738 * if information is unavailable. If there is information available, the content of embedded
739 * element with class 'fp-value' will be substituted with the value;
741 * Elements with classes 'fp-select-confirm' and 'fp-select-cancel' will hold corresponding
742 * onclick events;
744 * When confirm button is pressed and file is being selected, the top element receives
745 * additional class 'loading'. It is removed when response from server is received.
747 * @return string
749 private function fp_js_template_selectlayout() {
750 $rv = '
751 <div class="file-picker fp-select">
752 <div class="fp-select-loading">
753 <img src="'.$this->pix_url('i/loading_small').'" />
754 </div>
755 <form class="form-horizontal">
756 <div class="fp-forminset">
757 <div class="fp-linktype-2 control-group control-radio clearfix">
758 <label class="control-label control-radio">'.get_string('makefileinternal', 'repository').'</label>
759 <div class="controls control-radio">
760 <input type="radio"/>
761 </div>
762 </div>
763 <div class="fp-linktype-1 control-group control-radio clearfix">
764 <label class="control-label control-radio">'.get_string('makefilelink', 'repository').'</label>
765 <div class="controls control-radio">
766 <input type="radio"/>
767 </div>
768 </div>
769 <div class="fp-linktype-4 control-group control-radio clearfix">
770 <label class="control-label control-radio">'.get_string('makefilereference', 'repository').'</label>
771 <div class="controls control-radio">
772 <input type="radio"/>
773 </div>
774 </div>
775 <div class="fp-saveas control-group clearfix">
776 <label class="control-label">'.get_string('saveas', 'repository').'</label>
777 <div class="controls">
778 <input type="text"/>
779 </div>
780 </div>
781 <div class="fp-setauthor control-group clearfix">
782 <label class="control-label">'.get_string('author', 'repository').'</label>
783 <div class="controls">
784 <input type="text"/>
785 </div>
786 </div>
787 <div class="fp-setlicense control-group clearfix">
788 <label class="control-label">'.get_string('chooselicense', 'repository').'</label>
789 <div class="controls">
790 <select></select>
791 </div>
792 </div>
793 </div>
794 <div class="fp-select-buttons">
795 <button class="fp-select-confirm btn-primary btn">'.get_string('getfile', 'repository').'</button>
796 <button class="fp-select-cancel btn-cancel btn">'.get_string('cancel').'</button>
797 </div>
798 </form>
799 <div class="fp-info clearfix">
800 <div class="fp-hr"></div>
801 <p class="fp-thumbnail"></p>
802 <div class="fp-fileinfo">
803 <div class="fp-datemodified">'.get_string('lastmodified', 'repository').'<span class="fp-value"></span></div>
804 <div class="fp-datecreated">'.get_string('datecreated', 'repository').'<span class="fp-value"></span></div>
805 <div class="fp-size">'.get_string('size', 'repository').'<span class="fp-value"></span></div>
806 <div class="fp-license">'.get_string('license', 'repository').'<span class="fp-value"></span></div>
807 <div class="fp-author">'.get_string('author', 'repository').'<span class="fp-value"></span></div>
808 <div class="fp-dimensions">'.get_string('dimensions', 'repository').'<span class="fp-value"></span></div>
809 </div>
810 </div>
811 </div>';
812 return $rv;
816 * FilePicker JS template for 'Upload file' repository
818 * Content to display when user chooses 'Upload file' repository (will be nested inside
819 * element with class 'fp-content').
821 * Must contain form (enctype="multipart/form-data" method="POST")
823 * The elements with the following classnames must be present:
824 * 'fp-file', 'fp-saveas', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
825 * one input element (or select in case of fp-setlicense). They may also have labels.
827 * Element with class 'fp-upload-btn' will hold onclick event for uploading the file;
829 * Please note that some fields may be hidden using CSS if this is part of quickupload form
831 * @return string
833 private function fp_js_template_uploadform() {
834 $rv = '
835 <div class="fp-upload-form">
836 <div class="fp-content-center">
837 <form enctype="multipart/form-data" method="POST" class="form-horizontal">
838 <div class="fp-formset">
839 <div class="fp-file control-group clearfix">
840 <label class="control-label">'.get_string('attachment', 'repository').'</label>
841 <div class="controls">
842 <input type="file"/>
843 </div>
844 </div>
845 <div class="fp-saveas control-group clearfix">
846 <label class="control-label">'.get_string('saveas', 'repository').'</label>
847 <div class="controls">
848 <input type="text"/>
849 </div>
850 </div>
851 <div class="fp-setauthor control-group clearfix">
852 <label class="control-label">'.get_string('author', 'repository').'</label>
853 <div class="controls">
854 <input type="text"/>
855 </div>
856 </div>
857 <div class="fp-setlicense control-group clearfix">
858 <label class="control-label">'.get_string('chooselicense', 'repository').'</label>
859 <div class="controls">
860 <select ></select>
861 </div>
862 </div>
863 </div>
864 </form>
865 <div class="mdl-align">
866 <button class="fp-upload-btn btn-primary btn">'.get_string('upload', 'repository').'</button>
867 </div>
868 </div>
869 </div> ';
870 return $rv;
874 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
876 * @return string
878 private function fp_js_template_loading() {
879 return '
880 <div class="fp-content-loading">
881 <div class="fp-content-center">
882 <img src="'.$this->pix_url('i/loading_small').'" />
883 </div>
884 </div>';
888 * FilePicker JS template for error (inside element with class 'fp-content').
890 * must have element with class 'fp-error', its content will be replaced with error text
891 * and the error code will be assigned as additional class to this element
892 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
894 * @return string
896 private function fp_js_template_error() {
897 $rv = '
898 <div class="fp-content-error" ><div class="fp-error"></div></div>';
899 return $rv;
903 * FilePicker JS template for error/info message displayed as a separate popup window.
905 * Must be wrapped in one element, CSS for this element must define
906 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
907 * or 'fp-msg-info' depending on message type;
909 * content of element with class 'fp-msg-text' will be replaced with error/info text;
911 * element with class 'fp-msg-butok' will hold onclick event
913 * @return string
915 private function fp_js_template_message() {
916 $rv = '
917 <div class="file-picker fp-msg" role="alertdialog" aria-live="assertive" aria-labelledby="fp-msg-labelledby">
918 <p class="fp-msg-text" id="fp-msg-labelledby"></p>
919 <button class="fp-msg-butok btn-primary btn">'.get_string('ok').'</button>
920 </div>';
921 return $rv;
925 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
927 * Must have one top element, CSS for this element must define width and height of the window;
929 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
930 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename',
931 * 'fp-dlg-butoverwriteall', 'fp-dlg-butrenameall' and 'fp-dlg-butcancel' will
932 * hold onclick events;
934 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
935 * (Note that it may have long text)
937 * @return string
939 private function fp_js_template_processexistingfile() {
940 $rv = '
941 <div class="file-picker fp-dlg">
942 <p class="fp-dlg-text"></p>
943 <div class="fp-dlg-buttons">
944 <button class="fp-dlg-butoverwrite btn">'.get_string('overwrite', 'repository').'</button>
945 <button class="fp-dlg-butrename btn"></button>
946 <button class="fp-dlg-butcancel btn btn-cancel">'.get_string('cancel').'</button>
947 </div>
948 </div>';
949 return $rv;
953 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists (multiple-file version).
955 * Must have one top element, CSS for this element must define width and height of the window;
957 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
958 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename' and 'fp-dlg-butcancel' will
959 * hold onclick events;
961 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
962 * (Note that it may have long text)
964 * @return string
966 private function fp_js_template_processexistingfilemultiple() {
967 $rv = '
968 <div class="file-picker fp-dlg">
969 <p class="fp-dlg-text"></p>
970 <a class="fp-dlg-butoverwrite fp-panel-button" href="#">'.get_string('overwrite', 'repository').'</a>
971 <a class="fp-dlg-butcancel fp-panel-button" href="#">'.get_string('cancel').'</a>
972 <a class="fp-dlg-butrename fp-panel-button" href="#"></a>
973 <br/>
974 <a class="fp-dlg-butoverwriteall fp-panel-button" href="#">'.get_string('overwriteall', 'repository').'</a>
975 <a class="fp-dlg-butrenameall fp-panel-button" href="#">'.get_string('renameall', 'repository').'</a>
976 </div>';
977 return $rv;
981 * FilePicker JS template for repository login form including templates for each element type
983 * Must contain one <form> element with templates for different input types inside:
984 * Elements with classes 'fp-login-popup', 'fp-login-textarea', 'fp-login-select' and
985 * 'fp-login-input' are templates for displaying respective login form elements. Inside
986 * there must be exactly one element with type <button>, <textarea>, <select> or <input>
987 * (i.e. fp-login-popup should have <button>, fp-login-textarea should have <textarea>, etc.);
988 * They may also contain the <label> element and it's content will be substituted with
989 * label;
991 * You can also define elements with classes 'fp-login-checkbox', 'fp-login-text'
992 * but if they are not found, 'fp-login-input' will be used;
994 * Element with class 'fp-login-radiogroup' will be used for group of radio inputs. Inside
995 * it should hava a template for one radio input (with class 'fp-login-radio');
997 * Element with class 'fp-login-submit' will hold on click mouse event (form submission). It
998 * will be removed if at least one popup element is present;
1000 * @return string
1002 private function fp_js_template_loginform() {
1003 $rv = '
1004 <div class="fp-login-form">
1005 <div class="fp-content-center">
1006 <form class="form-horizontal">
1007 <div class="fp-formset">
1008 <div class="fp-login-popup control-group clearfix">
1009 <div class="controls fp-popup">
1010 <button class="fp-login-popup-but btn-primary btn">'.get_string('login', 'repository').'</button>
1011 </div>
1012 </div>
1013 <div class="fp-login-textarea control-group clearfix">
1014 <div class="controls"><textarea></textarea></div>
1015 </div>
1016 <div class="fp-login-select control-group clearfix">
1017 <label class="control-label"></label>
1019 <div class="controls"><select></select></div>
1020 </div>
1021 <div class="fp-login-input control-group clearfix">
1022 <label class="control-label"></label>
1023 <div class="controls"><input/></div>
1024 </div>
1025 <div class="fp-login-radiogroup control-group clearfix">
1026 <label class="control-label"></label>
1027 <div class="controls fp-login-radio"><input /> <label></label></div>
1028 </div>
1029 </div>
1030 <p><button class="fp-login-submit btn-primary btn">'.get_string('submit', 'repository').'</button></p>
1031 </form>
1032 </div>
1033 </div>';
1034 return $rv;
1038 * Returns all FilePicker JavaScript templates as an array.
1040 * @return array
1042 public function filepicker_js_templates() {
1043 $class_methods = get_class_methods($this);
1044 $templates = array();
1045 foreach ($class_methods as $method_name) {
1046 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
1047 $templates[$matches[1]] = $this->$method_name();
1049 return $templates;
1053 * Returns HTML for default repository searchform to be passed to Filepicker
1055 * This will be used as contents for search form defined in generallayout template
1056 * (form with id {TOOLSEARCHID}).
1057 * Default contents is one text input field with name="s"
1059 public function repository_default_searchform() {
1060 $searchinput = html_writer::label(get_string('searchrepo', 'repository'),
1061 'reposearch', false, array('class' => 'accesshide'));
1062 $searchinput .= html_writer::empty_tag('input', array('type' => 'text',
1063 'id' => 'reposearch', 'name' => 's', 'value' => get_string('search', 'repository')));
1064 $str = html_writer::tag('div', $searchinput, array('class' => "fp-def-search"));
1066 return $str;
1071 * Data structure representing a general moodle file tree viewer
1073 * @copyright 2010 Dongsheng Cai
1074 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1075 * @since Moodle 2.0
1077 class files_tree_viewer implements renderable {
1078 public $tree;
1079 public $path;
1080 public $context;
1083 * Constructor of moodle_file_tree_viewer class
1084 * @param file_info $file_info
1085 * @param array $options
1087 public function __construct(file_info $file_info, array $options = null) {
1088 global $CFG;
1090 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
1091 $this->options = (array)$options;
1092 $this->context = $options['context'];
1094 $this->tree = array();
1095 $children = $file_info->get_children();
1096 $current_file_params = $file_info->get_params();
1097 $parent_info = $file_info->get_parent();
1098 $level = $parent_info;
1099 $this->path = array();
1100 while ($level) {
1101 $params = $level->get_params();
1102 $context = context::instance_by_id($params['contextid']);
1103 // $this->context is current context
1104 if ($context->id != $this->context->id or empty($params['filearea'])) {
1105 break;
1107 // unset unused parameters
1108 unset($params['component']);
1109 unset($params['filearea']);
1110 unset($params['filename']);
1111 unset($params['itemid']);
1112 $url = new moodle_url('/files/index.php', $params);
1113 $this->path[] = html_writer::link($url, $level->get_visible_name());
1114 $level = $level->get_parent();
1116 $this->path = array_reverse($this->path);
1117 if ($current_file_params['filepath'] != '/') {
1118 $this->path[] = $file_info->get_visible_name();
1121 foreach ($children as $child) {
1122 $filedate = $child->get_timemodified();
1123 $filesize = $child->get_filesize();
1124 $mimetype = $child->get_mimetype();
1125 $params = $child->get_params();
1126 unset($params['component']);
1127 unset($params['filearea']);
1128 unset($params['filename']);
1129 unset($params['itemid']);
1130 $fileitem = array(
1131 'params' => $params,
1132 'filename' => $child->get_visible_name(),
1133 'mimetype' => $child->get_mimetype(),
1134 'filedate' => $filedate ? $filedate : '',
1135 'filesize' => $filesize ? $filesize : ''
1137 $url = new moodle_url('/files/index.php', $params);
1138 if ($child->is_directory()) {
1139 $fileitem['isdir'] = true;
1140 $fileitem['url'] = $url->out(false);
1141 } else {
1142 $fileitem['url'] = $child->get_url();
1144 $this->tree[] = $fileitem;