Merge branch 'wip-MDL-44287-master' of git://github.com/marinaglancy/moodle
[moodle.git] / files / renderer.php
blob76ffedf9c933f9b5268a0cfa9a71c86872af8d62
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');
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">
205 <a role="button" title="'.$straddfile.'" href="#"><img src="'.$this->pix_url('a/add_file').'" alt="" /></a>
206 </div>
207 <div class="fp-btn-mkdir">
208 <a role="button" title="'.$strmakedir.'" href="#"><img src="'.$this->pix_url('a/create_folder').'" alt="" /></a>
209 </div>
210 <div class="fp-btn-download">
211 <a role="button" title="'.$strdownload.'" href="#"><img src="'.$this->pix_url('a/download_all').'" alt="" /></a>
212 </div>
213 </div>
214 <div class="fp-viewbar">
215 <a title="'. get_string('displayicons', 'repository') .'" class="fp-vb-icons" href="#">
216 <img alt="" src="'. $this->pix_url('fp/view_icon_active', 'theme') .'" />
217 </a>
218 <a title="'. get_string('displaydetails', 'repository') .'" class="fp-vb-details" href="#">
219 <img alt="" src="'. $this->pix_url('fp/view_list_active', 'theme') .'" />
220 </a>
221 <a title="'. get_string('displaytree', 'repository') .'" class="fp-vb-tree" href="#">
222 <img alt="" src="'. $this->pix_url('fp/view_tree_active', 'theme') .'" />
223 </a>
224 </div>
225 </div>
226 <div class="fp-pathbar">
227 <span class="fp-path-folder"><a class="fp-path-folder-name" href="#"></a></span>
228 </div>
229 </div>
230 <div class="filemanager-loading mdl-align">'.$icon_progress.'</div>
231 <div class="filemanager-container" >
232 <div class="fm-content-wrapper">
233 <div class="fp-content"></div>
234 <div class="fm-empty-container">
235 <div class="dndupload-message">'.$strdndenabledinbox.'<br/><div class="dndupload-arrow"></div></div>
236 </div>
237 <div class="dndupload-target">'.$strdroptoupload.'<br/><div class="dndupload-arrow"></div></div>
238 <div class="dndupload-progressbars"></div>
239 <div class="dndupload-uploadinprogress">'.$icon_progress.'</div>
240 </div>
241 <div class="filemanager-updating">'.$icon_progress.'</div>
242 </div>
243 </div>';
244 return $html;
248 * FileManager JS template for displaying one file in 'icon view' mode.
250 * Except for elements described in fp_js_template_iconfilename, this template may also
251 * contain element with class 'fp-contextmenu'. If context menu is available for this
252 * file, the top element will receive the additional class 'fp-hascontextmenu' and
253 * the element with class 'fp-contextmenu' will hold onclick event for displaying
254 * the context menu.
256 * @see fp_js_template_iconfilename()
257 * @return string
259 private function fm_js_template_iconfilename() {
260 $rv = '
261 <div class="fp-file">
262 <a href="#">
263 <div style="position:relative;">
264 <div class="fp-thumbnail"></div>
265 <div class="fp-reficons1"></div>
266 <div class="fp-reficons2"></div>
267 </div>
268 <div class="fp-filename-field">
269 <div class="fp-filename"></div>
270 </div>
271 </a>
272 <a class="fp-contextmenu" href="#">'.$this->pix_icon('i/menu', 'â–¶').'</a>
273 </div>';
274 return $rv;
278 * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
280 * Except for elements described in fp_js_template_listfilename, this template may also
281 * contain element with class 'fp-contextmenu'. If context menu is available for this
282 * file, the top element will receive the additional class 'fp-hascontextmenu' and
283 * the element with class 'fp-contextmenu' will hold onclick event for displaying
284 * the context menu.
286 * @todo MDL-32736 remove onclick="return false;"
287 * @see fp_js_template_listfilename()
288 * @return string
290 private function fm_js_template_listfilename() {
291 $rv = '
292 <span class="fp-filename-icon">
293 <a href="#">
294 <span class="fp-icon"></span>
295 <span class="fp-reficons1"></span>
296 <span class="fp-reficons2"></span>
297 <span class="fp-filename"></span>
298 </a>
299 <a class="fp-contextmenu" href="#" onclick="return false;">'.$this->pix_icon('i/menu', 'â–¶').'</a>
300 </span>';
301 return $rv;
305 * FileManager JS template for displaying 'Make new folder' dialog.
307 * Must be wrapped in an element, CSS for this element must define width and height of the window;
309 * Must have one input element with type="text" (for users to enter the new folder name);
311 * content of element with class 'fp-dlg-curpath' will be replaced with current path where
312 * new folder is about to be created;
313 * elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
315 * @return string
317 private function fm_js_template_mkdir() {
318 $rv = '
319 <div class="filemanager fp-mkdir-dlg" role="dialog" aria-live="assertive" aria-labelledby="fp-mkdir-dlg-title">
320 <div class="fp-mkdir-dlg-text">
321 <label id="fp-mkdir-dlg-title">' . get_string('newfoldername', 'repository') . '</label><br/>
322 <input type="text" />
323 </div>
324 <button class="fp-dlg-butcreate btn-primary btn">'.get_string('makeafolder').'</button>
325 <button class="fp-dlg-butcancel btn-cancel btn">'.get_string('cancel').'</button>
326 </div>';
327 return $rv;
331 * FileManager JS template for error/info message displayed as a separate popup window.
333 * @see fp_js_template_message()
334 * @return string
336 private function fm_js_template_message() {
337 return $this->fp_js_template_message();
341 * FileManager JS template for window with file information/actions.
343 * All content must be enclosed in one element, CSS for this class must define width and
344 * height of the window;
346 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
348 * Inside the window the elements with the following classnames must be present:
349 * 'fp-saveas', 'fp-author', 'fp-license', 'fp-path'. Inside each of them must be
350 * one input element (or select in case of fp-license and fp-path). They may also have labels.
351 * The elements will be assign with class 'uneditable' and input/select element will become
352 * disabled if they are not applicable for the particular file;
354 * There may be present elements with classes 'fp-original', 'fp-datemodified', 'fp-datecreated',
355 * 'fp-size', 'fp-dimensions', 'fp-reflist'. They will receive additional class 'fp-unknown' if
356 * information is unavailable. If there is information available, the content of embedded
357 * element with class 'fp-value' will be substituted with the value;
359 * The value of Original ('fp-original') is loaded in separate request. When it is applicable
360 * but not yet loaded the 'fp-original' element receives additional class 'fp-loading';
362 * The value of 'Aliases/Shortcuts' ('fp-reflist') is also loaded in separate request. When it
363 * is applicable but not yet loaded the 'fp-original' element receives additional class
364 * 'fp-loading'. The string explaining that XX references exist will replace content of element
365 * 'fp-refcount'. Inside '.fp-reflist .fp-value' each reference will be enclosed in <li>;
367 * Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
368 * 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
369 * events (there may be several elements with class 'fp-file-cancel');
371 * When confirm button is pressed and file is being selected, the top element receives
372 * additional class 'loading'. It is removed when response from server is received.
374 * When any of the input fields is changed, the top element receives class 'fp-changed';
375 * When current file can be set as main - top element receives class 'fp-cansetmain';
376 * When current file is folder/zip/file - top element receives respectfully class
377 * 'fp-folder'/'fp-zip'/'fp-file';
379 * @return string
381 private function fm_js_template_fileselectlayout() {
382 global $OUTPUT;
383 $strloading = get_string('loading', 'repository');
384 $iconprogress = $this->pix_icon('i/loading_small', $strloading).'';
385 $rv = '
386 <div class="filemanager fp-select">
387 <div class="fp-select-loading">
388 <img src="'.$this->pix_url('i/loading_small').'" />
389 </div>
390 <form class="form-horizontal">
391 <button class="fp-file-download">'.get_string('download').'</button>
392 <button class="fp-file-delete">'.get_string('delete').'</button>
393 <button class="fp-file-setmain">'.get_string('setmainfile', 'repository').'</button>
394 <span class="fp-file-setmain-help">'.$OUTPUT->help_icon('setmainfile', 'repository').'</span>
395 <button class="fp-file-zip">'.get_string('zip', 'editor').'</button>
396 <button class="fp-file-unzip">'.get_string('unzip').'</button>
397 <div class="fp-hr"></div>
399 <div class="fp-forminset">
400 <div class="fp-saveas control-group clearfix">
401 <label class="control-label">'.get_string('name', 'repository').'</label>
402 <div class="controls">
403 <input type="text"/>
404 </div>
405 </div>
406 <div class="fp-author control-group clearfix">
407 <label class="control-label">'.get_string('author', 'repository').'</label>
408 <div class="controls">
409 <input type="text"/>
410 </div>
411 </div>
412 <div class="fp-license control-group clearfix">
413 <label class="control-label">'.get_string('chooselicense', 'repository').'</label>
414 <div class="controls">
415 <select></select>
416 </div>
417 </div>
418 <div class="fp-path control-group clearfix">
419 <label class="control-label">'.get_string('path', 'repository').'</label>
420 <div class="controls">
421 <select></select>
422 </div>
423 </div>
424 <div class="fp-original control-group clearfix">
425 <label class="control-label">'.get_string('original', 'repository').'</label>
426 <div class="controls">
427 <span class="fp-originloading">'.$iconprogress.' '.$strloading.'</span><span class="fp-value"></span>
428 </div>
429 </div>
430 <div class="fp-reflist control-group clearfix">
431 <label class="control-label">'.get_string('referenceslist', 'repository').'</label>
432 <div class="controls">
433 <p class="fp-refcount"></p>
434 <span class="fp-reflistloading">'.$iconprogress.' '.$strloading.'</span>
435 <ul class="fp-value"></ul>
436 </div>
437 </div>
438 </div>
439 <div class="fp-select-buttons">
440 <button class="fp-file-update btn-primary btn">'.get_string('update', 'moodle').'</button>
441 <button class="fp-file-cancel btn-cancel btn">'.get_string('cancel').'</button>
442 </div>
443 </form>
444 <div class="fp-info clearfix">
445 <div class="fp-hr"></div>
446 <p class="fp-thumbnail"></p>
447 <div class="fp-fileinfo">
448 <div class="fp-datemodified">'.get_string('lastmodified', 'repository').' <span class="fp-value"></span></div>
449 <div class="fp-datecreated">'.get_string('datecreated', 'repository').' <span class="fp-value"></span></div>
450 <div class="fp-size">'.get_string('size', 'repository').' <span class="fp-value"></span></div>
451 <div class="fp-dimensions">'.get_string('dimensions', 'repository').' <span class="fp-value"></span></div>
452 </div>
453 </div>
454 </div>';
455 return $rv;
459 * FileManager JS template for popup confirm dialogue window.
461 * Must have one top element, CSS for this element must define width and height of the window;
463 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
464 * elements with classes 'fp-dlg-butconfirm' and 'fp-dlg-butcancel' will
465 * hold onclick events;
467 * @return string
469 private function fm_js_template_confirmdialog() {
470 $rv = '
471 <div class="filemanager fp-dlg">
472 <div class="fp-dlg-text"></div>
473 <button class="fp-dlg-butconfirm btn-primary btn">'.get_string('ok').'</button>
474 <button class="fp-dlg-butcancel btn-cancel btn">'.get_string('cancel').'</button>
475 </div>';
476 return $rv;
480 * Returns all FileManager JavaScript templates as an array.
482 * @return array
484 public function filemanager_js_templates() {
485 $class_methods = get_class_methods($this);
486 $templates = array();
487 foreach ($class_methods as $method_name) {
488 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
489 $templates[$matches[1]] = $this->$method_name();
491 return $templates;
495 * Displays restrictions for the file manager
497 * @param form_filemanager $fm
498 * @return string
500 private function fm_print_restrictions($fm) {
501 $maxbytes = display_size($fm->options->maxbytes);
502 $strparam = (object) array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles,
503 'areasize' => display_size($fm->options->areamaxbytes));
504 $hasmaxfiles = !empty($fm->options->maxfiles) && $fm->options->maxfiles > 0;
505 $hasarealimit = !empty($fm->options->areamaxbytes) && $fm->options->areamaxbytes != -1;
506 if ($hasmaxfiles && $hasarealimit) {
507 $maxsize = get_string('maxsizeandattachmentsandareasize', 'moodle', $strparam);
508 } else if ($hasmaxfiles) {
509 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
510 } else if ($hasarealimit) {
511 $maxsize = get_string('maxsizeandareasize', 'moodle', $strparam);
512 } else {
513 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
515 // TODO MDL-32020 also should say about 'File types accepted'
516 return '<span>'. $maxsize . '</span>';
520 * Template for FilePicker with general layout (not QuickUpload).
522 * Must have one top element containing everything else (recommended <div class="file-picker">),
523 * CSS for this element must define width and height of the filepicker window. Or CSS must
524 * define min-width, max-width, min-height and max-height and in this case the filepicker
525 * window will be resizeable;
527 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
528 * can be changed or not;
529 * Inside element with class 'fp-viewbar' there are expected elements with classes
530 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
531 * between the view modes, the last clicked element will have the class 'checked';
533 * Element with class 'fp-repo' is a template for displaying one repository. Other repositories
534 * will be attached as siblings (classes first/last/even/odd will be added respectfully).
535 * The currently selected repostory will have class 'active'. Contents of element with class
536 * 'fp-repo-name' will be replaced with repository name, source of image with class
537 * 'fp-repo-icon' will be replaced with repository icon;
539 * Element with class 'fp-content' is obligatory and will hold the current contents;
541 * Element with class 'fp-paging' will contain page navigation (will be deprecated soon);
543 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
544 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
545 * Parent element will receive class 'empty' when there are no folders to be displayed;
546 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
548 * Element with class 'fp-toolbar' will have class 'empty' if all 'Back', 'Search', 'Refresh',
549 * 'Logout', 'Manage' and 'Help' are unavailable for this repo;
551 * Inside fp-toolbar there are expected elements with classes fp-tb-back, fp-tb-search,
552 * fp-tb-refresh, fp-tb-logout, fp-tb-manage and fp-tb-help. Each of them will have
553 * class 'enabled' or 'disabled' if particular repository has this functionality.
554 * Element with class 'fp-tb-search' must contain empty form inside, it's contents will
555 * be substituted with the search form returned by repository (in the most cases it
556 * is generated with template core_repository_renderer::repository_default_searchform);
557 * Other elements must have either <a> or <button> element inside, it will hold onclick
558 * event for corresponding action; labels for fp-tb-back and fp-tb-logout may be
559 * replaced with those specified by repository;
561 * @return string
563 private function fp_js_template_generallayout() {
564 $rv = '
565 <div tabindex="0" class="file-picker fp-generallayout" role="dialog" aria-live="assertive">
566 <div class="fp-repo-area">
567 <ul class="fp-list">
568 <li class="fp-repo">
569 <a href="#"><img class="fp-repo-icon" alt=" " width="16" height="16" />&nbsp;<span class="fp-repo-name"></span></a>
570 </li>
571 </ul>
572 </div>
573 <div class="fp-repo-items" tabindex="0">
574 <div class="fp-navbar">
575 <div>
576 <div class="fp-toolbar">
577 <div class="fp-tb-back">
578 <a href="#">'.get_string('back', 'repository').'</a>
579 </div>
580 <div class="fp-tb-search">
581 <form></form>
582 </div>
583 <div class="fp-tb-refresh">
584 <a title="'. get_string('refresh', 'repository') .'" href="#">
585 <img alt="" src="'.$this->pix_url('a/refresh').'" />
586 </a>
587 </div>
588 <div class="fp-tb-logout">
589 <a title="'. get_string('logout', 'repository') .'" href="#">
590 <img alt="" src="'.$this->pix_url('a/logout').'" />
591 </a>
592 </div>
593 <div class="fp-tb-manage">
594 <a title="'. get_string('settings', 'repository') .'" href="#">
595 <img alt="" src="'.$this->pix_url('a/setting').'" />
596 </a>
597 </div>
598 <div class="fp-tb-help">
599 <a title="'. get_string('help', 'repository') .'" href="#">
600 <img alt="" src="'.$this->pix_url('a/help').'" />
601 </a>
602 </div>
603 <div class="fp-tb-message"></div>
604 </div>
605 <div class="fp-viewbar">
606 <a title="'. get_string('displayicons', 'repository') .'" class="fp-vb-icons" href="#">
607 <img alt="" src="'. $this->pix_url('fp/view_icon_active', 'theme') .'" />
608 </a>
609 <a title="'. get_string('displaydetails', 'repository') .'" class="fp-vb-details" href="#">
610 <img alt="" src="'. $this->pix_url('fp/view_list_active', 'theme') .'" />
611 </a>
612 <a title="'. get_string('displaytree', 'repository') .'" class="fp-vb-tree" href="#">
613 <img alt="" src="'. $this->pix_url('fp/view_tree_active', 'theme') .'" />
614 </a>
615 </div>
616 <div class="fp-clear-left"></div>
617 </div>
618 <div class="fp-pathbar">
619 <span class="fp-path-folder"><a class="fp-path-folder-name" href="#"></a></span>
620 </div>
621 </div>
622 <div class="fp-content"></div>
623 </div>
624 </div>';
625 return $rv;
629 * FilePicker JS template for displaying one file in 'icon view' mode.
631 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
632 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
633 * an element will be replaced with an appropriate img;
635 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
636 * (unless min-width is set in css) and the content of an element will be replaced with filename
637 * supplied by repository;
639 * top element(s) will have class fp-folder if the element is a folder;
641 * List of files will have parent <div> element with class 'fp-iconview'
643 * @return string
645 private function fp_js_template_iconfilename() {
646 $rv = '
647 <a class="fp-file" href="#" >
648 <div style="position:relative;">
649 <div class="fp-thumbnail"></div>
650 <div class="fp-reficons1"></div>
651 <div class="fp-reficons2"></div>
652 </div>
653 <div class="fp-filename-field">
654 <p class="fp-filename"></p>
655 </div>
656 </a>';
657 return $rv;
661 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
663 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
665 * content of element with class 'fp-filename' will be replaced with filename supplied by
666 * repository;
668 * top element(s) will have class fp-folder if the element is a folder;
670 * Note that tree view and table view are the YUI widgets and therefore there are no
671 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
672 * fp-tableview (respectfully).
674 * @return string
676 private function fp_js_template_listfilename() {
677 $rv = '
678 <span class="fp-filename-icon">
679 <a href="#">
680 <span class="fp-icon"></span>
681 <span class="fp-filename"></span>
682 </a>
683 </span>';
684 return $rv;
688 * FilePicker JS template for displaying link/loading progress for fetching of the next page
690 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
692 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
693 * class 'loading' during loading of the next page (it is recommended that in this case the link
694 * becomes unavailable). Also must contain one element <a> or <button> that will hold
695 * onclick event for displaying of the next page. The event will be triggered automatically
696 * when user scrolls to this link.
698 * @return string
700 private function fp_js_template_nextpage() {
701 $rv = '
702 <div class="fp-nextpage">
703 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
704 <div class="fp-nextpage-loading">
705 <img src="'.$this->pix_url('i/loading_small').'" />
706 </div>
707 </div>';
708 return $rv;
712 * FilePicker JS template for window appearing to select a file.
714 * All content must be enclosed in one element, CSS for this class must define width and
715 * height of the window;
717 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
719 * Inside the window the elements with the following classnames must be present:
720 * 'fp-saveas', 'fp-linktype-2', 'fp-linktype-1', 'fp-linktype-4', 'fp-setauthor',
721 * 'fp-setlicense'. Inside each of them must have one input element (or select in case of
722 * fp-setlicense). They may also have labels.
723 * The elements will be assign with class 'uneditable' and input/select element will become
724 * disabled if they are not applicable for the particular file;
726 * There may be present elements with classes 'fp-datemodified', 'fp-datecreated', 'fp-size',
727 * 'fp-license', 'fp-author', 'fp-dimensions'. They will receive additional class 'fp-unknown'
728 * if information is unavailable. If there is information available, the content of embedded
729 * element with class 'fp-value' will be substituted with the value;
731 * Elements with classes 'fp-select-confirm' and 'fp-select-cancel' will hold corresponding
732 * onclick events;
734 * When confirm button is pressed and file is being selected, the top element receives
735 * additional class 'loading'. It is removed when response from server is received.
737 * @return string
739 private function fp_js_template_selectlayout() {
740 $rv = '
741 <div class="file-picker fp-select">
742 <div class="fp-select-loading">
743 <img src="'.$this->pix_url('i/loading_small').'" />
744 </div>
745 <form class="form-horizontal">
746 <div class="fp-forminset">
747 <div class="fp-linktype-2 control-group clearfix">
748 <label class="control-label">'.get_string('makefileinternal', 'repository').'</label>
749 <div class="controls">
750 <input type="radio"/>
751 </div>
752 </div>
753 <div class="fp-linktype-1 control-group clearfix">
754 <label class="control-label">'.get_string('makefilelink', 'repository').'</label>
755 <div class="controls">
756 <input type="radio"/>
757 </div>
758 </div>
759 <div class="fp-linktype-4 control-group clearfix">
760 <label class="control-label">'.get_string('makefilereference', 'repository').'</label>
761 <div class="controls">
762 <input type="radio"/>
763 </div>
764 </div>
765 <div class="fp-saveas control-group clearfix">
766 <label class="control-label">'.get_string('saveas', 'repository').'</label>
767 <div class="controls">
768 <input type="text"/>
769 </div>
770 </div>
771 <div class="fp-setauthor control-group clearfix">
772 <label class="control-label">'.get_string('author', 'repository').'</label>
773 <div class="controls">
774 <input type="text"/>
775 </div>
776 </div>
777 <div class="fp-setlicense control-group clearfix">
778 <label class="control-label">'.get_string('chooselicense', 'repository').'</label>
779 <div class="controls">
780 <select></select>
781 </div>
782 </div>
783 </div>
784 <div class="fp-select-buttons">
785 <button class="fp-select-confirm btn-primary btn">'.get_string('getfile', 'repository').'</button>
786 <button class="fp-select-cancel btn-cancel btn">'.get_string('cancel').'</button>
787 </div>
788 </form>
789 <div class="fp-info clearfix">
790 <div class="fp-hr"></div>
791 <p class="fp-thumbnail"></p>
792 <div class="fp-fileinfo">
793 <div class="fp-datemodified">'.get_string('lastmodified', 'moodle').'<span class="fp-value"></span></div>
794 <div class="fp-datecreated">'.get_string('datecreated', 'repository').'<span class="fp-value"></span></div>
795 <div class="fp-size">'.get_string('size', 'repository').'<span class="fp-value"></span></div>
796 <div class="fp-license">'.get_string('license', 'moodle').'<span class="fp-value"></span></div>
797 <div class="fp-author">'.get_string('author', 'repository').'<span class="fp-value"></span></div>
798 <div class="fp-dimensions">'.get_string('dimensions', 'repository').'<span class="fp-value"></span></div>
799 </div>
800 <div>
801 </div>';
802 return $rv;
806 * FilePicker JS template for 'Upload file' repository
808 * Content to display when user chooses 'Upload file' repository (will be nested inside
809 * element with class 'fp-content').
811 * Must contain form (enctype="multipart/form-data" method="POST")
813 * The elements with the following classnames must be present:
814 * 'fp-file', 'fp-saveas', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
815 * one input element (or select in case of fp-setlicense). They may also have labels.
817 * Element with class 'fp-upload-btn' will hold onclick event for uploading the file;
819 * Please note that some fields may be hidden using CSS if this is part of quickupload form
821 * @return string
823 private function fp_js_template_uploadform() {
824 $rv = '
825 <div class="fp-upload-form">
826 <div class="fp-content-center">
827 <form enctype="multipart/form-data" method="POST" class="form-horizontal">
828 <div class="fp-formset">
829 <div class="fp-file control-group clearfix">
830 <label class="control-label">'.get_string('attachment', 'repository').'</label>
831 <div class="controls">
832 <input type="file"/>
833 </div>
834 </div>
835 <div class="fp-saveas control-group clearfix">
836 <label class="control-label">'.get_string('saveas', 'repository').'</label>
837 <div class="controls">
838 <input type="text"/>
839 </div>
840 </div>
841 <div class="fp-setauthor control-group clearfix">
842 <label class="control-label">'.get_string('author', 'repository').'</label>
843 <div class="controls">
844 <input type="text"/>
845 </div>
846 </div>
847 <div class="fp-setlicense control-group clearfix">
848 <label class="control-label">'.get_string('chooselicense', 'repository').'</label>
849 <div class="controls">
850 <select ></select>
851 </div>
852 </div>
853 </div>
854 </form>
855 <div class="mdl-align">
856 <button class="fp-upload-btn btn-primary btn">'.get_string('upload', 'repository').'</button>
857 </div>
858 </div>
859 </div> ';
860 return $rv;
864 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
866 * @return string
868 private function fp_js_template_loading() {
869 return '
870 <div class="fp-content-loading">
871 <div class="fp-content-center">
872 <img src="'.$this->pix_url('i/loading_small').'" />
873 </div>
874 </div>';
878 * FilePicker JS template for error (inside element with class 'fp-content').
880 * must have element with class 'fp-error', its content will be replaced with error text
881 * and the error code will be assigned as additional class to this element
882 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
884 * @return string
886 private function fp_js_template_error() {
887 $rv = '
888 <div class="fp-content-error" ><div class="fp-error"></div></div>';
889 return $rv;
893 * FilePicker JS template for error/info message displayed as a separate popup window.
895 * Must be wrapped in one element, CSS for this element must define
896 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
897 * or 'fp-msg-info' depending on message type;
899 * content of element with class 'fp-msg-text' will be replaced with error/info text;
901 * element with class 'fp-msg-butok' will hold onclick event
903 * @return string
905 private function fp_js_template_message() {
906 $rv = '
907 <div class="file-picker fp-msg" role="alertdialog" aria-live="assertive" aria-labelledby="fp-msg-labelledby">
908 <p class="fp-msg-text" id="fp-msg-labelledby"></p>
909 <button class="fp-msg-butok btn-primary btn">'.get_string('ok').'</button>
910 </div>';
911 return $rv;
915 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
917 * Must have one top element, CSS for this element must define width and height of the window;
919 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
920 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename',
921 * 'fp-dlg-butoverwriteall', 'fp-dlg-butrenameall' and 'fp-dlg-butcancel' will
922 * hold onclick events;
924 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
925 * (Note that it may have long text)
927 * @return string
929 private function fp_js_template_processexistingfile() {
930 $rv = '
931 <div class="file-picker fp-dlg">
932 <p class="fp-dlg-text"></p>
933 <div class="fp-dlg-buttons">
934 <button class="fp-dlg-butoverwrite btn">'.get_string('overwrite', 'repository').'</button>
935 <button class="fp-dlg-butrename btn"></button>
936 <button class="fp-dlg-butcancel btn btn-cancel">'.get_string('cancel').'</button>
937 </div>
938 </div>';
939 return $rv;
943 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists (multiple-file version).
945 * Must have one top element, CSS for this element must define width and height of the window;
947 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
948 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename' and 'fp-dlg-butcancel' will
949 * hold onclick events;
951 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
952 * (Note that it may have long text)
954 * @return string
956 private function fp_js_template_processexistingfilemultiple() {
957 $rv = '
958 <div class="file-picker fp-dlg">
959 <p class="fp-dlg-text"></p>
960 <a class="fp-dlg-butoverwrite fp-panel-button" href="#">'.get_string('overwrite', 'repository').'</a>
961 <a class="fp-dlg-butcancel fp-panel-button" href="#">'.get_string('cancel').'</a>
962 <a class="fp-dlg-butrename fp-panel-button" href="#"></a>
963 <br/>
964 <a class="fp-dlg-butoverwriteall fp-panel-button" href="#">'.get_string('overwriteall', 'repository').'</a>
965 <a class="fp-dlg-butrenameall fp-panel-button" href="#">'.get_string('renameall', 'repository').'</a>
966 </div>';
967 return $rv;
971 * FilePicker JS template for repository login form including templates for each element type
973 * Must contain one <form> element with templates for different input types inside:
974 * Elements with classes 'fp-login-popup', 'fp-login-textarea', 'fp-login-select' and
975 * 'fp-login-input' are templates for displaying respective login form elements. Inside
976 * there must be exactly one element with type <button>, <textarea>, <select> or <input>
977 * (i.e. fp-login-popup should have <button>, fp-login-textarea should have <textarea>, etc.);
978 * They may also contain the <label> element and it's content will be substituted with
979 * label;
981 * You can also define elements with classes 'fp-login-checkbox', 'fp-login-text'
982 * but if they are not found, 'fp-login-input' will be used;
984 * Element with class 'fp-login-radiogroup' will be used for group of radio inputs. Inside
985 * it should hava a template for one radio input (with class 'fp-login-radio');
987 * Element with class 'fp-login-submit' will hold on click mouse event (form submission). It
988 * will be removed if at least one popup element is present;
990 * @return string
992 private function fp_js_template_loginform() {
993 $rv = '
994 <div class="fp-login-form">
995 <div class="fp-content-center">
996 <form class="form-horizontal">
997 <div class="fp-formset">
998 <div class="fp-login-popup control-group clearfix">
999 <div class="controls fp-popup">
1000 <button class="fp-login-popup-but btn-primary btn">'.get_string('login', 'repository').'</button>
1001 </div>
1002 </div>
1003 <div class="fp-login-textarea control-group clearfix">
1004 <div class="controls"><textarea></textarea></div>
1005 </div>
1006 <div class="fp-login-select control-group clearfix">
1007 <label class="control-label"></label>
1009 <div class="controls"><select></select></div>
1010 </div>
1011 <div class="fp-login-input control-group clearfix">
1012 <label class="control-label"></label>
1013 <div class="controls"><input/></div>
1014 </div>
1015 <div class="fp-login-radiogroup control-group clearfix">
1016 <label class="control-label"></label>
1017 <div class="controls fp-login-radio"><input /> <label></label></div>
1018 </div>
1019 </div>
1020 <p><button class="fp-login-submit btn-primary btn">'.get_string('submit', 'repository').'</button></p>
1021 </form>
1022 </div>
1023 </div>';
1024 return $rv;
1028 * Returns all FilePicker JavaScript templates as an array.
1030 * @return array
1032 public function filepicker_js_templates() {
1033 $class_methods = get_class_methods($this);
1034 $templates = array();
1035 foreach ($class_methods as $method_name) {
1036 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
1037 $templates[$matches[1]] = $this->$method_name();
1039 return $templates;
1043 * Returns HTML for default repository searchform to be passed to Filepicker
1045 * This will be used as contents for search form defined in generallayout template
1046 * (form with id {TOOLSEARCHID}).
1047 * Default contents is one text input field with name="s"
1049 public function repository_default_searchform() {
1050 $searchinput = html_writer::label(get_string('searchrepo', 'repository'),
1051 'reposearch', false, array('class' => 'accesshide'));
1052 $searchinput .= html_writer::empty_tag('input', array('type' => 'text',
1053 'id' => 'reposearch', 'name' => 's', 'value' => get_string('search', 'repository')));
1054 $str = html_writer::tag('div', $searchinput, array('class' => "fp-def-search"));
1056 return $str;
1061 * Data structure representing a general moodle file tree viewer
1063 * @copyright 2010 Dongsheng Cai
1064 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1065 * @since Moodle 2.0
1067 class files_tree_viewer implements renderable {
1068 public $tree;
1069 public $path;
1070 public $context;
1073 * Constructor of moodle_file_tree_viewer class
1074 * @param file_info $file_info
1075 * @param array $options
1077 public function __construct(file_info $file_info, array $options = null) {
1078 global $CFG;
1080 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
1081 $this->options = (array)$options;
1082 $this->context = $options['context'];
1084 $this->tree = array();
1085 $children = $file_info->get_children();
1086 $current_file_params = $file_info->get_params();
1087 $parent_info = $file_info->get_parent();
1088 $level = $parent_info;
1089 $this->path = array();
1090 while ($level) {
1091 $params = $level->get_params();
1092 $context = context::instance_by_id($params['contextid']);
1093 // $this->context is current context
1094 if ($context->id != $this->context->id or empty($params['filearea'])) {
1095 break;
1097 // unset unused parameters
1098 unset($params['component']);
1099 unset($params['filearea']);
1100 unset($params['filename']);
1101 unset($params['itemid']);
1102 $url = new moodle_url('/files/index.php', $params);
1103 $this->path[] = html_writer::link($url, $level->get_visible_name());
1104 $level = $level->get_parent();
1106 $this->path = array_reverse($this->path);
1107 if ($current_file_params['filepath'] != '/') {
1108 $this->path[] = $file_info->get_visible_name();
1111 foreach ($children as $child) {
1112 $filedate = $child->get_timemodified();
1113 $filesize = $child->get_filesize();
1114 $mimetype = $child->get_mimetype();
1115 $params = $child->get_params();
1116 unset($params['component']);
1117 unset($params['filearea']);
1118 unset($params['filename']);
1119 unset($params['itemid']);
1120 $fileitem = array(
1121 'params' => $params,
1122 'filename' => $child->get_visible_name(),
1123 'mimetype' => $child->get_mimetype(),
1124 'filedate' => $filedate ? $filedate : '',
1125 'filesize' => $filesize ? $filesize : ''
1127 $url = new moodle_url('/files/index.php', $params);
1128 if ($child->is_directory()) {
1129 $fileitem['isdir'] = true;
1130 $fileitem['url'] = $url->out(false);
1131 } else {
1132 $fileitem['url'] = $child->get_url();
1134 $this->tree[] = $fileitem;