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
 *
19
 * @package   theme_universe
20
 * @copyright 2022 Marcin Czaja (https://rosea.io)
21
 * @license   Commercial https://themeforest.net/licenses
22
 *
23
 */
24
 
25
/**
26
 * @copyright 2015 Jeremy Hopkins (Coventry University)
27
 * @copyright 2015 Fernando Acedo (3-bits.com)
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 *
30
 * Class to configure html editor for admin settings allowing use of repositories.
31
 *
32
 * TODO: Does not remove old files when no longer in use!  No separate file area for each setting.
33
 *
34
 * Special thanks to Iban Cardona i Subiela (http://icsbcn.blogspot.com.es/2015/03/use-image-repository-in-theme-settings.html)
35
 * This post laid the ground work for most of the code featured in this file.
36
 */
37
class universe_setting_confightmleditor extends admin_setting_configtext {
38
 
39
    /** @var int number of rows */
40
    private $rows;
41
 
42
    /** @var int number of columns */
43
    private $cols;
44
 
45
    /** @var string filearea - filearea within Moodle repository API */
46
    private $filearea;
47
 
48
    /**
49
     * Constructor
50
     *
51
     * @param string $name
52
     * @param string $visiblename
53
     * @param string $description
54
     * @param mixed $defaultsetting string or array
55
     * @param mixed $paramtype
56
     * @param int $cols
57
     * @param int $rows
58
     * @param string $filearea
59
     */
60
    public function __construct(
61
        $name,
62
        $visiblename,
63
        $description,
64
        $defaultsetting,
65
        $paramtype = PARAM_RAW,
66
        $cols = '60',
67
        $rows = '8',
68
        $filearea = 'spacesettingsimgs'
69
    ) {
70
        $this->rows = $rows;
71
        $this->cols = $cols;
72
        $this->filearea = $filearea;
73
        $this->nosave = (during_initial_install() || CLI_SCRIPT);
74
        parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype);
75
        editors_head_setup();
76
    }
77
 
78
    /**
79
     * Gets the file area options.
80
     *
81
     * @param context_user $ctx
82
     * @return array
83
     */
84
    private function get_options(context_user $ctx) {
85
        $default = [];
86
        $default['noclean'] = false;
87
        $default['context'] = $ctx;
88
        $default['maxbytes'] = 0;
89
        $default['maxfiles'] = -1;
90
        $default['forcehttps'] = false;
91
        $default['subdirs'] = false;
92
        $default['changeformat'] = 0;
93
        $default['areamaxbytes'] = FILE_AREA_MAX_BYTES_UNLIMITED;
94
        $default['return_types'] = (FILE_INTERNAL | FILE_EXTERNAL);
95
 
96
        return $default;
97
    }
98
 
99
    /**
100
     * Returns an XHTML string for the editor
101
     *
102
     * @param string $data
103
     * @param string $query
104
     * @return string XHTML string for the editor
105
     */
106
    public function output_html($data, $query = '') {
107
        if (PHPUNIT_TEST) {
108
            $userid = 2;  // Admin user.
109
        } else {
110
            global $USER;
111
            $userid = $USER->id;
112
        }
113
 
114
        $default = $this->get_defaultsetting();
115
 
116
        $defaultinfo = $default;
117
        if (!is_null($default) && $default !== '') {
118
            $defaultinfo = "\n" . $default;
119
        }
120
 
121
        $ctx = context_user::instance($userid);
122
        $editor = editors_get_preferred_editor(FORMAT_HTML);
123
        $options = $this->get_options($ctx);
124
        $draftitemid = file_get_unused_draft_itemid();
125
        $component = is_null($this->plugin) ? 'core' : $this->plugin;
126
        $data = file_prepare_draft_area(
127
            $draftitemid,
128
            $options['context']->id,
129
            $component,
130
            $this->get_full_name() . '_draftitemid',
131
            $draftitemid,
132
            $options,
133
            $data
134
        );
135
 
136
        $fpoptions = [];
137
        $args = new stdClass();
138
 
139
        // Need these three to filter repositories list.
140
        $args->accepted_types = ['web_image'];
141
        $args->return_types = $options['return_types'];
142
        $args->context = $ctx;
143
        $args->env = 'filepicker';
144
 
145
        // Advimage plugin.
146
        $imageoptions = initialise_filepicker($args);
147
        $imageoptions->context = $ctx;
148
        $imageoptions->client_id = uniqid();
149
        $imageoptions->maxbytes = $options['maxbytes'];
150
        $imageoptions->areamaxbytes = $options['areamaxbytes'];
151
        $imageoptions->env = 'editor';
152
        $imageoptions->itemid = $draftitemid;
153
 
154
        // Moodlemedia plugin.
155
        $args->accepted_types = ['video', 'audio'];
156
        $mediaoptions = initialise_filepicker($args);
157
        $mediaoptions->context = $ctx;
158
        $mediaoptions->client_id = uniqid();
159
        $mediaoptions->maxbytes = $options['maxbytes'];
160
        $mediaoptions->areamaxbytes = $options['areamaxbytes'];
161
        $mediaoptions->env = 'editor';
162
        $mediaoptions->itemid = $draftitemid;
163
 
164
        // Advlink plugin.
165
        $args->accepted_types = '*';
166
        $linkoptions = initialise_filepicker($args);
167
        $linkoptions->context = $ctx;
168
        $linkoptions->client_id = uniqid();
169
        $linkoptions->maxbytes = $options['maxbytes'];
170
        $linkoptions->areamaxbytes = $options['areamaxbytes'];
171
        $linkoptions->env = 'editor';
172
        $linkoptions->itemid = $draftitemid;
173
 
174
        $fpoptions['image'] = $imageoptions;
175
        $fpoptions['media'] = $mediaoptions;
176
        $fpoptions['link'] = $linkoptions;
177
 
178
        $editor->use_editor($this->get_id(), $options, $fpoptions);
179
 
180
        return format_admin_setting(
181
            $this,
182
            $this->visiblename,
183
            '<div class="form-textarea">
184
                    <textarea rows="' . $this->rows .
185
                '" cols="' .
186
                $this->cols .
187
                '" id="' .
188
                $this->get_id() .
189
                '" name="' .
190
                $this->get_full_name() .
191
                '"spellcheck="true">' . s($data) . '
192
                    </textarea>
193
                </div>
194
                <input value="' .
195
                $draftitemid .
196
                '" name="' .
197
                $this->get_full_name() .
198
                '_draftitemid" type="hidden" />',
199
            $this->description,
200
            true,
201
            '',
202
            $defaultinfo,
203
            $query
204
        );
205
    }
206
 
207
    /**
208
     * Writes the setting to the database.
209
     *
210
     * @param mixed $data
211
     * @return string
212
     * @throws coding_exception
213
     * @throws dml_exception
214
     * @throws file_exception
215
     * @throws stored_file_creation_exception
216
     */
217
    public function write_setting($data) {
218
        global $CFG, $USER;
219
 
220
        if ($this->nosave) {
221
            return '';
222
        }
223
 
224
        if ($this->paramtype === PARAM_INT && $data === '') {
225
            // Do not complain if '' used instead of 0 !
226
            $data = 0;
227
        }
228
        // ... $data is a string.
229
        $validated = $this->validate($data);
230
        if ($validated !== true) {
231
            return $validated;
232
        }
233
 
234
        $options = $this->get_options(context_user::instance($USER->id));
235
        $fs = get_file_storage();
236
        $component = is_null($this->plugin) ? 'core' : $this->plugin;
237
        $wwwroot = $CFG->wwwroot;
238
        if ($options['forcehttps']) {
239
            $wwwroot = str_replace('http://', 'https://', $wwwroot);
240
        }
241
 
242
        $draftitemidname = sprintf('%s_draftitemid', $this->get_full_name());
243
        if (PHPUNIT_TEST || !isset($_REQUEST[$draftitemidname])) {
244
            $draftitemid = 0;
245
        } else {
246
            $draftitemid = $_REQUEST[$draftitemidname];
247
        }
248
 
249
        $hasfiles = false;
250
        $draftfiles = $fs->get_area_files($options['context']->id, 'user', 'draft', $draftitemid, 'id');
251
        foreach ($draftfiles as $file) {
252
            if (!$file->is_directory()) {
253
                $urlfilename = rawurlencode($file->get_filename());
254
                $strtosearch = "$wwwroot/draftfile.php/" . $options['context']->id . "/user/draft/$draftitemid/" . $urlfilename;
255
                if (stripos($data, $strtosearch) !== false) {
256
                    $filerecord = [
257
                        'contextid' => context_system::instance()->id,
258
                        'component' => $component,
259
                        'filearea' => $this->filearea,
260
                        'filename' => $file->get_filename(),
261
                        'filepath' => '/',
262
                        'itemid' => 0,
263
                        'timemodified' => time(),
264
                    ];
265
                    if (
266
                        !$filerec = $fs->get_file(
267
                            $filerecord['contextid'],
268
                            $filerecord['component'],
269
                            $filerecord['filearea'],
270
                            $filerecord['itemid'],
271
                            $filerecord['filepath'],
272
                            $filerecord['filename']
273
                        )
274
                    ) {
275
                        $filerec = $fs->create_file_from_storedfile($filerecord, $file);
276
                    }
277
                    $url = moodle_url::make_pluginfile_url(
278
                        $filerec->get_contextid(),
279
                        $filerec->get_component(),
280
                        $filerec->get_filearea(),
281
                        $filerec->get_itemid(),
282
                        $filerec->get_filepath(),
283
                        $filerec->get_filename()
284
                    );
285
                    $data = str_ireplace($strtosearch, $url, $data);
286
                    $hasfiles = true;
287
                }
288
            }
289
        }
290
        if (!$hasfiles) {
291
            if (trim(html_to_text($data)) === '') {
292
                $data = '';
293
            }
294
        }
295
 
296
        return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
297
    }
298
}
299
 
