MDL-32941 use + operator instead array merge to avoid duplicate. Sort resulting array...
[moodle.git] / files / renderer.php
blob159716362675095a71a601cf6227b9a192f31baf
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('filename', 'backup'), get_string('size'), get_string('modified'));
60 $table->align = array('left', 'right', 'right');
61 $table->width = '100%';
62 $table->data = array();
64 foreach ($tree->tree as $file) {
65 if (!empty($file['isdir'])) {
66 $table->data[] = array(
67 html_writer::link($file['url'], $this->output->pix_icon('f/folder', 'icon') . ' ' . $file['filename']),
68 '',
69 $file['filedate'],
71 } else {
72 $table->data[] = array(
73 html_writer::link($file['url'], $this->output->pix_icon('f/'.mimeinfo('icon', $file['filename']), get_string('icon')) . ' ' . $file['filename']),
74 $file['filesize'],
75 $file['filedate'],
80 $html .= html_writer::table($table);
81 $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
82 $html .= $this->output->box_end();
83 return $html;
88 /**
89 * Data structure representing a general moodle file tree viewer
91 * @copyright 2010 Dongsheng Cai
92 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
93 * @since Moodle 2.0
95 class files_tree_viewer implements renderable {
96 public $tree;
97 public $path;
98 public $context;
101 * Constructor of moodle_file_tree_viewer class
102 * @param file_info $file_info
103 * @param array $options
105 public function __construct(file_info $file_info, array $options = null) {
106 global $CFG;
108 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
109 $this->options = (array)$options;
110 $this->context = $options['context'];
112 $this->tree = array();
113 $children = $file_info->get_children();
114 $current_file_params = $file_info->get_params();
115 $parent_info = $file_info->get_parent();
116 $level = $parent_info;
117 $this->path = array();
118 while ($level) {
119 $params = $level->get_params();
120 $context = get_context_instance_by_id($params['contextid']);
121 // $this->context is current context
122 if ($context->id != $this->context->id or empty($params['filearea'])) {
123 break;
125 // unset unused parameters
126 unset($params['component']);
127 unset($params['filearea']);
128 unset($params['filename']);
129 unset($params['itemid']);
130 $url = new moodle_url('/files/index.php', $params);
131 $this->path[] = html_writer::link($url, $level->get_visible_name());
132 $level = $level->get_parent();
134 $this->path = array_reverse($this->path);
135 if ($current_file_params['filepath'] != '/') {
136 $this->path[] = $file_info->get_visible_name();
139 foreach ($children as $child) {
140 $filedate = $child->get_timemodified();
141 $filesize = $child->get_filesize();
142 $mimetype = $child->get_mimetype();
143 $params = $child->get_params();
144 unset($params['component']);
145 unset($params['filearea']);
146 unset($params['filename']);
147 unset($params['itemid']);
148 $fileitem = array(
149 'params' => $params,
150 'filename' => $child->get_visible_name(),
151 'filedate' => $filedate ? userdate($filedate) : '',
152 'filesize' => $filesize ? display_size($filesize) : ''
154 $url = new moodle_url('/files/index.php', $params);
155 if ($child->is_directory()) {
156 $fileitem['isdir'] = true;
157 $fileitem['url'] = $url->out(false);
158 } else {
159 $fileitem['url'] = $child->get_url();
161 $this->tree[] = $fileitem;