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