300
/**
301
 * No setting - just heading and text.
302
 *
303
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
304
 */
305
class universe_setting_specialsettingheading extends admin_setting {
306
 
307
    /**
308
     * not a setting, just text
309
     * @param string $name unique ascii name, either 'mysetting' for settings that in config,
310
     * or 'myplugin/mysetting' for ones in config_plugins.
311
     * @param string $heading heading
312
     * @param string $information text in box
313
     */
314
    public function __construct($name, $heading, $information) {
315
        $this->nosave = true;
316
        parent::__construct($name, $heading, $information, '');
317
    }
318
 
319
    /**
320
     * Always returns true
321
     * @return bool Always returns true
322
     */
323
    public function get_setting() {
324
        return true;
325
    }
326
 
327
    /**
328
     * Always returns true
329
     * @return bool Always returns true
330
     */
331
    public function get_defaultsetting() {
332
        return true;
333
    }
334
 
335
    /**
336
     * Never write settings
337
     * @return string Always returns an empty string
338
     */
339
    public function write_setting($data) {
340
        // Do not write any setting.
341
        return '';
342
    }
343
 
344
    /**
345
     * Returns an HTML string
346
     * @return string Returns an HTML string
347
     */
348
    public function output_html($data, $query = '') {
349
        global $OUTPUT;
350
        $title = $this->visiblename;
351
        $description = $this->description;
352
        $descriptionformatted = highlight($query, markdown_to_html($this->description));
353
 
354
        $output = '<div class="rui-setting-heading-wrapper--special">
355
        <h3 class="lead-2 mb-2">' . $title . '</h3><div class="rui-setting-desc pb-3 mb-4">' .
356
            $descriptionformatted .
357
            '</div></div>';
358
 
359
        return $output;
360
    }
361
}
362
 
