MDL-41551 blocks: added tracking and recognition of custom block regions
[moodle.git] / lib / ajax / blocks.php
CommitLineData
56838156 1<?php
56838156
RK
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 * Provide interface for blocks AJAX actions
19 *
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
23 */
24
25define('AJAX_SCRIPT', true);
26require_once(dirname(__FILE__) . '/../../config.php');
27
28// Initialise ALL common incoming parameters here, up front.
29$courseid = required_param('courseid', PARAM_INT);
30$pagelayout = required_param('pagelayout', PARAM_ALPHAEXT);
31$pagetype = required_param('pagetype', PARAM_ALPHAEXT);
606554d5 32$contextid = required_param('contextid', PARAM_INT);
e893c6d4 33$subpage = optional_param('subpage', '', PARAM_ALPHANUMEXT);
56838156
RK
34$cmid = optional_param('cmid', null, PARAM_INT);
35$action = optional_param('action', '', PARAM_ALPHA);
8496ddd7
SH
36// Params for blocks-move actions.
37$buimoveid = optional_param('bui_moveid', 0, PARAM_INT);
38$buinewregion = optional_param('bui_newregion', '', PARAM_ALPHAEXT);
39$buibeforeid = optional_param('bui_beforeid', 0, PARAM_INT);
56838156 40
8496ddd7 41// Setting pagetype and URL.
56838156 42$PAGE->set_pagetype($pagetype);
70b36b48 43$PAGE->set_url('/lib/ajax/blocks.php', array('courseid' => $courseid, 'pagelayout' => $pagelayout, 'pagetype' => $pagetype));
56838156 44
8496ddd7 45// Verifying login and session.
56838156
RK
46$cm = null;
47if (!is_null($cmid)) {
48 $cm = get_coursemodule_from_id(null, $cmid, $courseid, false, MUST_EXIST);
49}
50require_login($courseid, false, $cm);
51require_sesskey();
52
606554d5
PN
53// Set context from ID, so we don't have to guess it from other info.
54$PAGE->set_context(context::instance_by_id($contextid));
55
8496ddd7 56// Setting layout to replicate blocks configuration for the page we edit.
56838156 57$PAGE->set_pagelayout($pagelayout);
e893c6d4 58$PAGE->set_subpage($subpage);
292dcf04 59$PAGE->blocks->add_custom_regions_for_pagetype($pagetype);
606554d5
PN
60$pagetype = explode('-', $pagetype);
61switch ($pagetype[0]) {
62 case 'my':
606554d5 63 $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
606554d5
PN
64 break;
65 case 'user':
66 if ($pagelayout == 'mydashboard') {
606554d5
PN
67 // If it's not the current user's profile, we need a different capability.
68 if ($PAGE->context->contextlevel == CONTEXT_USER && $PAGE->context->instanceid != $USER->id) {
69 $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
70 } else {
71 $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
72 }
73 }
74 break;
75}
76
8496ddd7
SH
77// Send headers.
78echo $OUTPUT->header();
56838156
RK
79
80switch ($action) {
81 case 'move':
8496ddd7 82 // Loading blocks and instances for the region.
56838156 83 $PAGE->blocks->load_blocks();
8496ddd7 84 $instances = $PAGE->blocks->get_blocks_for_region($buinewregion);
56838156 85
8496ddd7
SH
86 $buinewweight = null;
87 if ($buibeforeid == 0) {
efd556f7
SH
88 if (count($instances) === 0) {
89 // Moving the block into an empty region. Give it the default weight.
8496ddd7 90 $buinewweight = 0;
efd556f7
SH
91 } else {
92 // Moving to very bottom.
93 $last = end($instances);
8496ddd7 94 $buinewweight = $last->instance->weight + 1;
efd556f7 95 }
56838156 96 } else {
8496ddd7 97 // Moving somewhere.
56838156
RK
98 $lastweight = 0;
99 $lastblock = 0;
100 $first = reset($instances);
101 if ($first) {
102 $lastweight = $first->instance->weight - 2;
103 }
104
105 foreach ($instances as $instance) {
8496ddd7
SH
106 if ($instance->instance->id == $buibeforeid) {
107 // Location found, just calculate weight like in block_manager->create_block_contents() and quit the loop.
108 if ($lastblock == $buimoveid) {
109 // Same block, same place - nothing to move.
56838156
RK
110 break;
111 }
8496ddd7 112 $buinewweight = ($lastweight + $instance->instance->weight) / 2;
56838156
RK
113 break;
114 }
115 $lastweight = $instance->instance->weight;
116 $lastblock = $instance->instance->id;
117 }
118 }
119
8496ddd7
SH
120 // Move block if we need.
121 if (isset($buinewweight)) {
122 // Nasty hack.
123 $_POST['bui_newweight'] = $buinewweight;
56838156
RK
124 $PAGE->blocks->process_url_move();
125 }
126 break;
70b36b48 127}