Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / question / format / qti2 / qt_common.php
blob10229a2e4af86b04603c87a7d3897c49a88fb100
1 <?php
3 //***********************************
4 // qt_common.php
5 //***********************************
6 // This contains code common to mediagonal-modified questions
7 //
9 /**
10 * gets a list of all the media files for the given course
12 * @param int courseid
13 * @return array containing filenames
14 * @calledfrom type/<typename>/editquestion.php
15 * @package questionbank
16 * @subpackage importexport
18 function get_course_media_files($courseid)
20 // this code lifted from mod/quiz/question.php and modified
21 global $CFG;
22 $images = null;
24 make_upload_directory("$course->id"); // Just in case
25 $coursefiles = get_directory_list("$CFG->dataroot/$courseid", $CFG->moddata);
26 foreach ($coursefiles as $filename) {
27 if (is_media_by_extension($filename)) {
28 $images["$filename"] = $filename;
31 return $images;
34 /**
35 * determines whether or not a file is an image, based on the file extension
37 * @param string $file the filename
38 * @return boolean
40 function is_image_by_extentsion($file) {
41 $extensionsregex = '/\.(gif|jpg|jpeg|jpe|png|tif|tiff|bmp|xbm|rgb|svf)$/';
42 if (preg_match($extensionsregex, $file)) {
43 return true;
45 return false;
49 /**
50 * determines whether or not a file is a media file, based on the file extension
52 * @param string $file the filename
53 * @return boolean
55 function is_media_by_extension($file) {
56 $extensionsregex = '/\.(gif|jpg|jpeg|jpe|png|tif|tiff|bmp|xbm|rgb|svf|swf|mov|mpg|mpeg|wmf|avi|mpe|flv|mp3|ra|ram)$/';
57 if (preg_match($extensionsregex, $file)) {
58 return true;
60 return false;
63 /**
64 * determines whether or not a file is a multimedia file, based on the file extension
66 * @param string $file the filename
67 * @return boolean
69 function is_multimedia_by_extension($file) {
70 $extensionsregex = '/\.(swf|mov|mpg|mpeg|wmf|avi|mpe|flv)$/';
71 if (preg_match($extensionsregex, $file)) {
72 return true;
74 return false;
77 /**
78 * determines whether or not a file is a multimedia file of a type php can get the dimension for, based on the file extension
80 * @param string $file the filename
81 * @return boolean
83 function is_sizable_multimedia($file) {
84 $extensionsregex = '/\.(swf)$/';
85 if (preg_match($extensionsregex, $file)) {
86 return true;
88 return false;
91 /**
92 * creates a media tag to use for choice media
94 * @param string $file the filename
95 * @param string $courseid the course id
96 * @param string $alt to specify the alt tag
97 * @return string either an image tag, or html for an embedded object
99 function get_media_tag($file, $courseid = 0, $alt = 'media file', $width = 0, $height = 0) {
100 global $CFG;
102 // if it's a moodle library file, it will be served through file.php
103 if (substr(strtolower($file), 0, 7) == 'http://') {
104 $media = $file;
105 } else if ($CFG->slasharguments) { // Use this method if possible for better caching
106 $media = "{$CFG->wwwroot}/file.php/$courseid/$file";
107 } else {
108 $media = "{$CFG->wwwroot}/file.php?file=/$courseid/$file";
111 $ismultimedia = false;
112 if (!$isimage = is_image_by_extension($file)) {
113 $ismultimedia = is_multimedia_by_extension($file);
116 // if there is no known width and height, try to get one
117 if ($width == 0) {
118 if ($isimage || is_sizable_multimedia($file)) {
123 // create either an image link or a generic link.
124 // if the moodle multimedia filter is turned on, it'll catch multimedia content in the generic link
125 if (is_image_by_extension($file)) {
126 return "<img src=\"$media\" alt=\"$alt\" width=\"$width\" height=\"$height\" />";
128 else {
129 require_once("$CFG->dirroot/mod/quiz/format/qti/custommediafilter.php");
130 return custom_mediaplugin_filter('<a href="' . $media . '"></a>', $courseid, $width, $height);
135 * determines the x and y size of the given file
137 * @param string $file the filename
138 * @return array looks like array('x'=>171, 'y'=>323), or array('x'=>0, 'y'=>0) if size can't be determined
140 function get_file_dimensions($file) {
141 $imginfo = @getimagesize($file);
142 if ($imginfo !== FALSE) {
143 return array('x'=>$imginfo[0], 'y'=>$imginfo[1]);
144 } else {
145 return array('x'=> 0, 'y'=> 0);