141b3bc432ce1c3b4ede17606d4945b447186840
[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 manager 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 = '<div>';
50         foreach($tree->path as $path) {
51             $html .= $path;
52             $html .= ' / ';
53         }
54         $html .= '</div>';
56         $html .= '<div id="course-file-tree-view" class="filemanager-container">';
57         if (empty($tree->tree)) {
58             $html .= get_string('nofilesavailable', 'repository');
59         } else {
60             $this->page->requires->js_init_call('M.core_filetree.init');
61             $html .= '<ul>';
62             foreach($tree->tree as $node) {
63                 $link_attributes = array();
64                 if (!empty($node['isdir'])) {
65                     $class = ' class="file-tree-folder"';
66                 } else {
67                     $class = ' class="file-tree-file"';
68                     $link_attributes['target'] = '_blank';
69                 }
70                 $html .= '<li '.$class.'>';
71                 $html .= html_writer::link($node['url'], $node['filename'], $link_attributes);
72                 $html .= '</li>';
73             }
74             $html .= '</ul>';
75         }
76         $html .= '</div>';
77         return $html;
78     }
79 }
82 /**
83  * Data structure representing a general moodle file tree viewer
84  *
85  * @copyright 2010 Dongsheng Cai
86  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
87  * @since     Moodle 2.0
88  */
89 class files_tree_viewer implements renderable {
90     public $tree;
91     public $path;
93     /**
94      * Constructor of moodle_file_tree_viewer class
95      * @param file_info $file_info
96      * @param array $options
97      */
98     public function __construct(file_info $file_info, array $options = null) {
99         global $CFG;
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'];
106         } else {
107             $visible_areas = false;
108         }
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();
116         while ($level) {
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) {
121                 break;
122             }
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();
126         }
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);
136             $fileitem = array(
137                     'params'   => $params,
138                     'filename' => $child->get_visible_name(),
139                     'filedate' => $filedate ? userdate($filedate) : '',
140                     'filesize' => $filesize ? display_size($filesize) : ''
141                     );
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']])) {
147                         continue;
148                     }
149                 }
150             } else {
151                 $fileitem['url'] = $child->get_url();
152             }
153             $this->tree[] = $fileitem;
154         }
155     }