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 |
* Multiblock uninstallation.
|
|
|
19 |
*
|
|
|
20 |
* @package block_multiblock
|
|
|
21 |
* @copyright 2020 Peter Spicer <peter.spicer@catalyst-eu.net>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
use block_multiblock\helper;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Clean up multiblocks when uninstalling.
|
|
|
29 |
*
|
|
|
30 |
* Finds every multiblock instance and decomposes any blocks in them back to parent context.
|
|
|
31 |
*/
|
|
|
32 |
function xmldb_block_multiblock_uninstall() : bool {
|
|
|
33 |
global $DB;
|
|
|
34 |
|
|
|
35 |
// Set up the queries we're going to be using.
|
|
|
36 |
$sql = "SELECT c.id AS contextid, bi.*
|
|
|
37 |
FROM {context} c
|
|
|
38 |
JOIN {block_instances} bi ON bi.id = c.instanceid AND c.contextlevel = :contextlevel
|
|
|
39 |
WHERE bi.blockname = :blockname";
|
|
|
40 |
|
|
|
41 |
$params = [
|
|
|
42 |
'blockname' => 'multiblock',
|
|
|
43 |
'contextlevel' => CONTEXT_BLOCK,
|
|
|
44 |
];
|
|
|
45 |
|
|
|
46 |
$childsql = "SELECT c.id AS contextid, bi.id AS blockid
|
|
|
47 |
FROM {context} c
|
|
|
48 |
JOIN {block_instances} bi ON bi.id = c.instanceid AND c.contextlevel = :contextlevel
|
|
|
49 |
WHERE bi.parentcontextid = :parentcontext";
|
|
|
50 |
|
|
|
51 |
$multiblocks = $DB->get_records_sql($sql, $params);
|
|
|
52 |
foreach ($multiblocks as $multiblock) {
|
|
|
53 |
// Now we have the blocks, find each block's children.
|
|
|
54 |
$childparams = [
|
|
|
55 |
'contextlevel' => CONTEXT_BLOCK,
|
|
|
56 |
'parentcontext' => $multiblock->contextid,
|
|
|
57 |
];
|
|
|
58 |
$children = $DB->get_records_sql($childsql, $childparams);
|
|
|
59 |
|
|
|
60 |
// And split each child out of the parent.
|
|
|
61 |
foreach ($children as $childblock) {
|
|
|
62 |
helper::split_block($multiblock, $childblock->blockid);
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
return true;
|
|
|
67 |
}
|