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.
   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  namespace tiny_media;
  18  
  19  use context;
  20  use editor_tiny\editor;
  21  use editor_tiny\plugin;
  22  use editor_tiny\plugin_with_buttons;
  23  use editor_tiny\plugin_with_configuration;
  24  use editor_tiny\plugin_with_menuitems;
  25  
  26  /**
  27   * Tiny media plugin.
  28   *
  29   * @package    tiny_media
  30   * @copyright  2022 Andrew Lyons <andrew@nicols.co.uk>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class plugininfo extends plugin implements plugin_with_buttons, plugin_with_menuitems, plugin_with_configuration {
  34      /**
  35       * Whether the plugin is enabled
  36       *
  37       * @param context $context The context that the editor is used within
  38       * @param array $options The options passed in when requesting the editor
  39       * @param array $fpoptions The filepicker options passed in when requesting the editor
  40       * @param editor $editor The editor instance in which the plugin is initialised
  41       * @return boolean
  42       */
  43      public static function is_enabled(
  44          context $context,
  45          array $options,
  46          array $fpoptions,
  47          ?editor $editor = null
  48      ): bool {
  49          // Disabled if:
  50          // - Not logged in or guest.
  51          // - Files are not allowed.
  52          // - Only URL are supported.
  53          $canhavefiles = !empty($options['maxfiles']);
  54          $canhaveexternalfiles = !empty($options['return_types']) && ($options['return_types'] & FILE_EXTERNAL);
  55  
  56          return isloggedin() && !isguestuser() && ($canhavefiles || $canhaveexternalfiles);
  57      }
  58  
  59      public static function get_available_buttons(): array {
  60          return [
  61              'tiny_media/tiny_media_image',
  62          ];
  63      }
  64  
  65      public static function get_available_menuitems(): array {
  66          return [
  67              'tiny_media/tiny_media_image',
  68          ];
  69      }
  70  
  71      public static function get_plugin_configuration_for_context(
  72          context $context,
  73          array $options,
  74          array $fpoptions,
  75          ?editor $editor = null
  76      ): array {
  77  
  78          // TODO Fetch the actual permissions.
  79          $permissions = [
  80              'image' => [
  81                  'filepicker' => true,
  82              ],
  83              'embed' => [
  84                  'filepicker' => true,
  85              ]
  86          ];
  87  
  88          return array_merge([
  89              'permissions' => $permissions,
  90          ], self::get_file_manager_configuration($context, $options, $fpoptions));
  91      }
  92  
  93      protected static function get_file_manager_configuration(
  94          context $context,
  95          array $options,
  96          array $fpoptions
  97      ): array {
  98          global $USER;
  99  
 100          $params = [
 101              'area' => [],
 102              'usercontext' => \context_user::instance($USER->id)->id,
 103          ];
 104  
 105          $keys = [
 106              'itemid',
 107              'areamaxbytes',
 108              'maxbytes',
 109              'subdirs',
 110              'return_types',
 111              'removeorphaneddrafts',
 112          ];
 113          if (isset($options['context'])) {
 114              if (is_object($options['context'])) {
 115                  $params['area']['context'] = $options['context']->id;
 116              } else {
 117                  $params['area']['context'] = $options['context'];
 118              }
 119          }
 120          foreach ($keys as $key) {
 121              if (isset($options[$key])) {
 122                  $params['area'][$key] = $options[$key];
 123              }
 124          }
 125  
 126          return [
 127              'storeinrepo' => true,
 128              'data' => [
 129                  'params' => $params,
 130                  'fpoptions' => $fpoptions,
 131              ],
 132          ];
 133      }
 134  }