Merge branch 'MDL-61033-master' of https://github.com/nashtechdev01/moodle
[moodle.git] / mod / resource / locallib.php
CommitLineData
aa54ed7b 1<?php
2
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.
14//
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/>.
17
18/**
19 * Private resource module utility functions
20 *
11e41f1a 21 * @package mod_resource
702ab58c
PS
22 * @copyright 2009 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
aa54ed7b 24 */
25
702ab58c
PS
26defined('MOODLE_INTERNAL') || die;
27
aa54ed7b 28require_once("$CFG->libdir/filelib.php");
29require_once("$CFG->libdir/resourcelib.php");
30require_once("$CFG->dirroot/mod/resource/lib.php");
31
32/**
33 * Redirected to migrated resource if needed,
34 * return if incorrect parameters specified
35 * @param int $oldid
36 * @param int $cmid
37 * @return void
38 */
39function resource_redirect_if_migrated($oldid, $cmid) {
40 global $DB, $CFG;
41
42 if ($oldid) {
43 $old = $DB->get_record('resource_old', array('oldid'=>$oldid));
44 } else {
45 $old = $DB->get_record('resource_old', array('cmid'=>$cmid));
46 }
47
48 if (!$old) {
49 return;
50 }
51
52 redirect("$CFG->wwwroot/mod/$old->newmodule/view.php?id=".$old->cmid);
53}
54
55/**
56 * Display embedded resource file.
57 * @param object $resource
58 * @param object $cm
59 * @param object $course
60 * @param stored_file $file main file
61 * @return does not return
62 */
63function resource_display_embed($resource, $cm, $course, $file) {
64 global $CFG, $PAGE, $OUTPUT;
65
66 $clicktoopen = resource_get_clicktoopen($file, $resource->revision);
67
bf0f06b1 68 $context = context_module::instance($cm->id);
64f93798 69 $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
aa54ed7b 70 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
8b7d95b6 71 $moodleurl = new moodle_url('/pluginfile.php' . $path);
aa54ed7b 72
73 $mimetype = $file->get_mimetype();
74 $title = $resource->name;
75
fcd2cbaf
PS
76 $extension = resourcelib_get_extension($file->get_filename());
77
1abd4376 78 $mediamanager = core_media_manager::instance($PAGE);
8b7d95b6 79 $embedoptions = array(
fab11235
MG
80 core_media_manager::OPTION_TRUSTED => true,
81 core_media_manager::OPTION_BLOCK => true,
8b7d95b6 82 );
83
ae7f35b9 84 if (file_mimetype_in_typegroup($mimetype, 'web_image')) { // It's an image
aa54ed7b 85 $code = resourcelib_embed_image($fullurl, $title);
86
fcd2cbaf 87 } else if ($mimetype === 'application/pdf') {
b8037d73
MD
88 // PDF document
89 $code = resourcelib_embed_pdf($fullurl, $title, $clicktoopen);
90
fab11235 91 } else if ($mediamanager->can_embed_url($moodleurl, $embedoptions)) {
8b7d95b6 92 // Media (audio/video) file.
fab11235 93 $code = $mediamanager->embed_url($moodleurl, $title, 0, 0, $embedoptions);
aa54ed7b 94
95 } else {
1fad6ff3
SL
96 // We need a way to discover if we are loading remote docs inside an iframe.
97 $moodleurl->param('embed', 1);
98
aa54ed7b 99 // anything else - just try object tag enlarged as much as possible
1fad6ff3 100 $code = resourcelib_embed_general($moodleurl, $title, $clicktoopen, $mimetype);
aa54ed7b 101 }
102
103 resource_print_header($resource, $cm, $course);
104 resource_print_heading($resource, $cm, $course);
105
106 echo $code;
107
108 resource_print_intro($resource, $cm, $course);
109
110 echo $OUTPUT->footer();
111 die;
112}
113
114/**
115 * Display resource frames.
116 * @param object $resource
117 * @param object $cm
118 * @param object $course
119 * @param stored_file $file main file
120 * @return does not return
121 */
122function resource_display_frame($resource, $cm, $course, $file) {
123 global $PAGE, $OUTPUT, $CFG;
124
125 $frame = optional_param('frameset', 'main', PARAM_ALPHA);
126
127 if ($frame === 'top') {
78946b9b 128 $PAGE->set_pagelayout('frametop');
aa54ed7b 129 resource_print_header($resource, $cm, $course);
130 resource_print_heading($resource, $cm, $course);
131 resource_print_intro($resource, $cm, $course);
132 echo $OUTPUT->footer();
133 die;
134
135 } else {
136 $config = get_config('resource');
bf0f06b1 137 $context = context_module::instance($cm->id);
64f93798 138 $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
aa54ed7b 139 $fileurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
140 $navurl = "$CFG->wwwroot/mod/resource/view.php?id=$cm->id&amp;frameset=top";
141 $title = strip_tags(format_string($course->shortname.': '.$resource->name));
142 $framesize = $config->framesize;
c9b44599 143 $contentframetitle = s(format_string($resource->name));
aa54ed7b 144 $modulename = s(get_string('modulename','resource'));
e372f4c7 145 $dir = get_string('thisdirection', 'langconfig');
aa54ed7b 146
147 $file = <<<EOF
148<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
149<html dir="$dir">
150 <head>
151 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
152 <title>$title</title>
153 </head>
154 <frameset rows="$framesize,*">
155 <frame src="$navurl" title="$modulename" />
4d7e89d7 156 <frame src="$fileurl" title="$contentframetitle" />
aa54ed7b 157 </frameset>
158</html>
159EOF;
160
161 @header('Content-Type: text/html; charset=utf-8');
162 echo $file;
163 die;
164 }
165}
166
167/**
168 * Internal function - create click to open text with link.
169 */
170function resource_get_clicktoopen($file, $revision, $extra='') {
171 global $CFG;
172
173 $filename = $file->get_filename();
64f93798 174 $path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename();
aa54ed7b 175 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
176
177 $string = get_string('clicktoopen2', 'resource', "<a href=\"$fullurl\" $extra>$filename</a>");
178
179 return $string;
180}
181
182/**
183 * Internal function - create click to open text with link.
184 */
185function resource_get_clicktodownload($file, $revision) {
186 global $CFG;
187
188 $filename = $file->get_filename();
64f93798 189 $path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename();
aa54ed7b 190 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true);
191
192 $string = get_string('clicktodownload', 'resource', "<a href=\"$fullurl\">$filename</a>");
193
194 return $string;
195}
196
197/**
198 * Print resource info and workaround link when JS not available.
199 * @param object $resource
200 * @param object $cm
201 * @param object $course
202 * @param stored_file $file main file
203 * @return does not return
204 */
205function resource_print_workaround($resource, $cm, $course, $file) {
206 global $CFG, $OUTPUT;
207
208 resource_print_header($resource, $cm, $course);
209 resource_print_heading($resource, $cm, $course, true);
210 resource_print_intro($resource, $cm, $course, true);
211
f79321f1 212 $resource->mainfile = $file->get_filename();
aa54ed7b 213 echo '<div class="resourceworkaround">';
214 switch (resource_get_final_display_type($resource)) {
215 case RESOURCELIB_DISPLAY_POPUP:
64f93798 216 $path = '/'.$file->get_contextid().'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
aa54ed7b 217 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
218 $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
219 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
220 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
221 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
222 $extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
223 echo resource_get_clicktoopen($file, $resource->revision, $extra);
224 break;
225
226 case RESOURCELIB_DISPLAY_NEW:
227 $extra = 'onclick="this.target=\'_blank\'"';
228 echo resource_get_clicktoopen($file, $resource->revision, $extra);
229 break;
230
231 case RESOURCELIB_DISPLAY_DOWNLOAD:
232 echo resource_get_clicktodownload($file, $resource->revision);
233 break;
234
235 case RESOURCELIB_DISPLAY_OPEN:
236 default:
237 echo resource_get_clicktoopen($file, $resource->revision);
238 break;
239 }
240 echo '</div>';
241
242 echo $OUTPUT->footer();
243 die;
244}
245
246/**
247 * Print resource header.
248 * @param object $resource
249 * @param object $cm
250 * @param object $course
251 * @return void
252 */
253function resource_print_header($resource, $cm, $course) {
254 global $PAGE, $OUTPUT;
255
256 $PAGE->set_title($course->shortname.': '.$resource->name);
257 $PAGE->set_heading($course->fullname);
258 $PAGE->set_activity_record($resource);
ef8869c7 259 echo $OUTPUT->header();
aa54ed7b 260}
261
262/**
263 * Print resource heading.
264 * @param object $resource
265 * @param object $cm
266 * @param object $course
b25e7d85 267 * @param bool $notused This variable is no longer used
aa54ed7b 268 * @return void
269 */
b25e7d85 270function resource_print_heading($resource, $cm, $course, $notused = false) {
aa54ed7b 271 global $OUTPUT;
b25e7d85 272 echo $OUTPUT->heading(format_string($resource->name), 2);
aa54ed7b 273}
274
9dfe8704
MG
275
276/**
277 * Gets details of the file to cache in course cache to be displayed using {@link resource_get_optional_details()}
278 *
279 * @param object $resource Resource table row (only property 'displayoptions' is used here)
280 * @param object $cm Course-module table row
281 * @return string Size and type or empty string if show options are not enabled
282 */
283function resource_get_file_details($resource, $cm) {
284 $options = empty($resource->displayoptions) ? array() : @unserialize($resource->displayoptions);
285 $filedetails = array();
286 if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) {
287 $context = context_module::instance($cm->id);
288 $fs = get_file_storage();
289 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
290 // For a typical file resource, the sortorder is 1 for the main file
291 // and 0 for all other files. This sort approach is used just in case
292 // there are situations where the file has a different sort order.
293 $mainfile = $files ? reset($files) : null;
294 if (!empty($options['showsize'])) {
295 $filedetails['size'] = 0;
296 foreach ($files as $file) {
297 // This will also synchronize the file size for external files if needed.
298 $filedetails['size'] += $file->get_filesize();
299 if ($file->get_repository_id()) {
300 // If file is a reference the 'size' attribute can not be cached.
301 $filedetails['isref'] = true;
302 }
303 }
304 }
305 if (!empty($options['showtype'])) {
306 if ($mainfile) {
307 $filedetails['type'] = get_mimetype_description($mainfile);
308 // Only show type if it is not unknown.
309 if ($filedetails['type'] === get_mimetype_description('document/unknown')) {
310 $filedetails['type'] = '';
311 }
312 } else {
313 $filedetails['type'] = '';
314 }
315 }
316 if (!empty($options['showdate'])) {
317 if ($mainfile) {
318 // Modified date may be up to several minutes later than uploaded date just because
319 // teacher did not submit the form promptly. Give teacher up to 5 minutes to do it.
320 if ($mainfile->get_timemodified() > $mainfile->get_timecreated() + 5 * MINSECS) {
321 $filedetails['modifieddate'] = $mainfile->get_timemodified();
322 } else {
323 $filedetails['uploadeddate'] = $mainfile->get_timecreated();
324 }
325 if ($mainfile->get_repository_id()) {
326 // If main file is a reference the 'date' attribute can not be cached.
327 $filedetails['isref'] = true;
328 }
329 } else {
330 $filedetails['uploadeddate'] = '';
331 }
332 }
333 }
334 return $filedetails;
335}
336
a2c57666 337/**
338 * Gets optional details for a resource, depending on resource settings.
339 *
340 * Result may include the file size and type if those settings are chosen,
341 * or blank if none.
342 *
9dfe8704 343 * @param object $resource Resource table row (only property 'displayoptions' is used here)
a2c57666 344 * @param object $cm Course-module table row
345 * @return string Size and type or empty string if show options are not enabled
346 */
347function resource_get_optional_details($resource, $cm) {
348 global $DB;
349
350 $details = '';
351
9dfe8704 352 $options = empty($resource->displayoptions) ? array() : @unserialize($resource->displayoptions);
ba8b641d 353 if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) {
9dfe8704
MG
354 if (!array_key_exists('filedetails', $options)) {
355 $filedetails = resource_get_file_details($resource, $cm);
356 } else {
357 $filedetails = $options['filedetails'];
358 }
a2c57666 359 $size = '';
360 $type = '';
ba8b641d
AH
361 $date = '';
362 $langstring = '';
363 $infodisplayed = 0;
9dfe8704
MG
364 if (!empty($options['showsize'])) {
365 if (!empty($filedetails['size'])) {
366 $size = display_size($filedetails['size']);
367 $langstring .= 'size';
368 $infodisplayed += 1;
0b2bfbd1 369 }
a2c57666 370 }
9dfe8704
MG
371 if (!empty($options['showtype'])) {
372 if (!empty($filedetails['type'])) {
373 $type = $filedetails['type'];
374 $langstring .= 'type';
375 $infodisplayed += 1;
a2c57666 376 }
ba8b641d 377 }
9dfe8704
MG
378 if (!empty($options['showdate']) && (!empty($filedetails['modifieddate']) || !empty($filedetails['uploadeddate']))) {
379 if (!empty($filedetails['modifieddate'])) {
380 $date = get_string('modifieddate', 'mod_resource', userdate($filedetails['modifieddate'],
381 get_string('strftimedatetimeshort', 'langconfig')));
382 } else if (!empty($filedetails['uploadeddate'])) {
383 $date = get_string('uploadeddate', 'mod_resource', userdate($filedetails['uploadeddate'],
384 get_string('strftimedatetimeshort', 'langconfig')));
ba8b641d
AH
385 }
386 $langstring .= 'date';
387 $infodisplayed += 1;
a2c57666 388 }
389
ba8b641d
AH
390 if ($infodisplayed > 1) {
391 $details = get_string("resourcedetails_{$langstring}", 'resource',
392 (object)array('size' => $size, 'type' => $type, 'date' => $date));
a2c57666 393 } else {
ba8b641d
AH
394 // Only one of size, type and date is set, so just append.
395 $details = $size . $type . $date;
a2c57666 396 }
397 }
398
399 return $details;
400}
401
aa54ed7b 402/**
403 * Print resource introduction.
404 * @param object $resource
405 * @param object $cm
406 * @param object $course
407 * @param bool $ignoresettings print even if not specified in modedit
408 * @return void
409 */
410function resource_print_intro($resource, $cm, $course, $ignoresettings=false) {
411 global $OUTPUT;
412
413 $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
a2c57666 414
415 $extraintro = resource_get_optional_details($resource, $cm);
416 if ($extraintro) {
417 // Put a paragaph tag around the details
418 $extraintro = html_writer::tag('p', $extraintro, array('class' => 'resourcedetails'));
419 }
420
421 if ($ignoresettings || !empty($options['printintro']) || $extraintro) {
422 $gotintro = trim(strip_tags($resource->intro));
423 if ($gotintro || $extraintro) {
aa54ed7b 424 echo $OUTPUT->box_start('mod_introbox', 'resourceintro');
a2c57666 425 if ($gotintro) {
426 echo format_module_intro('resource', $resource, $cm->id);
427 }
428 echo $extraintro;
aa54ed7b 429 echo $OUTPUT->box_end();
430 }
431 }
432}
433
434/**
435 * Print warning that instance not migrated yet.
436 * @param object $resource
437 * @param object $cm
438 * @param object $course
439 * @return void, does not return
440 */
441function resource_print_tobemigrated($resource, $cm, $course) {
442 global $DB, $OUTPUT;
443
c6c9a3bc 444 $resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id));
aa54ed7b 445 resource_print_header($resource, $cm, $course);
446 resource_print_heading($resource, $cm, $course);
447 resource_print_intro($resource, $cm, $course);
c6c9a3bc 448 echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type));
aa54ed7b 449 echo $OUTPUT->footer();
450 die;
451}
452
453/**
454 * Print warning that file can not be found.
455 * @param object $resource
456 * @param object $cm
457 * @param object $course
458 * @return void, does not return
459 */
460function resource_print_filenotfound($resource, $cm, $course) {
461 global $DB, $OUTPUT;
462
c6c9a3bc 463 $resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id));
aa54ed7b 464 resource_print_header($resource, $cm, $course);
465 resource_print_heading($resource, $cm, $course);
466 resource_print_intro($resource, $cm, $course);
498c722d
DM
467 if ($resource_old) {
468 echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type));
469 } else {
470 echo $OUTPUT->notification(get_string('filenotfound', 'resource'));
471 }
aa54ed7b 472 echo $OUTPUT->footer();
473 die;
474}
475
476/**
df0671f0 477 * Decide the best display format.
aa54ed7b 478 * @param object $resource
479 * @return int display type constant
480 */
481function resource_get_final_display_type($resource) {
ae7f35b9 482 global $CFG, $PAGE;
aa54ed7b 483
484 if ($resource->display != RESOURCELIB_DISPLAY_AUTO) {
485 return $resource->display;
486 }
487
f79321f1
DC
488 if (empty($resource->mainfile)) {
489 return RESOURCELIB_DISPLAY_DOWNLOAD;
490 } else {
491 $mimetype = mimeinfo('type', $resource->mainfile);
492 }
aa54ed7b 493
ae7f35b9 494 if (file_mimetype_in_typegroup($mimetype, 'archive')) {
aa54ed7b 495 return RESOURCELIB_DISPLAY_DOWNLOAD;
496 }
df0671f0 497 if (file_mimetype_in_typegroup($mimetype, array('web_image', '.htm', 'web_video', 'web_audio'))) {
aa54ed7b 498 return RESOURCELIB_DISPLAY_EMBED;
499 }
500
501 // let the browser deal with it somehow
502 return RESOURCELIB_DISPLAY_OPEN;
503}
504
505/**
506 * File browsing support class
507 */
508class resource_content_file_info extends file_info_stored {
509 public function get_parent() {
510 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
511 return $this->browser->get_file_info($this->context);
512 }
513 return parent::get_parent();
514 }
515 public function get_visible_name() {
516 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
517 return $this->topvisiblename;
518 }
519 return parent::get_visible_name();
520 }
521}
f79321f1
DC
522
523function resource_set_mainfile($data) {
524 global $DB;
525 $fs = get_file_storage();
526 $cmid = $data->coursemodule;
527 $draftitemid = $data->files;
528
bf0f06b1 529 $context = context_module::instance($cmid);
f79321f1 530 if ($draftitemid) {
1fad6ff3
SL
531 $options = array('subdirs' => true, 'embed' => false);
532 if ($data->display == RESOURCELIB_DISPLAY_EMBED) {
533 $options['embed'] = true;
534 }
535 file_save_draft_area_files($draftitemid, $context->id, 'mod_resource', 'content', 0, $options);
f79321f1 536 }
64f93798 537 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder', false);
f79321f1
DC
538 if (count($files) == 1) {
539 // only one file attached, set it as main file automatically
540 $file = reset($files);
64f93798 541 file_set_sortorder($context->id, 'mod_resource', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
f79321f1
DC
542 }
543}