MDL-65788 tool_policy: Prevent two modals rendering for guests.
[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(
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.'),
67 ),
68 'Block contents (if required).', VALUE_OPTIONAL
69 ),
0f8b2604
JL
70 ), 'Block information.'
71 );
72 }
73
74 /**
75 * Convenience function for getting all the blocks of the current $PAGE.
76 *
77 * @param bool $includeinvisible Whether to include not visible blocks or not
cde3dc05 78 * @param bool $returncontents Whether to return the block contents
0f8b2604
JL
79 * @return array Block information
80 * @since Moodle 3.6
81 */
cde3dc05 82 private static function get_all_current_page_blocks($includeinvisible = false, $returncontents = false) {
0f8b2604
JL
83 global $PAGE, $OUTPUT;
84
85 // Load the block instances for all the regions.
86 $PAGE->blocks->load_blocks($includeinvisible);
87 $PAGE->blocks->create_all_block_instances();
88
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) {
cde3dc05 96 $blockinstances[$ri->instance->id] = $ri;
0f8b2604
JL
97 }
98
99 foreach ($regionblocks as $bc) {
cde3dc05 100 $block = [
0f8b2604 101 'instanceid' => $bc->blockinstanceid,
cde3dc05 102 'name' => $blockinstances[$bc->blockinstanceid]->instance->blockname,
0f8b2604
JL
103 'region' => $region,
104 'positionid' => $bc->blockpositionid,
105 'collapsible' => (bool) $bc->collapsible,
106 'dockable' => (bool) $bc->dockable,
cde3dc05
JL
107 'weight' => $blockinstances[$bc->blockinstanceid]->instance->weight,
108 'visible' => $blockinstances[$bc->blockinstanceid]->instance->visible,
0f8b2604 109 ];
cde3dc05
JL
110 if ($returncontents) {
111 $block['contents'] = (array) $blockinstances[$bc->blockinstanceid]->get_content_for_external($OUTPUT);
112 }
113 $allblocks[] = $block;
0f8b2604
JL
114 }
115 }
116 return $allblocks;
117 }
118
8446ba36
JL
119 /**
120 * Returns description of get_course_blocks parameters.
121 *
122 * @return external_function_parameters
123 * @since Moodle 3.3
124 */
125 public static function get_course_blocks_parameters() {
126 return new external_function_parameters(
127 array(
cde3dc05
JL
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),
8446ba36
JL
130 )
131 );
132 }
133
134 /**
135 * Returns blocks information for a course.
136 *
137 * @param int $courseid The course id
cde3dc05 138 * @param bool $returncontents Whether to return the block contents
8446ba36
JL
139 * @return array Blocks list and possible warnings
140 * @throws moodle_exception
141 * @since Moodle 3.3
142 */
cde3dc05 143 public static function get_course_blocks($courseid, $returncontents = false) {
0f8b2604 144 global $PAGE;
8446ba36
JL
145
146 $warnings = array();
cde3dc05
JL
147 $params = self::validate_parameters(self::get_course_blocks_parameters(),
148 ['courseid' => $courseid, 'returncontents' => $returncontents]);
8446ba36
JL
149
150 $course = get_course($params['courseid']);
151 $context = context_course::instance($course->id);
152 self::validate_context($context);
153
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);
163 }
164
cde3dc05 165 $allblocks = self::get_all_current_page_blocks(false, $params['returncontents']);
8446ba36
JL
166
167 return array(
0f8b2604 168 'blocks' => $allblocks,
8446ba36
JL
169 'warnings' => $warnings
170 );
171 }
172
173 /**
174 * Returns description of get_course_blocks result values.
175 *
176 * @return external_single_structure
177 * @since Moodle 3.3
178 */
179 public static function get_course_blocks_returns() {
180
181 return new external_single_structure(
182 array(
0f8b2604 183 'blocks' => new external_multiple_structure(self::get_block_structure(), 'List of blocks in the course.'),
8446ba36
JL
184 'warnings' => new external_warnings(),
185 )
186 );
187 }
188
0f8b2604
JL
189 /**
190 * Returns description of get_dashboard_blocks parameters.
191 *
192 * @return external_function_parameters
193 * @since Moodle 3.6
194 */
195 public static function get_dashboard_blocks_parameters() {
196 return new external_function_parameters(
197 array(
cde3dc05
JL
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),
0f8b2604
JL
200 )
201 );
202 }
203
204 /**
205 * Returns blocks information for the given user dashboard.
206 *
207 * @param int $userid The user id to retrive the blocks from, optional, default is to current user.
cde3dc05 208 * @param bool $returncontents Whether to return the block contents
0f8b2604
JL
209 * @return array Blocks list and possible warnings
210 * @throws moodle_exception
211 * @since Moodle 3.6
212 */
cde3dc05 213 public static function get_dashboard_blocks($userid = 0, $returncontents = false) {
0f8b2604
JL
214 global $CFG, $USER, $PAGE;
215
216 require_once($CFG->dirroot . '/my/lib.php');
217
218 $warnings = array();
cde3dc05
JL
219 $params = self::validate_parameters(self::get_dashboard_blocks_parameters(),
220 ['userid' => $userid, 'returncontents' => $returncontents]);
0f8b2604
JL
221
222 $userid = $params['userid'];
223 if (empty($userid)) {
224 $userid = $USER->id;
225 }
226
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);
232 }
233
234 $context = context_user::instance($userid);;
235 self::validate_context($context);
236
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');
240 }
241
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);
247
248 // Load the block instances in the current $PAGE for all the regions.
249 $returninvisible = has_capability('moodle/my:manageblocks', $context) ? true : false;
cde3dc05 250 $allblocks = self::get_all_current_page_blocks($returninvisible, $params['returncontents']);
0f8b2604
JL
251
252 return array(
253 'blocks' => $allblocks,
254 'warnings' => $warnings
255 );
256 }
257
258 /**
259 * Returns description of get_dashboard_blocks result values.
260 *
261 * @return external_single_structure
262 * @since Moodle 3.6
263 */
264 public static function get_dashboard_blocks_returns() {
265
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(),
270 )
271 );
272 }
8446ba36 273}