Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Main class for plugin 'media_html5audio'
  19   *
  20   * @package   media_html5audio
  21   * @copyright 2016 Marina Glancy
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Player that creates HTML5 <audio> tag.
  29   *
  30   * @package   media_html5audio
  31   * @copyright 2016 Marina Glancy
  32   * @author    2011 The Open University
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class media_html5audio_plugin extends core_media_player_native {
  36      public function embed($urls, $name, $width, $height, $options) {
  37  
  38          if (array_key_exists(core_media_manager::OPTION_ORIGINAL_TEXT, $options) &&
  39              preg_match('/^<(video|audio)\b/i', $options[core_media_manager::OPTION_ORIGINAL_TEXT], $matches)) {
  40              // We already had media tag, do nothing here.
  41              return $options[core_media_manager::OPTION_ORIGINAL_TEXT];
  42          }
  43  
  44          // Build array of source tags.
  45          $sources = array();
  46          foreach ($urls as $url) {
  47              $params = ['src' => $url];
  48              $ext = core_media_manager::instance()->get_extension($url);
  49              if ($ext !== 'aac') {
  50                  // Some browsers get confused by mimetype on source for AAC files.
  51                  $mimetype = core_media_manager::instance()->get_mimetype($url);
  52                  $params['type'] = $mimetype;
  53              }
  54              $sources[] = html_writer::empty_tag('source', $params);
  55          }
  56  
  57          $sources = implode("\n", $sources);
  58          $title = $this->get_name($name, $urls);
  59          // Escape title but prevent double escaping.
  60          $title = s(preg_replace(['/&amp;/', '/&gt;/', '/&lt;/'], ['&', '>', '<'], $title));
  61  
  62          // Default to not specify size (so it can be changed in css).
  63          $size = '';
  64          if ($width) {
  65              $size = 'width="' . $width . '"';
  66          }
  67  
  68          // We don't want fallback to another player because list_supported_urls() is already smart.
  69          // Otherwise we could end up with nested <audio> tags. Fallback to link only.
  70          $fallback = self::LINKPLACEHOLDER;
  71  
  72          return <<<OET
  73  <audio controls="true" $size class="mediaplugin mediaplugin_html5audio" preload="none" title="$title">
  74  $sources
  75  $fallback
  76  </audio>
  77  OET;
  78      }
  79  
  80      public function get_supported_extensions() {
  81          global $CFG;
  82          require_once($CFG->libdir . '/filelib.php');
  83          return file_get_typegroup('extension', 'html_audio');
  84      }
  85  
  86      public function list_supported_urls(array $urls, array $options = array()) {
  87          $extensions = $this->get_supported_extensions();
  88          $result = array();
  89          foreach ($urls as $url) {
  90              $ext = core_media_manager::instance()->get_extension($url);
  91              if (in_array('.' . $ext, $extensions) && core_useragent::supports_html5($ext)) {
  92                  // Unfortunately html5 video does not handle fallback properly.
  93                  // https://www.w3.org/Bugs/Public/show_bug.cgi?id=10975
  94                  // That means we need to do browser detect and not use html5 on
  95                  // browsers which do not support the given type, otherwise users
  96                  // will not even see the fallback link.
  97                  $result[] = $url;
  98              }
  99          }
 100          return $result;
 101      }
 102  
 103      /**
 104       * Default rank
 105       * @return int
 106       */
 107      public function get_rank() {
 108          return 20;
 109      }
 110  }
 111