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 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace core\external;
|
|
|
20 |
|
|
|
21 |
use coding_exception;
|
|
|
22 |
use context_system;
|
|
|
23 |
use core\output\dynamic_tabs\base;
|
|
|
24 |
use core_external\external_api;
|
|
|
25 |
use core_external\external_function_parameters;
|
|
|
26 |
use core_external\external_single_structure;
|
|
|
27 |
use core_external\external_value;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* External method for getting tab contents
|
|
|
31 |
*
|
|
|
32 |
* @package core
|
|
|
33 |
* @copyright 2021 David Matamoros <davidmc@moodle.com> based on code from Marina Glancy
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class dynamic_tabs_get_content extends external_api {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* External method parameters
|
|
|
40 |
*
|
|
|
41 |
* @return external_function_parameters
|
|
|
42 |
*/
|
|
|
43 |
public static function execute_parameters(): external_function_parameters {
|
|
|
44 |
return new external_function_parameters([
|
|
|
45 |
'tab' => new external_value(PARAM_RAW_TRIMMED, 'Tab class', VALUE_REQUIRED),
|
|
|
46 |
'jsondata' => new external_value(PARAM_RAW, 'Json-encoded data', VALUE_REQUIRED),
|
|
|
47 |
]);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Tab content
|
|
|
52 |
*
|
|
|
53 |
* @param string $tabclass class of the tab
|
|
|
54 |
* @param string $jsondata
|
|
|
55 |
* @return array
|
|
|
56 |
*/
|
|
|
57 |
public static function execute(string $tabclass, string $jsondata): array {
|
|
|
58 |
global $PAGE, $OUTPUT;
|
|
|
59 |
|
|
|
60 |
[
|
|
|
61 |
'tab' => $tabclass,
|
|
|
62 |
'jsondata' => $jsondata,
|
|
|
63 |
] = self::validate_parameters(self::execute_parameters(), [
|
|
|
64 |
'tab' => $tabclass,
|
|
|
65 |
'jsondata' => $jsondata,
|
|
|
66 |
]);
|
|
|
67 |
|
|
|
68 |
$data = @json_decode($jsondata, true);
|
|
|
69 |
|
|
|
70 |
$context = context_system::instance();
|
|
|
71 |
self::validate_context($context);
|
|
|
72 |
|
|
|
73 |
// This call is needed to avoid debug messages on webserver log.
|
|
|
74 |
$PAGE->set_url('/');
|
|
|
75 |
// This call is needed to initiate moodle page.
|
|
|
76 |
$OUTPUT->header();
|
|
|
77 |
|
|
|
78 |
if (!class_exists($tabclass) || !is_subclass_of($tabclass, base::class)) {
|
|
|
79 |
throw new coding_exception('unknown dynamic tab class', $tabclass);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/** @var base $tab */
|
|
|
83 |
$tab = new $tabclass($data);
|
|
|
84 |
$tab->require_access();
|
|
|
85 |
$PAGE->start_collecting_javascript_requirements();
|
|
|
86 |
|
|
|
87 |
$content = $tab->export_for_template($PAGE->get_renderer('core'));
|
|
|
88 |
$jsfooter = $PAGE->requires->get_end_code();
|
|
|
89 |
return [
|
|
|
90 |
'template' => $tab->get_template(),
|
|
|
91 |
'content' => json_encode($content),
|
|
|
92 |
'javascript' => $jsfooter,
|
|
|
93 |
];
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* External method return value
|
|
|
98 |
*
|
|
|
99 |
* @return external_single_structure
|
|
|
100 |
*/
|
|
|
101 |
public static function execute_returns(): external_single_structure {
|
|
|
102 |
return new external_single_structure([
|
|
|
103 |
'template' => new external_value(PARAM_PATH, 'Template name'),
|
|
|
104 |
'content' => new external_value(PARAM_RAW, 'JSON-encoded data for template'),
|
|
|
105 |
'javascript' => new external_value(PARAM_RAW, 'JavaScript fragment'),
|
|
|
106 |
]);
|
|
|
107 |
}
|
|
|
108 |
}
|