Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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_premium;
18
 
19
/**
20
 * Tiny Premium manager.
21
 *
22
 * @package    tiny_premium
23
 * @copyright  2024 David Woloszyn <david.woloszyn@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
class manager {
27
 
28
    /**
29
     * Get all Tiny Premium plugins currently supported.
30
     *
31
     * The plugin identifiers are taken from Tiny Cloud (https://www.tiny.cloud/docs/tinymce/6/plugins/#premium-plugins).
32
     *
33
     * @return array The array of plugins.
34
     */
35
    public static function get_plugins(): array {
36
        return [
1441 ariadna 37
            'a11ychecker',
1 efrain 38
            'advtable',
39
            'typography',
40
            'casechange',
41
            'checklist',
42
            'editimage',
43
            'export',
44
            'footnotes',
45
            'formatpainter',
46
            'linkchecker',
1441 ariadna 47
            'math',
1 efrain 48
            'pageembed',
49
            'permanentpen',
50
            'powerpaste',
51
            'tinymcespellchecker',
52
            'autocorrect',
53
            'tableofcontents',
54
        ];
55
    }
56
 
57
    /**
58
     * Get enabled Tiny Premium plugins.
59
     *
60
     * @return array The array of enabled plugins.
61
     */
62
    public static function get_enabled_plugins(): array {
63
        $plugins = self::get_plugins();
64
        $enabledplugins = [];
65
        foreach ($plugins as $plugin) {
66
            if (self::is_plugin_enabled($plugin)) {
67
                $enabledplugins[] = $plugin;
68
            }
69
        }
70
        return $enabledplugins;
71
    }
72
 
73
    /**
74
     * Check if a Tiny Premium plugin is enabled in config.
75
     *
76
     * @param string $plugin The plugin to check.
77
     * @return bool Return true if enabled.
78
     */
79
    public static function is_plugin_enabled(string $plugin): bool {
80
        $config = get_config('tiny_premium_' . $plugin, 'enabled');
81
        return ($config == 1);
82
    }
83
 
84
    /**
85
     * Set a new value for a Tiny Premium plugin config.
86
     *
87
     * @param array $data The data to set.
88
     * @param string $plugin The plugin to use.
89
     */
90
    public static function set_plugin_config(array $data, string $plugin): void {
91
        // Check this is a valid premium plugin.
92
        if (!in_array($plugin, self::get_plugins())) {
93
            return;
94
        }
95
 
96
        $plugin = 'tiny_premium_' . $plugin;
97
 
98
        foreach ($data as $key => $newvalue) {
99
            // Get the old value for the log.
100
            $oldvalue = get_config($plugin, $key) ?? null;
101
            add_to_config_log($key, $oldvalue, $newvalue, $plugin);
102
 
103
            // If we are disabling the plugin, remove it, otherwise, set the new value.
104
            if ($key === 'enabled' && $newvalue == 0) {
105
                unset_config($key, $plugin);
106
            } else {
107
                set_config($key, $newvalue, $plugin);
108
            }
109
        }
110
    }
111
}