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 |
* Form for adding a block to a multiblock instance.
|
|
|
19 |
*
|
|
|
20 |
* @package block_multiblock
|
|
|
21 |
* @copyright 2019 Peter Spicer <peter.spicer@catalyst-eu.net>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace block_multiblock\form;
|
|
|
26 |
use block_multiblock\helper;
|
|
|
27 |
use core_plugin_manager;
|
|
|
28 |
use moodleform;
|
|
|
29 |
|
|
|
30 |
defined('MOODLE_INTERNAL') || die();
|
|
|
31 |
|
|
|
32 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Form for adding a block to a multiblock instance.
|
|
|
36 |
*
|
|
|
37 |
* @package block_multiblock
|
|
|
38 |
* @copyright 2019 Peter Spicer <peter.spicer@catalyst-eu.net>
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class addblock extends moodleform {
|
|
|
42 |
/** @var array Storage of the block name -> block description of possibly addable sub-blocks. */
|
|
|
43 |
public $blocklist = [];
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Handles the general setup of the form for adding sub-blocks to a multiblock.
|
|
|
47 |
*/
|
|
|
48 |
public function definition() {
|
|
|
49 |
$mform =& $this->_form;
|
|
|
50 |
|
|
|
51 |
if (!empty($this->_customdata['id'])) {
|
|
|
52 |
$this->set_block_list();
|
|
|
53 |
}
|
|
|
54 |
if (empty($this->blocklist)) {
|
|
|
55 |
return;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
$mform->addElement('header', 'addnewblock', get_string('addnewblock', 'block_multiblock'));
|
|
|
59 |
|
|
|
60 |
$group = [];
|
|
|
61 |
$group[] = &$mform->createElement('select', 'addblock', get_string('addblock'), $this->blocklist);
|
|
|
62 |
$group[] = &$mform->createElement('submit', 'addsubmit', get_string('add'));
|
|
|
63 |
$mform->addGroup($group, 'addblockgroup', '', [' '], false);
|
|
|
64 |
|
|
|
65 |
$siblings = $this->get_sibling_blocks($this->_customdata['id']);
|
|
|
66 |
if (!empty($siblings)) {
|
|
|
67 |
$siblings = ['' => get_string('selectblock', 'block_multiblock')] + $siblings;
|
|
|
68 |
$mform->addElement('header', 'moveexistingblock', get_string('moveexistingblock', 'block_multiblock'));
|
|
|
69 |
$mform->setExpanded('moveexistingblock', false);
|
|
|
70 |
|
|
|
71 |
$siblinggroup = [];
|
|
|
72 |
$siblinggroup[] = &$mform->createElement('select', 'moveblock',
|
|
|
73 |
get_string('moveexistingblock', 'block_multiblock'), $siblings);
|
|
|
74 |
$siblinggroup[] = &$mform->createElement('submit', 'movesubmit', get_string('move'));
|
|
|
75 |
$mform->addGroup($siblinggroup, 'siblinggroup', '', [' '], false);
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Given the instance id of a multiblock, identify the possible addable blocks.
|
|
|
81 |
*/
|
|
|
82 |
public function set_block_list() {
|
|
|
83 |
global $DB, $PAGE;
|
|
|
84 |
|
|
|
85 |
$this->blocklist = [
|
|
|
86 |
'' => get_string('selectblock', 'block_multiblock'),
|
|
|
87 |
];
|
|
|
88 |
|
|
|
89 |
helper::set_page_fake_url();
|
|
|
90 |
$PAGE->blocks->load_blocks();
|
|
|
91 |
foreach ($PAGE->blocks->get_addable_blocks() as $block) {
|
|
|
92 |
if ($block->name == 'multiblock') {
|
|
|
93 |
continue;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$this->blocklist[$block->name] = trim($block->title) ? trim($block->title) : '[block_' . $block->name . ']';
|
|
|
97 |
}
|
|
|
98 |
helper::set_page_real_url();
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Finds other blocks in the same place to be merged in.
|
|
|
103 |
*
|
|
|
104 |
* @param int $instanceid The instance of a multiblock to find other blocks in the same context.
|
|
|
105 |
*/
|
|
|
106 |
public function get_sibling_blocks($instanceid) {
|
|
|
107 |
global $DB;
|
|
|
108 |
|
|
|
109 |
$available = [];
|
|
|
110 |
$invalidstatuses = [
|
|
|
111 |
core_plugin_manager::PLUGIN_STATUS_MISSING,
|
|
|
112 |
core_plugin_manager::PLUGIN_STATUS_DOWNGRADE,
|
|
|
113 |
core_plugin_manager::PLUGIN_STATUS_DELETE,
|
|
|
114 |
];
|
|
|
115 |
foreach (core_plugin_manager::instance()->get_plugins_of_type('block') as $block) {
|
|
|
116 |
if (!in_array($block->get_status(), $invalidstatuses)) {
|
|
|
117 |
$available[] = $block->name;
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// First we have to find the block's parent context, then the blocks in that context.
|
|
|
122 |
$record = $DB->get_record('block_instances', ['id' => $instanceid]);
|
|
|
123 |
$siblings = $DB->get_records('block_instances', ['parentcontextid' => $record->parentcontextid]);
|
|
|
124 |
// And remove the current block, we can't add ourselves to ourself now...
|
|
|
125 |
unset ($siblings[$instanceid]);
|
|
|
126 |
|
|
|
127 |
$blocks = [];
|
|
|
128 |
foreach ($siblings as $instanceid => $sibling) {
|
|
|
129 |
// Can't add a multiblock to itself in any universe.
|
|
|
130 |
if ($sibling->blockname == 'multiblock') {
|
|
|
131 |
continue;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
// Make sure that the plugin actually exists.
|
|
|
135 |
if (!in_array($sibling->blockname, $available)) {
|
|
|
136 |
continue;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
// Does it have a title?
|
|
|
140 |
if (!empty($sibling->configdata)) {
|
|
|
141 |
$config = unserialize(base64_decode($sibling->configdata));
|
|
|
142 |
if (!empty($config->title)) {
|
|
|
143 |
$blocks[$instanceid] = $config->title . ' (' . get_string('pluginname', 'block_' . $sibling->blockname) . ')';
|
|
|
144 |
continue;
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
// Add it to the list.
|
|
|
149 |
$blocks[$instanceid] = get_string('pluginname', 'block_' . $sibling->blockname);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
return $blocks;
|
|
|
153 |
}
|
|
|
154 |
}
|