Merge branch 'MDL-78173' of https://github.com/paulholden/moodle
[moodle.git] / lib / ajax / blocks.php
blobd4aae2a82913b6eb6908ee1bbf18df611b411ba9
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 * Provide interface for blocks AJAX actions
20 * @copyright 2011 Lancaster University Network Services Limited
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package core
25 define('AJAX_SCRIPT', true);
26 require_once(__DIR__ . '/../../config.php');
28 // Initialise ALL common incoming parameters here, up front.
29 $pagehash = required_param('pagehash', PARAM_RAW);
30 $action = optional_param('action', '', PARAM_ALPHA);
31 // Params for blocks-move actions.
32 $buimoveid = optional_param('bui_moveid', 0, PARAM_INT);
33 $buinewregion = optional_param('bui_newregion', '', PARAM_ALPHAEXT);
34 $buibeforeid = optional_param('bui_beforeid', 0, PARAM_INT);
36 $PAGE->set_url('/lib/ajax/blocks.php', ['pagehash' => $pagehash]);
38 // Retrieve the edited page from the session hash.
39 $page = moodle_page::retrieve_edited_page($pagehash, MUST_EXIST);
41 // Verifying login and session.
42 $cm = null;
43 if (!is_null($page->cm)) {
44 $cm = get_coursemodule_from_id(null, $page->cm->id, $page->course->id, false, MUST_EXIST);
46 require_login($page->course, false, $cm);
47 require_sesskey();
48 $PAGE->set_context($page->context);
50 if (!$page->user_can_edit_blocks() || !$page->user_is_editing()) {
51 throw new moodle_exception('nopermissions', '', $page->url->out(), get_string('editblock'));
54 // Send headers.
55 echo $OUTPUT->header();
57 switch ($action) {
58 case 'move':
59 // Loading blocks and instances for the region.
60 $page->blocks->load_blocks();
61 $instances = $page->blocks->get_blocks_for_region($buinewregion);
63 $buinewweight = null;
64 if ($buibeforeid == 0) {
65 if (count($instances) === 0) {
66 // Moving the block into an empty region. Give it the default weight.
67 $buinewweight = 0;
68 } else {
69 // Moving to very bottom.
70 $last = end($instances);
71 $buinewweight = $last->instance->weight + 1;
73 } else {
74 // Moving somewhere.
75 $lastweight = 0;
76 $lastblock = 0;
77 $first = reset($instances);
78 if ($first) {
79 $lastweight = $first->instance->weight - 2;
82 foreach ($instances as $instance) {
83 if ($instance->instance->id == $buibeforeid) {
84 // Location found, just calculate weight like in block_manager->create_block_contents() and quit the loop.
85 if ($lastblock == $buimoveid) {
86 // Same block, same place - nothing to move.
87 break;
89 $buinewweight = ($lastweight + $instance->instance->weight) / 2;
90 break;
92 $lastweight = $instance->instance->weight;
93 $lastblock = $instance->instance->id;
97 // Move block if we need.
98 if (isset($buinewweight)) {
99 // Nasty hack.
100 $_POST['bui_newweight'] = $buinewweight;
101 $page->blocks->process_url_move();
103 break;