Merge branch 'MDL-73076-master' of https://github.com/lameze/moodle
[moodle.git] / lib / ajax / blocks.php
blob7d1a7625d90a2042851b987790fc8d1f7ffa1100
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 $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.
46 $cm = null;
47 if (!is_null($cmid)) {
48 $cm = get_coursemodule_from_id(null, $cmid, $courseid, false, MUST_EXIST);
50 require_login($courseid, false, $cm);
51 require_sesskey();
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]) {
62 case 'my':
63 case 'mycourses':
64 $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
65 break;
66 case 'user':
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');
71 } else {
72 $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
74 break;
77 // Send headers.
78 echo $OUTPUT->header();
80 switch ($action) {
81 case 'move':
82 // Loading blocks and instances for the region.
83 $PAGE->blocks->load_blocks();
84 $instances = $PAGE->blocks->get_blocks_for_region($buinewregion);
86 $buinewweight = null;
87 if ($buibeforeid == 0) {
88 if (count($instances) === 0) {
89 // Moving the block into an empty region. Give it the default weight.
90 $buinewweight = 0;
91 } else {
92 // Moving to very bottom.
93 $last = end($instances);
94 $buinewweight = $last->instance->weight + 1;
96 } else {
97 // Moving somewhere.
98 $lastweight = 0;
99 $lastblock = 0;
100 $first = reset($instances);
101 if ($first) {
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.
110 break;
112 $buinewweight = ($lastweight + $instance->instance->weight) / 2;
113 break;
115 $lastweight = $instance->instance->weight;
116 $lastblock = $instance->instance->id;
120 // Move block if we need.
121 if (isset($buinewweight)) {
122 // Nasty hack.
123 $_POST['bui_newweight'] = $buinewweight;
124 $PAGE->blocks->process_url_move();
126 break;