Permalink
Browse files

MDL-29624 Media embedding system, part 3: resource module (and URL, l…

…esson)

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.
  • Loading branch information...
1 parent f4e7ba5 commit 8b7d95b6256cbcd912701db09a732880957a785b @sammarshallou sammarshallou committed Dec 13, 2011
Showing with 29 additions and 310 deletions.
  1. +0 −229 lib/resourcelib.php
  2. +9 −27 mod/lesson/locallib.php
  3. +10 −27 mod/resource/locallib.php
  4. +10 −27 mod/url/locallib.php
View
@@ -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 = '<div class="resourcecontent resourcemp3">';
- $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 .= '</div>';
-
- 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('&amp;', '&', $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 = '<div class="resourcecontent resourceflv">';
- $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 .= '</div>';
-
- 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 = <<<EOT
-<div class="resourcecontent resourceswf">
- <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="$width" height="$height">
- <param name="movie" value="$fullurl" />
- <param name="autoplay" value="true" />
- <param name="loop" value="true" />
- <param name="controller" value="true" />
- <param name="scale" value="aspect" />
- <param name="base" value="." />
-<!--[if !IE]>-->
- <object type="application/x-shockwave-flash" data="$fullurl" width="$width" height="$height">
- <param name="controller" value="true" />
- <param name="autoplay" value="true" />
- <param name="loop" value="true" />
- <param name="scale" value="aspect" />
- <param name="base" value="." />
-<!--<![endif]-->
-$clicktoopen
-<!--[if !IE]>-->
- </object>
-<!--<![endif]-->
- </object>
-</div>
-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 = <<<EOT
-<div class="resourcecontent resourcewmv">
- <object type="video/x-ms-wmv" data="$fullurl">
- <param name="controller" value="true" />
- <param name="autostart" value="true" />
- <param name="src" value="$fullurl" />
- <param name="scale" value="noScale" />
- $clicktoopen
- </object>
-</div>
-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 = <<<EOT
-<div class="resourcecontent resourceqt">
- <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab">
- <param name="src" value="$fullurl" />
- <param name="autoplay" value="true" />
- <param name="loop" value="true" />
- <param name="controller" value="true" />
- <param name="scale" value="aspect" />
-<!--[if !IE]>-->
- <object type="video/quicktime" data="$fullurl">
- <param name="controller" value="true" />
- <param name="autoplay" value="true" />
- <param name="loop" value="true" />
- <param name="scale" value="aspect" />
-<!--<![endif]-->
-$clicktoopen
-<!--[if !IE]>-->
- </object>
-<!--<![endif]-->
- </object>
-</div>
-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 = <<<EOT
-<div class="resourcecontent resourcempeg">
- <object classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsm p2inf.cab#Version=5,1,52,701" type="application/x-oleobject">
- <param name="fileName" value="$fullurl" />
- <param name="autoStart" value="true" />
- <param name="animationatStart" value="true" />
- <param name="transparentatStart" value="true" />
- <param name="showControls" value="true" />
- <param name="Volume" value="-450" />
-<!--[if !IE]>-->
- <object type="video/mpeg" data="$fullurl">
- <param name="controller" value="true" />
- <param name="autostart" value="true" />
- <param name="src" value="$fullurl" />
-<!--<![endif]-->
-$clicktoopen
-<!--[if !IE]>-->
- </object>
-<!--<![endif]-->
- </object>
-</div>
-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 = <<<EOT
-<div class="resourcecontent resourcerm">
- <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" data="$fullurl" width="320" height="240">
- <param name="src" value="$fullurl" />
- <param name="controls" value="All" />
-<!--[if !IE]>-->
- <object type="audio/x-pn-realaudio-plugin" data="$fullurl" width="320" height="240">
- <param name="src" value="$fullurl" />
- <param name="controls" value="All" />
-<!--<![endif]-->
-$clicktoopen
-<!--[if !IE]>-->
- </object>
-<!--<![endif]-->
- </object>
-</div>
-EOT;
-
- return $code;
-}
-
/**
* Returns general link or pdf embedding html.
* @param string $fullurl
@@ -475,7 +247,6 @@ function resourcelib_embed_pdf($fullurl, $title, $clicktoopen) {
return $code;
}
-
/**
* Returns general link or file embedding html.
* @param string $fullurl
View
@@ -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
View
@@ -69,46 +69,29 @@ 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);
} else if ($mimetype === 'application/pdf') {
// 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
View
@@ -307,39 +307,22 @@ function url_display_embed($url, $cm, $course) {
$link = html_writer::tag('a', $fullurl, array('href'=>str_replace('&amp;', '&', $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

0 comments on commit 8b7d95b

Please sign in to comment.