Skip to content

Commit

Permalink
Issue 108: Subblocks duplicated when a custom page is duplicated by W…
Browse files Browse the repository at this point in the history
…orkplace
  • Loading branch information
sumaiyamannan committed Dec 20, 2024
1 parent d6b9ab3 commit f094716
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions block_multiblock.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public function specialization() {
public function load_multiblocks($contextid) {
global $DB;

// Cleanup incorrect parentcontext.
$this->cleanup($contextid);

// Find all the things that relate to this block.
$this->blocks = $DB->get_records('block_instances', ['parentcontextid' => $contextid], 'defaultweight, id');
foreach ($this->blocks as $id => $block) {
Expand Down Expand Up @@ -134,6 +137,43 @@ public function add_default_blocks() {
}
}

/**
* Clean up incorrect parentcontextid entered by tool_custompages on page duplication.
*
* @param int $contextid
* @return void
*/
private function cleanup($contextid) {
global $DB;
$sql = 'SELECT b.subpagepattern FROM {context} c LEFT JOIN {block_instances} b ON b.id = c.instanceid WHERE c.id = :cid';
$subpagepattern = $DB->get_field_sql($sql, ['cid' => $contextid]);
// Clone page.
$blocks = $DB->get_records_sql('SELECT * FROM {block_instances}
WHERE subpagepattern LIKE :subpagepattern AND parentcontextid != 1 AND timecreated = timemodified',
['subpagepattern' => $subpagepattern]);
foreach ($blocks as $block) {
$path = $DB->get_field('context', 'path', ['instanceid' => $block->id]);
if ($block->parentcontextid != $contextid) {
$newpath = str_replace($block->parentcontextid, $contextid, $path);
$DB->execute('UPDATE {block_instances} SET parentcontextid = :cparentcontextid, timemodified = :timemodified WHERE id = :id',
['cparentcontextid' => $contextid, 'id' => $block->id, 'timemodified' => time()]);
$DB->execute('UPDATE {context} SET path = :newpath WHERE instanceid = :instanceid',
['newpath' => $newpath, 'instanceid' => $block->id]);
}
}
// Original page.
$blocks = $DB->get_records_sql('SELECT * FROM {block_instances}
WHERE parentcontextid = :contextid',
['contextid' => $contextid]);
foreach ($blocks as $block) {
if ($block->subpagepattern != $subpagepattern) {
$DB->execute('UPDATE {block_instances} SET parentcontextid = -1 WHERE id = :id',
['id' => $block->id]);
}
}

}

/**
* Used to generate the content for the block.
*
Expand Down Expand Up @@ -272,6 +312,10 @@ public function instance_copy($fromid) {
// Create all the new block instances.
$newblockinstanceids = [];
foreach ($blockinstances as $instance) {
// Do not add if creation is being initiated by tool_custompage.
if ($instance->pagetypepattern == 'admin-tool-custompage') {
continue;
}
$originalid = $instance->id;
unset($instance->id);
$instance->parentcontextid = $this->context->id;
Expand All @@ -295,8 +339,16 @@ public function instance_copy($fromid) {
* @return bool
*/
public function instance_delete() {
global $DB;

global $DB, $pageid;
// Do not delete if deletion is being initiated by tool_custompage.
foreach ($DB->get_records('block_instances', ['id' => $this->context->instanceid]) as $block) {
if ($block->pagetypepattern == 'admin-tool-custompage' && $pageid != $block->subpagepattern ) {
if ($this->instance->id == $block->id) {
$this->instance->id = -1;
}
return true;
}
}
// Find all the things that relate to this block.
foreach ($DB->get_records('block_instances', ['parentcontextid' => $this->context->id]) as $subblock) {
blocks_delete_instance($subblock);
Expand Down

0 comments on commit f094716

Please sign in to comment.