Merge branch 'MDL-63748-master' of git://github.com/jleyva/moodle
[moodle.git] / blocks / classes / external.php
blob10a10a389d22554ce955935889407bf00fb43041
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.
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/>.
17 /**
18 * Blocks external API
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
27 defined('MOODLE_INTERNAL') || die;
29 require_once("$CFG->libdir/externallib.php");
31 /**
32 * Blocks external functions
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
40 class core_block_external extends external_api {
43 /**
44 * Returns a block structure.
46 * @return external_single_structure a block single structure.
47 * @since Moodle 3.6
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),
60 'contents' => new external_single_structure(
61 array(
62 'title' => new external_value(PARAM_TEXT, 'Block title.'),
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.'),
68 'Block contents (if required).', VALUE_OPTIONAL
70 ), 'Block information.'
74 /**
75 * Convenience function for getting all the blocks of the current $PAGE.
77 * @param bool $includeinvisible Whether to include not visible blocks or not
78 * @param bool $returncontents Whether to return the block contents
79 * @return array Block information
80 * @since Moodle 3.6
82 private static function get_all_current_page_blocks($includeinvisible = false, $returncontents = false) {
83 global $PAGE, $OUTPUT;
85 // Load the block instances for all the regions.
86 $PAGE->blocks->load_blocks($includeinvisible);
87 $PAGE->blocks->create_all_block_instances();
89 $allblocks = array();
90 $blocks = $PAGE->blocks->get_content_for_all_regions($OUTPUT);
91 foreach ($blocks as $region => $regionblocks) {
92 $regioninstances = $PAGE->blocks->get_blocks_for_region($region);
93 // Index block instances to retrieve required info.
94 $blockinstances = array();
95 foreach ($regioninstances as $ri) {
96 $blockinstances[$ri->instance->id] = $ri;
99 foreach ($regionblocks as $bc) {
100 $block = [
101 'instanceid' => $bc->blockinstanceid,
102 'name' => $blockinstances[$bc->blockinstanceid]->instance->blockname,
103 'region' => $region,
104 'positionid' => $bc->blockpositionid,
105 'collapsible' => (bool) $bc->collapsible,
106 'dockable' => (bool) $bc->dockable,
107 'weight' => $blockinstances[$bc->blockinstanceid]->instance->weight,
108 'visible' => $blockinstances[$bc->blockinstanceid]->instance->visible,
110 if ($returncontents) {
111 $block['contents'] = (array) $blockinstances[$bc->blockinstanceid]->get_content_for_external($OUTPUT);
113 $allblocks[] = $block;
116 return $allblocks;
120 * Returns description of get_course_blocks parameters.
122 * @return external_function_parameters
123 * @since Moodle 3.3
125 public static function get_course_blocks_parameters() {
126 return new external_function_parameters(
127 array(
128 'courseid' => new external_value(PARAM_INT, 'course id'),
129 'returncontents' => new external_value(PARAM_BOOL, 'Whether to return the block contents.', VALUE_DEFAULT, false),
135 * Returns blocks information for a course.
137 * @param int $courseid The course id
138 * @param bool $returncontents Whether to return the block contents
139 * @return array Blocks list and possible warnings
140 * @throws moodle_exception
141 * @since Moodle 3.3
143 public static function get_course_blocks($courseid, $returncontents = false) {
144 global $PAGE;
146 $warnings = array();
147 $params = self::validate_parameters(self::get_course_blocks_parameters(),
148 ['courseid' => $courseid, 'returncontents' => $returncontents]);
150 $course = get_course($params['courseid']);
151 $context = context_course::instance($course->id);
152 self::validate_context($context);
154 // Specific layout for frontpage course.
155 if ($course->id == SITEID) {
156 $PAGE->set_pagelayout('frontpage');
157 $PAGE->set_pagetype('site-index');
158 } else {
159 $PAGE->set_pagelayout('course');
160 // Ensure course format is set (view course/view.php).
161 $course->format = course_get_format($course)->get_format();
162 $PAGE->set_pagetype('course-view-' . $course->format);
165 $allblocks = self::get_all_current_page_blocks(false, $params['returncontents']);
167 return array(
168 'blocks' => $allblocks,
169 'warnings' => $warnings
174 * Returns description of get_course_blocks result values.
176 * @return external_single_structure
177 * @since Moodle 3.3
179 public static function get_course_blocks_returns() {
181 return new external_single_structure(
182 array(
183 'blocks' => new external_multiple_structure(self::get_block_structure(), 'List of blocks in the course.'),
184 'warnings' => new external_warnings(),
190 * Returns description of get_dashboard_blocks parameters.
192 * @return external_function_parameters
193 * @since Moodle 3.6
195 public static function get_dashboard_blocks_parameters() {
196 return new external_function_parameters(
197 array(
198 'userid' => new external_value(PARAM_INT, 'User id (optional), default is current user.', VALUE_DEFAULT, 0),
199 'returncontents' => new external_value(PARAM_BOOL, 'Whether to return the block contents.', VALUE_DEFAULT, false),
205 * Returns blocks information for the given user dashboard.
207 * @param int $userid The user id to retrive the blocks from, optional, default is to current user.
208 * @param bool $returncontents Whether to return the block contents
209 * @return array Blocks list and possible warnings
210 * @throws moodle_exception
211 * @since Moodle 3.6
213 public static function get_dashboard_blocks($userid = 0, $returncontents = false) {
214 global $CFG, $USER, $PAGE;
216 require_once($CFG->dirroot . '/my/lib.php');
218 $warnings = array();
219 $params = self::validate_parameters(self::get_dashboard_blocks_parameters(),
220 ['userid' => $userid, 'returncontents' => $returncontents]);
222 $userid = $params['userid'];
223 if (empty($userid)) {
224 $userid = $USER->id;
227 if ($USER->id != $userid) {
228 // We must check if the current user can view other users dashboard.
229 require_capability('moodle/site:config', context_system::instance());
230 $user = core_user::get_user($userid, '*', MUST_EXIST);
231 core_user::require_active_user($user);
234 $context = context_user::instance($userid);;
235 self::validate_context($context);
237 // Get the My Moodle page info. Should always return something unless the database is broken.
238 if (!$currentpage = my_get_page($userid, MY_PAGE_PRIVATE)) {
239 throw new moodle_exception('mymoodlesetup');
242 $PAGE->set_context($context);
243 $PAGE->set_pagelayout('mydashboard');
244 $PAGE->set_pagetype('my-index');
245 $PAGE->blocks->add_region('content'); // Need to add this special regition to retrieve the central blocks.
246 $PAGE->set_subpage($currentpage->id);
248 // Load the block instances in the current $PAGE for all the regions.
249 $returninvisible = has_capability('moodle/my:manageblocks', $context) ? true : false;
250 $allblocks = self::get_all_current_page_blocks($returninvisible, $params['returncontents']);
252 return array(
253 'blocks' => $allblocks,
254 'warnings' => $warnings
259 * Returns description of get_dashboard_blocks result values.
261 * @return external_single_structure
262 * @since Moodle 3.6
264 public static function get_dashboard_blocks_returns() {
266 return new external_single_structure(
267 array(
268 'blocks' => new external_multiple_structure(self::get_block_structure(), 'List of blocks in the dashboard.'),
269 'warnings' => new external_warnings(),