Proyectos de Subversion Moodle

Rev

| 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
 * unilabel module.
19
 *
20
 * @package     mod_unilabel
21
 * @author      Andreas Grabs <info@grabs-edv.de>
22
 * @copyright   2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_unilabel;
27
 
28
/**
29
 * Placeholder class if an active type is currently not installed or otherwise not available.
30
 * @package     mod_unilabel
31
 * @author      Andreas Grabs <info@grabs-edv.de>
32
 * @copyright   2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
33
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class tinymce_helper extends \editor_tiny\editor {
36
    /**
37
     * Get an escaped and json encoded configuration object for reinitialize an tinymce instance by js.
38
     *
39
     * @param  array  $editoroptions
40
     * @param  int    $draftitemid
41
     * @return string The json encode configuration object
42
     */
43
    public function get_options(array $editoroptions = [], int $draftitemid = 0) {
44
        global $PAGE;
45
 
46
        list($options, $fpoptions) = static::split_editor_options(
47
            $editoroptions,
48
            $draftitemid
49
        );
50
 
51
        // Ensure that the default configuration is set.
52
        self::set_default_configuration($this->manager);
53
 
54
        if ($fpoptions === null) {
55
            $fpoptions = [];
56
        }
57
 
58
        $context = $PAGE->context;
59
 
60
        if (isset($options['context']) && ($options['context'] instanceof \context)) {
61
            // A different context was provided.
62
            // Use that instead.
63
            $context = $options['context'];
64
        }
65
 
66
        // Generate the configuration for this editor.
67
        $siteconfig = get_config('editor_tiny');
68
        $config     = (object) [
69
            // The URL to the CSS file for the editor.
70
            'css' => $PAGE->theme->editor_css_url()->out(false),
71
 
72
            // The current context for this page or editor.
73
            'context' => $context->id,
74
 
75
            // File picker options.
76
            'filepicker' => $fpoptions,
77
 
78
            'currentLanguage' => current_language(),
79
 
80
            'branding' => property_exists($siteconfig, 'branding') ? !empty($siteconfig->branding) : true,
81
 
82
            // Language options.
83
            'language' => [
84
                'currentlang' => current_language(),
85
                'installed'   => get_string_manager()->get_list_of_translations(true),
86
                'available'   => get_string_manager()->get_list_of_languages(),
87
            ],
88
 
89
            // Placeholder selectors.
90
            // Some contents (Example: placeholder elements) are only shown in the editor, and not to users. It is unrelated to the
91
            // real display. We created a list of placeholder selectors, so we can decide to or not to apply rules, styles... to
92
            // these elements.
93
            // The default of this list will be empty.
94
            // Other plugins can register their placeholder elements to placeholderSelectors list by calling
95
            // editor_tiny/options::registerPlaceholderSelectors.
96
            'placeholderSelectors' => [],
97
 
98
            // Plugin configuration.
99
            'plugins' => $this->manager->get_plugin_configuration($context, $options, $fpoptions, $this),
100
 
101
            // Nest menu inside parent DOM.
102
            'nestedmenu' => true,
103
        ];
104
 
105
        if (defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING) {
106
            // Add sample selectors for Behat test.
107
            $config->placeholderSelectors = ['.behat-tinymce-placeholder'];
108
        }
109
 
110
        foreach ($fpoptions as $fp) {
111
            // Guess the draftitemid for the editor.
112
            // Note: This is the best we can do at the moment.
113
            if (!empty($fp->itemid)) {
114
                $config->draftitemid = $fp->itemid;
115
                break;
116
            }
117
        }
118
 
119
        $configoptions = json_encode(convert_to_array($config), JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT);
120
 
121
        return str_replace('\\', '\\\\', $configoptions); // We have to escape the output because it gets directly into js.
122
    }
123
 
124
    /**
125
     * Extract the filepicker options.
126
     *
127
     * @param  array $options
128
     * @param  int   $draftitemid
129
     * @return array
130
     */
