MDL-23308, added mimetype icons to course files tree
[moodle.git] / files / renderer.php
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
31  */
33 /**
34  * File browser render
35  *
36  * @copyright 2010 Dongsheng Cai
37  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  * @since     Moodle 2.0
39  */
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);
45     }
47     public function render_files_tree_viewer(files_tree_viewer $tree) {
49         $html = $this->output->heading_with_help(get_string('coursefiles'), 'courselegacyfiles', 'moodle');
50         $html .= '<div class="file-tree-breadcrumb">';
51         foreach($tree->path as $path) {
52             $html .= $path;
53             $html .= ' / ';
54         }
55         $html .= '</div>';
57         $html .= '<div id="course-file-tree-view" class="filemanager-container">';
58         if (empty($tree->tree)) {
59             $html .= get_string('nofilesavailable', 'repository');
60         } else {
61             $this->page->requires->js_init_call('M.core_filetree.init');
62             $html .= '<ul>';
63             foreach($tree->tree as $node) {
64                 $link_attributes = array();
65                 if (!empty($node['isdir'])) {
66                     $class = ' class="file-tree-folder"';
67                     $icon = $this->output->pix_icon('f/folder', 'icon');
68                 } else {
69                     $class = ' class="file-tree-file"';
70                     $icon = $this->output->pix_icon('f/'.mimeinfo('icon', $node['filename']), get_string('icon'));
71                     $link_attributes['target'] = '_blank';
72                 }
73                 $html .= '<li '.$class.' yuiConfig="{\'type\':\'HTMLNode\'}">';
74                 $html .= '<div>';
75                 $html .= $icon;
76                 $html .= '&nbsp;';
77                 $html .= html_writer::link($node['url'], $node['filename'], $link_attributes);
78                 $html .= '</div>';
79                 $html .= '</li>';
80             }
81             $html .= '</ul>';
82         }
83         $html .= '</div>';
84         $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
85         return $html;
86     }
87 }
90 /**
91  * Data structure representing a general moodle file tree viewer
92  *
93  * @copyright 2010 Dongsheng Cai
94  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
95  * @since     Moodle 2.0
96  */
97 class files_tree_viewer implements renderable {
98     public $tree;
99     public $path;
100     public $context;
102     /**
103      * Constructor of moodle_file_tree_viewer class
104      * @param file_info $file_info
105      * @param array $options
106      */
107     public function __construct(file_info $file_info, array $options = null) {
108         global $CFG;
110         //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
111         $this->options = (array)$options;
112         $this->context = $options['context'];
114         if (isset($this->options['visible_areas'])) {
115             $visible_areas = (array)$this->options['visible_areas'];
116         } else {
117             $visible_areas = false;
118         }
120         $this->tree = array();
121         $children = $file_info->get_children();
122         $parent_info = $file_info->get_parent();
123         $level = $parent_info;
124         $this->path = array();
125         while ($level) {
126             $params = $level->get_params();
127             $context = get_context_instance_by_id($params['contextid']);
128             if ($context->id != $this->context->id) {
129                 break;
130             }
131             // unset unused parameters
132             unset($params['component']);
133             unset($params['filearea']);
134             unset($params['itemid']);
135             $url = new moodle_url('/files/index.php', $params);
136             $this->path[] = html_writer::link($url, $level->get_visible_name());
137             $level = $level->get_parent();
138         }
139         $this->path = array_reverse($this->path);
140         $this->path[] = $file_info->get_visible_name();
142         foreach ($children as $child) {
143             $filedate = $child->get_timemodified();
144             $filesize = $child->get_filesize();
145             $mimetype = $child->get_mimetype();
146             $params = $child->get_params();
147             $fileitem = array(
148                     'params'   => $params,
149                     'filename' => $child->get_visible_name(),
150                     'filedate' => $filedate ? userdate($filedate) : '',
151                     'filesize' => $filesize ? display_size($filesize) : ''
152                     );
153             $url = new moodle_url('/files/index.php', $params);
154             if ($child->is_directory()) {
155                 $fileitem['isdir'] = true;
156                 $fileitem['url'] = $url->out(false);
157                 if ($visible_areas !== false) {
158                     if (!isset($visible_areas[$params['component']][$params['filearea']])) {
159                         continue;
160                     }
161                 }
162             } else {
163                 $fileitem['url'] = $child->get_url();
164             }
165             $this->tree[] = $fileitem;
166         }
167     }