Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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_vimeo'
  19   *
  20   * @package   media_vimeo
  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 embeds Vimeo links.
  29   *
  30   * @package   media_vimeo
  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_vimeo_plugin extends core_media_player_external {
  36      protected function embed_external(moodle_url $url, $name, $width, $height, $options) {
  37          $videoid = $this->get_video_id();
  38          $info = s($name);
  39  
  40          // Note: resizing via url is not supported, user can click the fullscreen
  41          // button instead. iframe embedding is not xhtml strict but it is the only
  42          // option that seems to work on most devices.
  43          self::pick_video_size($width, $height);
  44  
  45          $output = <<<OET
  46  <span class="mediaplugin mediaplugin_vimeo">
  47  <iframe title="$info" src="https://player.vimeo.com/video/$videoid"
  48    width="$width" height="$height" frameborder="0"
  49    webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
  50  </span>
  51  OET;
  52  
  53          return $output;
  54      }
  55  
  56      /**
  57       * Get Vimeo video ID.
  58       * @return string
  59       */
  60      protected function get_video_id(): string {
  61          return $this->get_video_id_with_code() ?? $this->matches[1] ?? '';
  62      }
  63  
  64      /**
  65       * Get video id with code.
  66       * @return string|null If NULL then the URL does not contain the code.
  67       */
  68      protected function get_video_id_with_code(): ?string {
  69          $id = $this->matches[2] ?? null;
  70  
  71          if (!empty($id)) {
  72              $code = $this->matches[3] ?? null;
  73              if (!empty($code)) {
  74                  return "{$id}?h={$code}";
  75              }
  76  
  77              return $id;
  78          }
  79  
  80          return null;
  81      }
  82  
  83      /**
  84       * Returns regular expression to match vimeo URLs.
  85       * @return string
  86       */
  87      protected function get_regex() {
  88          // Initial part of link.
  89          $start = '~^https?://vimeo\.com/';
  90          // Middle bit: either 123456789 or 123456789/abdef12345.
  91          $middle = '(([0-9]+)/([0-9a-f]+)|[0-9]+)';
  92          return $start . $middle . core_media_player_external::END_LINK_REGEX_PART;
  93      }
  94  
  95      public function get_embeddable_markers() {
  96          return array('vimeo.com/');
  97      }
  98  
  99      /**
 100       * Default rank
 101       * @return int
 102       */
 103      public function get_rank() {
 104          return 1010;
 105      }
 106  }