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) {
50 foreach($tree->path as $path) {
56 $html .= '<div id="course-file-tree-view" class="filemanager-container">';
57 if (empty($tree->tree)) {
58 $html .= get_string('nofilesavailable', 'repository');
60 $this->page->requires->js_init_call('M.core_filetree.init');
62 foreach($tree->tree as $node) {
63 $link_attributes = array();
64 if (!empty($node['isdir'])) {
65 $class = ' class="file-tree-folder"';
67 $class = ' class="file-tree-file"';
68 $link_attributes['target'] = '_blank';
70 $html .= '<li '.$class.'>';
71 $html .= html_writer::link($node['url'], $node['filename'], $link_attributes);
83 * Data structure representing a general moodle file tree viewer
85 * @copyright 2010 Dongsheng Cai
86 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
89 class files_tree_viewer implements renderable {
94 * Constructor of moodle_file_tree_viewer class
95 * @param file_info $file_info
96 * @param array $options
98 public function __construct(file_info $file_info, array $options = null) {
101 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
103 $this->options = (array)$options;
104 if (isset($this->options['visible_areas'])) {
105 $visible_areas = (array)$this->options['visible_areas'];
107 $visible_areas = false;
110 $this->tree = array();
111 $children = $file_info->get_children();
112 $parent_info = $file_info->get_parent();
114 $level = $parent_info;
115 $this->path = array();
117 $params = $level->get_params();
118 $context = get_context_instance_by_id($params['contextid']);
119 // lock user in course level
120 if ($context->contextlevel == CONTEXT_COURSECAT or $context->contextlevel == CONTEXT_SYSTEM) {
123 $url = new moodle_url('/files/index.php', $params);
124 $this->path[] = html_writer::link($url->out(false), $level->get_visible_name());
125 $level = $level->get_parent();
127 $this->path = array_reverse($this->path);
128 $this->path[] = $file_info->get_visible_name();
130 foreach ($children as $child) {
131 $filedate = $child->get_timemodified();
132 $filesize = $child->get_filesize();
133 $mimetype = $child->get_mimetype();
134 $params = $child->get_params();
135 $url = new moodle_url('/files/index.php', $params);
138 'filename' => $child->get_visible_name(),
139 'filedate' => $filedate ? userdate($filedate) : '',
140 'filesize' => $filesize ? display_size($filesize) : ''
142 if ($child->is_directory()) {
143 $fileitem['isdir'] = true;
144 $fileitem['url'] = $url->out(false);
145 if ($visible_areas !== false) {
146 if (!isset($visible_areas[$params['component']][$params['filearea']])) {
151 $fileitem['url'] = $child->get_url();
153 $this->tree[] = $fileitem;