363
/**
364
 * No setting - just heading and text.
365
 *
366
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
367
 */
368
class universe_setting_infosettingheading extends admin_setting {
369
 
370
    /**
371
     * not a setting, just text
372
     * @param string $name unique ascii name, either 'mysetting' for settings that in config,
373
     * or 'myplugin/mysetting' for ones in config_plugins.
374
     * @param string $heading heading
375
     * @param string $information text in box
376
     */
377
    public function __construct($name, $heading, $information) {
378
        $this->nosave = true;
379
        parent::__construct($name, $heading, $information, '');
380
    }
381
 
382
    /**
383
     * Always returns true
384
     * @return bool Always returns true
385
     */
386
    public function get_setting() {
387
        return true;
388
    }
389
 
390
    /**
391
     * Always returns true
392
     * @return bool Always returns true
393
     */
394
    public function get_defaultsetting() {
395
        return true;
396
    }
397
 
398
    /**
399
     * Never write settings
400
     * @return string Always returns an empty string
401
     */
402
    public function write_setting($data) {
403
        // Do not write any setting.
404
        return '';
405
    }
406
 
407
    /**
408
     * Returns an HTML string
409
     * @return string Returns an HTML string
410
     */
411
    public function output_html($data, $query = '') {
412
        global $OUTPUT;
413
        $title = $this->visiblename;
414
        $description = $this->description;
415
        $descriptionformatted = highlight($query, markdown_to_html($this->description));
416
 
417
        $output = '<div class="rui-setting-heading-wrapper--info">
418
        <h3 class="display-4 mb-2">' . $title . '</h3><div class="rui-setting-desc">' .
419
            $descriptionformatted .
420
            '</div></div>';
421
 
422
        return $output;
423
    }
424
}