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_media\external;
18
 
19
use context;
20
use core_external\external_api;
21
use core_external\external_function_parameters;
22
use core_external\external_single_structure;
23
use core_external\external_value;
24
 
25
/**
26
 * TinyMCE Media external API for filtering the content.
27
 *
28
 * @package    tiny_media
29
 * @copyright  2025 Huong Nguyen <huongnv13@gmail.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class preview extends external_api {
33
 
34
    /**
35
     * Describes the parameters for filtering the content.
36
     *
37
     * @return external_function_parameters
38
     * @since Moodle 5.0
39
     */
40
    public static function execute_parameters(): external_function_parameters {
41
        return new external_function_parameters([
42
            'contextid' => new external_value(PARAM_INT, 'The context ID', VALUE_REQUIRED),
43
            'content' => new external_value(PARAM_RAW, 'The media content', VALUE_REQUIRED),
44
        ]);
45
    }
46
 
47
    /**
48
     * External function to filter the content.
49
     *
50
     * @param int $contextid Context ID.
51
     * @param string $content Equation content.
52
     * @return array
53
     * @since Moodle 5.0
54
     */
55
    public static function execute(
56
        int $contextid,
57
        string $content,
58
    ): array {
59
        [
60
            'contextid' => $contextid,
61
            'content' => $content,
62
        ] = self::validate_parameters(self::execute_parameters(), [
63
            'contextid' => $contextid,
64
            'content' => $content,
65
        ]);
66
 
67
        $context = context::instance_by_id($contextid);
68
        self::validate_context($context);
69
        [$result, $format] = \core_external\util::format_text($content, FORMAT_HTML, $context, 'tiny_media');
70
 
71
        return [
72
            'content' => $result,
73
        ];
74
    }
75
 
76
    /**
77
     * Describes the data returned from the external function.
78
     *
79
     * @return external_single_structure
80
     * @since Moodle 5.0
81
     */
82
    public static function execute_returns(): external_single_structure {
83
        return new external_single_structure([
84
            'content' => new external_value(PARAM_RAW, 'Filtered content'),
85
        ]);
86
    }
87
}