MDL-23308, added mimetype icons to course files tree
[moodle.git] / files / renderer.php
CommitLineData
64f93798
PS
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///////////////////////////////////////////////////////////////////////////
21
22defined('MOODLE_INTERNAL') || die();
23
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 */
32
33/**
e921afa8 34 * File browser render
64f93798
PS
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 */
40class core_files_renderer extends plugin_renderer_base {
41
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 }
46
47 public function render_files_tree_viewer(files_tree_viewer $tree) {
48
e0873f13 49 $html = $this->output->heading_with_help(get_string('coursefiles'), 'courselegacyfiles', 'moodle');
e921afa8 50 $html .= '<div class="file-tree-breadcrumb">';
64f93798
PS
51 foreach($tree->path as $path) {
52 $html .= $path;
53 $html .= ' / ';
54 }
55 $html .= '</div>';
56
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"';
12fcd26d 67 $icon = $this->output->pix_icon('f/folder', 'icon');
64f93798
PS
68 } else {
69 $class = ' class="file-tree-file"';
12fcd26d 70 $icon = $this->output->pix_icon('f/'.mimeinfo('icon', $node['filename']), get_string('icon'));
64f93798
PS
71 $link_attributes['target'] = '_blank';
72 }
12fcd26d
DC
73 $html .= '<li '.$class.' yuiConfig="{\'type\':\'HTMLNode\'}">';
74 $html .= '<div>';
75 $html .= $icon;
76 $html .= '&nbsp;';
64f93798 77 $html .= html_writer::link($node['url'], $node['filename'], $link_attributes);
12fcd26d 78 $html .= '</div>';
64f93798
PS
79 $html .= '</li>';
80 }
81 $html .= '</ul>';
82 }
83 $html .= '</div>';
e921afa8 84 $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
64f93798
PS
85 return $html;
86 }
87}
88
89
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 */
97class files_tree_viewer implements renderable {
98 public $tree;
99 public $path;
e921afa8 100 public $context;
64f93798
PS
101
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;
109
110 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
64f93798 111 $this->options = (array)$options;
e921afa8
DC
112 $this->context = $options['context'];
113
64f93798
PS
114 if (isset($this->options['visible_areas'])) {
115 $visible_areas = (array)$this->options['visible_areas'];
116 } else {
117 $visible_areas = false;
118 }
119
120 $this->tree = array();
121 $children = $file_info->get_children();
122 $parent_info = $file_info->get_parent();
64f93798
PS
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']);
e921afa8 128 if ($context->id != $this->context->id) {
64f93798
PS
129 break;
130 }
e921afa8
DC
131 // unset unused parameters
132 unset($params['component']);
133 unset($params['filearea']);
134 unset($params['itemid']);
64f93798 135 $url = new moodle_url('/files/index.php', $params);
e921afa8 136 $this->path[] = html_writer::link($url, $level->get_visible_name());
64f93798
PS
137 $level = $level->get_parent();
138 }
139 $this->path = array_reverse($this->path);
140 $this->path[] = $file_info->get_visible_name();
141
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();
64f93798
PS
147 $fileitem = array(
148 'params' => $params,
149 'filename' => $child->get_visible_name(),
150 'filedate' => $filedate ? userdate($filedate) : '',
151 'filesize' => $filesize ? display_size($filesize) : ''
152 );
e921afa8 153 $url = new moodle_url('/files/index.php', $params);
64f93798
PS
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 }
168}