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