131
    public static function split_editor_options(array $options, int $draftitemid = 0) {
132
        global $CFG;
133
        require_once($CFG->dirroot . '/repository/lib.php');
134
 
135
        $defaultoptions = [
136
            'subdirs'               => 0,
137
            'maxbytes'              => 0,
138
            'maxfiles'              => 0,
139
            'changeformat'          => 0,
140
            'areamaxbytes'          => FILE_AREA_MAX_BYTES_UNLIMITED,
141
            'context'               => null,
142
            'noclean'               => 0,
143
            'trusttext'             => 0,
144
            'return_types'          => 15,
145
            'enable_filemanagement' => true,
146
            'removeorphaneddrafts'  => false,
147
            'autosave'              => true,
148
        ];
149
 
150
        $options = array_merge($defaultoptions, $options);
151
 
152
        /** @var \context $ctx */
153
        $ctx = $options['context'];
154
 
155
        $maxfiles = $options['maxfiles'];
156
 
157
        // Get filepicker info.
158
        //
159
        $fpoptions = [];
160
        if ($maxfiles != 0 && $draftitemid > 0) {
161
            $args = new \stdClass();
162
            // Need these three to filter repositories list.
163
            $args->accepted_types = ['web_image'];
164
            $args->return_types   = $options['return_types'];
165
            $args->context        = $ctx;
166
            $args->env            = 'filepicker';
167
            // Advimage plugin.
168
            $imageoptions               = initialise_filepicker($args);
169
            $imageoptions->context      = $ctx;
170
            $imageoptions->client_id    = uniqid();
171
            $imageoptions->maxbytes     = $options['maxbytes'];
172
            $imageoptions->areamaxbytes = $options['areamaxbytes'];
173
            $imageoptions->env          = 'editor';
174
            $imageoptions->itemid       = $draftitemid;
175
 
176
            // Moodlemedia plugin.
177
            $args->accepted_types        = ['video', 'audio'];
178
            $mediaoptions               = initialise_filepicker($args);
179
            $mediaoptions->context      = $ctx;
180
            $mediaoptions->client_id    = uniqid();
181
            $mediaoptions->maxbytes     = $options['maxbytes'];
182
            $mediaoptions->areamaxbytes = $options['areamaxbytes'];
183
            $mediaoptions->env          = 'editor';
184
            $mediaoptions->itemid       = $draftitemid;
185
 
186
            // Advlink plugin.
187
            $args->accepted_types       = '*';
188
            $linkoptions               = initialise_filepicker($args);
189
            $linkoptions->context      = $ctx;
190
            $linkoptions->client_id    = uniqid();
191
            $linkoptions->maxbytes     = $options['maxbytes'];
192
            $linkoptions->areamaxbytes = $options['areamaxbytes'];
193
            $linkoptions->env          = 'editor';
194
            $linkoptions->itemid       = $draftitemid;
195
 
196
            $args->accepted_types           = ['.vtt'];
197
            $subtitleoptions               = initialise_filepicker($args);
198
            $subtitleoptions->context      = $ctx;
199
            $subtitleoptions->client_id    = uniqid();
200
            $subtitleoptions->maxbytes     = $options['maxbytes'];
201
            $subtitleoptions->areamaxbytes = $options['areamaxbytes'];
202
            $subtitleoptions->env          = 'editor';
203
            $subtitleoptions->itemid       = $draftitemid;
204
 
205
            if (has_capability('moodle/h5p:deploy', $ctx)) {
206
                // Only set H5P Plugin settings if the user can deploy new H5P content.
207
                // H5P plugin.
208
                $args->accepted_types     = ['.h5p'];
209
                $h5poptions               = initialise_filepicker($args);
210
                $h5poptions->context      = $ctx;
211
                $h5poptions->client_id    = uniqid();
212
                $h5poptions->maxbytes     = $options['maxbytes'];
213
                $h5poptions->areamaxbytes = $options['areamaxbytes'];
214
                $h5poptions->env          = 'editor';
215
                $h5poptions->itemid       = $draftitemid;
216
                $fpoptions['h5p']         = $h5poptions;
217
            }
218
 
219
            $fpoptions['image']    = $imageoptions;
220
            $fpoptions['media']    = $mediaoptions;
221
            $fpoptions['link']     = $linkoptions;
222
            $fpoptions['subtitle'] = $subtitleoptions;
223
        }
224
 
225
        return [$options, $fpoptions];
226
    }
227
 
228
    /**
229
     * Checks whether or not tinymce is the current editor.
230
     * This is needed because the drag and drop feature does not fully support this editor.
231
     *
232
     * @return bool
233
     */
234
    public static function tiny_active() {
235
        $editor = editors_get_preferred_editor();
236
        if (get_class($editor) == 'editor_tiny\editor') {
237
            return true;
238
        }
239
 
240
        return false;
241
    }
242
}