This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.php
321 lines (275 loc) · 10.3 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?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/>.
/**
* ompdf module renderering methods are defined here.
*
* @package mod_ompdf
* @copyright 2013 Dian Mukti Wibowo <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/ompdf/locallib.php');
/**
* ompdf module renderer class
*/
class mod_ompdf_renderer extends plugin_renderer_base {
/**
* Renders the ompdf page header.
*
* @param ompdf ompdf
* @return string
*/
public function pdf_header($ompdf, cm_info $cm) {
$output = '';
if (method_exists($cm, 'get_formatted_name')) {
$name = $cm->get_formatted_name();
} else {
$name = format_string($cm->name, true, null);
}
$title = $this->page->course->shortname . ': ' . $name;
$context = context_module::instance($cm->id);
// Header setup.
$this->page->set_title($title);
$this->page->set_heading($this->page->course->fullname);
$output .= $this->output->header();
$output .= $this->output->heading($name, 3);
if (!empty($ompdf->get_instance()->intro)) {
$output .= $this->output->box_start('generalbox boxaligncenter', 'intro');
$output .= format_module_intro('ompdf',
$ompdf->get_instance(),
$cm->id);
$output .= $this->output->box_end();
}
return $output;
}
/**
* Render the footer
*
* @return string
*/
public function pdf_footer() {
return $this->output->footer();
}
/**
* Render the ompdf page
*
* @param ompdf ompdf
* @return string The page output.
*/
public function render_ompdf($ompdf) {
$output = '';
$coursemodule = $ompdf->get_course_module();
$instance = $ompdf->get_instance();
$course = $ompdf->get_course();
$context = $ompdf->get_context();
// Get cm_info with uservisible.
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($coursemodule->id);
if (!$cm->uservisible ||
!has_capability('mod/ompdf:view', $context)) {
// Module is not visible to the user. Don't throw any
// errors in renderer, just return empty string.
return $output;
}
if ($instance->display == OMPDF_MANAGER_DISPLAY_INLINE &&
$cm->showdescription &&
!empty($instance->intro)) {
$output .= format_module_intro('ompdf',
$instance,
$cm->id,
false);
}
if ($instance->display != OMPDF_MANAGER_DISPLAY_INLINE) {
$output .= $this->pdf_header($ompdf, $cm);
}
$output .= $this->pdfs($ompdf, $cm);
if ($instance->display != OMPDF_MANAGER_DISPLAY_INLINE) {
$output .= $this->pdf_footer($cm);
}
return $output;
}
/**
* Utility function for getting area files
*
* @param int $contextid
* @param string $areaname file area name (e.g. "pdfs")
* @return array of stored_file objects
*/
private function util_get_area_tree($contextid, $areaname) {
$fs = get_file_storage();
return $fs->get_area_tree($contextid,
'mod_ompdf',
$areaname,
false);
}
/**
* Utility function for creating the pdf folder HTML.
*
* @param int $contextid
* @param ompdf $ompdf
* @param cm_info $cm
* @return string HTML
*/
protected function get_pdf_folder_html(ompdf $ompdf,
cm_info $cm) {
$output = '';
$tree = $this->util_get_area_tree($ompdf->get_context()->id,
'pdfs');
$tree['dirname'] = $cm->name;
$toptree = array('files' => array(),
'subdirs' => array($tree));
$openinnewtab = $ompdf->get_instance()->openinnewtab;
$showdownloadlinks = $ompdf->get_default_config()->showdownloadlinks;
$output .= $this->htmlize_folder($tree,
$toptree,
$openinnewtab,
$showdownloadlinks);
return $output;
}
/**
* Utility function for rendering folder structure.
*
* @param array $tree
* @param array $dir
* @param boolean $openinnewtab
* @param boolean $showdownloadlinks
* @return string HTML
*/
protected function htmlize_folder($tree,
$dir,
$openinnewtab,
$showdownloadlinks) {
if (empty($dir['subdirs']) and empty($dir['files'])) {
return '';
}
$output = '<ul>';
foreach ($dir['subdirs'] as $subdir) {
$icon = new pix_icon(file_folder_icon(24),
$subdir['dirname'],
'moodle');
$imagehtml = $this->output->render($icon);
$iconhtml = html_writer::tag(
'span',
$imagehtml,
array('class' => 'fp-icon'));
$namehtml = html_writer::tag(
'span',
s($subdir['dirname']),
array('class' => 'fp-filename'));
$divhtml = html_writer::tag(
'div',
$iconhtml . $namehtml ,
array('class' => 'fp-filename-icon'));
$output .= html_writer::tag(
'li',
$divhtml . $this->htmlize_folder($tree,
$subdir,
$openinnewtab,
$showdownloadlinks));
}
foreach ($dir['files'] as $pdf) {
$filename = $pdf->get_filename();
$fileurl = moodle_url::make_pluginfile_url(
$pdf->get_contextid(),
$pdf->get_component(),
$pdf->get_filearea(),
$pdf->get_itemid(),
$pdf->get_filepath(),
$filename,
false);
$fileurlforcedownload = moodle_url::make_pluginfile_url(
$pdf->get_contextid(),
$pdf->get_component(),
$pdf->get_filearea(),
$pdf->get_itemid(),
$pdf->get_filepath(),
$filename,
true);
if (file_extension_in_typegroup($filename, 'web_image')) {
$image = $fileurl->out(
false,
array('preview' => 'tinyicon',
'oid' => $pdf->get_timemodified()));
$image = html_writer::empty_tag('img', array('src' => $image));
$url = $fileurl;
$isimage = true;
} else {
$icon = new pix_icon(file_file_icon($pdf, 24),
$filename,
'moodle');
$image = $this->output->render($icon);
$ompdfurl = new moodle_url(
'/mod/ompdf/pdfjs/web/viewer.html');
$fileurl = urlencode(base64_encode($fileurl));
$url = $ompdfurl . '?file=' . $fileurl;
$isimage = false;
}
if ($openinnewtab) {
$linkoptions = array('target' => '_blank');
} else {
$linkoptions = array();
}
$fileicon = html_writer::tag(
'span', $image, array('class' => 'fp-icon'));
$filenamespan = html_writer::tag(
'span', $filename, array('class' => 'fp-filename'));
$filelink = html_writer::link(
$url,
$fileicon . $filenamespan,
$linkoptions);
if (!$isimage && $showdownloadlinks) {
$downloadlink = html_writer::link(
$fileurlforcedownload,
get_string('downloadlinktext', 'ompdf'),
$linkoptions);
$filelink .= ' ' . html_writer::tag('em', '(' . $downloadlink . ')');
}
$filespan = html_writer::tag(
'span',
$filelink,
array('class' => 'fp-filename-icon'));
$output .= html_writer::tag('li', $filespan);
}
$output .= '</ul>';
return $output;
}
/**
* Renders pdfjs folder.
*
* @param ompdf $ompdf
* @param cm_info $cm
* @return string HTML
*/
public function pdfs(ompdf $ompdf, cm_info $cm) {
static $treecounter = 0;
$output = '';
// Open folder div.
$id = 'ompdf_manager_' . ($treecounter++);
$output .= $this->output->container_start('ompdf-onyet filemanager',
$id);
// Elements for folder.
$output .= $this->get_pdf_folder_html($ompdf, $cm);
// Close folder div.
$output .= $this->output->container_end();
$showexpanded = true;
if (empty($ompdf->get_instance()->showexpanded)) {
$showexpanded = false;
}
$this->page->requires->js_init_call('M.mod_ompdf.init_tree',
array($id, $showexpanded));
return $output;
}
}