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
/**
18
 * Defines classes used for plugin info.
19
 *
20
 * @package    core
21
 * @copyright  2013 Petr Skoda {@link http://skodak.org}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace core\plugininfo;
25
 
26
use admin_settingpage;
27
use moodle_url;
28
use part_of_admin_tree;
29
 
30
/**
31
 * Class for HTML editors
32
 */
33
class editor extends base {
1441 ariadna 34
    #[\Override]
1 efrain 35
    public static function plugintype_supports_disabling(): bool {
36
        return true;
37
    }
38
 
1441 ariadna 39
    #[\Override]
1 efrain 40
    public static function get_enabled_plugins() {
41
        global $CFG;
42
 
43
        if (empty($CFG->texteditors)) {
1441 ariadna 44
            return [
45
                'tiny' => 'tiny',
46
                'textarea' => 'textarea',
47
            ];
1 efrain 48
        }
49
 
1441 ariadna 50
        $enabled = [];
1 efrain 51
        foreach (explode(',', $CFG->texteditors) as $editor) {
52
            $enabled[$editor] = $editor;
53
        }
54
 
55
        return $enabled;
56
    }
57
 
1441 ariadna 58
    #[\Override]
1 efrain 59
    public static function enable_plugin(string $pluginname, int $enabled): bool {
60
        global $CFG;
61
 
62
        $haschanged = false;
63
        if (!empty($CFG->texteditors)) {
64
            $plugins = array_flip(explode(',', $CFG->texteditors));
65
        } else {
66
            $plugins = [];
67
        }
68
 
69
        // Only set visibility if it's different from the current value.
70
        if ($enabled && !array_key_exists($pluginname, $plugins)) {
71
            $plugins[$pluginname] = $pluginname;
72
            $haschanged = true;
73
        } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
74
            unset($plugins[$pluginname]);
75
            $haschanged = true;
76
        }
77
 
78
        // At least one editor must be active.
79
        if (empty($plugins)) {
80
            $plugins['textarea'] = 'textarea';
81
            $haschanged = true;
82
        }
83
 
84
        if ($haschanged) {
85
            $new = implode(',', array_flip($plugins));
86
            add_to_config_log('editor_visibility', !$enabled, $enabled, $pluginname);
87
            set_config('texteditors', $new);
88
            // Reset caches.
89
            \core_plugin_manager::reset_caches();
90
        }
91
 
92
        return $haschanged;
93
    }
94
 
1441 ariadna 95
    #[\Override]
1 efrain 96
    public function get_settings_section_name() {
97
        return 'editorsettings' . $this->name;
98
    }
99
 
1441 ariadna 100
    #[\Override]
1 efrain 101
    public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
102
        global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
103
        /** @var \admin_root $ADMIN */
104
        $ADMIN = $adminroot; // May be used in settings.php.
105
        $plugininfo = $this; // Also can be used inside settings.php.
106
        $editor = $this;     // Also can be used inside settings.php.
107
 
108
        if (!$this->is_installed_and_upgraded()) {
109
            return;
110
        }
111
 
112
        if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
113
            return;
114
        }
115
 
116
        $section = $this->get_settings_section_name();
117
 
118
        $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
119
        include($this->full_path('settings.php')); // This may also set $settings to null.
120
 
121
        if ($settings) {
122
            $ADMIN->add($parentnodename, $settings);
123
        }
124
    }
125
 
1441 ariadna 126
    #[\Override]
1 efrain 127
    public function is_uninstall_allowed() {
128
        if ($this->name === 'textarea') {
129
            return false;
130
        } else {
131
            return true;
132
        }
133
    }
134
 
1441 ariadna 135
    #[\Override]
136
    public function uninstall_cleanup(): void {
137
        global $DB;
138
 
139
        self::enable_plugin($this->name, 0);
140
        $DB->delete_records_select(
141
            'user_preferences',
142
            "name = :name AND " . $DB->sql_compare_text('value') . " = " . $DB->sql_compare_text(':value'),
143
            [
144
                'name' => 'htmleditor',
145
                'value' => $this->name,
146
            ],
147
        );
148
    }
149
 
150
    #[\Override]
1 efrain 151
    public static function get_manage_url() {
152
        return new moodle_url('/admin/settings.php', array('section'=>'manageeditors'));
153
    }
154
 
1441 ariadna 155
    #[\Override]
1 efrain 156
    public static function plugintype_supports_ordering(): bool {
157
        return true;
158
    }
159
 
1441 ariadna 160
    #[\Override]
1 efrain 161
    public static function get_sorted_plugins(bool $enabledonly = false): ?array {
162
        global $CFG;
163
 
164
        $pluginmanager = \core_plugin_manager::instance();
165
        $plugins = $pluginmanager->get_plugins_of_type('editor');
166
 
167
        // The Editor list is stored in an ordered string.
168
        $activeeditors = explode(',', $CFG->texteditors);
169
 
170
        $sortedplugins = [];
171
        foreach ($activeeditors as $editor) {
172
            if (isset($plugins[$editor])) {
173
                $sortedplugins[$editor] = $plugins[$editor];
174
                unset($plugins[$editor]);
175
            }
176
        }
177
 
178
        if ($enabledonly) {
179
            return $sortedplugins;
180
        }
181
 
182
        // Sort the rest of the plugins lexically.
183
        uasort($plugins, function ($a, $b) {
184
            return strnatcasecmp($a->name, $b->name);
185
        });
186
 
187
        return array_merge(
188
            $sortedplugins,
189
            $plugins,
190
        );
191
    }
192
 
1441 ariadna 193
    #[\Override]
1 efrain 194
    public static function change_plugin_order(string $pluginname, int $direction): bool {
195
        $activeeditors = array_keys(self::get_sorted_plugins(true));
196
        $key = array_search($pluginname, $activeeditors);
197
 
198
        if ($key === false) {
199
            return false;
200
        }
201
 
202
        if ($direction === self::MOVE_DOWN) {
203
            // Move down the list.
204
            if ($key < (count($activeeditors) - 1)) {
205
                $fsave = $activeeditors[$key];
206
                $activeeditors[$key] = $activeeditors[$key + 1];
207
                $activeeditors[$key + 1] = $fsave;
208
                add_to_config_log('editor_position', $key, $key + 1, $pluginname);
209
                set_config('texteditors', implode(',', $activeeditors));
210
                \core_plugin_manager::reset_caches();
211
 
212
                return true;
213
            }
214
        } else if ($direction === self::MOVE_UP) {
215
            if ($key >= 1) {
216
                $fsave = $activeeditors[$key];
217
                $activeeditors[$key] = $activeeditors[$key - 1];
218
                $activeeditors[$key - 1] = $fsave;
219
                add_to_config_log('editor_position', $key, $key - 1, $pluginname);
220
                set_config('texteditors', implode(',', $activeeditors));
221
                \core_plugin_manager::reset_caches();
222
 
223
                return true;
224
            }
225
        }
226
 
227
        return false;
228
    }
229
}