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 2022 University of Bath
|
|
|
22 |
* @author James Pearce <jmp201@bath.ac.uk>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace block_multiblock;
|
|
|
27 |
use block_multiblock\helper;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Library for adding a block to a multiblock instance.
|
|
|
31 |
*
|
|
|
32 |
* @package block_multiblock
|
|
|
33 |
*/
|
|
|
34 |
class adddefaultblock {
|
|
|
35 |
/** @var array Storage of the block name -> block description of possibly addable sub-blocks. */
|
|
|
36 |
public $blocklist = [];
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Initialise the adding of a block to the multiblock.
|
|
|
40 |
*
|
|
|
41 |
* @param int $id is the id of the multiblock
|
|
|
42 |
* @param array $arraytoadd is an array of blocks to add
|
|
|
43 |
* @param object $multiblockinstance is the parent multiblock instance
|
|
|
44 |
*/
|
|
|
45 |
public function init($id, $arraytoadd, $multiblockinstance) {
|
|
|
46 |
|
|
|
47 |
$this->set_blocks_to_add($id, $arraytoadd, $multiblockinstance);
|
|
|
48 |
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* This checks the blocks that can bee added to a multiblock,
|
|
|
53 |
* selects the ones defined in the current default blocks setting and creates an instance of that new block
|
|
|
54 |
* then moves it to the multiblock.
|
|
|
55 |
*
|
|
|
56 |
* @param int $blockid is the id of the multiblock
|
|
|
57 |
* @param array $arraytoadd is an array of blocks to add
|
|
|
58 |
* @param object $multiblockinstance is the parent multiblock instance
|
|
|
59 |
*/
|
|
|
60 |
public function set_blocks_to_add($blockid, $arraytoadd, $multiblockinstance) {
|
|
|
61 |
global $DB, $PAGE;
|
|
|
62 |
|
|
|
63 |
// Load all possible blocks for the page.
|
|
|
64 |
$blockmanager = $PAGE->blocks;
|
|
|
65 |
$blockmanager->load_blocks();
|
|
|
66 |
|
|
|
67 |
// Loop over the $arraytoadd to find the blocks to add, within the addable blocks.
|
|
|
68 |
foreach ($arraytoadd as $toadd) {
|
|
|
69 |
foreach ($blockmanager->get_addable_blocks() as $block) {
|
|
|
70 |
if ($block->name == 'multiblock') {
|
|
|
71 |
continue;
|
|
|
72 |
}
|
|
|
73 |
// Found a block to add.
|
|
|
74 |
if ($toadd == $block->name) {
|
|
|
75 |
// Add the block to the block list.
|
|
|
76 |
$this->blocklist[$block->name] = $block;
|
|
|
77 |
|
|
|
78 |
// Create a new instance of the block to add. This will put the new default blocks in the side-pre region.
|
|
|
79 |
$blockmanager->add_block($toadd, 'side-pre', 10, false);
|
|
|
80 |
|
|
|
81 |
$addedblocks = $DB->get_records('block_instances', ['parentcontextid' => $multiblockinstance->parentcontextid]);
|
|
|
82 |
|
|
|
83 |
foreach ($addedblocks as $addedblock) {
|
|
|
84 |
if ($addedblock->blockname == $toadd) {
|
|
|
85 |
|
|
|
86 |
// Move the newly created blocks into the multiblock.
|
|
|
87 |
helper::move_block($addedblock->id, $blockid);
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|