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