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_extentsion($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://') {
105 } else if ($CFG->slasharguments
) { // Use this method if possible for better caching
106 $media = "{$CFG->wwwroot}/file.php/$courseid/$file";
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
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\" />";
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]);
145 return array('x'=> 0, 'y'=> 0);