MDL-67058 core_h5p: task to get latest content types version from H5P
[moodle.git] / files / renderer.php
blobf48feb76ffdd1fa580296188b9d9099949424f8d
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 $html = $this->fm_print_generallayout($fm);
106 $module = array(
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'),
110 'strings' => array(
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')
119 if ($this->page->requires->should_create_one_time_item_now('core_file_managertemplate')) {
120 $this->page->requires->js_init_call('M.form_filemanager.set_templates',
121 array($this->filemanager_js_templates()), true, $module);
123 $this->page->requires->js_init_call('M.form_filemanager.init', array($fm->options), true, $module);
125 // non javascript file manager
126 $html .= '<noscript>';
127 $html .= "<div><object type='text/html' data='".$fm->get_nonjsurl()."' height='160' width='600' style='border:1px solid #000'></object></div>";
128 $html .= '</noscript>';
131 return $html;
135 * Returns html for displaying one file manager
137 * @param form_filemanager $fm
138 * @return string
140 protected function fm_print_generallayout($fm) {
141 $context = [
142 'client_id' => $fm->options->client_id,
143 'helpicon' => $this->help_icon('setmainfile', 'repository'),
144 'restrictions' => $this->fm_print_restrictions($fm)
146 return $this->render_from_template('core/filemanager_page_generallayout', $context);
150 * FileManager JS template for displaying one file in 'icon view' mode.
152 * Except for elements described in fp_js_template_iconfilename, this template may also
153 * contain element with class 'fp-contextmenu'. If context menu is available for this
154 * file, the top element will receive the additional class 'fp-hascontextmenu' and
155 * the element with class 'fp-contextmenu' will hold onclick event for displaying
156 * the context menu.
158 * @see fp_js_template_iconfilename()
159 * @return string
161 protected function fm_js_template_iconfilename() {
162 $rv = '
163 <div class="fp-file">
164 <a href="#">
165 <div style="position:relative;">
166 <div class="fp-thumbnail"></div>
167 <div class="fp-reficons1"></div>
168 <div class="fp-reficons2"></div>
169 </div>
170 <div class="fp-filename-field">
171 <div class="fp-filename text-truncate"></div>
172 </div>
173 </a>
174 <a class="fp-contextmenu" href="#">'.$this->pix_icon('i/menu', '▶').'</a>
175 </div>';
176 return $rv;
180 * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
182 * Except for elements described in fp_js_template_listfilename, this template may also
183 * contain element with class 'fp-contextmenu'. If context menu is available for this
184 * file, the top element will receive the additional class 'fp-hascontextmenu' and
185 * the element with class 'fp-contextmenu' will hold onclick event for displaying
186 * the context menu.
188 * @todo MDL-32736 remove onclick="return false;"
189 * @see fp_js_template_listfilename()
190 * @return string
192 protected function fm_js_template_listfilename() {
193 $rv = '
194 <span class="fp-filename-icon">
195 <a href="#">
196 <span class="fp-icon"></span>
197 <span class="fp-reficons1"></span>
198 <span class="fp-reficons2"></span>
199 <span class="fp-filename"></span>
200 </a>
201 <a class="fp-contextmenu" href="#" onclick="return false;">'.$this->pix_icon('i/menu', '▶').'</a>
202 </span>';
203 return $rv;
207 * FileManager JS template for displaying 'Make new folder' dialog.
209 * Must be wrapped in an element, CSS for this element must define width and height of the window;
211 * Must have one input element with type="text" (for users to enter the new folder name);
213 * content of element with class 'fp-dlg-curpath' will be replaced with current path where
214 * new folder is about to be created;
215 * elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
217 * @return string
219 protected function fm_js_template_mkdir() {
220 $rv = '
221 <div class="filemanager fp-mkdir-dlg" role="dialog" aria-live="assertive" aria-labelledby="fp-mkdir-dlg-title">
222 <div class="fp-mkdir-dlg-text">
223 <label id="fp-mkdir-dlg-title">' . get_string('newfoldername', 'repository') . '</label><br/>
224 <input type="text" />
225 </div>
226 <button class="fp-dlg-butcreate btn-primary btn">'.get_string('makeafolder').'</button>
227 <button class="fp-dlg-butcancel btn-cancel btn">'.get_string('cancel').'</button>
228 </div>';
229 return $rv;
233 * FileManager JS template for error/info message displayed as a separate popup window.
235 * @see fp_js_template_message()
236 * @return string
238 protected function fm_js_template_message() {
239 return $this->fp_js_template_message();
243 * FileManager JS template for window with file information/actions.
246 protected function fm_js_template_fileselectlayout() {
247 $context = [
248 'helpicon' => $this->help_icon('setmainfile', 'repository')
250 return $this->render_from_template('core/filemanager_fileselect', $context);
254 * FileManager JS template for popup confirm dialogue window.
256 * @return string
258 protected function fm_js_template_confirmdialog() {
259 return $this->render_from_template('core/filemanager_confirmdialog', []);
263 * Returns all FileManager JavaScript templates as an array.
265 * @return array
267 public function filemanager_js_templates() {
268 $class_methods = get_class_methods($this);
269 $templates = array();
270 foreach ($class_methods as $method_name) {
271 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
272 $templates[$matches[1]] = $this->$method_name();
274 return $templates;
278 * Displays restrictions for the file manager
280 * @param form_filemanager $fm
281 * @return string
283 protected function fm_print_restrictions($fm) {
284 $maxbytes = display_size($fm->options->maxbytes);
285 $strparam = (object) array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles,
286 'areasize' => display_size($fm->options->areamaxbytes));
287 $hasmaxfiles = !empty($fm->options->maxfiles) && $fm->options->maxfiles > 0;
288 $hasarealimit = !empty($fm->options->areamaxbytes) && $fm->options->areamaxbytes != -1;
289 if ($hasmaxfiles && $hasarealimit) {
290 $maxsize = get_string('maxsizeandattachmentsandareasize', 'moodle', $strparam);
291 } else if ($hasmaxfiles) {
292 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
293 } else if ($hasarealimit) {
294 $maxsize = get_string('maxsizeandareasize', 'moodle', $strparam);
295 } else {
296 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
298 // TODO MDL-32020 also should say about 'File types accepted'
299 return '<span>'. $maxsize . '</span>';
303 * Template for FilePicker with general layout (not QuickUpload).
306 * @return string
308 protected function fp_js_template_generallayout() {
309 return $this->render_from_template('core/filemanager_modal_generallayout', []);
313 * FilePicker JS template for displaying one file in 'icon view' mode.
315 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
316 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
317 * an element will be replaced with an appropriate img;
319 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
320 * (unless min-width is set in css) and the content of an element will be replaced with filename
321 * supplied by repository;
323 * top element(s) will have class fp-folder if the element is a folder;
325 * List of files will have parent <div> element with class 'fp-iconview'
327 * @return string
329 protected function fp_js_template_iconfilename() {
330 $rv = '
331 <a class="fp-file" href="#" >
332 <div style="position:relative;">
333 <div class="fp-thumbnail"></div>
334 <div class="fp-reficons1"></div>
335 <div class="fp-reficons2"></div>
336 </div>
337 <div class="fp-filename-field">
338 <p class="fp-filename text-truncate"></p>
339 </div>
340 </a>';
341 return $rv;
345 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
347 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
349 * content of element with class 'fp-filename' will be replaced with filename supplied by
350 * repository;
352 * top element(s) will have class fp-folder if the element is a folder;
354 * Note that tree view and table view are the YUI widgets and therefore there are no
355 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
356 * fp-tableview (respectfully).
358 * @return string
360 protected function fp_js_template_listfilename() {
361 $rv = '
362 <span class="fp-filename-icon">
363 <a href="#">
364 <span class="fp-icon"></span>
365 <span class="fp-filename"></span>
366 </a>
367 </span>';
368 return $rv;
372 * FilePicker JS template for displaying link/loading progress for fetching of the next page
374 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
376 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
377 * class 'loading' during loading of the next page (it is recommended that in this case the link
378 * becomes unavailable). Also must contain one element <a> or <button> that will hold
379 * onclick event for displaying of the next page. The event will be triggered automatically
380 * when user scrolls to this link.
382 * @return string
384 protected function fp_js_template_nextpage() {
385 $rv = '
386 <div class="fp-nextpage">
387 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
388 <div class="fp-nextpage-loading">
389 ' . $this->pix_icon('i/loading_small', '') . '
390 </div>
391 </div>';
392 return $rv;
396 * FilePicker JS template for window appearing to select a file.
398 * @return string
400 protected function fp_js_template_selectlayout() {
401 return $this->render_from_template('core/filemanager_selectlayout', []);
405 * FilePicker JS template for 'Upload file' repository
407 * @return string
409 protected function fp_js_template_uploadform() {
410 return $this->render_from_template('core/filemanager_uploadform', []);
414 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
416 * @return string
418 protected function fp_js_template_loading() {
419 return '
420 <div class="fp-content-loading">
421 <div class="fp-content-center">
422 ' . $this->pix_icon('i/loading_small', '') . '
423 </div>
424 </div>';
428 * FilePicker JS template for error (inside element with class 'fp-content').
430 * must have element with class 'fp-error', its content will be replaced with error text
431 * and the error code will be assigned as additional class to this element
432 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
434 * @return string
436 protected function fp_js_template_error() {
437 $rv = '
438 <div class="fp-content-error" ><div class="fp-error"></div></div>';
439 return $rv;
443 * FilePicker JS template for error/info message displayed as a separate popup window.
445 * Must be wrapped in one element, CSS for this element must define
446 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
447 * or 'fp-msg-info' depending on message type;
449 * content of element with class 'fp-msg-text' will be replaced with error/info text;
451 * element with class 'fp-msg-butok' will hold onclick event
453 * @return string
455 protected function fp_js_template_message() {
456 $rv = '
457 <div class="file-picker fp-msg" role="alertdialog" aria-live="assertive" aria-labelledby="fp-msg-labelledby">
458 <p class="fp-msg-text" id="fp-msg-labelledby"></p>
459 <button class="fp-msg-butok btn-primary btn">'.get_string('ok').'</button>
460 </div>';
461 return $rv;
465 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
467 * @return string
469 protected function fp_js_template_processexistingfile() {
470 return $this->render_from_template('core/filemanager_processexistingfile', []);
474 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists
475 * (multiple-file version).
477 * @return string
479 protected function fp_js_template_processexistingfilemultiple() {
480 return $this->render_from_template('core/filemanager_processexistingfilemultiple', []);
484 * FilePicker JS template for repository login form including templates for each element type
486 * @return string
488 protected function fp_js_template_loginform() {
489 return $this->render_from_template('core/filemanager_loginform', []);
493 * Returns all FilePicker JavaScript templates as an array.
495 * @return array
497 public function filepicker_js_templates() {
498 $class_methods = get_class_methods($this);
499 $templates = array();
500 foreach ($class_methods as $method_name) {
501 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
502 $templates[$matches[1]] = $this->$method_name();
504 return $templates;
508 * Returns HTML for default repository searchform to be passed to Filepicker
510 * This will be used as contents for search form defined in generallayout template
511 * (form with id {TOOLSEARCHID}).
512 * Default contents is one text input field with name="s"
514 public function repository_default_searchform() {
515 return $this->render_from_template('core/filemanager_default_searchform', []);
520 * Data structure representing a general moodle file tree viewer
522 * @copyright 2010 Dongsheng Cai
523 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
524 * @since Moodle 2.0
526 class files_tree_viewer implements renderable {
527 public $tree;
528 public $path;
529 public $context;
532 * Constructor of moodle_file_tree_viewer class
533 * @param file_info $file_info
534 * @param array $options
536 public function __construct(file_info $file_info, array $options = null) {
537 global $CFG;
539 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
540 $this->options = (array)$options;
541 $this->context = $options['context'];
543 $this->tree = array();
544 $children = $file_info->get_children();
545 $current_file_params = $file_info->get_params();
546 $parent_info = $file_info->get_parent();
547 $level = $parent_info;
548 $this->path = array();
549 while ($level) {
550 $params = $level->get_params();
551 $context = context::instance_by_id($params['contextid']);
552 // $this->context is current context
553 if ($context->id != $this->context->id or empty($params['filearea'])) {
554 break;
556 // unset unused parameters
557 unset($params['component']);
558 unset($params['filearea']);
559 unset($params['filename']);
560 unset($params['itemid']);
561 $url = new moodle_url('/files/index.php', $params);
562 $this->path[] = html_writer::link($url, $level->get_visible_name());
563 $level = $level->get_parent();
565 $this->path = array_reverse($this->path);
566 if ($current_file_params['filepath'] != '/') {
567 $this->path[] = $file_info->get_visible_name();
570 foreach ($children as $child) {
571 $filedate = $child->get_timemodified();
572 $filesize = $child->get_filesize();
573 $mimetype = $child->get_mimetype();
574 $params = $child->get_params();
575 unset($params['component']);
576 unset($params['filearea']);
577 unset($params['filename']);
578 unset($params['itemid']);
579 $fileitem = array(
580 'params' => $params,
581 'filename' => $child->get_visible_name(),
582 'mimetype' => $child->get_mimetype(),
583 'filedate' => $filedate ? $filedate : '',
584 'filesize' => $filesize ? $filesize : ''
586 $url = new moodle_url('/files/index.php', $params);
587 if ($child->is_directory()) {
588 $fileitem['isdir'] = true;
589 $fileitem['url'] = $url->out(false);
590 } else {
591 $fileitem['url'] = $child->get_url();
593 $this->tree[] = $fileitem;