2 ///////////////////////////////////////////////////////////////////////////
4 // This file is part of Moodle - http://moodle.org/ //
5 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
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. //
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. //
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/>. //
20 ///////////////////////////////////////////////////////////////////////////
22 defined('MOODLE_INTERNAL') ||
die();
25 * Rendering of files viewer related widgets.
28 * @copyright 2010 Dongsheng Cai
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 * @copyright 2010 Dongsheng Cai
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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) {
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);
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']),
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();
93 * Prints the file manager and initializes all necessary libraries
96 * $fm = new form_filemanager($options);
97 * $output = get_renderer('core', 'files');
98 * echo $output->render($fm);
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);
108 'name'=>'form_filemanager',
109 'fullpath'=>'/lib/form/filemanager.js',
110 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'core_dndupload', 'panel', 'resize-plugin', 'dd-plugin'),
112 array('error', 'moodle'), array('info', 'moodle'), array('confirmdeletefile', 'repository'),
113 array('draftareanofiles', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
114 array('invalidjson', 'repository'), array('popupblockeddownload', 'repository'),
115 array('unknownoriginal', 'repository'), array('confirmdeletefolder', 'repository'),
116 array('confirmdeletefilewithhref', 'repository'), array('confirmrenamefolder', 'repository'),
117 array('confirmrenamefile', 'repository')
120 if (empty($filemanagertemplateloaded)) {
121 $filemanagertemplateloaded = true;
122 $this->page
->requires
->js_init_call('M.form_filemanager.set_templates',
123 array($this->filemanager_js_templates()), true, $module);
125 $this->page
->requires
->js_init_call('M.form_filemanager.init', array($fm->options
), true, $module);
127 // non javascript file manager
128 $html .= '<noscript>';
129 $html .= "<div><object type='text/html' data='".$fm->get_nonjsurl()."' height='160' width='600' style='border:1px solid #000'></object></div>";
130 $html .= '</noscript>';
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
180 private function fm_print_generallayout($fm) {
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 $strdndenabled = get_string('dndenabled_insentence', 'moodle').$OUTPUT->help_icon('dndenabled');
192 $strdndenabledinbox = get_string('dndenabled_inbox', 'moodle');
193 $loading = get_string('loading', 'repository');
196 <div id="filemanager-'.$client_id.'" class="filemanager fm-loading">
197 <div class="fp-restrictions">
199 <span class="dndupload-message"> - '.$strdndenabled.' </span>
201 <div class="fp-navbar">
202 <div class="filemanager-toolbar">
203 <div class="fp-toolbar">
204 <div class="{!}fp-btn-add"><a href="#"><img src="'.$this->pix_url('a/add_file').'" /> '.$straddfile.'</a></div>
205 <div class="{!}fp-btn-mkdir"><a href="#"><img src="'.$this->pix_url('a/create_folder').'" /> '.$strmakedir.'</a></div>
206 <div class="{!}fp-btn-download"><a href="#"><img src="'.$this->pix_url('a/download_all').'" /> '.$strdownload.'</a></div>
208 <div class="{!}fp-viewbar">
209 <a class="{!}fp-vb-icons" href="#"></a>
210 <a class="{!}fp-vb-details" href="#"></a>
211 <a class="{!}fp-vb-tree" href="#"></a>
214 <div class="fp-pathbar">
215 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
218 <div class="filemanager-loading mdl-align">'.$icon_progress.'</div>
219 <div class="filemanager-container" >
220 <div class="fm-content-wrapper">
221 <div class="fp-content"></div>
222 <div class="fm-empty-container <!--mdl-align-->">
223 <span class="dndupload-message">'.$strdndenabledinbox.'<br/><span class="dndupload-arrow"></span></span>
225 <div class="dndupload-target">'.$strdroptoupload.'<br/><span class="dndupload-arrow"></span></div>
226 <div class="dndupload-uploadinprogress">'.$icon_progress.'</div>
228 <div class="filemanager-updating">'.$icon_progress.'</div>
231 return preg_replace('/\{\!\}/', '', $html);
235 * FileManager JS template for displaying one file in 'icon view' mode.
237 * Except for elements described in fp_js_template_iconfilename, this template may also
238 * contain element with class 'fp-contextmenu'. If context menu is available for this
239 * file, the top element will receive the additional class 'fp-hascontextmenu' and
240 * the element with class 'fp-contextmenu' will hold onclick event for displaying
243 * @see fp_js_template_iconfilename()
246 private function fm_js_template_iconfilename() {
248 <div class="fp-file">
250 <div style="position:relative;">
251 <div class="{!}fp-thumbnail"></div>
252 <div class="fp-reficons1"></div>
253 <div class="fp-reficons2"></div>
255 <div class="fp-filename-field">
256 <div class="{!}fp-filename"></div>
259 <a class="{!}fp-contextmenu" href="#">'.$this->pix_icon('i/menu', 'â–¶').'</a>
261 return preg_replace('/\{\!\}/', '', $rv);
265 * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
267 * Except for elements described in fp_js_template_listfilename, this template may also
268 * contain element with class 'fp-contextmenu'. If context menu is available for this
269 * file, the top element will receive the additional class 'fp-hascontextmenu' and
270 * the element with class 'fp-contextmenu' will hold onclick event for displaying
273 * @todo MDL-32736 remove onclick="return false;"
274 * @see fp_js_template_listfilename()
277 private function fm_js_template_listfilename() {
279 <span class="fp-filename-icon">
281 <span class="{!}fp-icon"></span>
282 <span class="{!}fp-filename"></span>
284 <a class="{!}fp-contextmenu" href="#" onclick="return false;">'.$this->pix_icon('i/menu', 'â–¶').'</a>
286 return preg_replace('/\{\!\}/', '', $rv);
290 * FileManager JS template for displaying 'Make new folder' dialog.
292 * Must be wrapped in an element, CSS for this element must define width and height of the window;
294 * Must have one input element with type="text" (for users to enter the new folder name);
296 * content of element with class 'fp-dlg-curpath' will be replaced with current path where
297 * new folder is about to be created;
298 * elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
302 private function fm_js_template_mkdir() {
304 <div class="filemanager fp-mkdir-dlg">
305 <div class="fp-mkdir-dlg-text">'.get_string('newfoldername','repository').'<br/><input type="text" /></div>
306 <a class="{!}fp-dlg-butcreate fp-panel-button" href="#">'.get_string('makeafolder').'</a>
307 <a class="{!}fp-dlg-butcancel fp-panel-button" href="#">'.get_string('cancel').'</a>
309 return preg_replace('/\{\!\}/', '', $rv);
313 * FileManager JS template for error/info message displayed as a separate popup window.
315 * @see fp_js_template_message()
318 private function fm_js_template_message() {
319 return $this->fp_js_template_message();
323 * FileManager JS template for window with file information/actions.
325 * All content must be enclosed in one element, CSS for this class must define width and
326 * height of the window;
328 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
330 * Inside the window the elements with the following classnames must be present:
331 * 'fp-saveas', 'fp-author', 'fp-license', 'fp-path'. Inside each of them must be
332 * one input element (or select in case of fp-license and fp-path). They may also have labels.
333 * The elements will be assign with class 'uneditable' and input/select element will become
334 * disabled if they are not applicable for the particular file;
336 * There may be present elements with classes 'fp-original', 'fp-datemodified', 'fp-datecreated',
337 * 'fp-size', 'fp-dimensions', 'fp-reflist'. They will receive additional class 'fp-unknown' if
338 * information is unavailable. If there is information available, the content of embedded
339 * element with class 'fp-value' will be substituted with the value;
341 * The value of Original ('fp-original') is loaded in separate request. When it is applicable
342 * but not yet loaded the 'fp-original' element receives additional class 'fp-loading';
344 * The value of 'Aliases/Shortcuts' ('fp-reflist') is also loaded in separate request. When it
345 * is applicable but not yet loaded the 'fp-original' element receives additional class
346 * 'fp-loading'. The string explaining that XX references exist will replace content of element
347 * 'fp-refcount'. Inside '.fp-reflist .fp-value' each reference will be enclosed in <li>;
349 * Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
350 * 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
351 * events (there may be several elements with class 'fp-file-cancel');
353 * When confirm button is pressed and file is being selected, the top element receives
354 * additional class 'loading'. It is removed when response from server is received.
356 * When any of the input fields is changed, the top element receives class 'fp-changed';
357 * When current file can be set as main - top element receives class 'fp-cansetmain';
358 * When current file is folder/zip/file - top element receives respectfully class
359 * 'fp-folder'/'fp-zip'/'fp-file';
363 private function fm_js_template_fileselectlayout() {
364 $strloading = get_string('loading', 'repository');
365 $icon_progress = $this->pix_icon('i/loading_small', $strloading).'';
367 <div class="filemanager fp-select">
368 <div class="fp-select-loading">
369 <img src="'.$this->pix_url('i/loading').'" />
370 <p>'.get_string('loading', 'repository').'</p>
373 <div><a class="{!}fp-file-download fp-panel-button" href="#">'.get_string('download').'</a>
374 <a class="{!}fp-file-delete fp-panel-button" href="#">'.get_string('delete').'</a>
375 <a class="{!}fp-file-setmain fp-panel-button" href="#">'.get_string('setmainfile', 'repository').'</a>
376 <a class="{!}fp-file-zip fp-panel-button" href="#">'.get_string('zip', 'editor').'</a>
377 <a class="{!}fp-file-unzip fp-panel-button" href="#">'.get_string('unzip').'</a>
379 <div class="fp-hr"></div>
381 <tr class="{!}fp-saveas"><td class="mdl-right"><label>'.get_string('name', 'moodle').'</label>:</td>
382 <td class="mdl-left"><input type="text"/></td></tr>
383 <tr class="{!}fp-author"><td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
384 <td class="mdl-left"><input type="text"/></td></tr>
385 <tr class="{!}fp-license"><td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
386 <td class="mdl-left"><select></select></td></tr>
387 <tr class="{!}fp-path"><td class="mdl-right"><label>'.get_string('path', 'moodle').'</label>:</td>
388 <td class="mdl-left"><select></select></td></tr>
389 <tr class="{!}fp-original"><td class="mdl-right"><label>'.get_string('original', 'repository').'</label>:</td>
390 <td class="mdl-left"><span class="fp-originloading">'.$icon_progress.' '.$strloading.'</span><span class="fp-value"></span></td></tr>
391 <tr class="{!}fp-reflist"><td class="mdl-right"><label>'.get_string('referenceslist', 'repository').'</label>:</td>
392 <td class="mdl-left"><p class="{!}fp-refcount"></p><span class="fp-reflistloading">'.$icon_progress.' '.$strloading.'</span><ul class="fp-value"></ul></td></tr>
395 <p class="{!}fp-thumbnail"></p>
397 <p class="fp-select-update">
398 <a class="{!}fp-file-update" href="#"><span>'.get_string('update', 'moodle').'</span></a>
399 <a class="{!}fp-file-cancel" href="#"><span>'.get_string('cancel').'</span></a>
402 <div class="fp-fileinfo">
403 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"></span></div>
404 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"></span></div>
405 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"></span></div>
406 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"></span></div>
409 return preg_replace('/\{\!\}/', '', $rv);
413 * FileManager JS template for popup confirm dialogue window.
415 * Must have one top element, CSS for this element must define width and height of the window;
417 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
418 * elements with classes 'fp-dlg-butconfirm' and 'fp-dlg-butcancel' will
419 * hold onclick events;
423 private function fm_js_template_confirmdialog() {
425 <div class="filemanager fp-dlg">
426 <div class="{!}fp-dlg-text"></div>
427 <a class="{!}fp-dlg-butconfirm fp-panel-button" href="#">'.get_string('ok').'</a>
428 <a class="{!}fp-dlg-butcancel fp-panel-button" href="#">'.get_string('cancel').'</a>
430 return preg_replace('/\{\!\}/', '', $rv);
434 * Returns all FileManager JavaScript templates as an array.
438 public function filemanager_js_templates() {
439 $class_methods = get_class_methods($this);
440 $templates = array();
441 foreach ($class_methods as $method_name) {
442 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
443 $templates[$matches[1]] = $this->$method_name();
449 * Displays restrictions for the file manager
451 * @param form_filemanager $fm
454 private function fm_print_restrictions($fm) {
455 $maxbytes = display_size($fm->options
->maxbytes
);
456 if (empty($options->maxfiles
) ||
$options->maxfiles
== -1) {
457 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
458 //$string['maxfilesize'] = 'Maximum size for new files: {$a}';
460 $strparam = (object)array('size' => $maxbytes, 'attachments' => $options->maxfiles
);
461 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
462 //$string['maxsizeandattachments'] = 'Maximum size for new files: {$a->size}, maximum attachments: {$a->attachments}';
464 // TODO MDL-32020 also should say about 'File types accepted'
465 return '<span>'. $maxsize. '</span>';
469 * Template for FilePicker with general layout (not QuickUpload).
471 * Must have one top element containing everything else (recommended <div class="file-picker">),
472 * CSS for this element must define width and height of the filepicker window. Or CSS must
473 * define min-width, max-width, min-height and max-height and in this case the filepicker
474 * window will be resizeable;
476 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
477 * can be changed or not;
478 * Inside element with class 'fp-viewbar' there are expected elements with classes
479 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
480 * between the view modes, the last clicked element will have the class 'checked';
482 * Element with class 'fp-repo' is a template for displaying one repository. Other repositories
483 * will be attached as siblings (classes first/last/even/odd will be added respectfully).
484 * The currently selected repostory will have class 'active'. Contents of element with class
485 * 'fp-repo-name' will be replaced with repository name, source of image with class
486 * 'fp-repo-icon' will be replaced with repository icon;
488 * Element with class 'fp-content' is obligatory and will hold the current contents;
490 * Element with class 'fp-paging' will contain page navigation (will be deprecated soon);
492 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
493 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
494 * Parent element will receive class 'empty' when there are no folders to be displayed;
495 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
497 * Element with class 'fp-toolbar' will have class 'empty' if all 'Back', 'Search', 'Refresh',
498 * 'Logout', 'Manage' and 'Help' are unavailable for this repo;
500 * Inside fp-toolbar there are expected elements with classes fp-tb-back, fp-tb-search,
501 * fp-tb-refresh, fp-tb-logout, fp-tb-manage and fp-tb-help. Each of them will have
502 * class 'enabled' or 'disabled' if particular repository has this functionality.
503 * Element with class 'fp-tb-search' must contain empty form inside, it's contents will
504 * be substituted with the search form returned by repository (in the most cases it
505 * is generated with template core_repository_renderer::repository_default_searchform);
506 * Other elements must have either <a> or <button> element inside, it will hold onclick
507 * event for corresponding action; labels for fp-tb-back and fp-tb-logout may be
508 * replaced with those specified by repository;
512 private function fp_js_template_generallayout() {
514 <div class="file-picker fp-generallayout">
515 <div class="fp-repo-area">
517 <li class="{!}fp-repo"><a href="#"><img class="{!}fp-repo-icon" width="16" height="16" /> <span class="{!}fp-repo-name"></span></a></li>
520 <div class="fp-repo-items">
521 <div class="fp-navbar">
523 <div class="{!}fp-toolbar">
524 <div class="{!}fp-tb-back"><a href="#">'.get_string('back', 'repository').'</a></div>
525 <div class="{!}fp-tb-search"><form></form></div>
526 <div class="{!}fp-tb-refresh"><a href="#"><img src="'.$this->pix_url('a/refresh').'" /></a></div>
527 <div class="{!}fp-tb-logout"><img src="'.$this->pix_url('a/logout').'" /><a href="#"></a></div>
528 <div class="{!}fp-tb-manage"><a href="#"><img src="'.$this->pix_url('a/setting').'" /> '.get_string('manageurl', 'repository').'</a></div>
529 <div class="{!}fp-tb-help"><a href="#"><img src="'.$this->pix_url('a/help').'" /> '.get_string('help').'</a></div>
531 <div class="{!}fp-viewbar">
532 <a class="{!}fp-vb-icons" href="#"></a>
533 <a class="{!}fp-vb-details" href="#"></a>
534 <a class="{!}fp-vb-tree" href="#"></a>
536 <div class="fp-clear-left"></div>
538 <div class="fp-pathbar">
539 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
542 <div class="{!}fp-content"></div>
545 return preg_replace('/\{\!\}/', '', $rv);
549 * FilePicker JS template for displaying one file in 'icon view' mode.
551 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
552 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
553 * an element will be replaced with an appropriate img;
555 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
556 * (unless min-width is set in css) and the content of an element will be replaced with filename
557 * supplied by repository;
559 * top element(s) will have class fp-folder if the element is a folder;
561 * List of files will have parent <div> element with class 'fp-iconview'
565 private function fp_js_template_iconfilename() {
567 <a class="fp-file" href="#" >
568 <div class="{!}fp-thumbnail"></div>
569 <div class="fp-filename-field">
570 <p class="{!}fp-filename"></p>
573 return preg_replace('/\{\!\}/', '', $rv);
577 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
579 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
581 * content of element with class 'fp-filename' will be replaced with filename supplied by
584 * top element(s) will have class fp-folder if the element is a folder;
586 * Note that tree view and table view are the YUI widgets and therefore there are no
587 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
588 * fp-tableview (respectfully).
592 private function fp_js_template_listfilename() {
594 <span class="fp-filename-icon">
596 <span class="{!}fp-icon"></span>
597 <span class="{!}fp-filename"></span>
600 return preg_replace('/\{\!\}/', '', $rv);
604 * FilePicker JS template for displaying link/loading progress for fetching of the next page
606 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
608 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
609 * class 'loading' during loading of the next page (it is recommended that in this case the link
610 * becomes unavailable). Also must contain one element <a> or <button> that will hold
611 * onclick event for displaying of the next page. The event will be triggered automatically
612 * when user scrolls to this link.
616 private function fp_js_template_nextpage() {
618 <div class="{!}fp-nextpage">
619 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
620 <div class="fp-nextpage-loading">
621 <img src="'.$this->pix_url('i/loading').'" />
622 <p>'.get_string('loading', 'repository').'</p>
625 return preg_replace('/\{\!\}/', '', $rv);
629 * FilePicker JS template for window appearing to select a file.
631 * All content must be enclosed in one element, CSS for this class must define width and
632 * height of the window;
634 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
636 * Inside the window the elements with the following classnames must be present:
637 * 'fp-saveas', 'fp-linktype-2', 'fp-linktype-1', 'fp-linktype-4', 'fp-setauthor',
638 * 'fp-setlicense'. Inside each of them must have one input element (or select in case of
639 * fp-setlicense). They may also have labels.
640 * The elements will be assign with class 'uneditable' and input/select element will become
641 * disabled if they are not applicable for the particular file;
643 * There may be present elements with classes 'fp-datemodified', 'fp-datecreated', 'fp-size',
644 * 'fp-license', 'fp-author', 'fp-dimensions'. They will receive additional class 'fp-unknown'
645 * if information is unavailable. If there is information available, the content of embedded
646 * element with class 'fp-value' will be substituted with the value;
648 * Elements with classes 'fp-select-confirm' and 'fp-select-cancel' will hold corresponding
651 * When confirm button is pressed and file is being selected, the top element receives
652 * additional class 'loading'. It is removed when response from server is received.
656 private function fp_js_template_selectlayout() {
658 <div class="file-picker fp-select">
659 <div class="fp-select-loading">
660 <img src="'.$this->pix_url('i/loading').'" />
661 <p>'.get_string('loading', 'repository').'</p>
665 <a class="{!}fp-select-confirm fp-panel-button" href="#">'.get_string('getfile', 'repository').'</a>
666 <a class="{!}fp-select-cancel fp-panel-button" href="#">'.get_string('cancel').'</a>
668 <div class="fp-hr"></div>
670 <tr class="{!}fp-linktype-2">
672 <td class="mdl-left"><input type="radio"/><label> '.get_string('makefileinternal', 'repository').'</label></td></tr>
673 <tr class="{!}fp-linktype-1">
675 <td class="mdl-left"><input type="radio"/><label> '.get_string('makefilelink', 'repository').'</label></td></tr>
676 <tr class="{!}fp-linktype-4">
678 <td class="mdl-left"><input type="radio"/><label> '.get_string('makefilereference', 'repository').'</label></td></tr>
679 <tr class="{!}fp-saveas">
680 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
681 <td class="mdl-left"><input type="text"/></td></tr>
682 <tr class="{!}fp-setauthor">
683 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
684 <td class="mdl-left"><input type="text" /></td></tr>
685 <tr class="{!}fp-setlicense">
686 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
687 <td class="mdl-left"><select></select></td></tr>
690 <p class="{!}fp-thumbnail"></p>
691 <div class="fp-fileinfo">
692 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"></span></div>
693 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"></span></div>
694 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"></span></div>
695 <div class="{!}fp-license">'.get_string('license', 'moodle').': <span class="fp-value"></span></div>
696 <div class="{!}fp-author">'.get_string('author', 'repository').': <span class="fp-value"></span></div>
697 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"></span></div>
700 return preg_replace('/\{\!\}/', '', $rv);
704 * FilePicker JS template for 'Upload file' repository
706 * Content to display when user chooses 'Upload file' repository (will be nested inside
707 * element with class 'fp-content').
709 * Must contain form (enctype="multipart/form-data" method="POST")
711 * The elements with the following classnames must be present:
712 * 'fp-file', 'fp-saveas', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
713 * one input element (or select in case of fp-setlicense). They may also have labels.
715 * Element with class 'fp-upload-btn' will hold onclick event for uploading the file;
717 * Please note that some fields may be hidden using CSS if this is part of quickupload form
721 private function fp_js_template_uploadform() {
723 <div class="fp-upload-form mdl-align">
724 <div class="fp-content-center">
725 <form enctype="multipart/form-data" method="POST">
727 <tr class="{!}fp-file">
728 <td class="mdl-right"><label>'.get_string('attachment', 'repository').'</label>:</td>
729 <td class="mdl-left"><input type="file"/></td></tr>
730 <tr class="{!}fp-saveas">
731 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
732 <td class="mdl-left"><input type="text"/></td></tr>
733 <tr class="{!}fp-setauthor">
734 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
735 <td class="mdl-left"><input type="text"/></td></tr>
736 <tr class="{!}fp-setlicense">
737 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
738 <td class="mdl-left"><select></select></td></tr>
741 <div><button class="{!}fp-upload-btn">'.get_string('upload', 'repository').'</button></div>
744 return preg_replace('/\{\!\}/', '', $rv);
748 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
752 private function fp_js_template_loading() {
754 <div class="fp-content-loading">
755 <div class="fp-content-center">
756 <img src="'.$this->pix_url('i/loading').'" />
757 <p>'.get_string('loading', 'repository').'</p>
763 * FilePicker JS template for error (inside element with class 'fp-content').
765 * must have element with class 'fp-error', its content will be replaced with error text
766 * and the error code will be assigned as additional class to this element
767 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
771 private function fp_js_template_error() {
773 <div class="fp-content-error" ><div class="{!}fp-error"></div></div>';
774 return preg_replace('/\{\!\}/', '', $rv);
778 * FilePicker JS template for error/info message displayed as a separate popup window.
780 * Must be wrapped in one element, CSS for this element must define
781 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
782 * or 'fp-msg-info' depending on message type;
784 * content of element with class 'fp-msg-text' will be replaced with error/info text;
786 * element with class 'fp-msg-butok' will hold onclick event
790 private function fp_js_template_message() {
792 <div class="file-picker fp-msg">
793 <p class="{!}fp-msg-text"></p>
794 <a class="{!}fp-msg-butok fp-panel-button" href="#">'.get_string('ok').'</a>
796 return preg_replace('/\{\!\}/', '', $rv);
800 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
802 * Must have one top element, CSS for this element must define width and height of the window;
804 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
805 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename' and 'fp-dlg-butcancel' will
806 * hold onclick events;
808 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
809 * (Note that it may have long text)
813 private function fp_js_template_processexistingfile() {
815 <div class="file-picker fp-dlg">
816 <p class="{!}fp-dlg-text"></p>
817 <a class="{!}fp-dlg-butoverwrite fp-panel-button" href="#">'.get_string('overwrite', 'repository').'</a>
818 <a class="{!}fp-dlg-butcancel fp-panel-button" href="#">'.get_string('cancel').'</a>
819 <a class="{!}fp-dlg-butrename fp-panel-button" href="#"></a>
821 return preg_replace('/\{\!\}/', '', $rv);
825 * FilePicker JS template for repository login form including templates for each element type
827 * Must contain one <form> element with templates for different input types inside:
828 * Elements with classes 'fp-login-popup', 'fp-login-textarea', 'fp-login-select' and
829 * 'fp-login-input' are templates for displaying respective login form elements. Inside
830 * there must be exactly one element with type <button>, <textarea>, <select> or <input>
831 * (i.e. fp-login-popup should have <button>, fp-login-textarea should have <textarea>, etc.);
832 * They may also contain the <label> element and it's content will be substituted with
835 * You can also define elements with classes 'fp-login-checkbox', 'fp-login-text'
836 * but if they are not found, 'fp-login-input' will be used;
838 * Element with class 'fp-login-radiogroup' will be used for group of radio inputs. Inside
839 * it should hava a template for one radio input (with class 'fp-login-radio');
841 * Element with class 'fp-login-submit' will hold on click mouse event (form submission). It
842 * will be removed if at least one popup element is present;
846 private function fp_js_template_loginform() {
848 <div class="fp-login-form">
849 <div class="fp-content-center">
852 <tr class="{!}fp-login-popup">
854 <label>'.get_string('popup', 'repository').'</label>
855 <p class="fp-popup"><button class="{!}fp-login-popup-but">'.get_string('login', 'repository').'</button></p></td></tr>
856 <tr class="{!}fp-login-textarea">
857 <td colspan="2"><p><textarea></textarea></p></td></tr>
858 <tr class="{!}fp-login-select">
859 <td align="right"><label></label></td>
860 <td align="left"><select></select></td></tr>
861 <tr class="{!}fp-login-input">
862 <td class="label"><label></label></td>
863 <td class="input"><input/></td></tr>
864 <tr class="{!}fp-login-radiogroup">
865 <td align="right" width="30%" valign="top"><label></label></td>
866 <td align="left" valign="top"><p class="{!}fp-login-radio"><input /> <label></label></p></td></tr>
868 <p><button class="{!}fp-login-submit">'.get_string('submit', 'repository').'</button></p>
872 return preg_replace('/\{\!\}/', '', $rv);
876 * Returns all FilePicker JavaScript templates as an array.
880 public function filepicker_js_templates() {
881 $class_methods = get_class_methods($this);
882 $templates = array();
883 foreach ($class_methods as $method_name) {
884 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
885 $templates[$matches[1]] = $this->$method_name();
891 * Returns HTML for default repository searchform to be passed to Filepicker
893 * This will be used as contents for search form defined in generallayout template
894 * (form with id {TOOLSEARCHID}).
895 * Default contents is one text input field with name="s"
897 public function repository_default_searchform() {
898 $str = '<div class="fp-def-search"><input name="s" value='.get_string('search', 'repository').' /></div>';
904 * Data structure representing a general moodle file tree viewer
906 * @copyright 2010 Dongsheng Cai
907 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
910 class files_tree_viewer
implements renderable
{
916 * Constructor of moodle_file_tree_viewer class
917 * @param file_info $file_info
918 * @param array $options
920 public function __construct(file_info
$file_info, array $options = null) {
923 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
924 $this->options
= (array)$options;
925 $this->context
= $options['context'];
927 $this->tree
= array();
928 $children = $file_info->get_children();
929 $current_file_params = $file_info->get_params();
930 $parent_info = $file_info->get_parent();
931 $level = $parent_info;
932 $this->path
= array();
934 $params = $level->get_params();
935 $context = get_context_instance_by_id($params['contextid']);
936 // $this->context is current context
937 if ($context->id
!= $this->context
->id
or empty($params['filearea'])) {
940 // unset unused parameters
941 unset($params['component']);
942 unset($params['filearea']);
943 unset($params['filename']);
944 unset($params['itemid']);
945 $url = new moodle_url('/files/index.php', $params);
946 $this->path
[] = html_writer
::link($url, $level->get_visible_name());
947 $level = $level->get_parent();
949 $this->path
= array_reverse($this->path
);
950 if ($current_file_params['filepath'] != '/') {
951 $this->path
[] = $file_info->get_visible_name();
954 foreach ($children as $child) {
955 $filedate = $child->get_timemodified();
956 $filesize = $child->get_filesize();
957 $mimetype = $child->get_mimetype();
958 $params = $child->get_params();
959 unset($params['component']);
960 unset($params['filearea']);
961 unset($params['filename']);
962 unset($params['itemid']);
965 'filename' => $child->get_visible_name(),
966 'mimetype' => $child->get_mimetype(),
967 'filedate' => $filedate ?
$filedate : '',
968 'filesize' => $filesize ?
$filesize : ''
970 $url = new moodle_url('/files/index.php', $params);
971 if ($child->is_directory()) {
972 $fileitem['isdir'] = true;
973 $fileitem['url'] = $url->out(false);
975 $fileitem['url'] = $child->get_url();
977 $this->tree
[] = $fileitem;