1 |
efrain |
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 |
* 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 |
|
|
|
25 |
define('AJAX_SCRIPT', true);
|
|
|
26 |
require_once(__DIR__ . '/../../config.php');
|
|
|
27 |
|
|
|
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);
|
|
|
35 |
|
|
|
36 |
$PAGE->set_url('/lib/ajax/blocks.php', ['pagehash' => $pagehash]);
|
|
|
37 |
|
|
|
38 |
// Retrieve the edited page from the session hash.
|
|
|
39 |
$page = moodle_page::retrieve_edited_page($pagehash, MUST_EXIST);
|
|
|
40 |
|
|
|
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);
|
|
|
45 |
}
|
|
|
46 |
require_login($page->course, false, $cm);
|
|
|
47 |
require_sesskey();
|
|
|
48 |
$PAGE->set_context($page->context);
|
|
|
49 |
|
|
|
50 |
if (!$page->user_can_edit_blocks() || !$page->user_is_editing()) {
|
|
|
51 |
throw new moodle_exception('nopermissions', '', $page->url->out(), get_string('editblock'));
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
// Send headers.
|
|
|
55 |
echo $OUTPUT->header();
|
|
|
56 |
|
|
|
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);
|
|
|
62 |
|
|
|
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;
|
|
|
72 |
}
|
|
|
73 |
} else {
|
|
|
74 |
// Moving somewhere.
|
|
|
75 |
$lastweight = 0;
|
|
|
76 |
$lastblock = 0;
|
|
|
77 |
$first = reset($instances);
|
|
|
78 |
if ($first) {
|
|
|
79 |
$lastweight = $first->instance->weight - 2;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
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;
|
|
|
88 |
}
|
|
|
89 |
$buinewweight = ($lastweight + $instance->instance->weight) / 2;
|
|
|
90 |
break;
|
|
|
91 |
}
|
|
|
92 |
$lastweight = $instance->instance->weight;
|
|
|
93 |
$lastblock = $instance->instance->id;
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
// Move block if we need.
|
|
|
98 |
if (isset($buinewweight)) {
|
|
|
99 |
// Nasty hack.
|
|
|
100 |
$_POST['bui_newweight'] = $buinewweight;
|
|
|
101 |
$page->blocks->process_url_move();
|
|
|
102 |
}
|
|
|
103 |
break;
|
|
|
104 |
}
|