Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / question / format / qti2 / qt_common.php
blob26a7fd8a8e260baa4e9b667a39e0b180794bb838
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_extension($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 {
106 require_once($CFG->libdir.'/filelib.php');
107 $media = get_file_url("$courseid/$file");
110 $ismultimedia = false;
111 if (!$isimage = is_image_by_extension($file)) {
112 $ismultimedia = is_multimedia_by_extension($file);
115 // if there is no known width and height, try to get one
116 if ($width == 0) {
117 if ($isimage || is_sizable_multimedia($file)) {
122 // create either an image link or a generic link.
123 // if the moodle multimedia filter is turned on, it'll catch multimedia content in the generic link
124 if (is_image_by_extension($file)) {
125 return "<img src=\"$media\" alt=\"$alt\" width=\"$width\" height=\"$height\" />";
127 else {
128 require_once("$CFG->dirroot/mod/quiz/format/qti/custommediafilter.php");
129 return custom_mediaplugin_filter('<a href="' . $media . '"></a>', $courseid, $width, $height);
134 * determines the x and y size of the given file
136 * @param string $file the filename
137 * @return array looks like array('x'=>171, 'y'=>323), or array('x'=>0, 'y'=>0) if size can't be determined
139 function get_file_dimensions($file) {
140 $imginfo = @getimagesize($file);
141 if ($imginfo !== FALSE) {
142 return array('x'=>$imginfo[0], 'y'=>$imginfo[1]);
143 } else {
144 return array('x'=> 0, 'y'=> 0);