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 core\route\api;
|
|
|
18 |
|
|
|
19 |
use core\exception;
|
|
|
20 |
use core\param;
|
|
|
21 |
use core\router\route;
|
|
|
22 |
use core\output\mustache_template_source_loader;
|
|
|
23 |
use core\router\schema\response\payload_response;
|
|
|
24 |
use Psr\Http\Message\ResponseInterface;
|
|
|
25 |
use Psr\Http\Message\ServerRequestInterface;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Template Controller.
|
|
|
29 |
*
|
|
|
30 |
* @package core
|
|
|
31 |
* @copyright Andrew Lyons <andrew@nicols.co.uk>
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
class templates {
|
|
|
35 |
use \core\router\route_controller;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Fetch a single template for a component in a theme.
|
|
|
39 |
*
|
|
|
40 |
* @param ResponseInterface $response
|
|
|
41 |
* @param string $themename
|
|
|
42 |
* @param string $component
|
|
|
43 |
* @param null|string $identifier
|
|
|
44 |
* @return payload_response
|
|
|
45 |
*/
|
|
|
46 |
#[route(
|
|
|
47 |
path: '/templates/{themename}/{component}/{identifier:.*}',
|
|
|
48 |
method: ['GET'],
|
|
|
49 |
title: 'Fetch a single template',
|
|
|
50 |
description: 'Fetch a single template for a component in a theme',
|
|
|
51 |
security: [],
|
|
|
52 |
pathtypes: [
|
|
|
53 |
new \core\router\parameters\path_themename(),
|
|
|
54 |
new \core\router\parameters\path_component(),
|
|
|
55 |
new \core\router\schema\parameters\path_parameter(
|
|
|
56 |
name: 'identifier',
|
|
|
57 |
type: param::SAFEPATH,
|
|
|
58 |
),
|
|
|
59 |
],
|
|
|
60 |
queryparams: [
|
|
|
61 |
new \core\router\schema\parameters\query_parameter(
|
|
|
62 |
name: 'includecomments',
|
|
|
63 |
type: param::BOOL,
|
|
|
64 |
description: 'Include comments in the template',
|
|
|
65 |
default: false,
|
|
|
66 |
),
|
|
|
67 |
],
|
|
|
68 |
headerparams: [
|
|
|
69 |
new \core\router\parameters\header_language(),
|
|
|
70 |
],
|
|
|
71 |
responses: [
|
|
|
72 |
new \core\router\schema\response\response(
|
|
|
73 |
statuscode: 200,
|
|
|
74 |
description: 'OK',
|
|
|
75 |
content: [
|
|
|
76 |
new \core\router\schema\response\content\json_media_type(
|
|
|
77 |
schema: new \core\router\schema\objects\schema_object(
|
|
|
78 |
content: [
|
|
|
79 |
'templates' => new \core\router\schema\objects\array_of_strings(
|
|
|
80 |
keyparamtype: param::TEXT,
|
|
|
81 |
valueparamtype: param::RAW,
|
|
|
82 |
),
|
|
|
83 |
'strings' => new \core\router\schema\objects\array_of_strings(
|
|
|
84 |
keyparamtype: param::TEXT,
|
|
|
85 |
valueparamtype: param::RAW,
|
|
|
86 |
),
|
|
|
87 |
],
|
|
|
88 |
),
|
|
|
89 |
examples: [
|
|
|
90 |
new \core\router\schema\example(
|
|
|
91 |
name: 'Single template value',
|
|
|
92 |
summary: 'A json response containing the template for a single template',
|
|
|
93 |
value: [
|
|
|
94 |
'templates' => [
|
|
|
95 |
"mod_example/template_identifier" => "<div class=\"example\">Hello World</div>",
|
|
|
96 |
"mod_example/other_template" => "<div class=\"example\">Hello World</div>",
|
|
|
97 |
],
|
|
|
98 |
'strings' => [
|
|
|
99 |
'core/loading' => 'Loading',
|
|
|
100 |
],
|
|
|
101 |
],
|
|
|
102 |
),
|
|
|
103 |
]
|
|
|
104 |
),
|
|
|
105 |
],
|
|
|
106 |
),
|
|
|
107 |
],
|
|
|
108 |
)]
|
|
|
109 |
public function get_templates(
|
|
|
110 |
ServerRequestInterface $request,
|
|
|
111 |
ResponseInterface $response,
|
|
|
112 |
mustache_template_source_loader $loader,
|
|
|
113 |
string $themename,
|
|
|
114 |
string $component,
|
|
|
115 |
string $identifier,
|
|
|
116 |
): payload_response {
|
|
|
117 |
global $PAGE;
|
|
|
118 |
|
|
|
119 |
$PAGE->set_context(\core\context\system::instance());
|
|
|
120 |
|
|
|
121 |
$params = $request->getQueryParams();
|
|
|
122 |
$comments = $params['includecomments'];
|
|
|
123 |
|
|
|
124 |
try {
|
|
|
125 |
$dependencies = $loader->load_with_dependencies(
|
|
|
126 |
templatecomponent: $component,
|
|
|
127 |
templatename: $identifier,
|
|
|
128 |
themename: $themename,
|
|
|
129 |
includecomments: $comments,
|
|
|
130 |
lang: $request->getHeaderLine('language'),
|
|
|
131 |
);
|
|
|
132 |
} catch (\moodle_exception $e) {
|
|
|
133 |
throw new exception\not_found_exception('template', "{$component}/{$identifier}");
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
$result = [
|
|
|
137 |
'templates' => [],
|
|
|
138 |
'strings' => [],
|
|
|
139 |
];
|
|
|
140 |
|
|
|
141 |
foreach ($dependencies['templates'] as $component => $templates) {
|
|
|
142 |
foreach ($templates as $template => $value) {
|
|
|
143 |
$result['templates']["{$component}/{$template}"] = $value;
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
foreach ($dependencies['strings'] as $component => $templates) {
|
|
|
147 |
foreach ($templates as $template => $value) {
|
|
|
148 |
$result['strings']["{$component}/{$template}"] = $value;
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
return new payload_response(
|
|
|
153 |
payload: $result,
|
|
|
154 |
request: $request,
|
|
|
155 |
);
|
|
|
156 |
}
|
|
|
157 |
}
|