-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocallib.php
390 lines (339 loc) · 13.8 KB
/
locallib.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?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/>.
/**
* Private exeweb module utility functions
*
* @package mod_exeweb
* @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;
use mod_exeweb\exeweb_package;
require_once("$CFG->libdir/filelib.php");
require_once("$CFG->libdir/resourcelib.php");
require_once("$CFG->dirroot/mod/exeweb/lib.php");
/**
* Display embedded exeweb file.
* @param object $exeweb
* @param object $cm
* @param object $course
* @param stored_file $file main file
* @return does not return
*/
function exeweb_display_embed($exeweb, $cm, $course, $file) {
global $PAGE, $OUTPUT;
$clicktoopen = exeweb_get_clicktoopen($file, $exeweb->revision);
$context = context_module::instance($cm->id);
$moodleurl = moodle_url::make_pluginfile_url($context->id, 'mod_exeweb', 'content', $exeweb->revision,
$file->get_filepath(), $file->get_filename());
$title = $exeweb->name;
// We need a way to discover if we are loading remote docs inside an iframe.
$moodleurl->param('embed', 1);
// Let the module handle the display.
$PAGE->activityheader->set_description(exeweb_get_intro($exeweb, $cm));
exeweb_print_header($exeweb, $cm, $course);
echo $PAGE->get_renderer('mod_exeweb')->generate_embed_general($cm, $moodleurl, $title, $clicktoopen);
echo $OUTPUT->footer();
die;
}
/**
* Display exeweb frames.
* @param object $exeweb
* @param object $cm
* @param object $course
* @param stored_file $file main file
* @return does not return
*/
function exeweb_display_frame($exeweb, $cm, $course, $file) {
global $PAGE, $OUTPUT, $CFG;
$frame = optional_param('frameset', 'main', PARAM_ALPHA);
if ($frame === 'top') {
$PAGE->set_pagelayout('frametop');
$PAGE->activityheader->set_description(exeweb_get_intro($exeweb, $cm, true));
exeweb_print_header($exeweb, $cm, $course);
echo $OUTPUT->footer();
die;
} else {
$config = get_config('exeweb');
$context = context_module::instance($cm->id);
$fileurl = moodle_url::make_pluginfile_url($context->id, 'mod_exeweb', 'content', $exeweb->revision,
$file->get_filepath(), $file->get_filename());
$navurl = "$CFG->wwwroot/mod/exeweb/view.php?id=$cm->id&frameset=top";
$title = strip_tags(format_string($course->shortname.': '.$exeweb->name));
$framesize = $config->framesize;
$contentframetitle = s(format_string($exeweb->name));
$modulename = s(get_string('modulename', 'mod_exeweb'));
$dir = get_string('thisdirection', 'langconfig');
$file = <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html dir="$dir">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>$title</title>
</head>
<frameset rows="$framesize,*">
<frame src="$navurl" title="$modulename" />
<frame src="$fileurl" title="$contentframetitle" />
</frameset>
</html>
EOF;
@header('Content-Type: text/html; charset=utf-8');
echo $file;
die;
}
}
/**
* Internal function - create click to open text with link.
*/
function exeweb_get_clicktoopen($file, $revision, $extra='') {
global $CFG;
$filename = $file->get_filename();
$fullurl = moodle_url::make_pluginfile_url($file->get_contextid(), 'mod_exeweb', 'content', $revision,
$file->get_filepath(), $filename);
$string = get_string('clicktoopen2', 'mod_exeweb', "<a href=\"$fullurl\" $extra>$filename</a>");
return $string;
}
/**
* Print exeweb info and workaround link when JS not available.
* @param object $exeweb
* @param object $cm
* @param object $course
* @param stored_file $file main file
* @return does not return
*/
function exeweb_print_workaround($exeweb, $cm, $course, $file) {
global $OUTPUT, $PAGE;
// Let the module handle the display.
$PAGE->activityheader->set_description(exeweb_get_intro($exeweb, $cm, true));
exeweb_print_header($exeweb, $cm, $course);
echo '<div class="exewebworkaround">';
switch ($exeweb->display) {
case RESOURCELIB_DISPLAY_POPUP:
$fullurl = moodle_url::make_pluginfile_url($file->get_contextid(), 'mod_exeweb', 'content', $exeweb->revision,
$file->get_filepath(), $file->get_filename());
$options = empty($exeweb->displayoptions) ? [] : (array) unserialize_array($exeweb->displayoptions);
$width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
$height = empty($options['popupheight']) ? 450 : $options['popupheight'];
$wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,"
. "status=no,directories=no,scrollbars=yes,resizable=yes";
$extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
echo exeweb_get_clicktoopen($file, $exeweb->revision, $extra);
break;
case RESOURCELIB_DISPLAY_NEW:
$extra = 'onclick="this.target=\'_blank\'"';
echo exeweb_get_clicktoopen($file, $exeweb->revision, $extra);
break;
case RESOURCELIB_DISPLAY_OPEN:
default:
echo exeweb_get_clicktoopen($file, $exeweb->revision);
break;
}
echo '</div>';
echo $OUTPUT->footer();
die;
}
/**
* Print exeweb header.
* @param object $exeweb
* @param object $cm
* @param object $course
* @return void
*/
function exeweb_print_header($exeweb, $cm, $course) {
global $PAGE, $OUTPUT;
$PAGE->set_title($course->shortname.': '.$exeweb->name);
$PAGE->set_heading($course->fullname);
$PAGE->set_activity_record($exeweb);
echo $OUTPUT->header();
}
/**
* Gets details of the file to cache in course cache to be displayed using {@link exeweb_get_optional_details()}
*
* @param object $exeweb Exeweb table row (only property 'displayoptions' is used here)
* @param object $cm Course-module table row
* @return string Size and type or empty string if show options are not enabled
*/
function exeweb_get_file_details($exeweb, $cm) {
$options = empty($exeweb->displayoptions) ? [] : (array) unserialize_array($exeweb->displayoptions);
$filedetails = [];
if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) {
$context = context_module::instance($cm->id);
$fs = get_file_storage();
$mainfile = $fs->get_file($context->id, 'mod_exeweb', 'content', 0, $exeweb->entrypath, $exeweb->entryname);
if (!empty($options['showsize'])) {
$filedetails['size'] = 0;
$files = $fs->get_area_files($context->id, 'mod_exeweb', 'content', 0, 'id', false);
foreach ($files as $file) {
// This will also synchronize the file size for external files if needed.
$filedetails['size'] += $file->get_filesize();
if ($file->get_repository_id()) {
// If file is a reference the 'size' attribute can not be cached.
$filedetails['isref'] = true;
}
}
}
if (!empty($options['showtype'])) {
if ($mainfile) {
$filedetails['type'] = get_mimetype_description($mainfile);
$filedetails['mimetype'] = $mainfile->get_mimetype();
// Only show type if it is not unknown.
if ($filedetails['type'] === get_mimetype_description('document/unknown')) {
$filedetails['type'] = '';
}
} else {
$filedetails['type'] = '';
}
}
if (!empty($options['showdate'])) {
if ($mainfile) {
// Modified date may be up to several minutes later than uploaded date just because
// teacher did not submit the form promptly. Give teacher up to 5 minutes to do it.
if ($mainfile->get_timemodified() > $mainfile->get_timecreated() + 5 * MINSECS) {
$filedetails['modifieddate'] = $mainfile->get_timemodified();
} else {
$filedetails['uploadeddate'] = $mainfile->get_timecreated();
}
if ($mainfile->get_repository_id()) {
// If main file is a reference the 'date' attribute can not be cached.
$filedetails['isref'] = true;
}
} else {
$filedetails['uploadeddate'] = '';
}
}
}
return $filedetails;
}
/**
* Gets optional details for a exeweb, depending on exeweb settings.
*
* Result may include the file size and type if those settings are chosen,
* or blank if none.
*
* @param object $exeweb Exeweb table row (only property 'displayoptions' is used here)
* @param object $cm Course-module table row
* @return string Size and type or empty string if show options are not enabled
*/
function exeweb_get_optional_details($exeweb, $cm) {
global $DB;
$details = '';
$options = empty($exeweb->displayoptions) ? [] : (array) unserialize_array($exeweb->displayoptions);
if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) {
if (!array_key_exists('filedetails', $options)) {
$filedetails = exeweb_get_file_details($exeweb, $cm);
} else {
$filedetails = $options['filedetails'];
}
$size = '';
$type = '';
$date = '';
$langstring = '';
$infodisplayed = 0;
if (!empty($options['showsize'])) {
if (!empty($filedetails['size'])) {
$size = display_size($filedetails['size']);
$langstring .= 'size';
$infodisplayed += 1;
}
}
if (!empty($options['showtype'])) {
if (!empty($filedetails['type'])) {
$type = $filedetails['type'];
$langstring .= 'type';
$infodisplayed += 1;
}
}
if (!empty($options['showdate']) && (!empty($filedetails['modifieddate']) || !empty($filedetails['uploadeddate']))) {
if (!empty($filedetails['modifieddate'])) {
$date = get_string('modifieddate', 'mod_exeweb', userdate($filedetails['modifieddate'],
get_string('strftimedatetimeshort', 'langconfig')));
} else if (!empty($filedetails['uploadeddate'])) {
$date = get_string('uploadeddate', 'mod_exeweb', userdate($filedetails['uploadeddate'],
get_string('strftimedatetimeshort', 'langconfig')));
}
$langstring .= 'date';
$infodisplayed += 1;
}
if ($infodisplayed > 1) {
$details = get_string("exewebdetails_{$langstring}", 'mod_exeweb',
(object)['size' => $size, 'type' => $type, 'date' => $date]);
} else {
// Only one of size, type and date is set, so just append.
$details = $size . $type . $date;
}
}
return $details;
}
/**
* Get exeweb introduction.
*
* @param object $exeweb
* @param object $cm
* @param bool $ignoresettings print even if not specified in modedit
* @return string
*/
function exeweb_get_intro(object $exeweb, object $cm, bool $ignoresettings = false): string {
global $PAGE;
$options = empty($exeweb->displayoptions) ? [] : (array) unserialize_array($exeweb->displayoptions);
$extraintro = exeweb_get_optional_details($exeweb, $cm);
if ($extraintro) {
// Put a paragaph tag around the details.
$extraintro = html_writer::tag('p', $extraintro, ['class' => 'exewebdetails']);
}
$content = "";
if ($ignoresettings || !empty($options['printintro']) || $extraintro) {
$gotintro = !html_is_blank($exeweb->intro);
if ($gotintro || $extraintro) {
if ($gotintro) {
$content = format_module_intro('exeweb', $exeweb, $cm->id);
}
$content .= $extraintro;
}
}
return $content;
}
/**
* Print warning that file can not be found.
* @param object $exeweb
* @param object $cm
* @param object $course
* @return void, does not return
*/
function exeweb_print_filenotfound($exeweb, $cm, $course) {
global $DB, $OUTPUT, $PAGE;
$PAGE->activityheader->set_description(exeweb_get_intro($exeweb, $cm));
exeweb_print_header($exeweb, $cm, $course);
echo $OUTPUT->notification(get_string('filenotfound', 'mod_exeweb'));
echo $OUTPUT->footer();
die;
}
/**
* File browsing support class
*/
class exeweb_content_file_info extends file_info_stored {
public function get_parent() {
if ($this->lf->get_filepath() === '/' && $this->lf->get_filename() === '.') {
return $this->browser->get_file_info($this->context);
}
return parent::get_parent();
}
public function get_visible_name() {
if ($this->lf->get_filepath() === '/' && $this->lf->get_filename() === '.') {
return $this->topvisiblename;
}
return parent::get_visible_name();
}
}