Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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_aiplacement;
18
 
19
use aiplacement_editor\utils;
20
use core\context;
21
use core_ai\aiactions\generate_image;
22
use core_ai\aiactions\generate_text;
23
use core_ai\manager;
24
use editor_tiny\editor;
25
use editor_tiny\plugin;
26
use editor_tiny\plugin_with_buttons;
27
use editor_tiny\plugin_with_configuration;
28
use editor_tiny\plugin_with_configuration_for_external;
29
use editor_tiny\plugin_with_menuitems;
30
 
31
/**
32
 * Tiny AI placement plugin.
33
 *
34
 * @package    tiny_aiplacement
35
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class plugininfo extends plugin implements
39
    plugin_with_buttons,
40
    plugin_with_menuitems,
41
    plugin_with_configuration,
42
    plugin_with_configuration_for_external {
43
 
44
    /**
45
     * @var array|string[] The possible actions for the plugin.
46
     */
47
    protected static array $possibleactions = [
48
        'generate_text' => generate_text::class,
49
        'generate_image' => generate_image::class,
50
    ];
51
 
52
    #[\Override]
53
    public static function is_enabled(
54
        context $context,
55
        array $options,
56
        array $fpoptions,
57
        ?editor $editor = null
58
    ): bool {
59
        return in_array(true, self::get_allowed_actions($context, $options));
60
    }
61
 
62
    #[\Override]
63
    public static function is_enabled_for_external(context $context, array $options): bool {
64
        // Assume files are allowed. Otherwise, the generate_image action is ignored.
65
        $options['maxfiles'] = 1;
66
        return self::is_enabled($context, $options, []);
67
    }
68
 
69
    #[\Override]
70
    public static function get_available_buttons(): array {
71
        return array_map(fn ($action) => "tiny_aiplacement/{$action}", self::$possibleactions);
72
    }
73
 
74
    #[\Override]
75
    public static function get_available_menuitems(): array {
76
        return array_map(fn ($action) => "tiny_aiplacement/{$action}", self::$possibleactions);
77
    }
78
 
79
    #[\Override]
80
    public static function get_plugin_configuration_for_context(
81
        context $context,
82
        array $options,
83
        array $fpoptions,
84
        ?editor $editor = null
85
    ): array {
86
        global $USER;
87
 
88
        $userid = (int) $USER->id;
89
        $allowedactions = self::get_allowed_actions($context, $options);
90
 
91
        return array_merge([
92
            'contextid' => $context->id,
93
            'userid' => $userid,
94
            'policyagreed' => manager::get_user_policy_status($userid),
95
        ], $allowedactions);
96
    }
97
 
98
    #[\Override]
99
    public static function get_plugin_configuration_for_external(context $context): array {
100
        // Assume files are allowed. Otherwise, the generate_image action is ignored.
101
        $options = ['maxfiles' => 1];
102
        $settings = self::get_plugin_configuration_for_context($context, $options, []);
103
        unset($settings['contextid']);
104
        unset($settings['userid']);
105
        return array_map(fn($value) =>  is_bool($value) ? ($value ? '1' : '0') : $value, $settings);
106
    }
107
 
108
    /**
109
     * Get the allowed actions for the plugin.
110
     *
111
     * @param context $context The context that the editor is used within
112
     * @param array $options The options passed in when requesting the editor
113
     * @return array The allowed actions.
114
     */
115
    private static function get_allowed_actions(context $context, array $options): array {
116
        $allowedactions = [];
117
        foreach (self::$possibleactions as $action => $actionclass) {
118
            $allowedactions[$action] = utils::is_html_editor_placement_action_available(
119
                context: $context,
120
                actionname: $action,
121
                actionclass: $actionclass,
122
            );
123
 
124
            if ($allowedactions[$action] && $action == 'generate_image') {
125
                // For generate image, we need to check if the user has the capability to upload files.
126
                $canhavefiles = !empty($options['maxfiles']);
127
                $canhaveexternalfiles = !empty($options['return_types']) && ($options['return_types'] & FILE_EXTERNAL);
128
                $allowedactions[$action] = $canhavefiles || $canhaveexternalfiles;
129
            }
130
        }
131
        return $allowedactions;
132
    }
133
}