Merge branch 'MDL-48753_m27' of git://github.com/markn86/moodle into MOODLE_27_STABLE
[moodle.git] / mod / folder / renderer.php
blobcf31a3d7d41e4705ea1668dfaec6b1803f821489
1 <?php
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.
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/>.
18 /**
19 * Folder module renderer
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
25 defined('MOODLE_INTERNAL') || die();
27 class mod_folder_renderer extends plugin_renderer_base {
29 /**
30 * Returns html to display the content of mod_folder
31 * (Description, folder files and optionally Edit button)
33 * @param stdClass $folder record from 'folder' table (please note
34 * it may not contain fields 'revision' and 'timemodified')
35 * @return string
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]) ||
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.
46 return $output;
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);
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.
62 $foldertree->dir['dirname'] = $cm->get_formatted_name();
64 $output .= $this->output->box($this->render($foldertree),
65 'generalbox foldertree');
67 // Do not append the edit button on the course page.
68 if ($folder->display != FOLDER_DISPLAY_INLINE && has_capability('mod/folder:managefiles', $context)) {
69 $output .= $this->output->container(
70 $this->output->single_button(new moodle_url('/mod/folder/edit.php',
71 array('id' => $cm->id)), get_string('edit')),
72 'mdl-align folder-edit-button');
74 return $output;
77 public function render_folder_tree(folder_tree $tree) {
78 static $treecounter = 0;
80 $content = '';
81 $id = 'folder_tree'. ($treecounter++);
82 $content .= '<div id="'.$id.'" class="filemanager">';
83 $content .= $this->htmllize_tree($tree, array('files' => array(), 'subdirs' => array($tree->dir)));
84 $content .= '</div>';
85 $showexpanded = true;
86 if (empty($tree->folder->showexpanded)) {
87 $showexpanded = false;
89 $this->page->requires->js_init_call('M.mod_folder.init_tree', array($id, $showexpanded));
90 return $content;
93 /**
94 * Internal function - creates htmls structure suitable for YUI tree.
96 protected function htmllize_tree($tree, $dir) {
97 global $CFG;
99 if (empty($dir['subdirs']) and empty($dir['files'])) {
100 return '';
102 $result = '<ul>';
103 foreach ($dir['subdirs'] as $subdir) {
104 $image = $this->output->pix_icon(file_folder_icon(24), $subdir['dirname'], 'moodle');
105 $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
106 html_writer::tag('span', s($subdir['dirname']), array('class' => 'fp-filename'));
107 $filename = html_writer::tag('div', $filename, array('class' => 'fp-filename-icon'));
108 $result .= html_writer::tag('li', $filename. $this->htmllize_tree($tree, $subdir));
110 foreach ($dir['files'] as $file) {
111 $filename = $file->get_filename();
112 $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
113 $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $filename, false);
114 if (file_extension_in_typegroup($filename, 'web_image')) {
115 $image = $url->out(false, array('preview' => 'tinyicon', 'oid' => $file->get_timemodified()));
116 $image = html_writer::empty_tag('img', array('src' => $image));
117 } else {
118 $image = $this->output->pix_icon(file_file_icon($file, 24), $filename, 'moodle');
120 $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
121 html_writer::tag('span', $filename, array('class' => 'fp-filename'));
122 $filename = html_writer::tag('span',
123 html_writer::link($url->out(false, array('forcedownload' => 1)), $filename),
124 array('class' => 'fp-filename-icon'));
125 $result .= html_writer::tag('li', $filename);
127 $result .= '</ul>';
129 return $result;
133 class folder_tree implements renderable {
134 public $context;
135 public $folder;
136 public $cm;
137 public $dir;
139 public function __construct($folder, $cm) {
140 $this->folder = $folder;
141 $this->cm = $cm;
143 $this->context = context_module::instance($cm->id);
144 $fs = get_file_storage();
145 $this->dir = $fs->get_area_tree($this->context->id, 'mod_folder', 'content', 0);