Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

Manager for media files

Copyright: 2016 Marina Glancy
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 479 lines (18 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

core_media_manager:: (15 methods):
  instance()
  __construct()
  setup()
  reset_caches()
  get_players()
  embed_url()
  embed_alternatives()
  fallback_to_link()
  can_embed_url()
  can_embed_urls()
  get_embeddable_markers()
  split_alternatives()
  get_extension()
  get_filename()
  get_mimetype()


Class: core_media_manager  - X-Ref

Manager for media files.

Used in file resources, media filter, and any other places that need to
output embedded media.

Usage:
$manager = core_media_manager::instance();


instance($page = null)   X-Ref
Returns a singleton instance of a manager

Note as of Moodle 3.3, this will call setup for you.

return: core_media_manager

__construct($page)   X-Ref
Construct a new core_media_manager instance

param: moodle_page $page The page we are going to add requirements to.

setup()   X-Ref


reset_caches()   X-Ref
Resets cached singleton instance. To be used after $CFG->media_plugins_sortorder is modified


get_players()   X-Ref
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: core_media_player[] Array of core_media_player objects in rank order

embed_url(moodle_url $url, $name = '', $width = 0, $height = 0,$options = array()   X-Ref
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

embed_alternatives($alternatives, $name = '', $width = 0, $height = 0,$options = array()   X-Ref
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

fallback_to_link($urls, $name, $options)   X-Ref
Returns links to the specified URLs unless OPTION_NO_LINK is passed.

param: array $urls URLs of media files
param: string $name Display name; '' to use default
param: array $options Options array
return: string HTML code for embed

can_embed_url(moodle_url $url, $options = array()   X-Ref
Checks whether a file can be embedded. If this returns true you will get
an embedded player; if this returns false, you will just get a download
link.

This is a wrapper for can_embed_urls.

param: moodle_url $url URL of media file
param: array $options Options (same as when embedding)
return: bool True if file can be embedded

can_embed_urls(array $urls, $options = array()   X-Ref
Checks whether a file can be embedded. If this returns true you will get
an embedded player; if this returns false, you will just get a download
link.

param: array $urls URL of media file and any alternatives (moodle_url)
param: array $options Options (same as when embedding)
return: bool True if file can be embedded

get_embeddable_markers()   X-Ref
Obtains a list of markers that can be used in a regular expression when
searching for URLs that can be embedded by any player type.

This string is used to improve peformance of regex matching by ensuring
that the (presumably C) regex code can do a quick keyword check on the
URL part of a link to see if it matches one of these, rather than having
to go into PHP code for every single link to see if it can be embedded.

return: string String suitable for use in regex such as '(\.mp4|\.flv)'

split_alternatives($combinedurl, &$width, &$height)   X-Ref
Given a string containing multiple URLs separated by #, this will split
it into an array of moodle_url objects suitable for using when calling
embed_alternatives.

Note that the input string should NOT be html-escaped (i.e. if it comes
from html, call html_entity_decode first).

param: string $combinedurl String of 1 or more alternatives separated by #
param: int $width Output variable: width (will be set to 0 if not specified)
param: int $height Output variable: height (0 if not specified)
return: array Array of 1 or more moodle_url objects

get_extension(moodle_url $url)   X-Ref
Returns the file extension for a URL.

param: moodle_url $url URL

get_filename(moodle_url $url)   X-Ref
Obtains the filename from the moodle_url.

param: moodle_url $url URL
return: string Filename only (not escaped)

get_mimetype(moodle_url $url)   X-Ref
Guesses MIME type for a moodle_url based on file extension.

param: moodle_url $url URL
return: string MIME type