2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
25 define('AJAX_SCRIPT', true);
26 require_once(__DIR__
. '/../../config.php');
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_ALPHANUMEXT
);
32 $contextid = required_param('contextid', PARAM_INT
);
33 $subpage = optional_param('subpage', '', PARAM_ALPHANUMEXT
);
34 $cmid = optional_param('cmid', null, PARAM_INT
);
35 $action = optional_param('action', '', PARAM_ALPHA
);
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
);
41 // Setting pagetype and URL.
42 $PAGE->set_pagetype($pagetype);
43 $PAGE->set_url('/lib/ajax/blocks.php', array('courseid' => $courseid, 'pagelayout' => $pagelayout, 'pagetype' => $pagetype));
45 // Verifying login and session.
47 if (!is_null($cmid)) {
48 $cm = get_coursemodule_from_id(null, $cmid, $courseid, false, MUST_EXIST
);
50 require_login($courseid, false, $cm);
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));
56 // Setting layout to replicate blocks configuration for the page we edit.
57 $PAGE->set_pagelayout($pagelayout);
58 $PAGE->set_subpage($subpage);
59 $PAGE->blocks
->add_custom_regions_for_pagetype($pagetype);
60 $pagetype = explode('-', $pagetype);
61 switch ($pagetype[0]) {
64 $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
67 if ($pagetype[1] === 'profile' && $PAGE->context
->contextlevel
== CONTEXT_USER
68 && $PAGE->context
->instanceid
== $USER->id
) {
69 // A user can only move blocks on their own site profile.
70 $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
72 $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
78 echo $OUTPUT->header();
82 // Loading blocks and instances for the region.
83 $PAGE->blocks
->load_blocks();
84 $instances = $PAGE->blocks
->get_blocks_for_region($buinewregion);
87 if ($buibeforeid == 0) {
88 if (count($instances) === 0) {
89 // Moving the block into an empty region. Give it the default weight.
92 // Moving to very bottom.
93 $last = end($instances);
94 $buinewweight = $last->instance
->weight +
1;
100 $first = reset($instances);
102 $lastweight = $first->instance
->weight
- 2;
105 foreach ($instances as $instance) {
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.
112 $buinewweight = ($lastweight +
$instance->instance
->weight
) / 2;
115 $lastweight = $instance->instance
->weight
;
116 $lastblock = $instance->instance
->id
;
120 // Move block if we need.
121 if (isset($buinewweight)) {
123 $_POST['bui_newweight'] = $buinewweight;
124 $PAGE->blocks
->process_url_move();