MDL-23308, added help text to course files page
[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
29750492 49 $html = $this->output->heading_with_help(get_string('coursefileswarning'), 'coursefileswarning', '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"';
67 } else {
68 $class = ' class="file-tree-file"';
69 $link_attributes['target'] = '_blank';
70 }
71 $html .= '<li '.$class.'>';
72 $html .= html_writer::link($node['url'], $node['filename'], $link_attributes);
73 $html .= '</li>';
74 }
75 $html .= '</ul>';
76 }
77 $html .= '</div>';
e921afa8 78 $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
64f93798
PS
79 return $html;
80 }
81}
82
83
84/**
85 * Data structure representing a general moodle file tree viewer
86 *
87 * @copyright 2010 Dongsheng Cai
88 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
89 * @since Moodle 2.0
90 */
91class files_tree_viewer implements renderable {
92 public $tree;
93 public $path;
e921afa8 94 public $context;
64f93798
PS
95
96 /**
97 * Constructor of moodle_file_tree_viewer class
98 * @param file_info $file_info
99 * @param array $options
100 */
101 public function __construct(file_info $file_info, array $options = null) {
102 global $CFG;
103
104 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
64f93798 105 $this->options = (array)$options;
e921afa8
DC
106 $this->context = $options['context'];
107
64f93798
PS
108 if (isset($this->options['visible_areas'])) {
109 $visible_areas = (array)$this->options['visible_areas'];
110 } else {
111 $visible_areas = false;
112 }
113
114 $this->tree = array();
115 $children = $file_info->get_children();
116 $parent_info = $file_info->get_parent();
64f93798
PS
117 $level = $parent_info;
118 $this->path = array();
119 while ($level) {
120 $params = $level->get_params();
121 $context = get_context_instance_by_id($params['contextid']);
e921afa8 122 if ($context->id != $this->context->id) {
64f93798
PS
123 break;
124 }
e921afa8
DC
125 // unset unused parameters
126 unset($params['component']);
127 unset($params['filearea']);
128 unset($params['itemid']);
64f93798 129 $url = new moodle_url('/files/index.php', $params);
e921afa8 130 $this->path[] = html_writer::link($url, $level->get_visible_name());
64f93798
PS
131 $level = $level->get_parent();
132 }
133 $this->path = array_reverse($this->path);
134 $this->path[] = $file_info->get_visible_name();
135
136 foreach ($children as $child) {
137 $filedate = $child->get_timemodified();
138 $filesize = $child->get_filesize();
139 $mimetype = $child->get_mimetype();
140 $params = $child->get_params();
64f93798
PS
141 $fileitem = array(
142 'params' => $params,
143 'filename' => $child->get_visible_name(),
144 'filedate' => $filedate ? userdate($filedate) : '',
145 'filesize' => $filesize ? display_size($filesize) : ''
146 );
e921afa8 147 $url = new moodle_url('/files/index.php', $params);
64f93798
PS
148 if ($child->is_directory()) {
149 $fileitem['isdir'] = true;
150 $fileitem['url'] = $url->out(false);
151 if ($visible_areas !== false) {
152 if (!isset($visible_areas[$params['component']][$params['filearea']])) {
153 continue;
154 }
155 }
156 } else {
157 $fileitem['url'] = $child->get_url();
158 }
159 $this->tree[] = $fileitem;
160 }
161 }
162}