From 8b7d95b6256cbcd912701db09a732880957a785b Mon Sep 17 00:00:00 2001 From: sam marshall Date: Tue, 13 Dec 2011 17:21:06 +0000 Subject: [PATCH] MDL-29624 Media embedding system, part 3: resource module (and URL, lesson) This change makes the resource module use the new API when it embeds media files (audio/video/Flash), and removes the resourcelib library functions which were previously used for this. The old API is still used for embedding non-media files such as PDFs and HTML. The URL and Lesson module both used copy/paste duplicates of a section of resource module code which called the removed functions. I have changed these too. As a bonus this significantly reduces the duplication involved. --- lib/resourcelib.php | 229 -------------------------------------- mod/lesson/locallib.php | 36 ++---- mod/resource/locallib.php | 37 ++---- mod/url/locallib.php | 37 ++---- 4 files changed, 29 insertions(+), 310 deletions(-) diff --git a/lib/resourcelib.php b/lib/resourcelib.php index 7e3047c5bb5..c2d796f78d7 100644 --- a/lib/resourcelib.php +++ b/lib/resourcelib.php @@ -222,234 +222,6 @@ function resourcelib_embed_image($fullurl, $title) { return $code; } -/** - * Returns mp3 embedding html. - * @param string $fullurl - * @param string $title - * @param string $clicktoopen - * @return string html - */ -function resourcelib_embed_mp3($fullurl, $title, $clicktoopen) { - - if ($fullurl instanceof moodle_url) { - $fullurl = $fullurl->out(false); - } else { - $fullurl = str_replace('&', '&', $fullurl); - } - - $id = 'resource_mp3_'.time(); //we need something unique because it might be stored in text cache - - // note: size is specified in theme, it can be made as wide as necessary, but the height can not be changed - - $output = '
'; - $output .= html_writer::tag('span', $clicktoopen, array('id'=>$id, 'class'=>'resourcemediaplugin resourcemediaplugin_mp3', 'title'=>$title)); - $output .= html_writer::script(js_writer::function_call('M.util.add_audio_player', array($id, $fullurl, false))); - $output .= '
'; - - return $output; -} - -/** - * Returns flash video embedding html. - * @param string $fullurl - * @param string $title - * @param string $clicktoopen - * @return string html - */ -function resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen) { - global $CFG, $PAGE; - - if ($fullurl instanceof moodle_url) { - $fullurl = $fullurl->out(false); - } else { - $fullurl = str_replace('&', '&', $fullurl); - } - - $id = 'resource_flv_'.time(); //we need something unique because it might be stored in text cache - - //note: nobody should be adding any dimensions to themes!!! - - if (preg_match('/\?d=([\d]{1,4}%?)x([\d]{1,4}%?)/', $fullurl, $matches)) { - $width = $matches[1]; - $height = $matches[2]; - $autosize = false; - } else { - $width = 400; - $height = 300; - $autosize = true; - } - $output = '
'; - $output .= html_writer::tag('span', $clicktoopen, array('id'=>$id, 'class'=>'resourcemediaplugin resourcemediaplugin_flv', 'title'=>$title)); - $output .= html_writer::script(js_writer::function_call('M.util.add_video_player', array($id, addslashes_js($fullurl), $width, $height, $autosize))); - $output .= '
'; - - return $output; -} - -/** - * Returns flash embedding html. - * @param string $fullurl - * @param string $title - * @param string $clicktoopen - * @return string html - */ -function resourcelib_embed_flash($fullurl, $title, $clicktoopen) { - if (preg_match('/[#\?]d=([\d]{1,4}%?)x([\d]{1,4}%?)/', $fullurl, $matches)) { - $width = $matches[1]; - $height = $matches[2]; - } else { - $width = 400; - $height = 300; - } - - $code = << - - - - - - - - - - - - - - - -$clicktoopen - - - - - -EOT; - - return $code; -} - -/** - * Returns ms media embedding html. - * @param string $fullurl - * @param string $title - * @param string $clicktoopen - * @return string html - */ -function resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen) { - $code = << - - - - - - $clicktoopen - - -EOT; - - return $code; -} - -/** - * Returns quicktime embedding html. - * @param string $fullurl - * @param string $title - * @param string $clicktoopen - * @return string html - */ -function resourcelib_embed_quicktime($fullurl, $title, $clicktoopen) { - $code = << - - - - - - - - - - - - - -$clicktoopen - - - - - -EOT; - - return $code; -} - -/** - * Returns mpeg embedding html. - * @param string $fullurl - * @param string $title - * @param string $clicktoopen - * @return string html - */ -function resourcelib_embed_mpeg($fullurl, $title, $clicktoopen) { - $code = << - - - - - - - - - - - - - -$clicktoopen - - - - - -EOT; - - return $code; -} - -/** - * Returns real media embedding html. - * @param string $fullurl - * @param string $title - * @param string $clicktoopen - * @return string html - */ -function resourcelib_embed_real($fullurl, $title, $clicktoopen) { - $code = << - - - - - - - - -$clicktoopen - - - - - -EOT; - - return $code; -} - /** * Returns general link or pdf embedding html. * @param string $fullurl @@ -475,7 +247,6 @@ EOT; return $code; } - /** * Returns general link or file embedding html. * @param string $fullurl diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index 0eefb5079d0..13982a81742 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -609,37 +609,19 @@ function lesson_get_media_html($lesson, $context) { $extension = resourcelib_get_extension($url->out(false)); + $mediarenderer = $PAGE->get_renderer('core', 'media'); + $embedoptions = array( + core_media::OPTION_TRUSTED => true, + core_media::OPTION_BLOCK => true + ); + // find the correct type and print it out if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) { // It's an image $code = resourcelib_embed_image($url, $title); - } else if ($mimetype == 'audio/mp3') { - // MP3 audio file - $code = resourcelib_embed_mp3($url, $title, $clicktoopen); - - } else if ($mimetype == 'video/x-flv' or $extension === 'f4v') { - // Flash video file - $code = resourcelib_embed_flashvideo($url, $title, $clicktoopen); - - } else if ($mimetype == 'application/x-shockwave-flash') { - // Flash file - $code = resourcelib_embed_flash($url, $title, $clicktoopen); - - } else if (substr($mimetype, 0, 10) == 'video/x-ms') { - // Windows Media Player file - $code = resourcelib_embed_mediaplayer($url, $title, $clicktoopen); - - } else if ($mimetype == 'video/quicktime') { - // Quicktime file - $code = resourcelib_embed_quicktime($url, $title, $clicktoopen); - - } else if ($mimetype == 'video/mpeg') { - // Mpeg file - $code = resourcelib_embed_mpeg($url, $title, $clicktoopen); - - } else if ($mimetype == 'audio/x-pn-realaudio-plugin') { - // RealMedia file - $code = resourcelib_embed_real($url, $title, $clicktoopen); + } else if ($mediarenderer->can_embed_url($url, $embedoptions)) { + // Media (audio/video) file. + $code = $mediarenderer->embed_url($url, $title, 0, 0, $embedoptions); } else { // anything else - just try object tag enlarged as much as possible diff --git a/mod/resource/locallib.php b/mod/resource/locallib.php index df2b0b638f2..af380035367 100644 --- a/mod/resource/locallib.php +++ b/mod/resource/locallib.php @@ -69,12 +69,19 @@ function resource_display_embed($resource, $cm, $course, $file) { $context = get_context_instance(CONTEXT_MODULE, $cm->id); $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename(); $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false); + $moodleurl = new moodle_url('/pluginfile.php' . $path); $mimetype = $file->get_mimetype(); $title = $resource->name; $extension = resourcelib_get_extension($file->get_filename()); + $mediarenderer = $PAGE->get_renderer('core', 'media'); + $embedoptions = array( + core_media::OPTION_TRUSTED => true, + core_media::OPTION_BLOCK => true, + ); + if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) { // It's an image $code = resourcelib_embed_image($fullurl, $title); @@ -82,33 +89,9 @@ function resource_display_embed($resource, $cm, $course, $file) { // PDF document $code = resourcelib_embed_pdf($fullurl, $title, $clicktoopen); - } else if ($mimetype === 'audio/mp3') { - // MP3 audio file - $code = resourcelib_embed_mp3($fullurl, $title, $clicktoopen); - - } else if ($mimetype === 'video/x-flv' or $extension === 'f4v') { - // Flash video file - $code = resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen); - - } else if ($mimetype === 'application/x-shockwave-flash') { - // Flash file - $code = resourcelib_embed_flash($fullurl, $title, $clicktoopen); - - } else if (substr($mimetype, 0, 10) === 'video/x-ms') { - // Windows Media Player file - $code = resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen); - - } else if ($mimetype === 'video/quicktime') { - // Quicktime file - $code = resourcelib_embed_quicktime($fullurl, $title, $clicktoopen); - - } else if ($mimetype === 'video/mpeg') { - // Mpeg file - $code = resourcelib_embed_mpeg($fullurl, $title, $clicktoopen); - - } else if ($mimetype === 'audio/x-pn-realaudio') { - // RealMedia file - $code = resourcelib_embed_real($fullurl, $title, $clicktoopen); + } else if ($mediarenderer->can_embed_url($moodleurl, $embedoptions)) { + // Media (audio/video) file. + $code = $mediarenderer->embed_url($moodleurl, $title, 0, 0, $embedoptions); } else { // anything else - just try object tag enlarged as much as possible diff --git a/mod/url/locallib.php b/mod/url/locallib.php index 070f0e59413..68664cb36b8 100644 --- a/mod/url/locallib.php +++ b/mod/url/locallib.php @@ -307,39 +307,22 @@ function url_display_embed($url, $cm, $course) { $link = html_writer::tag('a', $fullurl, array('href'=>str_replace('&', '&', $fullurl))); $clicktoopen = get_string('clicktoopen', 'url', $link); + $moodleurl = new moodle_url($fullurl); $extension = resourcelib_get_extension($url->externalurl); + $mediarenderer = $PAGE->get_renderer('core', 'media'); + $embedoptions = array( + core_media::OPTION_TRUSTED => true, + core_media::OPTION_BLOCK => true + ); + if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) { // It's an image $code = resourcelib_embed_image($fullurl, $title); - } else if ($mimetype == 'audio/mp3') { - // MP3 audio file - $code = resourcelib_embed_mp3($fullurl, $title, $clicktoopen); - - } else if ($mimetype == 'video/x-flv' or $extension === 'f4v') { - // Flash video file - $code = resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen); - - } else if ($mimetype == 'application/x-shockwave-flash') { - // Flash file - $code = resourcelib_embed_flash($fullurl, $title, $clicktoopen); - - } else if (substr($mimetype, 0, 10) == 'video/x-ms') { - // Windows Media Player file - $code = resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen); - - } else if ($mimetype == 'video/quicktime') { - // Quicktime file - $code = resourcelib_embed_quicktime($fullurl, $title, $clicktoopen); - - } else if ($mimetype == 'video/mpeg') { - // Mpeg file - $code = resourcelib_embed_mpeg($fullurl, $title, $clicktoopen); - - } else if ($mimetype == 'audio/x-pn-realaudio-plugin') { - // RealMedia file - $code = resourcelib_embed_real($fullurl, $title, $clicktoopen); + } else if ($mediarenderer->can_embed_url($moodleurl, $embedoptions)) { + // Media (audio/video) file. + $code = $mediarenderer->embed_url($moodleurl, $title, 0, 0, $embedoptions); } else { // anything else - just try object tag enlarged as much as possible