forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocallib.php
525 lines (457 loc) · 18.7 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?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 resource module utility functions
*
* @package mod_resource
* @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;
require_once("$CFG->libdir/filelib.php");
require_once("$CFG->libdir/resourcelib.php");
require_once("$CFG->dirroot/mod/resource/lib.php");
/**
* Redirected to migrated resource if needed,
* return if incorrect parameters specified
* @param int $oldid
* @param int $cmid
* @return void
*/
function resource_redirect_if_migrated($oldid, $cmid) {
global $DB, $CFG;
if ($oldid) {
$old = $DB->get_record('resource_old', array('oldid'=>$oldid));
} else {
$old = $DB->get_record('resource_old', array('cmid'=>$cmid));
}
if (!$old) {
return;
}
redirect("$CFG->wwwroot/mod/$old->newmodule/view.php?id=".$old->cmid);
}
/**
* Display embedded resource file.
* @param object $resource
* @param object $cm
* @param object $course
* @param stored_file $file main file
* @return does not return
*/
function resource_display_embed($resource, $cm, $course, $file) {
global $PAGE, $OUTPUT;
$clicktoopen = resource_get_clicktoopen($file, $resource->revision);
$context = context_module::instance($cm->id);
$moodleurl = moodle_url::make_pluginfile_url($context->id, 'mod_resource', 'content', $resource->revision,
$file->get_filepath(), $file->get_filename());
$mimetype = $file->get_mimetype();
$title = $resource->name;
$extension = resourcelib_get_extension($file->get_filename());
$mediamanager = core_media_manager::instance($PAGE);
$embedoptions = array(
core_media_manager::OPTION_TRUSTED => true,
core_media_manager::OPTION_BLOCK => true,
);
if (file_mimetype_in_typegroup($mimetype, 'web_image')) { // It's an image
$code = resourcelib_embed_image($moodleurl->out(), $title);
} else if ($mimetype === 'application/pdf') {
// PDF document
$code = resourcelib_embed_pdf($moodleurl->out(), $title, $clicktoopen);
} else if ($mediamanager->can_embed_url($moodleurl, $embedoptions)) {
// Media (audio/video) file.
$code = $mediamanager->embed_url($moodleurl, $title, 0, 0, $embedoptions);
} else {
// We need a way to discover if we are loading remote docs inside an iframe.
$moodleurl->param('embed', 1);
// anything else - just try object tag enlarged as much as possible
$code = resourcelib_embed_general($moodleurl, $title, $clicktoopen, $mimetype);
}
// Let the module handle the display.
$PAGE->activityheader->set_description(resource_get_intro($resource, $cm));
resource_print_header($resource, $cm, $course);
echo format_text($code, FORMAT_HTML, ['noclean' => true]);
echo $OUTPUT->footer();
die;
}
/**
* Display resource frames.
* @param object $resource
* @param object $cm
* @param object $course
* @param stored_file $file main file
* @return does not return
*/
function resource_display_frame($resource, $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(resource_get_intro($resource, $cm, true));
resource_print_header($resource, $cm, $course);
echo $OUTPUT->footer();
die;
} else {
$config = get_config('resource');
$context = context_module::instance($cm->id);
$path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
$fileurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
$navurl = "$CFG->wwwroot/mod/resource/view.php?id=$cm->id&frameset=top";
$title = strip_tags(format_string($course->shortname.': '.$resource->name));
$framesize = $config->framesize;
$contentframetitle = s(format_string($resource->name));
$modulename = s(get_string('modulename','resource'));
$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 resource_get_clicktoopen($file, $revision, $extra='') {
global $CFG;
$filename = $file->get_filename();
$path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename();
$fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
$string = get_string('clicktoopen2', 'resource', "<a href=\"$fullurl\" $extra>$filename</a>");
return $string;
}
/**
* Internal function - create click to open text with link.
*/
function resource_get_clicktodownload($file, $revision) {
global $CFG;
$filename = $file->get_filename();
$path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename();
$fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true);
$string = get_string('clicktodownload', 'resource', "<a href=\"$fullurl\">$filename</a>");
return $string;
}
/**
* Print resource info and workaround link when JS not available.
* @param object $resource
* @param object $cm
* @param object $course
* @param stored_file $file main file
* @return does not return
*/
function resource_print_workaround($resource, $cm, $course, $file) {
global $CFG, $OUTPUT, $PAGE;
// Let the module handle the display.
$PAGE->activityheader->set_description(resource_get_intro($resource, $cm, true));
resource_print_header($resource, $cm, $course);
$resource->mainfile = $file->get_filename();
echo '<div class="resourceworkaround">';
switch (resource_get_final_display_type($resource)) {
case RESOURCELIB_DISPLAY_POPUP:
$path = '/'.$file->get_contextid().'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
$fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
$options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->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 resource_get_clicktoopen($file, $resource->revision, $extra);
break;
case RESOURCELIB_DISPLAY_NEW:
$extra = 'onclick="this.target=\'_blank\'"';
echo resource_get_clicktoopen($file, $resource->revision, $extra);
break;
case RESOURCELIB_DISPLAY_DOWNLOAD:
echo resource_get_clicktodownload($file, $resource->revision);
break;
case RESOURCELIB_DISPLAY_OPEN:
default:
echo resource_get_clicktoopen($file, $resource->revision);
break;
}
echo '</div>';
echo $OUTPUT->footer();
die;
}
/**
* Print resource header.
* @param object $resource
* @param object $cm
* @param object $course
* @return void
*/
function resource_print_header($resource, $cm, $course) {
global $PAGE, $OUTPUT;
$PAGE->set_title($course->shortname.': '.$resource->name);
$PAGE->set_heading($course->fullname);
$PAGE->set_activity_record($resource);
echo $OUTPUT->header();
}
/**
* Gets details of the file to cache in course cache to be displayed using {@link resource_get_optional_details()}
*
* @param object $resource Resource 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 resource_get_file_details($resource, $cm) {
$options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->displayoptions);
$filedetails = array();
if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) {
$context = context_module::instance($cm->id);
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
// For a typical file resource, the sortorder is 1 for the main file
// and 0 for all other files. This sort approach is used just in case
// there are situations where the file has a different sort order.
$mainfile = $files ? reset($files) : null;
if (!empty($options['showsize'])) {
$filedetails['size'] = 0;
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 resource, depending on resource settings.
*
* Result may include the file size and type if those settings are chosen,
* or blank if none.
*
* @param object $resource Resource 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 resource_get_optional_details($resource, $cm) {
global $DB;
$details = '';
$options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->displayoptions);
if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) {
if (!array_key_exists('filedetails', $options)) {
$filedetails = resource_get_file_details($resource, $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_resource', userdate($filedetails['modifieddate'],
get_string('strftimedatetimeshort', 'langconfig')));
} else if (!empty($filedetails['uploadeddate'])) {
$date = get_string('uploadeddate', 'mod_resource', userdate($filedetails['uploadeddate'],
get_string('strftimedatetimeshort', 'langconfig')));
}
$langstring .= 'date';
$infodisplayed += 1;
}
if ($infodisplayed > 1) {
$details = get_string("resourcedetails_{$langstring}", 'resource',
(object)array('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 resource introduction.
*
* @param object $resource
* @param object $cm
* @param bool $ignoresettings print even if not specified in modedit
* @return string
*/
function resource_get_intro(object $resource, object $cm, bool $ignoresettings = false): string {
$options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->displayoptions);
$extraintro = resource_get_optional_details($resource, $cm);
if ($extraintro) {
// Put a paragaph tag around the details
$extraintro = html_writer::tag('p', $extraintro, array('class' => 'resourcedetails'));
}
$content = "";
if ($ignoresettings || !empty($options['printintro']) || $extraintro) {
$gotintro = !html_is_blank($resource->intro);
if ($gotintro || $extraintro) {
if ($gotintro) {
$content = format_module_intro('resource', $resource, $cm->id);
}
$content .= $extraintro;
}
}
return $content;
}
/**
* Print warning that instance not migrated yet.
* @param object $resource
* @param object $cm
* @param object $course
* @return void, does not return
*/
function resource_print_tobemigrated($resource, $cm, $course) {
global $DB, $OUTPUT, $PAGE;
$PAGE->activityheader->set_description(resource_get_intro($resource, $cm));
$resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id));
resource_print_header($resource, $cm, $course);
echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type));
echo $OUTPUT->footer();
die;
}
/**
* Print warning that file can not be found.
* @param object $resource
* @param object $cm
* @param object $course
* @return void, does not return
*/
function resource_print_filenotfound($resource, $cm, $course) {
global $DB, $OUTPUT, $PAGE;
$resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id));
$PAGE->activityheader->set_description(resource_get_intro($resource, $cm));
resource_print_header($resource, $cm, $course);
if ($resource_old) {
echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type));
} else {
echo $OUTPUT->notification(get_string('filenotfound', 'resource'));
}
echo $OUTPUT->footer();
die;
}
/**
* Decide the best display format.
* @param object $resource
* @return int display type constant
*/
function resource_get_final_display_type($resource) {
global $CFG, $PAGE;
if ($resource->display != RESOURCELIB_DISPLAY_AUTO) {
return $resource->display;
}
if (empty($resource->mainfile)) {
return RESOURCELIB_DISPLAY_DOWNLOAD;
} else {
$mimetype = mimeinfo('type', $resource->mainfile);
}
if (file_mimetype_in_typegroup($mimetype, 'archive')) {
return RESOURCELIB_DISPLAY_DOWNLOAD;
}
if (file_mimetype_in_typegroup($mimetype, array('web_image', '.htm', 'web_video', 'web_audio'))) {
return RESOURCELIB_DISPLAY_EMBED;
}
// let the browser deal with it somehow
return RESOURCELIB_DISPLAY_OPEN;
}
/**
* File browsing support class
*/
class resource_content_file_info extends file_info_stored {
public function get_parent() {
if ($this->lf->get_filepath() === '/' and $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() === '/' and $this->lf->get_filename() === '.') {
return $this->topvisiblename;
}
return parent::get_visible_name();
}
}
function resource_set_mainfile($data) {
global $DB;
$fs = get_file_storage();
$cmid = $data->coursemodule;
$draftitemid = $data->files;
$context = context_module::instance($cmid);
if ($draftitemid) {
$options = array('subdirs' => true, 'embed' => false);
if ($data->display == RESOURCELIB_DISPLAY_EMBED) {
$options['embed'] = true;
}
file_save_draft_area_files($draftitemid, $context->id, 'mod_resource', 'content', 0, $options);
}
$files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder', false);
if (count($files) == 1) {
// only one file attached, set it as main file automatically
$file = reset($files);
file_set_sortorder($context->id, 'mod_resource', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
}
}