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 $html = $this->fm_print_generallayout($fm);
107 'name'=>'form_filemanager',
108 'fullpath'=>'/lib/form/filemanager.js',
109 'requires' => array('moodle-core-notification-dialogue', 'core_filepicker', 'base', 'io-base', 'node', 'json', 'core_dndupload', 'panel', 'resize-plugin', 'dd-plugin'),
111 array('error', 'moodle'), array('info', 'moodle'), array('confirmdeletefile', 'repository'),
112 array('draftareanofiles', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
113 array('invalidjson', 'repository'), array('popupblockeddownload', 'repository'),
114 array('unknownoriginal', 'repository'), array('confirmdeletefolder', 'repository'),
115 array('confirmdeletefilewithhref', 'repository'), array('confirmrenamefolder', 'repository'),
116 array('confirmrenamefile', 'repository'), array('newfolder', 'repository'), array('edit', 'moodle'),
117 array('originalextensionchange', 'repository'), array('originalextensionremove', 'repository'),
118 array('aliaseschange', 'repository'), ['nofilesselected', 'repository'],
119 ['confirmdeleteselectedfile', 'repository'], ['selectall', 'moodle'], ['deselectall', 'moodle'],
120 ['selectallornone', 'form'],
123 if ($this->page
->requires
->should_create_one_time_item_now('core_file_managertemplate')) {
124 $this->page
->requires
->js_init_call('M.form_filemanager.set_templates',
125 array($this->filemanager_js_templates()), true, $module);
127 $this->page
->requires
->js_call_amd('core/checkbox-toggleall', 'init');
128 $this->page
->requires
->js_init_call('M.form_filemanager.init', array($fm->options
), true, $module);
130 // non javascript file manager
131 $html .= '<noscript>';
132 $html .= "<div><object type='text/html' data='".$fm->get_nonjsurl()."' height='160' width='600' style='border:1px solid #000'></object></div>";
133 $html .= '</noscript>';
140 * Returns html for displaying one file manager
142 * @param form_filemanager $fm
145 protected function fm_print_generallayout($fm) {
147 'client_id' => $fm->options
->client_id
,
148 'helpicon' => $this->help_icon('setmainfile', 'repository'),
149 'restrictions' => $this->fm_print_restrictions($fm)
151 return $this->render_from_template('core/filemanager_page_generallayout', $context);
155 * FileManager JS template for displaying one file in 'icon view' mode.
157 * Except for elements described in fp_js_template_iconfilename, this template may also
158 * contain element with class 'fp-contextmenu'. If context menu is available for this
159 * file, the top element will receive the additional class 'fp-hascontextmenu' and
160 * the element with class 'fp-contextmenu' will hold onclick event for displaying
163 * @see fp_js_template_iconfilename()
166 protected function fm_js_template_iconfilename() {
168 <div class="fp-file">
169 <a href="#" class="d-block aabtn">
170 <div style="position:relative;">
171 <div class="fp-thumbnail"></div>
172 <div class="fp-reficons1"></div>
173 <div class="fp-reficons2"></div>
175 <div class="fp-filename-field">
176 <div class="fp-filename text-truncate"></div>
179 <a class="fp-contextmenu btn btn-icon btn-light border icon-no-margin icon-size-3" href="#">
180 <span>'.$this->pix_icon('i/menu', '▶').'</span></a>
186 * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
188 * Except for elements described in fp_js_template_listfilename, this template may also
189 * contain element with class 'fp-contextmenu'. If context menu is available for this
190 * file, the top element will receive the additional class 'fp-hascontextmenu' and
191 * the element with class 'fp-contextmenu' will hold onclick event for displaying
194 * @todo MDL-32736 remove onclick="return false;"
195 * @see fp_js_template_listfilename()
198 protected function fm_js_template_listfilename() {
200 <span class="fp-filename-icon">
202 <span class="fp-icon"></span>
203 <span class="fp-reficons1"></span>
204 <span class="fp-reficons2"></span>
205 <span class="fp-filename"></span>
207 <a class="fp-contextmenu" href="#" onclick="return false;">'.$this->pix_icon('i/menu', '▶').'</a>
213 * FileManager JS template for displaying 'Make new folder' dialog.
215 * Must be wrapped in an element, CSS for this element must define width and height of the window;
217 * Must have one input element with type="text" (for users to enter the new folder name);
219 * content of element with class 'fp-dlg-curpath' will be replaced with current path where
220 * new folder is about to be created;
221 * elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
225 protected function fm_js_template_mkdir() {
227 <div class="filemanager fp-mkdir-dlg" role="dialog" aria-live="assertive" aria-labelledby="fp-mkdir-dlg-title">
228 <div class="fp-mkdir-dlg-text">
229 <label id="fp-mkdir-dlg-title">' . get_string('newfoldername', 'repository') . '</label><br/>
230 <input type="text" class="form-control"/>
232 <button class="fp-dlg-butcreate btn-primary btn">'.get_string('makeafolder').'</button>
233 <button class="fp-dlg-butcancel btn-cancel btn">'.get_string('cancel').'</button>
239 * FileManager JS template for error/info message displayed as a separate popup window.
241 * @see fp_js_template_message()
244 protected function fm_js_template_message() {
245 return $this->fp_js_template_message();
249 * FileManager JS template for window with file information/actions.
252 protected function fm_js_template_fileselectlayout() {
254 'helpicon' => $this->help_icon('setmainfile', 'repository'),
255 'licensehelpicon' => $this->create_license_help_icon_context(),
258 return $this->render_from_template('core/filemanager_fileselect', $context);
262 * FileManager JS template for popup confirm dialogue window.
266 protected function fm_js_template_confirmdialog() {
267 return $this->render_from_template('core/filemanager_confirmdialog', []);
271 * Returns all FileManager JavaScript templates as an array.
275 public function filemanager_js_templates() {
276 $class_methods = get_class_methods($this);
277 $templates = array();
278 foreach ($class_methods as $method_name) {
279 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
280 $templates[$matches[1]] = $this->$method_name();
286 * Displays restrictions for the file manager
288 * @param form_filemanager $fm
291 protected function fm_print_restrictions($fm) {
292 $maxbytes = display_size($fm->options
->maxbytes
);
293 $strparam = (object) array('size' => $maxbytes, 'attachments' => $fm->options
->maxfiles
,
294 'areasize' => display_size($fm->options
->areamaxbytes
));
295 $hasmaxfiles = !empty($fm->options
->maxfiles
) && $fm->options
->maxfiles
> 0;
296 $hasarealimit = !empty($fm->options
->areamaxbytes
) && $fm->options
->areamaxbytes
!= -1;
297 if ($hasmaxfiles && $hasarealimit) {
298 $maxsize = get_string('maxsizeandattachmentsandareasize', 'moodle', $strparam);
299 } else if ($hasmaxfiles) {
300 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
301 } else if ($hasarealimit) {
302 $maxsize = get_string('maxsizeandareasize', 'moodle', $strparam);
304 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
306 // TODO MDL-32020 also should say about 'File types accepted'
307 return '<span>'. $maxsize . '</span>';
311 * Template for FilePicker with general layout (not QuickUpload).
316 protected function fp_js_template_generallayout() {
317 return $this->render_from_template('core/filemanager_modal_generallayout', []);
321 * FilePicker JS template for displaying one file in 'icon view' mode.
323 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
324 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
325 * an element will be replaced with an appropriate img;
327 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
328 * (unless min-width is set in css) and the content of an element will be replaced with filename
329 * supplied by repository;
331 * top element(s) will have class fp-folder if the element is a folder;
333 * List of files will have parent <div> element with class 'fp-iconview'
337 protected function fp_js_template_iconfilename() {
339 <a class="fp-file" href="#" >
340 <div style="position:relative;">
341 <div class="fp-thumbnail"></div>
342 <div class="fp-reficons1"></div>
343 <div class="fp-reficons2"></div>
345 <div class="fp-filename-field">
346 <p class="fp-filename text-truncate"></p>
353 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
355 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
357 * content of element with class 'fp-filename' will be replaced with filename supplied by
360 * top element(s) will have class fp-folder if the element is a folder;
362 * Note that tree view and table view are the YUI widgets and therefore there are no
363 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
364 * fp-tableview (respectfully).
368 protected function fp_js_template_listfilename() {
370 <span class="fp-filename-icon">
372 <span class="fp-icon"></span>
373 <span class="fp-filename"></span>
380 * FilePicker JS template for displaying link/loading progress for fetching of the next page
382 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
384 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
385 * class 'loading' during loading of the next page (it is recommended that in this case the link
386 * becomes unavailable). Also must contain one element <a> or <button> that will hold
387 * onclick event for displaying of the next page. The event will be triggered automatically
388 * when user scrolls to this link.
392 protected function fp_js_template_nextpage() {
394 <div class="fp-nextpage">
395 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
396 <div class="fp-nextpage-loading">
397 ' . $this->pix_icon('i/loading_small', '') . '
404 * FilePicker JS template for window appearing to select a file.
408 protected function fp_js_template_selectlayout() {
410 'licensehelpicon' => $this->create_license_help_icon_context()
412 return $this->render_from_template('core/filemanager_selectlayout', $context);
416 * FilePicker JS template for 'Upload file' repository
420 protected function fp_js_template_uploadform() {
422 'licensehelpicon' => $this->create_license_help_icon_context()
424 return $this->render_from_template('core/filemanager_uploadform', $context);
428 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
432 protected function fp_js_template_loading() {
434 <div class="fp-content-loading">
435 <div class="fp-content-center">
436 ' . $this->pix_icon('i/loading_small', '') . '
442 * FilePicker JS template for error (inside element with class 'fp-content').
444 * must have element with class 'fp-error', its content will be replaced with error text
445 * and the error code will be assigned as additional class to this element
446 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
450 protected function fp_js_template_error() {
452 <div class="fp-content-error" ><div class="fp-error"></div></div>';
457 * FilePicker JS template for error/info message displayed as a separate popup window.
459 * Must be wrapped in one element, CSS for this element must define
460 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
461 * or 'fp-msg-info' depending on message type;
463 * content of element with class 'fp-msg-text' will be replaced with error/info text;
465 * element with class 'fp-msg-butok' will hold onclick event
469 protected function fp_js_template_message() {
471 <div class="file-picker fp-msg" role="alertdialog" aria-live="assertive" aria-labelledby="fp-msg-labelledby">
472 <p class="fp-msg-text" id="fp-msg-labelledby"></p>
473 <button class="fp-msg-butok btn-primary btn">'.get_string('ok').'</button>
479 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
483 protected function fp_js_template_processexistingfile() {
484 return $this->render_from_template('core/filemanager_processexistingfile', []);
488 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists
489 * (multiple-file version).
493 protected function fp_js_template_processexistingfilemultiple() {
494 return $this->render_from_template('core/filemanager_processexistingfilemultiple', []);
498 * FilePicker JS template for repository login form including templates for each element type
502 protected function fp_js_template_loginform() {
503 return $this->render_from_template('core/filemanager_loginform', []);
507 * Returns all FilePicker JavaScript templates as an array.
511 public function filepicker_js_templates() {
512 $class_methods = get_class_methods($this);
513 $templates = array();
514 foreach ($class_methods as $method_name) {
515 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
516 $templates[$matches[1]] = $this->$method_name();
522 * Returns HTML for default repository searchform to be passed to Filepicker
524 * This will be used as contents for search form defined in generallayout template
525 * (form with id {TOOLSEARCHID}).
526 * Default contents is one text input field with name="s"
528 public function repository_default_searchform() {
529 return $this->render_from_template('core/filemanager_default_searchform', []);
533 * Create the context for rendering help icon with license links displaying all licenses and sources.
535 * @return \stdClass $iconcontext the context for rendering license help info.
537 protected function create_license_help_icon_context() : stdClass
{
538 $licensecontext = new stdClass();
541 // Discard licenses without a name or source from enabled licenses.
542 foreach (license_manager
::get_active_licenses() as $license) {
543 if (!empty($license->fullname
) && !empty($license->source
)) {
544 $licenses[] = $license;
548 $licensecontext->licenses
= $licenses;
549 $helptext = $this->render_from_template('core/filemanager_licenselinks', $licensecontext);
551 $iconcontext = new stdClass();
552 $iconcontext->text
= $helptext;
553 $iconcontext->alt
= get_string('helpprefix2', 'moodle', get_string('chooselicense', 'repository'));
560 * Data structure representing a general moodle file tree viewer
562 * @copyright 2010 Dongsheng Cai
563 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
566 class files_tree_viewer
implements renderable
{
572 * Constructor of moodle_file_tree_viewer class
573 * @param file_info $file_info
574 * @param array $options
576 public function __construct(file_info
$file_info, array $options = null) {
579 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
580 $this->options
= (array)$options;
581 $this->context
= $options['context'];
583 $this->tree
= array();
584 $children = $file_info->get_children();
585 $current_file_params = $file_info->get_params();
586 $parent_info = $file_info->get_parent();
587 $level = $parent_info;
588 $this->path
= array();
590 $params = $level->get_params();
591 $context = context
::instance_by_id($params['contextid']);
592 // $this->context is current context
593 if ($context->id
!= $this->context
->id
or empty($params['filearea'])) {
596 // unset unused parameters
597 unset($params['component']);
598 unset($params['filearea']);
599 unset($params['filename']);
600 unset($params['itemid']);
601 $url = new moodle_url('/files/index.php', $params);
602 $this->path
[] = html_writer
::link($url, $level->get_visible_name());
603 $level = $level->get_parent();
605 $this->path
= array_reverse($this->path
);
606 if ($current_file_params['filepath'] != '/') {
607 $this->path
[] = $file_info->get_visible_name();
610 foreach ($children as $child) {
611 $filedate = $child->get_timemodified();
612 $filesize = $child->get_filesize();
613 $mimetype = $child->get_mimetype();
614 $params = $child->get_params();
615 unset($params['component']);
616 unset($params['filearea']);
617 unset($params['filename']);
618 unset($params['itemid']);
621 'filename' => $child->get_visible_name(),
622 'mimetype' => $child->get_mimetype(),
623 'filedate' => $filedate ?
$filedate : '',
624 'filesize' => $filesize ?
$filesize : ''
626 $url = new moodle_url('/files/index.php', $params);
627 if ($child->is_directory()) {
628 $fileitem['isdir'] = true;
629 $fileitem['url'] = $url->out(false);
631 $fileitem['url'] = $child->get_url();
633 $this->tree
[] = $fileitem;