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