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 core\output;
18
 
19
use core_external\external_api;
20
use core_external\external_function_parameters;
21
use core_external\external_multiple_structure;
22
use core_external\external_single_structure;
23
use core_external\external_value;
24
use context_system;
25
use core\external\output\icon_system\load_fontawesome_map;
26
 
27
/**
28
 * This class contains a list of webservice functions related to output.
29
 *
30
 * @package    core
31
 * @copyright  2015 Damyon Wiese
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @since      2.9
34
 */
35
class external extends external_api {
36
    /**
37
     * Returns description of load_template() parameters.
38
     *
39
     * @return external_function_parameters
40
     */
41
    public static function load_template_parameters() {
1441 ariadna 42
        return new external_function_parameters([
43
            'component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
44
            'template' => new external_value(PARAM_SAFEPATH, 'name of the template'),
45
            'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
46
            'includecomments' => new external_value(PARAM_BOOL, 'Include comments or not', VALUE_DEFAULT, false),
47
        ]);
1 efrain 48
    }
49
 
50
    /**
51
     * Return a mustache template, and all the strings it requires.
52
     *
53
     * @param string $component The component that holds the template.
54
     * @param string $templatename The name of the template.
55
     * @param string $themename The name of the current theme.
56
     * @return string the template
57
     */
58
    public static function load_template($component, $template, $themename, $includecomments = false) {
59
        global $DB, $CFG, $PAGE;
60
 
61
        $PAGE->set_context(context_system::instance());
1441 ariadna 62
        $params = self::validate_parameters(
63
            self::load_template_parameters(),
64
            [
65
                'component' => $component,
66
                'template' => $template,
67
                'themename' => $themename,
68
                'includecomments' => $includecomments,
69
            ],
70
        );
1 efrain 71
 
72
        $loader = new mustache_template_source_loader();
73
        // Will throw exceptions if the template does not exist.
74
        return $loader->load(
75
            $params['component'],
76
            $params['template'],
77
            $params['themename'],
1441 ariadna 78
            $params['includecomments'],
1 efrain 79
        );
80
    }
81
 
82
    /**
83
     * Returns description of load_template() result value.
84
     *
85
     * @return \core_external\external_description
86
     */
87
    public static function load_template_returns() {
88
        return new external_value(PARAM_RAW, 'template');
89
    }
90
 
91
    /**
92
     * Returns description of load_template_with_dependencies() parameters.
93
     *
94
     * @return external_function_parameters
95
     */
96
    public static function load_template_with_dependencies_parameters() {
97
        return new external_function_parameters([
98
            'component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
99
            'template' => new external_value(PARAM_SAFEPATH, 'name of the template'),
100
            'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
101
            'includecomments' => new external_value(PARAM_BOOL, 'Include comments or not', VALUE_DEFAULT, false),
102
            'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
103
        ]);
104
    }
105
 
106
    /**
107
     * Return a mustache template, and all the child templates and strings it requires.
108
     *
109
     * @param string $component The component that holds the template.
110
     * @param string $template The name of the template.
111
     * @param string $themename The name of the current theme.
112
     * @param bool $includecomments Whether to strip comments from the template source.
113
     * @param string $lang moodle translation language, null means use current.
1441 ariadna 114
     * @return array the template
1 efrain 115
     */
116
    public static function load_template_with_dependencies(
117
        string $component,
118
        string $template,
119
        string $themename,
120
        bool $includecomments = false,
1441 ariadna 121
        ?string $lang = null
1 efrain 122
    ) {
123
        global $DB, $CFG, $PAGE;
124
 
125
        $params = self::validate_parameters(
126
            self::load_template_with_dependencies_parameters(),
127
            [
128
                'component' => $component,
129
                'template' => $template,
130
                'themename' => $themename,
131
                'includecomments' => $includecomments,
1441 ariadna 132
                'lang' => $lang,
1 efrain 133
            ]
134
        );
135
 
136
        $loader = new mustache_template_source_loader();
137
        // Will throw exceptions if the template does not exist.
138
        $dependencies = $loader->load_with_dependencies(
139
            $params['component'],
140
            $params['template'],
141
            $params['themename'],
142
            $params['includecomments'],
143
            [],
144
            [],
145
            $params['lang']
146
        );
1441 ariadna 147
        $formatdependencies = function ($dependency) {
1 efrain 148
            $results = [];
149
            foreach ($dependency as $dependencycomponent => $dependencyvalues) {
150
                foreach ($dependencyvalues as $dependencyname => $dependencyvalue) {
151
                    array_push($results, [
152
                        'component' => $dependencycomponent,
153
                        'name' => $dependencyname,
1441 ariadna 154
                        'value' => $dependencyvalue,
1 efrain 155
                    ]);
156
                }
157
            }
158
            return $results;
159
        };
160
 
161
        // Now we have to unpack the dependencies into a format that can be returned
162
        // by external functions (because they don't support dynamic keys).
163
        return [
164
            'templates' => $formatdependencies($dependencies['templates']),
1441 ariadna 165
            'strings' => $formatdependencies($dependencies['strings']),
1 efrain 166
        ];
167
    }
168
 
169
    /**
170
     * Returns description of load_template_with_dependencies() result value.
171
     *
172
     * @return \core_external\external_description
173
     */
174
    public static function load_template_with_dependencies_returns() {
175
        $resourcestructure = new external_single_structure([
176
            'component' => new external_value(PARAM_COMPONENT, 'component containing the resource'),
177
            'name' => new external_value(PARAM_TEXT, 'name of the resource'),
1441 ariadna 178
            'value' => new external_value(PARAM_RAW, 'resource value'),
1 efrain 179
        ]);
180
 
181
        return new external_single_structure([
182
            'templates' => new external_multiple_structure($resourcestructure),
1441 ariadna 183
            'strings' => new external_multiple_structure($resourcestructure),
1 efrain 184
        ]);
185
    }
186
}