Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Defines classes used for plugin info.
  19   *
  20   * @package    core
  21   * @copyright  2016 Marina Glancy
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\plugininfo;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Class for media plugins
  30   *
  31   * @package    core
  32   * @copyright  2016 Marina Glancy
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class media extends base {
  36  
  37      public function is_uninstall_allowed() {
  38          return true;
  39      }
  40  
  41      /**
  42       * Get the name for the settings section.
  43       *
  44       * @return string
  45       */
  46      public function get_settings_section_name() {
  47          return 'mediasetting' . $this->name;
  48      }
  49  
  50      /**
  51       * Load the global settings for a particular availability plugin (if there are any)
  52       *
  53       * @param \part_of_admin_tree $adminroot
  54       * @param string $parentnodename
  55       * @param bool $hassiteconfig
  56       */
  57      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  58          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  59          $ADMIN = $adminroot; // May be used in settings.php.
  60          $plugininfo = $this; // Also can be used inside settings.php
  61          $availability = $this; // Also to be used inside settings.php.
  62  
  63          if (!$this->is_installed_and_upgraded()) {
  64              return;
  65          }
  66  
  67          if (!$hassiteconfig) {
  68              return;
  69          }
  70  
  71          $section = $this->get_settings_section_name();
  72  
  73          $settings = null;
  74          if (file_exists($this->full_path('settings.php'))) {
  75              $settings = new \admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
  76              include($this->full_path('settings.php')); // This may also set $settings to null.
  77          }
  78          if ($settings) {
  79              $ADMIN->add($parentnodename, $settings);
  80          }
  81      }
  82  
  83      /**
  84       * Return URL used for management of plugins of this type.
  85       * @return \moodle_url
  86       */
  87      public static function get_manage_url() {
  88          return new \moodle_url('/admin/settings.php', array('section' => 'managemediaplayers'));
  89      }
  90  
  91      public static function get_enabled_plugins() {
  92          global $CFG;
  93  
  94          $order = (!empty($CFG->media_plugins_sortorder)) ? explode(',', $CFG->media_plugins_sortorder) : [];
  95          if ($order) {
  96              $plugins = \core_plugin_manager::instance()->get_installed_plugins('media');
  97              $order = array_intersect($order, array_keys($plugins));
  98          }
  99          return array_combine($order, $order);
 100      }
 101  
 102      /**
 103       * Sets the current plugin as enabled or disabled
 104       * When enabling tries to guess the sortorder based on default rank returned by the plugin.
 105       * @param bool $newstate
 106       */
 107      public function set_enabled($newstate = true) {
 108          $enabled = self::get_enabled_plugins();
 109          if (array_key_exists($this->name, $enabled) == $newstate) {
 110              // Nothing to do.
 111              return;
 112          }
 113          if ($newstate) {
 114              // Enable media plugin.
 115              $plugins = \core_plugin_manager::instance()->get_plugins_of_type('media');
 116              if (!array_key_exists($this->name, $plugins)) {
 117                  // Can not be enabled.
 118                  return;
 119              }
 120              $rank = $this->get_rank();
 121              $position = 0;
 122              // Insert before the first enabled plugin which default rank is smaller than the default rank of this one.
 123              foreach ($enabled as $playername) {
 124                  if (($player = $plugins[$playername]) && ($rank > $player->get_rank())) {
 125                      break;
 126                  }
 127                  $position++;
 128              }
 129              array_splice($enabled, $position, 0, [$this->name]);
 130              self::set_enabled_plugins($enabled);
 131          } else {
 132              // Disable media plugin.
 133              unset($enabled[$this->name]);
 134              self::set_enabled_plugins($enabled);
 135          }
 136      }
 137  
 138      /**
 139       * Set the list of enabled media players in the specified sort order
 140       * To be used when changing settings or in unit tests
 141       * @param string|array $list list of plugin names without frankenstyle prefix - comma-separated string or an array
 142       */
 143      public static function set_enabled_plugins($list) {
 144          if (empty($list)) {
 145              $list = [];
 146          } else if (!is_array($list)) {
 147              $list = explode(',', $list);
 148          }
 149          if ($list) {
 150              $plugins = \core_plugin_manager::instance()->get_installed_plugins('media');
 151              $list = array_intersect($list, array_keys($plugins));
 152          }
 153          set_config('media_plugins_sortorder', join(',', $list));
 154          \core_plugin_manager::reset_caches();
 155          \core_media_manager::reset_caches();
 156      }
 157  
 158      /**
 159       * Returns the default rank of this plugin for default sort order
 160       * @return int
 161       */
 162      public function get_rank() {
 163          $classname = '\media_'.$this->name.'_plugin';
 164          if (class_exists($classname)) {
 165              $object = new $classname();
 166              return $object->get_rank();
 167          }
 168          return 0;
 169      }
 170  
 171      /**
 172       * Returns human-readable string of supported file/link types for the "Manage media players" page
 173       * @param array $extensions
 174       * @return string
 175       */
 176      public function supports(&$extensions) {
 177          $classname = '\media_'.$this->name.'_plugin';
 178          if (class_exists($classname)) {
 179              $object = new $classname();
 180              $result = $object->supports($extensions);
 181              foreach ($object->get_supported_extensions() as $ext) {
 182                  $extensions[$ext] = $ext;
 183              }
 184              return $result;
 185          }
 186          return '';
 187      }
 188  }