Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 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      public static function enable_plugin(string $pluginname, int $enabled): bool {
 103          global $CFG;
 104  
 105          $haschanged = false;
 106          $plugins = [];
 107          if (!empty($CFG->media_plugins_sortorder)) {
 108              $plugins = explode(',', $CFG->media_plugins_sortorder);
 109          }
 110          // Only set visibility if it's different from the current value.
 111          if ($enabled && !in_array($pluginname, $plugins)) {
 112              // Enable media plugin.
 113  
 114              $pluginsbytype = \core_plugin_manager::instance()->get_plugins_of_type('media');
 115              if (!array_key_exists($pluginname, $pluginsbytype)) {
 116                  // Can not be enabled.
 117                  return false;
 118              }
 119  
 120              $rank = $pluginsbytype[$pluginname]->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 ($plugins as $playername) {
 124                  if (($player = $pluginsbytype[$playername]) && ($rank > $player->get_rank())) {
 125                      break;
 126                  }
 127                  $position++;
 128              }
 129              array_splice($plugins, $position, 0, [$pluginname]);
 130              $haschanged = true;
 131          } else if (!$enabled && in_array($pluginname, $plugins)) {
 132              // Disable media plugin.
 133              $key = array_search($pluginname, $plugins);
 134              unset($plugins[$key]);
 135              $haschanged = true;
 136          }
 137  
 138          if ($haschanged) {
 139              add_to_config_log('media_plugins_sortorder', !$enabled, $enabled, $pluginname);
 140              self::set_enabled_plugins($plugins);
 141          }
 142  
 143          return $haschanged;
 144      }
 145  
 146      /**
 147       * Sets the current plugin as enabled or disabled
 148       * When enabling tries to guess the sortorder based on default rank returned by the plugin.
 149       * @param bool $newstate
 150       */
 151      public function set_enabled($newstate = true) {
 152          self::enable_plugin($this->name, $newstate);
 153      }
 154  
 155      /**
 156       * Set the list of enabled media players in the specified sort order
 157       * To be used when changing settings or in unit tests
 158       * @param string|array $list list of plugin names without frankenstyle prefix - comma-separated string or an array
 159       */
 160      public static function set_enabled_plugins($list) {
 161          if (empty($list)) {
 162              $list = [];
 163          } else if (!is_array($list)) {
 164              $list = explode(',', $list);
 165          }
 166          if ($list) {
 167              $plugins = \core_plugin_manager::instance()->get_installed_plugins('media');
 168              $list = array_intersect($list, array_keys($plugins));
 169          }
 170          set_config('media_plugins_sortorder', join(',', $list));
 171          \core_plugin_manager::reset_caches();
 172          \core_media_manager::reset_caches();
 173      }
 174  
 175      /**
 176       * Returns the default rank of this plugin for default sort order
 177       * @return int
 178       */
 179      public function get_rank() {
 180          $classname = '\media_'.$this->name.'_plugin';
 181          if (class_exists($classname)) {
 182              $object = new $classname();
 183              return $object->get_rank();
 184          }
 185          return 0;
 186      }
 187  
 188      /**
 189       * Returns human-readable string of supported file/link types for the "Manage media players" page
 190       * @param array $extensions
 191       * @return string
 192       */
 193      public function supports(&$extensions) {
 194          $classname = '\media_'.$this->name.'_plugin';
 195          if (class_exists($classname)) {
 196              $object = new $classname();
 197              $result = $object->supports($extensions);
 198              foreach ($object->get_supported_extensions() as $ext) {
 199                  $extensions[$ext] = $ext;
 200              }
 201              return $result;
 202          }
 203          return '';
 204      }
 205  }