Commit daefd6eb authored by sam marshall's avatar sam marshall
Browse files

MDL-29624 Media embedding system, part 1: new API and filter changes

Includes new API in medialib.php and core_media_renderer in outputrenderers.php.
Enable/disable settings moved from filter to systemwide appearance page.
Filter changed to use new API.

AMOS BEGIN
 MOV [flashanimation,filter_mediaplugin],[flashanimation,core_media]
 MOV [flashanimation_help,filter_mediaplugin],[flashanimation_desc,core_media]
 MOV [flashvideo,filter_mediaplugin],[flashvideo,core_media]
 MOV [flashvideo_help,filter_mediaplugin],[flashvideo_desc,core_media]
 MOV [html5audio,filter_mediaplugin],[html5audio,core_media]
 MOV [html5audio_help,filter_mediaplugin],[html5audio_desc,core_media]
 MOV [html5video,filter_mediaplugin],[html5video,core_media]
 MOV [html5video_help,filter_mediaplugin],[html5video_desc,core_media]
 MOV [mp3audio,filter_mediaplugin],[mp3audio,core_media]
 MOV [mp3audio_help,filter_mediaplugin],[mp3audio_desc,core_media]
 MOV [legacyquicktime,filter_mediaplugin],[legacyquicktime,core_media]
 MOV [legacyquicktime_help,filter_mediaplugin],[legacyquicktime_desc,core_media]
 MOV [legacyreal,filter_mediaplugin],[legacyreal,core_media]
 MOV [legacyreal_help,filter_mediaplugin],[legacyreal_desc,core_media]
 MOV [legacywmp,filter_mediaplugin],[legacywmp,core_media]
 MOV [legacywmp_help,filter_mediaplugin],[legacywmp_desc,core_media]
 MOV [legacyheading,filter_mediaplugin],[legacyheading,core_media]
 MOV [legacyheading_help,filter_mediaplugin],[legacyheading_desc,core_media]
 MOV [sitevimeo,filter_mediaplugin],[sitevimeo,core_media]
 MOV [sitevimeo_help,filter_mediaplugin],[sitevimeo_desc,core_media]
 MOV [siteyoutube,filter_mediaplugin],[siteyoutube,core_media]
 MOV [siteyoutube_help,filter_mediaplugin],[siteyoutube_desc,core_media]
