| 1 | efrain | 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 {
 | 
        
           | 1441 | ariadna | 34 |   | 
        
           |  |  | 35 |     #[\Override]
 | 
        
           | 1 | efrain | 36 |     public static function is_enabled(
 | 
        
           |  |  | 37 |         context $context,
 | 
        
           |  |  | 38 |         array $options,
 | 
        
           |  |  | 39 |         array $fpoptions,
 | 
        
           |  |  | 40 |         ?editor $editor = null
 | 
        
           |  |  | 41 |     ): bool {
 | 
        
           |  |  | 42 |         // Disabled if:
 | 
        
           |  |  | 43 |         // - Not logged in or guest.
 | 
        
           |  |  | 44 |         // - Files are not allowed.
 | 
        
           |  |  | 45 |         // - Only URL are supported.
 | 
        
           | 1441 | ariadna | 46 |         // - Don't have the correct capability.
 | 
        
           | 1 | efrain | 47 |         $canhavefiles = !empty($options['maxfiles']);
 | 
        
           |  |  | 48 |         $canhaveexternalfiles = !empty($options['return_types']) && ($options['return_types'] & FILE_EXTERNAL);
 | 
        
           |  |  | 49 |   | 
        
           | 1441 | ariadna | 50 |         return isloggedin() && !isguestuser() && ($canhavefiles || $canhaveexternalfiles) &&
 | 
        
           |  |  | 51 |                 has_capability('tiny/media:use', $context);
 | 
        
           | 1 | efrain | 52 |     }
 | 
        
           |  |  | 53 |   | 
        
           | 1441 | ariadna | 54 |     #[\Override]
 | 
        
           |  |  | 55 |     public static function is_enabled_for_external(context $context, array $options): bool {
 | 
        
           |  |  | 56 |         // Assume files are allowed.
 | 
        
           |  |  | 57 |         $options['maxfiles'] = 1;
 | 
        
           |  |  | 58 |         return self::is_enabled($context, $options, []);
 | 
        
           |  |  | 59 |     }
 | 
        
           |  |  | 60 |   | 
        
           | 1 | efrain | 61 |     public static function get_available_buttons(): array {
 | 
        
           |  |  | 62 |         return [
 | 
        
           |  |  | 63 |             'tiny_media/tiny_media_image',
 | 
        
           |  |  | 64 |         ];
 | 
        
           |  |  | 65 |     }
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |     public static function get_available_menuitems(): array {
 | 
        
           |  |  | 68 |         return [
 | 
        
           |  |  | 69 |             'tiny_media/tiny_media_image',
 | 
        
           |  |  | 70 |         ];
 | 
        
           |  |  | 71 |     }
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 |     public static function get_plugin_configuration_for_context(
 | 
        
           |  |  | 74 |         context $context,
 | 
        
           |  |  | 75 |         array $options,
 | 
        
           |  |  | 76 |         array $fpoptions,
 | 
        
           |  |  | 77 |         ?editor $editor = null
 | 
        
           |  |  | 78 |     ): array {
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |         // TODO Fetch the actual permissions.
 | 
        
           |  |  | 81 |         $permissions = [
 | 
        
           |  |  | 82 |             'image' => [
 | 
        
           |  |  | 83 |                 'filepicker' => true,
 | 
        
           |  |  | 84 |             ],
 | 
        
           |  |  | 85 |             'embed' => [
 | 
        
           |  |  | 86 |                 'filepicker' => true,
 | 
        
           |  |  | 87 |             ]
 | 
        
           |  |  | 88 |         ];
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |         return array_merge([
 | 
        
           |  |  | 91 |             'permissions' => $permissions,
 | 
        
           |  |  | 92 |         ], self::get_file_manager_configuration($context, $options, $fpoptions));
 | 
        
           |  |  | 93 |     }
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |     protected static function get_file_manager_configuration(
 | 
        
           |  |  | 96 |         context $context,
 | 
        
           |  |  | 97 |         array $options,
 | 
        
           |  |  | 98 |         array $fpoptions
 | 
        
           |  |  | 99 |     ): array {
 | 
        
           |  |  | 100 |         global $USER;
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |         $params = [
 | 
        
           |  |  | 103 |             'area' => [],
 | 
        
           |  |  | 104 |             'usercontext' => \context_user::instance($USER->id)->id,
 | 
        
           |  |  | 105 |         ];
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |         $keys = [
 | 
        
           |  |  | 108 |             'itemid',
 | 
        
           |  |  | 109 |             'areamaxbytes',
 | 
        
           |  |  | 110 |             'maxbytes',
 | 
        
           |  |  | 111 |             'subdirs',
 | 
        
           |  |  | 112 |             'return_types',
 | 
        
           |  |  | 113 |             'removeorphaneddrafts',
 | 
        
           |  |  | 114 |         ];
 | 
        
           |  |  | 115 |         if (isset($options['context'])) {
 | 
        
           |  |  | 116 |             if (is_object($options['context'])) {
 | 
        
           |  |  | 117 |                 $params['area']['context'] = $options['context']->id;
 | 
        
           |  |  | 118 |             } else {
 | 
        
           |  |  | 119 |                 $params['area']['context'] = $options['context'];
 | 
        
           |  |  | 120 |             }
 | 
        
           |  |  | 121 |         }
 | 
        
           |  |  | 122 |         foreach ($keys as $key) {
 | 
        
           |  |  | 123 |             if (isset($options[$key])) {
 | 
        
           |  |  | 124 |                 $params['area'][$key] = $options[$key];
 | 
        
           |  |  | 125 |             }
 | 
        
           |  |  | 126 |         }
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 |         return [
 | 
        
           |  |  | 129 |             'storeinrepo' => true,
 | 
        
           |  |  | 130 |             'data' => [
 | 
        
           |  |  | 131 |                 'params' => $params,
 | 
        
           |  |  | 132 |                 'fpoptions' => $fpoptions,
 | 
        
           |  |  | 133 |             ],
 | 
        
           |  |  | 134 |         ];
 | 
        
           |  |  | 135 |     }
 | 
        
           |  |  | 136 | }
 |