weekly release 2.3dev (blame stronk7 for 0202 mistake)
[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 *
702ab58c
PS
21 * @package mod
22 * @subpackage resource
23 * @copyright 2009 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
aa54ed7b 25 */
26
702ab58c
PS
27defined('MOODLE_INTERNAL') || die;
28
aa54ed7b 29require_once("$CFG->libdir/filelib.php");
30require_once("$CFG->libdir/resourcelib.php");
31require_once("$CFG->dirroot/mod/resource/lib.php");
32
33/**
34 * Redirected to migrated resource if needed,
35 * return if incorrect parameters specified
36 * @param int $oldid
37 * @param int $cmid
38 * @return void
39 */
40function resource_redirect_if_migrated($oldid, $cmid) {
41 global $DB, $CFG;
42
43 if ($oldid) {
44 $old = $DB->get_record('resource_old', array('oldid'=>$oldid));
45 } else {
46 $old = $DB->get_record('resource_old', array('cmid'=>$cmid));
47 }
48
49 if (!$old) {
50 return;
51 }
52
53 redirect("$CFG->wwwroot/mod/$old->newmodule/view.php?id=".$old->cmid);
54}
55
56/**
57 * Display embedded resource file.
58 * @param object $resource
59 * @param object $cm
60 * @param object $course
61 * @param stored_file $file main file
62 * @return does not return
63 */
64function resource_display_embed($resource, $cm, $course, $file) {
65 global $CFG, $PAGE, $OUTPUT;
66
67 $clicktoopen = resource_get_clicktoopen($file, $resource->revision);
68
69 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
64f93798 70 $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
aa54ed7b 71 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
72
73 $mimetype = $file->get_mimetype();
74 $title = $resource->name;
75
fcd2cbaf
PS
76 $extension = resourcelib_get_extension($file->get_filename());
77
aa54ed7b 78 if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) { // It's an image
79 $code = resourcelib_embed_image($fullurl, $title);
80
fcd2cbaf 81 } else if ($mimetype === 'application/pdf') {
b8037d73
MD
82 // PDF document
83 $code = resourcelib_embed_pdf($fullurl, $title, $clicktoopen);
84
fcd2cbaf 85 } else if ($mimetype === 'audio/mp3') {
aa54ed7b 86 // MP3 audio file
87 $code = resourcelib_embed_mp3($fullurl, $title, $clicktoopen);
88
fcd2cbaf 89 } else if ($mimetype === 'video/x-flv' or $extension === 'f4v') {
aa54ed7b 90 // Flash video file
91 $code = resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen);
92
fcd2cbaf 93 } else if ($mimetype === 'application/x-shockwave-flash') {
aa54ed7b 94 // Flash file
95 $code = resourcelib_embed_flash($fullurl, $title, $clicktoopen);
96
fcd2cbaf 97 } else if (substr($mimetype, 0, 10) === 'video/x-ms') {
aa54ed7b 98 // Windows Media Player file
99 $code = resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen);
100
fcd2cbaf 101 } else if ($mimetype === 'video/quicktime') {
aa54ed7b 102 // Quicktime file
103 $code = resourcelib_embed_quicktime($fullurl, $title, $clicktoopen);
104
fcd2cbaf 105 } else if ($mimetype === 'video/mpeg') {
aa54ed7b 106 // Mpeg file
107 $code = resourcelib_embed_mpeg($fullurl, $title, $clicktoopen);
108
fcd2cbaf 109 } else if ($mimetype === 'audio/x-pn-realaudio') {
aa54ed7b 110 // RealMedia file
111 $code = resourcelib_embed_real($fullurl, $title, $clicktoopen);
112
113 } else {
114 // anything else - just try object tag enlarged as much as possible
115 $code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype);
aa54ed7b 116 }
117
118 resource_print_header($resource, $cm, $course);
119 resource_print_heading($resource, $cm, $course);
120
121 echo $code;
122
123 resource_print_intro($resource, $cm, $course);
124
125 echo $OUTPUT->footer();
126 die;
127}
128
129/**
130 * Display resource frames.
131 * @param object $resource
132 * @param object $cm
133 * @param object $course
134 * @param stored_file $file main file
135 * @return does not return
136 */
137function resource_display_frame($resource, $cm, $course, $file) {
138 global $PAGE, $OUTPUT, $CFG;
139
140 $frame = optional_param('frameset', 'main', PARAM_ALPHA);
141
142 if ($frame === 'top') {
78946b9b 143 $PAGE->set_pagelayout('frametop');
aa54ed7b 144 resource_print_header($resource, $cm, $course);
145 resource_print_heading($resource, $cm, $course);
146 resource_print_intro($resource, $cm, $course);
147 echo $OUTPUT->footer();
148 die;
149
150 } else {
151 $config = get_config('resource');
152 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
64f93798 153 $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
aa54ed7b 154 $fileurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
155 $navurl = "$CFG->wwwroot/mod/resource/view.php?id=$cm->id&amp;frameset=top";
156 $title = strip_tags(format_string($course->shortname.': '.$resource->name));
157 $framesize = $config->framesize;
158 $modulename = s(get_string('modulename','resource'));
e372f4c7 159 $dir = get_string('thisdirection', 'langconfig');
aa54ed7b 160
161 $file = <<<EOF
162<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
163<html dir="$dir">
164 <head>
165 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
166 <title>$title</title>
167 </head>
168 <frameset rows="$framesize,*">
169 <frame src="$navurl" title="$modulename" />
170 <frame src="$fileurl" title="$modulename" />
171 </frameset>
172</html>
173EOF;
174
175 @header('Content-Type: text/html; charset=utf-8');
176 echo $file;
177 die;
178 }
179}
180
181/**
182 * Internal function - create click to open text with link.
183 */
184function resource_get_clicktoopen($file, $revision, $extra='') {
185 global $CFG;
186
187 $filename = $file->get_filename();
64f93798 188 $path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename();
aa54ed7b 189 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
190
191 $string = get_string('clicktoopen2', 'resource', "<a href=\"$fullurl\" $extra>$filename</a>");
192
193 return $string;
194}
195
196/**
197 * Internal function - create click to open text with link.
198 */
199function resource_get_clicktodownload($file, $revision) {
200 global $CFG;
201
202 $filename = $file->get_filename();
64f93798 203 $path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename();
aa54ed7b 204 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true);
205
206 $string = get_string('clicktodownload', 'resource', "<a href=\"$fullurl\">$filename</a>");
207
208 return $string;
209}
210
211/**
212 * Print resource info and workaround link when JS not available.
213 * @param object $resource
214 * @param object $cm
215 * @param object $course
216 * @param stored_file $file main file
217 * @return does not return
218 */
219function resource_print_workaround($resource, $cm, $course, $file) {
220 global $CFG, $OUTPUT;
221
222 resource_print_header($resource, $cm, $course);
223 resource_print_heading($resource, $cm, $course, true);
224 resource_print_intro($resource, $cm, $course, true);
225
f79321f1 226 $resource->mainfile = $file->get_filename();
aa54ed7b 227 echo '<div class="resourceworkaround">';
228 switch (resource_get_final_display_type($resource)) {
229 case RESOURCELIB_DISPLAY_POPUP:
64f93798 230 $path = '/'.$file->get_contextid().'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
aa54ed7b 231 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
232 $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
233 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
234 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
235 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
236 $extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
237 echo resource_get_clicktoopen($file, $resource->revision, $extra);
238 break;
239
240 case RESOURCELIB_DISPLAY_NEW:
241 $extra = 'onclick="this.target=\'_blank\'"';
242 echo resource_get_clicktoopen($file, $resource->revision, $extra);
243 break;
244
245 case RESOURCELIB_DISPLAY_DOWNLOAD:
246 echo resource_get_clicktodownload($file, $resource->revision);
247 break;
248
249 case RESOURCELIB_DISPLAY_OPEN:
250 default:
251 echo resource_get_clicktoopen($file, $resource->revision);
252 break;
253 }
254 echo '</div>';
255
256 echo $OUTPUT->footer();
257 die;
258}
259
260/**
261 * Print resource header.
262 * @param object $resource
263 * @param object $cm
264 * @param object $course
265 * @return void
266 */
267function resource_print_header($resource, $cm, $course) {
268 global $PAGE, $OUTPUT;
269
270 $PAGE->set_title($course->shortname.': '.$resource->name);
271 $PAGE->set_heading($course->fullname);
272 $PAGE->set_activity_record($resource);
273 $PAGE->set_button(update_module_button($cm->id, '', get_string('modulename', 'resource')));
ef8869c7 274 echo $OUTPUT->header();
aa54ed7b 275}
276
277/**
278 * Print resource heading.
279 * @param object $resource
280 * @param object $cm
281 * @param object $course
282 * @param bool $ignoresettings print even if not specified in modedit
283 * @return void
284 */
285function resource_print_heading($resource, $cm, $course, $ignoresettings=false) {
286 global $OUTPUT;
287
288 $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
289 if ($ignoresettings or !empty($options['printheading'])) {
290 echo $OUTPUT->heading(format_string($resource->name), 2, 'main', 'resourceheading');
291 }
292}
293
294/**
295 * Print resource introduction.
296 * @param object $resource
297 * @param object $cm
298 * @param object $course
299 * @param bool $ignoresettings print even if not specified in modedit
300 * @return void
301 */
302function resource_print_intro($resource, $cm, $course, $ignoresettings=false) {
303 global $OUTPUT;
304
305 $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
306 if ($ignoresettings or !empty($options['printintro'])) {
307 if (trim(strip_tags($resource->intro))) {
308 echo $OUTPUT->box_start('mod_introbox', 'resourceintro');
309 echo format_module_intro('resource', $resource, $cm->id);
310 echo $OUTPUT->box_end();
311 }
312 }
313}
314
315/**
316 * Print warning that instance not migrated yet.
317 * @param object $resource
318 * @param object $cm
319 * @param object $course
320 * @return void, does not return
321 */
322function resource_print_tobemigrated($resource, $cm, $course) {
323 global $DB, $OUTPUT;
324
c6c9a3bc 325 $resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id));
aa54ed7b 326 resource_print_header($resource, $cm, $course);
327 resource_print_heading($resource, $cm, $course);
328 resource_print_intro($resource, $cm, $course);
c6c9a3bc 329 echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type));
aa54ed7b 330 echo $OUTPUT->footer();
331 die;
332}
333
334/**
335 * Print warning that file can not be found.
336 * @param object $resource
337 * @param object $cm
338 * @param object $course
339 * @return void, does not return
340 */
341function resource_print_filenotfound($resource, $cm, $course) {
342 global $DB, $OUTPUT;
343
c6c9a3bc 344 $resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id));
aa54ed7b 345 resource_print_header($resource, $cm, $course);
346 resource_print_heading($resource, $cm, $course);
347 resource_print_intro($resource, $cm, $course);
498c722d
DM
348 if ($resource_old) {
349 echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type));
350 } else {
351 echo $OUTPUT->notification(get_string('filenotfound', 'resource'));
352 }
aa54ed7b 353 echo $OUTPUT->footer();
354 die;
355}
356
357/**
358 * Decide the best diaply format.
359 * @param object $resource
360 * @return int display type constant
361 */
362function resource_get_final_display_type($resource) {
363 global $CFG;
364
365 if ($resource->display != RESOURCELIB_DISPLAY_AUTO) {
366 return $resource->display;
367 }
368
369 static $download = array('application/zip', 'application/x-tar', 'application/g-zip'); // binary formats
370 static $embed = array('image/gif', 'image/jpeg', 'image/png', 'image/svg+xml', // images
371 'application/x-shockwave-flash', 'video/x-flv', 'video/x-ms-wm', // video formats
fcd2cbaf 372 'video/quicktime', 'video/mpeg', 'video/mp4',
aa54ed7b 373 'audio/mp3', 'audio/x-realaudio-plugin', 'x-realaudio-plugin', // audio formats
374 'application/pdf', 'text/html',
375 );
376
f79321f1
DC
377 if (empty($resource->mainfile)) {
378 return RESOURCELIB_DISPLAY_DOWNLOAD;
379 } else {
380 $mimetype = mimeinfo('type', $resource->mainfile);
381 }
aa54ed7b 382
383 if (in_array($mimetype, $download)) {
384 return RESOURCELIB_DISPLAY_DOWNLOAD;
385 }
386 if (in_array($mimetype, $embed)) {
387 return RESOURCELIB_DISPLAY_EMBED;
388 }
389
390 // let the browser deal with it somehow
391 return RESOURCELIB_DISPLAY_OPEN;
392}
393
394/**
395 * File browsing support class
396 */
397class resource_content_file_info extends file_info_stored {
398 public function get_parent() {
399 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
400 return $this->browser->get_file_info($this->context);
401 }
402 return parent::get_parent();
403 }
404 public function get_visible_name() {
405 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
406 return $this->topvisiblename;
407 }
408 return parent::get_visible_name();
409 }
410}
f79321f1
DC
411
412function resource_set_mainfile($data) {
413 global $DB;
414 $fs = get_file_storage();
415 $cmid = $data->coursemodule;
416 $draftitemid = $data->files;
417
418 $context = get_context_instance(CONTEXT_MODULE, $cmid);
419 if ($draftitemid) {
64f93798 420 file_save_draft_area_files($draftitemid, $context->id, 'mod_resource', 'content', 0, array('subdirs'=>true));
f79321f1 421 }
64f93798 422 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder', false);
f79321f1
DC
423 if (count($files) == 1) {
424 // only one file attached, set it as main file automatically
425 $file = reset($files);
64f93798 426 file_set_sortorder($context->id, 'mod_resource', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
f79321f1
DC
427 }
428}