MDL-62628 resource: Folder name formatting issue when displayed inline
[moodle.git] / mod / folder / renderer.php
CommitLineData
64f93798
PS
1<?php
2
9fe1fbcc
PS
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Folder module renderer
20 *
11e41f1a
SH
21 * @package mod_folder
22 * @copyright 2009 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9fe1fbcc 24 */
64f93798
PS
25defined('MOODLE_INTERNAL') || die();
26
27class mod_folder_renderer extends plugin_renderer_base {
28
29 /**
38199c24
MG
30 * Returns html to display the content of mod_folder
31 * (Description, folder files and optionally Edit button)
32 *
ec62d8b4
MG
33 * @param stdClass $folder record from 'folder' table (please note
34 * it may not contain fields 'revision' and 'timemodified')
38199c24 35 * @return string
64f93798 36 */
38199c24
MG
37 public function display_folder(stdClass $folder) {
38 $output = '';
39 $folderinstances = get_fast_modinfo($folder->course)->get_instances_of('folder');
40 if (!isset($folderinstances[$folder->id]) ||
41 !($cm = $folderinstances[$folder->id]) ||
b9c3f07a
MG
42 !($context = context_module::instance($cm->id))) {
43 // Some error in parameters.
44 // Don't throw any errors in renderer, just return empty string.
45 // Capability to view module must be checked before calling renderer.
38199c24
MG
46 return $output;
47 }
48
49 if (trim($folder->intro)) {
50 if ($folder->display != FOLDER_DISPLAY_INLINE) {
51 $output .= $this->output->box(format_module_intro('folder', $folder, $cm->id),
52 'generalbox', 'intro');
53 } else if ($cm->showdescription) {
54 // for "display inline" do not filter, filters run at display time.
55 $output .= format_module_intro('folder', $folder, $cm->id, false);
56 }
57 }
58
f6937172
MG
59 $foldertree = new folder_tree($folder, $cm);
60 if ($folder->display == FOLDER_DISPLAY_INLINE) {
61 // Display module name as the name of the root directory.
6f484613 62 $foldertree->dir['dirname'] = $cm->get_formatted_name(array('escape' => false));
f6937172
MG
63 }
64 $output .= $this->output->box($this->render($foldertree),
38199c24
MG
65 'generalbox foldertree');
66
9f1b00a8 67 // Do not append the edit button on the course page.
26ca6df7 68 $downloadable = folder_archive_available($folder, $cm);
279918e1 69
26ca6df7
AH
70 $buttons = '';
71 if ($downloadable) {
72 $downloadbutton = $this->output->single_button(
73 new moodle_url('/mod/folder/download_folder.php', array('id' => $cm->id)),
74 get_string('downloadfolder', 'folder')
75 );
50ed9363 76
26ca6df7
AH
77 $buttons .= $downloadbutton;
78 }
279918e1 79
26ca6df7
AH
80 // Display the "Edit" button if current user can edit folder contents.
81 // Do not display it on the course page for the teachers because there
82 // is an "Edit settings" button right next to it with the same functionality.
83 if (has_capability('mod/folder:managefiles', $context) &&
84 ($folder->display != FOLDER_DISPLAY_INLINE || !has_capability('moodle/course:manageactivities', $context))) {
85 $editbutton = $this->output->single_button(
86 new moodle_url('/mod/folder/edit.php', array('id' => $cm->id)),
87 get_string('edit')
88 );
50ed9363 89
26ca6df7 90 $buttons .= $editbutton;
38199c24 91 }
26ca6df7
AH
92
93 if ($buttons) {
94 $output .= $this->output->box($buttons, 'generalbox folderbuttons');
95 }
96
38199c24 97 return $output;
64f93798
PS
98 }
99
100 public function render_folder_tree(folder_tree $tree) {
38199c24 101 static $treecounter = 0;
64f93798 102
38199c24
MG
103 $content = '';
104 $id = 'folder_tree'. ($treecounter++);
105 $content .= '<div id="'.$id.'" class="filemanager">';
106 $content .= $this->htmllize_tree($tree, array('files' => array(), 'subdirs' => array($tree->dir)));
107 $content .= '</div>';
57e22804
RL
108 $showexpanded = true;
109 if (empty($tree->folder->showexpanded)) {
110 $showexpanded = false;
f2957278 111 }
57e22804 112 $this->page->requires->js_init_call('M.mod_folder.init_tree', array($id, $showexpanded));
20139cbc 113 return $content;
64f93798
PS
114 }
115
116 /**
117 * Internal function - creates htmls structure suitable for YUI tree.
118 */
119 protected function htmllize_tree($tree, $dir) {
120 global $CFG;
121
122 if (empty($dir['subdirs']) and empty($dir['files'])) {
123 return '';
124 }
125 $result = '<ul>';
126 foreach ($dir['subdirs'] as $subdir) {
4b8d51d7 127 $image = $this->output->pix_icon(file_folder_icon(24), $subdir['dirname'], 'moodle');
eb8d220d
MG
128 $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
129 html_writer::tag('span', s($subdir['dirname']), array('class' => 'fp-filename'));
4b8d51d7
MG
130 $filename = html_writer::tag('div', $filename, array('class' => 'fp-filename-icon'));
131 $result .= html_writer::tag('li', $filename. $this->htmllize_tree($tree, $subdir));
64f93798
PS
132 }
133 foreach ($dir['files'] as $file) {
64f93798 134 $filename = $file->get_filename();
eb8d220d
MG
135 $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
136 $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $filename, false);
8852faea 137 $filenamedisplay = clean_filename($filename);
eb8d220d
MG
138 if (file_extension_in_typegroup($filename, 'web_image')) {
139 $image = $url->out(false, array('preview' => 'tinyicon', 'oid' => $file->get_timemodified()));
4b8d51d7
MG
140 $image = html_writer::empty_tag('img', array('src' => $image));
141 } else {
8852faea 142 $image = $this->output->pix_icon(file_file_icon($file, 24), $filenamedisplay, 'moodle');
4b8d51d7 143 }
eb8d220d 144 $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
8852faea 145 html_writer::tag('span', $filenamedisplay, array('class' => 'fp-filename'));
eb8d220d
MG
146 $filename = html_writer::tag('span',
147 html_writer::link($url->out(false, array('forcedownload' => 1)), $filename),
148 array('class' => 'fp-filename-icon'));
4b8d51d7 149 $result .= html_writer::tag('li', $filename);
64f93798
PS
150 }
151 $result .= '</ul>';
152
153 return $result;
154 }
155}
156
157class folder_tree implements renderable {
158 public $context;
159 public $folder;
160 public $cm;
64f93798
PS
161 public $dir;
162
38199c24 163 public function __construct($folder, $cm) {
64f93798
PS
164 $this->folder = $folder;
165 $this->cm = $cm;
64f93798 166
c068b41b 167 $this->context = context_module::instance($cm->id);
64f93798
PS
168 $fs = get_file_storage();
169 $this->dir = $fs->get_area_tree($this->context->id, 'mod_folder', 'content', 0);
170 }
7772fed1 171}