AMOS END
parent ffe4de97
......@@ -115,6 +115,49 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page
$ADMIN->add('appearance', new admin_externalpage('resetemoticons', new lang_string('emoticonsreset', 'admin'),
new moodle_url('/admin/resetemoticons.php'), 'moodle/site:config', true));
// The "media" subpage.
$temp = new admin_settingpage('mediasettings', get_string('mediasettings', 'core_media'));
$temp->add(new admin_setting_heading('mediaformats', get_string('mediaformats', 'core_media'),
format_text(get_string('mediaformats_desc', 'core_media'), FORMAT_MARKDOWN)));
// External services.
$temp->add(new admin_setting_configcheckbox('core_media_enable_youtube',
get_string('siteyoutube', 'core_media'), get_string('siteyoutube_desc', 'core_media'), 1));
$temp->add(new admin_setting_configcheckbox('core_media_enable_vimeo',
get_string('sitevimeo', 'core_media'), get_string('sitevimeo_desc', 'core_media'), 0));
// Options which require Flash.
$temp->add(new admin_setting_configcheckbox('core_media_enable_mp3',
get_string('mp3audio', 'core_media'), get_string('mp3audio_desc', 'core_media'), 1));
$temp->add(new admin_setting_configcheckbox('core_media_enable_flv',
get_string('flashvideo', 'core_media'), get_string('flashvideo_desc', 'core_media'), 1));
$temp->add(new admin_setting_configcheckbox('core_media_enable_swf',
get_string('flashanimation', 'core_media'), get_string('flashanimation_desc', 'core_media'), 1));
// HTML 5 media.
// Audio now enabled by default so that it can provide a fallback for mp3 on devices without flash.
$temp->add(new admin_setting_configcheckbox('core_media_enable_html5audio',
get_string('html5audio', 'core_media'), get_string('html5audio_desc', 'core_media'), 1));
// Video now enabled by default so it can provide mp4 support.
$temp->add(new admin_setting_configcheckbox('core_media_enable_html5video',
get_string('html5video', 'core_media'), get_string('html5video_desc', 'core_media'), 1));
// Legacy players.
$temp->add(new admin_setting_heading('legacymediaformats',
get_string('legacyheading', 'core_media'), get_string('legacyheading_desc', 'core_media')));
$temp->add(new admin_setting_configcheckbox('core_media_enable_qt',
get_string('legacyquicktime', 'core_media'), get_string('legacyquicktime_desc', 'core_media'), 1));
$temp->add(new admin_setting_configcheckbox('core_media_enable_wmp',
get_string('legacywmp', 'core_media'), get_string('legacywmp_desc', 'core_media'), 1));
$temp->add(new admin_setting_configcheckbox('core_media_enable_rm',
get_string('legacyreal', 'core_media'), get_string('legacyreal_desc', 'core_media'), 1));
$ADMIN->add('appearance', $temp);
// "documentation" settingpage
$temp = new admin_settingpage('documentation', new lang_string('moodledocs'));
$temp->add(new admin_setting_configtext('docroot', new lang_string('docroot', 'admin'), new lang_string('configdocroot', 'admin'), 'http://docs.moodle.org', PARAM_URL));
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
......
......@@ -33,5 +33,26 @@ function xmldb_filter_mediaplugin_upgrade($oldversion) {
$dbman = $DB->get_manager();
if ($oldversion < 2011121200) {
// Move all the media enable setttings that are now handled by core media renderer.
foreach (array('html5video', 'html5audio', 'mp3', 'flv', 'wmp', 'qt', 'rm',
'youtube', 'vimeo', 'swf') as $type) {
$existingkey = 'filter_mediaplugin_enable_' . $type;
if (array_key_exists($existingkey, $CFG)) {
set_config('core_media_enable_' . $type, $CFG->{$existingkey});
unset_config($existingkey);
}
}
// Override setting for html5 to turn it on (previous default was off; because
// of changes in the way fallbacks are handled, this is now unlikely to cause
// a problem, and is required for mobile a/v support on non-Flash devices, so
// this change is basically needed in order to maintain existing behaviour).
set_config('core_media_enable_html5video', 1);
set_config('core_media_enable_html5audio', 1);
upgrade_plugin_savepoint(true, 2011121200, 'filter', 'mediaplugin');
}
return true;
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Media filter performance test script.
*
* For developer test usage only. This can be used to compare performance if
* there are changes to the system in future.
*
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package filter_mediaplugin
*/
require(dirname(__FILE__) . '/../../../config.php');
require_once($CFG->dirroot . '/filter/mediaplugin/filter.php');
// Only available to site admins.
require_login();
if (!is_siteadmin()) {
print_error('nopermissions', 'error', '', 'perftest');
}
// Set up page.
$PAGE->set_context(context_system::instance());
$PAGE->set_url(new moodle_url('/filter/mediaplugin/perftest.php'));
$PAGE->set_heading($SITE->fullname);
print $OUTPUT->header();
// Hack setup to enable all players.
$CFG->core_media_enable_youtube = 1;
$CFG->core_media_enable_vimeo = 1;
$CFG->core_media_enable_mp3 = 1;
$CFG->core_media_enable_flv = 1;
$CFG->core_media_enable_swf = 1;
$CFG->core_media_enable_html5audio = 1;
$CFG->core_media_enable_html5video = 1;
$CFG->core_media_enable_qt = 1;
$CFG->core_media_enable_wmp = 1;
$CFG->core_media_enable_rm = 1;
$CFG->filter_mediaplugin_enable_youtube = 1;
$CFG->filter_mediaplugin_enable_vimeo = 1;
$CFG->filter_mediaplugin_enable_mp3 = 1;
$CFG->filter_mediaplugin_enable_flv = 1;
$CFG->filter_mediaplugin_enable_swf = 1;
$CFG->filter_mediaplugin_enable_html5audio = 1;
$CFG->filter_mediaplugin_enable_html5video = 1;
$CFG->filter_mediaplugin_enable_qt = 1;
$CFG->filter_mediaplugin_enable_wmp = 1;
$CFG->filter_mediaplugin_enable_rm = 1;
// Create plugin.
$filterplugin = new filter_mediaplugin(null, array());
// Note: As this is a developer test page, language strings are not used: all
// text is English-only.
/**
* Starts time counter.
*/
function filter_mediaplugin_perf_start() {
global $filter_mediaplugin_starttime;
$filter_mediaplugin_starttime = microtime(true);
}
/**
* Ends and displays time counter.
* @param string $name Counter name to display
*/
function filter_mediaplugin_perf_stop($name) {
global $filter_mediaplugin_starttime;
$time = microtime(true) - $filter_mediaplugin_starttime;
echo html_writer::tag('li', $name . ': ' . html_writer::tag('strong', round($time, 2)) . 'ms');
}
// 1) Some sample text strings.
// Note: These are from a random sample of real forum data. Just in case there
// are any privacy concerns I have altered names as may be clear.
$samples = array(
"<p>Hi,</p>&#13;\n<p>I've got myself 2 Heaney's \"The Burial at Thebes\"</p>",
"best mark iv heard so far v v good",
"<p>I have a script draft anyone want to look at it?",
"<p>Thanks for your input Legolas and Ghimli!</p>",
"<p>Just to say that I'm thinking of those of you who are working on TMA02.</p>",
"<p><strong>1.</strong> <strong>If someone asks you 'where do you come from?'</strong></p>",
"<p>With regards to Aragorn's question 'what would we do different'?</p>&#13;\n",
"<p>Just thought I'd drop a line to see how everyone is managing generally?</p>&#13;\n",
"<p>Feb '12 - Oct '12  AA100</p>&#13;\n<p>Nov '12 - April '13 - A150</p>&#13;\n",
"<p>So where does that leave the bible???</p>",
);
// 2) Combine sample text strings into one really big (20KB) string.
$length = 0;
$bigstring = '';
$index = 0;
while ($length < 20 * 1024) {
$bigstring .= $samples[$index];
$length += strlen($samples[$index]);
$index++;
if ($index >= count($samples)) {
$index = 0;
}
}
// 3) Make random samples from this. I did the following stats on recent forum
// posts:
// 0-199 characters approx 30%
// 200-1999 approx 60%
// 2000-19999 approx 10%.
$samplebank = array();
foreach (array(100 => 300, 1000 => 600, 10000 => 100) as $chars => $num) {
for ($i = 0; $i < $num; $i++) {
$start = rand(0, $length - $chars - 1);
$samplebank[] = substr($bigstring, $start, $chars);
}
}
echo html_writer::start_tag('ul');
// First test: filter text that doesn't have any links.
filter_mediaplugin_perf_start();
foreach ($samplebank as $sample) {
$filterplugin->filter($sample);
}
filter_mediaplugin_perf_stop('No links');
// Second test: filter text with one link added (that doesn't match).
$link = '<a href="http://www.example.org/another/link/">Link</a>';
$linksamples = array();
foreach ($samplebank as $sample) {
// Make it the same length but with $link replacing the end part.
$linksamples[] = substr($sample, 0, -strlen($link)) . $link;
}
filter_mediaplugin_perf_start();
foreach ($linksamples as $sample) {
$filterplugin->filter($sample);
}
filter_mediaplugin_perf_stop('One link (no match)');
// Third test: filter text with one link added that does match (mp3).
$link = '<a href="http://www.example.org/another/file.mp3">MP3 audio</a>';
$linksamples = array();
foreach ($samplebank as $sample) {
// Make it the same length but with $link replacing the end part.
$linksamples[] = substr($sample, 0, -strlen($link)) . $link;
}
filter_mediaplugin_perf_start();
foreach ($linksamples as $sample) {
$filterplugin->filter($sample);
}
filter_mediaplugin_perf_stop('One link (mp3)');
// End page.
echo html_writer::end_tag('ul');
print $OUTPUT->footer();
\ No newline at end of file
This diff is collapsed.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Mediaplugin filter settings
*
* @package filter
* @subpackage mediaplugin
* @copyright 2017 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
if ($ADMIN->fulltree) {
// External services
$settings->add(new admin_setting_configcheckbox('filter_mediaplugin_enable_youtube', get_string('siteyoutube','filter_mediaplugin'), get_string('siteyoutube_help','filter_mediaplugin'), 1));
$settings->add(new admin_setting_configcheckbox('filter_mediaplugin_enable_vimeo', get_string('sitevimeo','filter_mediaplugin'), get_string('sitevimeo_help','filter_mediaplugin'), 0));
// these require flash
$settings->add(new admin_setting_configcheckbox('filter_mediaplugin_enable_mp3', get_string('mp3audio','filter_mediaplugin'), get_string('mp3audio_help','filter_mediaplugin'), 1));
$settings->add(new admin_setting_configcheckbox('filter_mediaplugin_enable_flv', get_string('flashvideo','filter_mediaplugin'), get_string('flashvideo_help','filter_mediaplugin'), 1));
$settings->add(new admin_setting_configcheckbox('filter_mediaplugin_enable_swf', get_string('flashanimation','filter_mediaplugin'), get_string('flashanimation_help','filter_mediaplugin'), 1));
// HTML 5 media
$settings->add(new admin_setting_configcheckbox('filter_mediaplugin_enable_html5audio', get_string('html5audio','filter_mediaplugin'), get_string('html5audio_help','filter_mediaplugin'), 0)); // disabled because mp3 is much better choice
$settings->add(new admin_setting_configcheckbox('filter_mediaplugin_enable_html5video', get_string('html5video','filter_mediaplugin'), get_string('html5video_help','filter_mediaplugin'), 0)); // disabled because flv with html5 fallback works better
// legacy players
$settings->add(new admin_setting_heading('legacymediaformats', get_string('legacyheading', 'filter_mediaplugin'), get_string('legacyheading_help', 'filter_mediaplugin')));
$settings->add(new admin_setting_configcheckbox('filter_mediaplugin_enable_qt', get_string('legacyquicktime','filter_mediaplugin'), get_string('legacyquicktime_help','filter_mediaplugin'), 1));
$settings->add(new admin_setting_configcheckbox('filter_mediaplugin_enable_wmp', get_string('legacywmp','filter_mediaplugin'), get_string('legacywmp_help','filter_mediaplugin'), 1));
$settings->add(new admin_setting_configcheckbox('filter_mediaplugin_enable_rm', get_string('legacyreal','filter_mediaplugin'), get_string('legacyreal_help','filter_mediaplugin'), 1));
}
......@@ -25,28 +25,4 @@
$string['fallbackaudio'] = 'Audio link';
$string['fallbackvideo'] = 'Video link';
$string['filtername'] = 'Multimedia plugins';
$string['flashanimation'] = 'Flash animation';
$string['flashanimation_help'] = 'Files with extension *.swf. For security reasons this filter is used only in trusted texts.';
$string['flashvideo'] = 'Flash video';
$string['flashvideo_help'] = 'Files with extension *.flv and *.f4v. Plays video clips using Flowplayer, requires Flash plugin and javascript. Uses HTML 5 video fallback if multiple sources specified.';
$string['html5audio'] = 'HTML 5 audio';
$string['html5audio_help'] = 'Audio files with extension *.ogg, *.aac and others. It is compatible with latest web browsers only, unfortunately there is no format that is supported by all browsers.
Workaround is to specify fallbacks separated with # (ex: http://example.org/audio.aac#http://example.org/audio.aac#http://example.org/audio.mp3#), QuickTime player is used as a fallback for old browsers, fallback can be any audio type.';
$string['html5video'] = 'HTML 5 video';
$string['html5video_help'] = 'Video files with extension *.webm, *.m4v, *.ogv, *.mp4 and others. It is compatible with latest web browsers only, unfortunately there is no format that is supported by all browsers.
Workaround is to specify fallbacks sources separated with # (ex: http://example.org/video.m4v#http://example.org/video.aac#http://example.org/video.ogv#d=640x480), QuickTime player is used as a fallback for old browsers.';
$string['mp3audio'] = 'MP3 audio';
$string['mp3audio_help'] = 'Files with extension *.mp3. Plays audio using Flowplayer, requires Flash plugin.';
$string['legacyquicktime'] = 'QuickTime player';
$string['legacyquicktime_help'] = 'Files with extension *.mov, *.mp4, *.m4a, *.mp4 and *.mpg. Requires QuickTime player or codecs.';
$string['legacyreal'] = 'Real media player';
$string['legacyreal_help'] = 'Files with extension *.rm, *.ra, *.ram, *.rp, *.rv. Requires RealPlayer.';
$string['legacywmp'] = 'Windows media player';
$string['legacywmp_help'] = 'Files with extension *.avi and *.wmv. Fully compatible with Internet Explorer in Windows, may be problematic in other browsers or operating systems.';
$string['legacyheading'] = 'Legacy media players';
$string['legacyheading_help'] = 'Following formats are not recommended for general usage, they are usually used in intranet installation with centrally managed clients.';
$string['sitevimeo'] = 'Vimeo';
$string['sitevimeo_help'] = 'Vimeo video sharing site.';
$string['siteyoutube'] = 'YouTube';
$string['siteyoutube_help'] = 'YouTube video sharing site, video and playlist links supported.';
......@@ -37,16 +37,16 @@ class filter_mediaplugin_testcase extends advanced_testcase {
$this->resetAfterTest(true);
// we need to enable the plugins somehow
$CFG->filter_mediaplugin_enable_youtube = 1;
$CFG->filter_mediaplugin_enable_vimeo = 1;
$CFG->filter_mediaplugin_enable_mp3 = 1;
$CFG->filter_mediaplugin_enable_flv = 1;
$CFG->filter_mediaplugin_enable_swf = 1;
$CFG->filter_mediaplugin_enable_html5audio = 1;
$CFG->filter_mediaplugin_enable_html5video = 1;
$CFG->filter_mediaplugin_enable_qt = 1;
$CFG->filter_mediaplugin_enable_wmp = 1;
$CFG->filter_mediaplugin_enable_rm = 1;
$CFG->core_media_enable_youtube = 1;
$CFG->core_media_enable_vimeo = 1;
$CFG->core_media_enable_mp3 = 1;
$CFG->core_media_enable_flv = 1;
$CFG->core_media_enable_swf = 1;
$CFG->core_media_enable_html5audio = 1;
$CFG->core_media_enable_html5video = 1;
$CFG->core_media_enable_qt = 1;
$CFG->core_media_enable_wmp = 1;
$CFG->core_media_enable_rm = 1;
$filterplugin = new filter_mediaplugin(null, array());
......
......@@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2011112900; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2011112900; // Requires this Moodle version
$plugin->version = 2011121200; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2011120500; // Requires this Moodle version
$plugin->component = 'filter_mediaplugin'; // Full name of the plugin (used for diagnostics)
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Language strings for media embedding.
* @package core
* @subpackage media
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['flashanimation'] = 'Flash animation';
$string['flashanimation_desc'] = 'Files with extension *.swf. For security reasons this format is only embedded within trusted text.';
$string['flashvideo'] = 'Flash video';
$string['flashvideo_desc'] = 'Files with extension *.flv and *.f4v. Plays video clips using Flowplayer, requires Flash plugin and javascript.';
$string['html5audio'] = 'HTML 5 audio';
$string['html5audio_desc'] = 'Audio files with extension *.ogg, *.aac and *.mp3. Used primarily for mobile devices. (Format support depends on browser.)';
$string['html5video'] = 'HTML 5 video';
$string['html5video_desc'] = 'Video files with extension *.webm, *.m4v, *.ogv, *.mp4 and others. Used primarily for mobile devices. (Format support depends on browser.)';
$string['mediaformats'] = 'Available players';
$string['mediaformats_desc'] = 'When players are enabled in these settings, files can be embedded using the media filter (if enabled) or using a File or URL resources with the Embed option. When not enabled, these formats are not embedded and users can manually download or follow links to these resources.
Where two players support the same format, enabling both increases compatibility across different devices such as mobile phones. It is possible to increase compatibility further by providing multiple files in different formats for a single audio or video clip.';
$string['mediasettings'] = 'Media embedding';
$string['mp3audio'] = 'MP3 audio';
$string['mp3audio_desc'] = 'Files with extension *.mp3. Plays audio using Flowplayer, requires Flash plugin.';
$string['legacyquicktime'] = 'QuickTime player';
$string['legacyquicktime_desc'] = 'Files with extension *.mov, *.mp4, *.m4a, *.mp4 and *.mpg. Requires QuickTime player or codecs.';
$string['legacyreal'] = 'Real media player';
$string['legacyreal_desc'] = 'Files with extension *.rm, *.ra, *.ram, *.rp and *.rv. Requires RealPlayer.';
$string['legacywmp'] = 'Windows media player';
$string['legacywmp_desc'] = 'Files with extension *.avi and *.wmv. Fully compatible with Internet Explorer in Windows; may not work in other browsers or operating systems.';
$string['legacyheading'] = 'Legacy media players';
$string['legacyheading_desc'] = 'These players are not frequently used on the Web and require browser plugins that are less widely installed.';
$string['sitevimeo'] = 'Vimeo';
$string['sitevimeo_desc'] = 'Vimeo video sharing site.';
$string['siteyoutube'] = 'YouTube';
$string['siteyoutube_desc'] = 'YouTube video sharing site, video and playlist links supported.';
This diff is collapsed.
......@@ -7707,6 +7707,7 @@ function get_core_subsystems() {
'langconfig' => NULL,
'license' => NULL,
'mathslib' => NULL,
'media' => 'media',
'message' => 'message',
'mimetypes' => NULL,
'mnet' => 'mnet',
......
......@@ -2901,3 +2901,257 @@ class core_renderer_ajax extends core_renderer {
*/
public function heading($text, $level = 2, $classes = 'main', $id = null) {}
}
/**
* Renderer for media files.
*
* Used in file resources, media filter, and any other places that need to
* output embedded media.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_media_renderer extends plugin_renderer_base {
/** @var array Array of available 'player' objects */
private $players;
/** @var string Regex pattern for links which may contain embeddable content */
private $embeddablemarkers;
/**
* Constructor requires medialib.php.
*
* This is needed in the constructor (not later) so that you can use the
* constants and static functions that are defined in core_media class
* before you call renderer functions.
*/
public function __construct() {
global $CFG;
require_once($CFG->libdir . '/medialib.php');
}
/**
* Obtains the list of core_media_player objects currently in use to render
* items.
*
* The list is in rank order (highest first) and does not include players
* which are disabled.
*
* @return array Array of core_media_player objects in rank order
*/
protected function get_players() {
global $CFG;
// Save time by only building the list once
if (!$this->players) {
// Get raw list of players.
$players = $this->get_players_raw();
// Chuck all the ones that are disabled.
foreach ($players as $key => $player) {
if (!$player->is_enabled()) {
unset($players[$key]);
}
}
// Sort in rank order (highest first).
usort($players, array('core_media_player', 'compare_by_rank'));
$this->players = $players;
}
return $this->players;
}
/**
* Obtains a raw list of player objects that includes objects regardless
* of whether they are disabled or not, and without sorting.
*
* You can override this in a subclass if you need to add additional
* players.
*
* The return array is be indexed by player name to make it easier to
* remove players in a subclass.
*
* @return array $players Array of core_media_player objects in any order
*/
protected function get_players_raw() {
return array(
'vimeo' => new core_media_player_vimeo(),
'youtube' => new core_media_player_youtube(),
'youtube_playlist' => new core_media_player_youtube_playlist(),
'html5video' => new core_media_player_html5video(),
'html5audio' => new core_media_player_html5audio(),
'mp3' => new core_media_player_mp3(),
'flv' => new core_media_player_flv(),
'wmp' => new core_media_player_wmp(),
'qt' => new core_media_player_qt(),
'rm' => new core_media_player_rm(),
'swf' => new core_media_player_swf(),
'link' => new core_media_player_link(),
);
}
/**
* Renders a media file (audio or video) using suitable embedded player.
*
* See embed_alternatives function for full description of parameters.
* This function calls through to that one.
*
* When using this function you can also specify width and height in the
* URL by including ?d=100x100 at the end. If specified in the URL, this
* will override the $width and $height parameters.
*
* @param moodle_url $url Full URL of media file
* @param string $name Optional user-readable name to display in download link
* @param int $width Width in pixels (optional)
* @param int $height Height in pixels (optional)
* @param array $options Array of key/value pairs
* @return string HTML content of embed
*/
public function embed_url(moodle_url $url, $name = '', $width = 0, $height = 0,
$options = array()) {
// Get width and height from URL if specified (overrides parameters in
// function call).
$rawurl = $url->out(false);
if (preg_match('/[?#]d=([\d]{1,4}%?)x([\d]{1,4}%?)/', $rawurl, $matches)) {
$width = $matches[1];
$height = $matches[2];
$url = new moodle_url(str_replace($matches[0], '', $rawurl));
}
// Defer to array version of function.
return $this->embed_alternatives(array($url), $name, $width, $height, $options);
}
/**
* Renders media files (audio or video) using suitable embedded player.
* The list of URLs should be alternative versions of the same content in
* multiple formats. If there is only one format it should have a single
* entry.
*
* If the media files are not in a supported format, this will give students
* a download link to each format. The download link uses the filename
* unless you supply the optional name parameter.
*
* Width and height are optional. If specified, these are suggested sizes
* and should be the exact values supplied by the user, if they come from
* user input. These will be treated as relating to the size of the video
* content, not including any player control bar.
*
* For audio files, height will be ignored. For video files, a few formats
* work if you specify only width, but in general if you specify width
* you must specify height as well.
*
* The $options array is passed through to the core_media_player classes
* that render the object tag. The keys can contain values from
* core_media::OPTION_xx.
*
* @param array $alternatives Array of moodle_url to media files
* @param string $name Optional user-readable name to display in download link
* @param int $width Width in pixels (optional)
* @param int $height Height in pixels (optional)
* @param array $options Array of key/value pairs
* @return string HTML content of embed
*/
public function embed_alternatives($alternatives, $name = '', $width = 0, $height = 0,
$options = array()) {
// Get list of player plugins (will also require the library).