forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal.php
297 lines (263 loc) · 11.7 KB
/
external.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Blocks external API
*
* @package core_block
* @category external
* @copyright 2017 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.3
*/
defined('MOODLE_INTERNAL') || die;
require_once("$CFG->libdir/externallib.php");
/**
* Blocks external functions
*
* @package core_block
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.3
*/
class core_block_external extends external_api {
/**
* Returns a block structure.
*
* @return external_single_structure a block single structure.
* @since Moodle 3.6
*/
private static function get_block_structure() {
return new external_single_structure(
array(
'instanceid' => new external_value(PARAM_INT, 'Block instance id.'),
'name' => new external_value(PARAM_PLUGIN, 'Block name.'),
'region' => new external_value(PARAM_ALPHANUMEXT, 'Block region.'),
'positionid' => new external_value(PARAM_INT, 'Position id.'),
'collapsible' => new external_value(PARAM_BOOL, 'Whether the block is collapsible.'),
'dockable' => new external_value(PARAM_BOOL, 'Whether the block is dockable.'),
'weight' => new external_value(PARAM_INT, 'Used to order blocks within a region.', VALUE_OPTIONAL),
'visible' => new external_value(PARAM_BOOL, 'Whether the block is visible.', VALUE_OPTIONAL),
'contents' => new external_single_structure(
array(
'title' => new external_value(PARAM_RAW, 'Block title.'),
'content' => new external_value(PARAM_RAW, 'Block contents.'),
'contentformat' => new external_format_value('content'),
'footer' => new external_value(PARAM_RAW, 'Block footer.'),
'files' => new external_files('Block files.'),
),
'Block contents (if required).', VALUE_OPTIONAL
),
'configs' => new external_multiple_structure(
new external_single_structure(
array(
'name' => new external_value(PARAM_RAW, 'Name.'),
'value' => new external_value(PARAM_RAW, 'JSON encoded representation of the config value.'),
'type' => new external_value(PARAM_ALPHA, 'Type (instance or plugin).'),
)
),
'Block instance and plugin configuration settings.', VALUE_OPTIONAL
),
), 'Block information.'
);
}
/**
* Convenience function for getting all the blocks of the current $PAGE.
*
* @param bool $includeinvisible Whether to include not visible blocks or not
* @param bool $returncontents Whether to return the block contents
* @return array Block information
* @since Moodle 3.6
*/
private static function get_all_current_page_blocks($includeinvisible = false, $returncontents = false) {
global $PAGE, $OUTPUT;
// Set page URL to a fake URL to avoid errors.
$PAGE->set_url(new \moodle_url('/webservice/core_block_external/'));
// Load the block instances for all the regions.
$PAGE->blocks->load_blocks($includeinvisible);
$PAGE->blocks->create_all_block_instances();
$allblocks = array();
$blocks = $PAGE->blocks->get_content_for_all_regions($OUTPUT);
foreach ($blocks as $region => $regionblocks) {
$regioninstances = $PAGE->blocks->get_blocks_for_region($region);
// Index block instances to retrieve required info.
$blockinstances = array();
foreach ($regioninstances as $ri) {
$blockinstances[$ri->instance->id] = $ri;
}
foreach ($regionblocks as $bc) {
$block = [
'instanceid' => $bc->blockinstanceid,
'name' => $blockinstances[$bc->blockinstanceid]->instance->blockname,
'region' => $region,
'positionid' => $bc->blockpositionid,
'collapsible' => (bool) $bc->collapsible,
'dockable' => (bool) $bc->dockable,
'weight' => $blockinstances[$bc->blockinstanceid]->instance->weight,
'visible' => $blockinstances[$bc->blockinstanceid]->instance->visible,
];
if ($returncontents) {
$block['contents'] = (array) $blockinstances[$bc->blockinstanceid]->get_content_for_external($OUTPUT);
}
$configs = (array) $blockinstances[$bc->blockinstanceid]->get_config_for_external();
foreach ($configs as $type => $data) {
foreach ((array) $data as $name => $value) {
$block['configs'][] = [
'name' => $name,
'value' => json_encode($value), // Always JSON encode, we may receive non-scalar values.
'type' => $type,
];
}
}
$allblocks[] = $block;
}
}
return $allblocks;
}
/**
* Returns description of get_course_blocks parameters.
*
* @return external_function_parameters
* @since Moodle 3.3
*/
public static function get_course_blocks_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'course id'),
'returncontents' => new external_value(PARAM_BOOL, 'Whether to return the block contents.', VALUE_DEFAULT, false),
)
);
}
/**
* Returns blocks information for a course.
*
* @param int $courseid The course id
* @param bool $returncontents Whether to return the block contents
* @return array Blocks list and possible warnings
* @throws moodle_exception
* @since Moodle 3.3
*/
public static function get_course_blocks($courseid, $returncontents = false) {
global $PAGE;
$warnings = array();
$params = self::validate_parameters(self::get_course_blocks_parameters(),
['courseid' => $courseid, 'returncontents' => $returncontents]);
$course = get_course($params['courseid']);
$context = context_course::instance($course->id);
self::validate_context($context);
// Specific layout for frontpage course.
if ($course->id == SITEID) {
$PAGE->set_pagelayout('frontpage');
$PAGE->set_pagetype('site-index');
} else {
$PAGE->set_pagelayout('course');
// Ensure course format is set (view course/view.php).
$course->format = course_get_format($course)->get_format();
$PAGE->set_pagetype('course-view-' . $course->format);
}
$allblocks = self::get_all_current_page_blocks(false, $params['returncontents']);
return array(
'blocks' => $allblocks,
'warnings' => $warnings
);
}
/**
* Returns description of get_course_blocks result values.
*
* @return external_single_structure
* @since Moodle 3.3
*/
public static function get_course_blocks_returns() {
return new external_single_structure(
array(
'blocks' => new external_multiple_structure(self::get_block_structure(), 'List of blocks in the course.'),
'warnings' => new external_warnings(),
)
);
}
/**
* Returns description of get_dashboard_blocks parameters.
*
* @return external_function_parameters
* @since Moodle 3.6
*/
public static function get_dashboard_blocks_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'User id (optional), default is current user.', VALUE_DEFAULT, 0),
'returncontents' => new external_value(PARAM_BOOL, 'Whether to return the block contents.', VALUE_DEFAULT, false),
)
);
}
/**
* Returns blocks information for the given user dashboard.
*
* @param int $userid The user id to retrive the blocks from, optional, default is to current user.
* @param bool $returncontents Whether to return the block contents
* @return array Blocks list and possible warnings
* @throws moodle_exception
* @since Moodle 3.6
*/
public static function get_dashboard_blocks($userid = 0, $returncontents = false) {
global $CFG, $USER, $PAGE;
require_once($CFG->dirroot . '/my/lib.php');
$warnings = array();
$params = self::validate_parameters(self::get_dashboard_blocks_parameters(),
['userid' => $userid, 'returncontents' => $returncontents]);
$userid = $params['userid'];
if (empty($userid)) {
$userid = $USER->id;
}
if ($USER->id != $userid) {
// We must check if the current user can view other users dashboard.
require_capability('moodle/site:config', context_system::instance());
$user = core_user::get_user($userid, '*', MUST_EXIST);
core_user::require_active_user($user);
}
$context = context_user::instance($userid);;
self::validate_context($context);
// Get the My Moodle page info. Should always return something unless the database is broken.
if (!$currentpage = my_get_page($userid, MY_PAGE_PRIVATE)) {
throw new moodle_exception('mymoodlesetup');
}
$PAGE->set_context($context);
$PAGE->set_pagelayout('mydashboard');
$PAGE->set_pagetype('my-index');
$PAGE->blocks->add_region('content'); // Need to add this special regition to retrieve the central blocks.
$PAGE->set_subpage($currentpage->id);
// Load the block instances in the current $PAGE for all the regions.
$returninvisible = has_capability('moodle/my:manageblocks', $context) ? true : false;
$allblocks = self::get_all_current_page_blocks($returninvisible, $params['returncontents']);
return array(
'blocks' => $allblocks,
'warnings' => $warnings
);
}
/**
* Returns description of get_dashboard_blocks result values.
*
* @return external_single_structure
* @since Moodle 3.6
*/
public static function get_dashboard_blocks_returns() {
return new external_single_structure(
array(
'blocks' => new external_multiple_structure(self::get_block_structure(), 'List of blocks in the dashboard.'),
'warnings' => new external_warnings(),
)
);
}
}