3 //***********************************
5 //***********************************
6 // This contains code common to mediagonal-modified questions
10 * gets a list of all the media files for the given course
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
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;
35 * determines whether or not a file is an image, based on the file extension
37 * @param string $file the filename
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)) {
50 * determines whether or not a file is a media file, based on the file extension
52 * @param string $file the filename
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)) {
64 * determines whether or not a file is a multimedia file, based on the file extension
66 * @param string $file the filename
69 function is_multimedia_by_extension($file) {
70 $extensionsregex = '/\.(swf|mov|mpg|mpeg|wmf|avi|mpe|flv)$/';
71 if (preg_match($extensionsregex, $file)) {
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
83 function is_sizable_multimedia($file) {
84 $extensionsregex = '/\.(swf)$/';
85 if (preg_match($extensionsregex, $file)) {
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) {
102 // if it's a moodle library file, it will be served through file.php
103 if (substr(strtolower($file), 0, 7) == 'http://') {
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
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\" />";
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]);
144 return array('x'=> 0, 'y'=> 0);