Merge branch 'install_33_STABLE' of https://git.in.moodle.com/amosbot/moodle-install...
[moodle.git] / blocks / classes / external.php
blob95de3b808e9ac63be3b56b2474e8af256351bf00
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 {
42 /**
43 * Returns description of get_course_blocks parameters.
45 * @return external_function_parameters
46 * @since Moodle 3.3
48 public static function get_course_blocks_parameters() {
49 return new external_function_parameters(
50 array(
51 'courseid' => new external_value(PARAM_INT, 'course id')
56 /**
57 * Returns blocks information for a course.
59 * @param int $courseid The course id
60 * @return array Blocks list and possible warnings
61 * @throws moodle_exception
62 * @since Moodle 3.3
64 public static function get_course_blocks($courseid) {
65 global $OUTPUT, $PAGE;
67 $warnings = array();
68 $params = self::validate_parameters(self::get_course_blocks_parameters(), ['courseid' => $courseid]);
70 $course = get_course($params['courseid']);
71 $context = context_course::instance($course->id);
72 self::validate_context($context);
74 // Specific layout for frontpage course.
75 if ($course->id == SITEID) {
76 $PAGE->set_pagelayout('frontpage');
77 $PAGE->set_pagetype('site-index');
78 } else {
79 $PAGE->set_pagelayout('course');
80 // Ensure course format is set (view course/view.php).
81 $course->format = course_get_format($course)->get_format();
82 $PAGE->set_pagetype('course-view-' . $course->format);
85 // Load the block instances for all the regions.
86 $PAGE->blocks->load_blocks();
87 $PAGE->blocks->create_all_block_instances();
89 $finalblocks = array();
90 $blocks = $PAGE->blocks->get_content_for_all_regions($OUTPUT);
91 foreach ($blocks as $region => $regionblocks) {
92 foreach ($regionblocks as $bc) {
93 $finalblocks[] = [
94 'instanceid' => $bc->blockinstanceid,
95 'name' => $bc->attributes['data-block'],
96 'region' => $region,
97 'positionid' => $bc->blockpositionid,
98 'collapsible' => (bool) $bc->collapsible,
99 'dockable' => (bool) $bc->dockable,
104 return array(
105 'blocks' => $finalblocks,
106 'warnings' => $warnings
111 * Returns description of get_course_blocks result values.
113 * @return external_single_structure
114 * @since Moodle 3.3
116 public static function get_course_blocks_returns() {
118 return new external_single_structure(
119 array(
120 'blocks' => new external_multiple_structure(
121 new external_single_structure(
122 array(
123 'instanceid' => new external_value(PARAM_INT, 'Block instance id.'),
124 'name' => new external_value(PARAM_PLUGIN, 'Block name.'),
125 'region' => new external_value(PARAM_ALPHANUMEXT, 'Block region.'),
126 'positionid' => new external_value(PARAM_INT, 'Position id.'),
127 'collapsible' => new external_value(PARAM_BOOL, 'Whether the block is collapsible.'),
128 'dockable' => new external_value(PARAM_BOOL, 'hether the block is dockable.'),
129 ), 'Block information.'
130 ), 'List of blocks in the course.'
132 'warnings' => new external_warnings(),