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