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 |
* External API.
|
|
|
19 |
*
|
|
|
20 |
* @package block_dash
|
|
|
21 |
* @copyright 2019 bdecent gmbh <https://bdecent.de>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace block_dash;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die('No direct access');
|
|
|
28 |
|
|
|
29 |
require_once("$CFG->libdir/externallib.php");
|
|
|
30 |
|
|
|
31 |
use block_dash\local\block_builder;
|
|
|
32 |
use block_dash\local\data_source\form\preferences_form;
|
|
|
33 |
use block_dash\output\renderer;
|
|
|
34 |
use block_dash\local\configuration\configuration;
|
|
|
35 |
use block_dash\local\data_source\data_source_factory;
|
|
|
36 |
use external_api;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* External API class.
|
|
|
40 |
*
|
|
|
41 |
* @package block_dash
|
|
|
42 |
* @copyright 2019 bdecent gmbh <https://bdecent.de>
|
|
|
43 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
44 |
*/
|
|
|
45 |
class external extends external_api {
|
|
|
46 |
// Region get_block_content.
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Returns description of get_database_schema_structure() parameters.
|
|
|
50 |
*
|
|
|
51 |
* @return \external_function_parameters
|
|
|
52 |
*/
|
|
|
53 |
public static function get_block_content_parameters() {
|
|
|
54 |
return new \external_function_parameters([
|
|
|
55 |
'block_instance_id' => new \external_value(PARAM_INT),
|
|
|
56 |
'filter_form_data' => new \external_value(PARAM_RAW),
|
|
|
57 |
'page' => new \external_value(PARAM_INT, 'Paginator page.', VALUE_DEFAULT, 0),
|
|
|
58 |
'sort_field' => new \external_value(PARAM_TEXT, 'Field to sort by', VALUE_DEFAULT, null),
|
|
|
59 |
'sort_direction' => new \external_value(PARAM_TEXT, 'Sort direction of field', VALUE_DEFAULT, null),
|
|
|
60 |
'pagelayout' => new \external_value(PARAM_TEXT, 'pagelayout', VALUE_DEFAULT, ''),
|
|
|
61 |
'pagecontext' => new \external_value(PARAM_INT, 'Page Context', VALUE_DEFAULT, 0),
|
|
|
62 |
]);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Get block content.
|
|
|
67 |
*
|
|
|
68 |
* @param int $blockinstanceid
|
|
|
69 |
* @param string $filterformdata
|
|
|
70 |
* @param int $page
|
|
|
71 |
* @param string $sortfield
|
|
|
72 |
* @param string $sortdirection
|
|
|
73 |
* @param string $pagelayout
|
|
|
74 |
* @param int $pagecontext
|
|
|
75 |
*
|
|
|
76 |
* @return array
|
|
|
77 |
* @throws \coding_exception
|
|
|
78 |
* @throws \invalid_parameter_exception
|
|
|
79 |
* @throws \moodle_exception
|
|
|
80 |
* @throws \restricted_context_exception
|
|
|
81 |
*/
|
|
|
82 |
public static function get_block_content($blockinstanceid, $filterformdata, $page, $sortfield, $sortdirection,
|
|
|
83 |
$pagelayout = '', $pagecontext = 0) {
|
|
|
84 |
global $PAGE, $DB, $OUTPUT, $SITE;
|
|
|
85 |
|
|
|
86 |
$params = self::validate_parameters(self::get_block_content_parameters(), [
|
|
|
87 |
'block_instance_id' => $blockinstanceid,
|
|
|
88 |
'page' => $page,
|
|
|
89 |
'filter_form_data' => $filterformdata,
|
|
|
90 |
'sort_field' => $sortfield,
|
|
|
91 |
'sort_direction' => $sortdirection,
|
|
|
92 |
'pagelayout' => $pagelayout,
|
|
|
93 |
'pagecontext' => $pagecontext,
|
|
|
94 |
]);
|
|
|
95 |
|
|
|
96 |
if ($pagecontext) {
|
|
|
97 |
$context = \context::instance_by_id($pagecontext);
|
|
|
98 |
$PAGE->set_context($context);
|
|
|
99 |
}
|
|
|
100 |
if ($pagelayout) {
|
|
|
101 |
$PAGE->set_pagelayout($pagelayout);
|
|
|
102 |
}
|
|
|
103 |
$public = false;
|
|
|
104 |
$blockinstance = $DB->get_record('block_instances', ['id' => $params['block_instance_id']]);
|
|
|
105 |
$block = block_instance($blockinstance->blockname, $blockinstance);
|
|
|
106 |
if (strpos($block->instance->pagetypepattern, 'dashaddon-dashboard') !== false) {
|
|
|
107 |
if ($dashboard = \dashaddon_dashboard\model\dashboard::get_record(
|
|
|
108 |
['shortname' => $block->instance->defaultregion])) {
|
|
|
109 |
if ($dashboard->get('permission') == \dashaddon_dashboard\model\dashboard::PERMISSION_PUBLIC) {
|
|
|
110 |
$public = true;
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if (!$public) {
|
|
|
116 |
// Verify the block created for frontpage. and user not loggedin allow to access the block content.
|
|
|
117 |
list($unused, $course, $cm) = get_context_info_array($block->context->id);
|
|
|
118 |
if (isset($course->id) && $course->id == $SITE->id && !isloggedin()) {
|
|
|
119 |
require_course_login($course);
|
|
|
120 |
$coursecontext = \context_course::instance($course->id);
|
|
|
121 |
$PAGE->set_context($coursecontext);
|
|
|
122 |
} else {
|
|
|
123 |
self::validate_context($block->context);
|
|
|
124 |
}
|
|
|
125 |
} else {
|
|
|
126 |
$PAGE->set_context($block->context);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/** @var renderer $renderer */
|
|
|
130 |
$renderer = $PAGE->get_renderer('block_dash');
|
|
|
131 |
|
|
|
132 |
if ($block) {
|
|
|
133 |
if ($params['sort_field']) {
|
|
|
134 |
$block->set_sort($params['sort_field'], $params['sort_direction']);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
$bb = block_builder::create($block);
|
|
|
138 |
foreach (json_decode($params['filter_form_data'], true) as $filter) {
|
|
|
139 |
$bb->get_configuration()
|
|
|
140 |
->get_data_source()
|
|
|
141 |
->get_filter_collection()
|
|
|
142 |
->apply_filter($filter['name'], $filter['value']);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
$datasource = $bb->get_configuration()->get_data_source();
|
|
|
146 |
|
|
|
147 |
$bb->get_configuration()->get_data_source()->get_paginator()->set_current_page($params['page']);
|
|
|
148 |
if (get_class($datasource->get_layout()) == 'local_dash\layout\cards_layout' || $datasource->is_widget()
|
|
|
149 |
&& $datasource->supports_currentscript()) {
|
|
|
150 |
// Cloned from moodle lib\external\externalib.php 422.
|
|
|
151 |
// Hack alert: Set a default URL to stop the annoying debug.
|
|
|
152 |
$PAGE->set_url('/');
|
|
|
153 |
// Hack alert: Forcing bootstrap_renderer to initiate moodle page.
|
|
|
154 |
$OUTPUT->header();
|
|
|
155 |
|
|
|
156 |
$PAGE->start_collecting_javascript_requirements();
|
|
|
157 |
|
|
|
158 |
$datarendered = $renderer->render_data_source($bb->get_configuration()->get_data_source());
|
|
|
159 |
|
|
|
160 |
$javascript = $PAGE->requires->get_end_code();
|
|
|
161 |
} else {
|
|
|
162 |
$datarendered = $renderer->render_data_source($bb->get_configuration()->get_data_source());
|
|
|
163 |
$javascript = '';
|
|
|
164 |
}
|
|
|
165 |
return ['html' => $datarendered, 'scripts' => $javascript];
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
return ['html' => 'Error', 'scripts' => ''];
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Returns description of get_block_content() result value.
|
|
|
173 |
*
|
|
|
174 |
* @return \external_description
|
|
|
175 |
*/
|
|
|
176 |
public static function get_block_content_returns() {
|
|
|
177 |
return new \external_single_structure([
|
|
|
178 |
'html' => new \external_value(PARAM_RAW),
|
|
|
179 |
'scripts' => new \external_value(PARAM_RAW),
|
|
|
180 |
]);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
// Endregion.
|
|
|
184 |
|
|
|
185 |
// Region submit_preferences_form.
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Describes the parameters for submit_create_group_form webservice.
|
|
|
189 |
* @return \external_function_parameters
|
|
|
190 |
*/
|
|
|
191 |
public static function submit_preferences_form_parameters() {
|
|
|
192 |
return new \external_function_parameters([
|
|
|
193 |
'contextid' => new \external_value(PARAM_INT, 'The context id for the block'),
|
|
|
194 |
'jsonformdata' => new \external_value(PARAM_RAW, 'The form data encoded as a json array'),
|
|
|
195 |
]);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Submit the preferences form.
|
|
|
200 |
*
|
|
|
201 |
* @param int $contextid The context id for the course.
|
|
|
202 |
* @param string $jsonformdata The data from the form, encoded as a json array.
|
|
|
203 |
* @return array
|
|
|
204 |
* @throws \invalid_parameter_exception
|
|
|
205 |
* @throws \coding_exception
|
|
|
206 |
* @throws \required_capability_exception
|
|
|
207 |
* @throws \moodle_exception
|
|
|
208 |
*/
|
|
|
209 |
public static function submit_preferences_form($contextid, $jsonformdata) {
|
|
|
210 |
global $DB;
|
|
|
211 |
|
|
|
212 |
$params = self::validate_parameters(self::submit_preferences_form_parameters(), [
|
|
|
213 |
'contextid' => $contextid,
|
|
|
214 |
'jsonformdata' => $jsonformdata,
|
|
|
215 |
]);
|
|
|
216 |
|
|
|
217 |
$context = \context::instance_by_id($params['contextid'], MUST_EXIST);
|
|
|
218 |
|
|
|
219 |
self::validate_context($context);
|
|
|
220 |
require_capability('block/dash:addinstance', $context);
|
|
|
221 |
|
|
|
222 |
$serialiseddata = json_decode($params['jsonformdata']);
|
|
|
223 |
$data = [];
|
|
|
224 |
parse_str($serialiseddata, $data);
|
|
|
225 |
$blockinstance = $DB->get_record('block_instances', ['id' => $context->instanceid]);
|
|
|
226 |
$block = block_instance($blockinstance->blockname, $blockinstance);
|
|
|
227 |
if (!empty($block->config)) {
|
|
|
228 |
$config = clone($block->config);
|
|
|
229 |
} else {
|
|
|
230 |
$config = new \stdClass;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
if (!isset($config->preferences)) {
|
|
|
234 |
$config->preferences = [];
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
if (!isset($data['config_preferences']) || is_null($data['config_preferences'])) {
|
|
|
238 |
$data['config_preferences'] = [];
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
if (isset($data['config_data_source_idnumber'])) {
|
|
|
242 |
$config->data_source_idnumber = $data['config_data_source_idnumber'];
|
|
|
243 |
$datasource = data_source_factory::build_data_source($config->data_source_idnumber,
|
|
|
244 |
$context);
|
|
|
245 |
if ($datasource) {
|
|
|
246 |
if (method_exists($datasource, 'set_default_preferences')) {
|
|
|
247 |
$datasource->set_default_preferences($data);
|
|
|
248 |
}
|
|
|
249 |
}
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
$configpreferences = $data['config_preferences'];
|
|
|
253 |
$config->preferences = self::recursive_config_merge($config->preferences, $configpreferences, '');
|
|
|
254 |
$block->instance_config_save($config);
|
|
|
255 |
|
|
|
256 |
return [
|
|
|
257 |
'validationerrors' => false,
|
|
|
258 |
];
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Recursively merge in new config.
|
|
|
263 |
*
|
|
|
264 |
* @param string $existingconfig
|
|
|
265 |
* @param array|object $newconfig
|
|
|
266 |
* @param bool $recursive
|
|
|
267 |
* @return mixed
|
|
|
268 |
*/
|
|
|
269 |
private static function recursive_config_merge($existingconfig, $newconfig, $recursive = false) {
|
|
|
270 |
// If existing config is a scalar value than always overwrite. No point in looping new config.
|
|
|
271 |
// This allows preferences that were a scalar to be assigned as arrays by new preferences.
|
|
|
272 |
if (is_scalar($existingconfig)) {
|
|
|
273 |
return $newconfig;
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
// If array contains only scalars, overwrite with new config. No more looping required for this level.
|
|
|
277 |
if (is_array($existingconfig) && !self::is_array_multidimensional($existingconfig)) {
|
|
|
278 |
if (!$recursive) {
|
|
|
279 |
return array_merge($existingconfig, $newconfig);
|
|
|
280 |
} else {
|
|
|
281 |
return $newconfig;
|
|
|
282 |
}
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
// Recursively overwrite values.
|
|
|
286 |
foreach ($newconfig as $key => $value) {
|
|
|
287 |
if (is_scalar($value)) {
|
|
|
288 |
$existingconfig[$key] = $value;
|
|
|
289 |
} else if (is_array($value)) {
|
|
|
290 |
$v = self::recursive_config_merge(isset($existingconfig[$key]) ? $existingconfig[$key]
|
|
|
291 |
: [], $newconfig[$key], true);
|
|
|
292 |
unset($existingconfig[$key]);
|
|
|
293 |
$existingconfig[$key] = $v;
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
return $existingconfig;
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
/**
|
|
|
301 |
* Check if array is multidimensional. True if it contains an array, false meaning all values are scalar.
|
|
|
302 |
*
|
|
|
303 |
* @param array $array
|
|
|
304 |
* @return bool
|
|
|
305 |
*/
|
|
|
306 |
private static function is_array_multidimensional(array $array): bool {
|
|
|
307 |
foreach ($array as $element) {
|
|
|
308 |
if (is_array($element)) {
|
|
|
309 |
return true;
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
return false;
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
/**
|
|
|
317 |
* Returns description of method result value.
|
|
|
318 |
*
|
|
|
319 |
* @return \external_description
|
|
|
320 |
* @since Moodle 3.0
|
|
|
321 |
*/
|
|
|
322 |
public static function submit_preferences_form_returns() {
|
|
|
323 |
return new \external_single_structure([
|
|
|
324 |
'validationerrors' => new \external_value(PARAM_BOOL, 'Were there validation errors', VALUE_REQUIRED),
|
|
|
325 |
]);
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
// Endregion.
|
|
|
329 |
}
|