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