From 8ee47dfc41c3f22afd9f30d6b6fda465b0f27b11 Mon Sep 17 00:00:00 2001 From: Kevin Pham Date: Thu, 19 Oct 2023 16:38:38 +1100 Subject: [PATCH] fix: restore issue for an instance with empty configdata - Add some additional checks to optimise route - The syncing of slides from old to new will only happen if there are slides and data to facilitate it. Resolves #50 --- backup/moodle2/restore_carousel_stepslib.php | 51 +++++++++++++++----- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/backup/moodle2/restore_carousel_stepslib.php b/backup/moodle2/restore_carousel_stepslib.php index e247e6c..d8a03c5 100644 --- a/backup/moodle2/restore_carousel_stepslib.php +++ b/backup/moodle2/restore_carousel_stepslib.php @@ -78,21 +78,13 @@ public function process_block($data) { // Save the mapping to reuse in the after_execute step. $this->slidemapping = $slidesarr; - // Adjust the serialized configdata->order to the created/mapped feeds. - $configdata = $DB->get_field('block_instances', 'configdata', ['id' => $this->task->get_blockid()]); - $config = unserialize(base64_decode($configdata)); - - $oldorder = explode(',', $config->order); - $neworder = []; - foreach ($oldorder as $oldid) { - $neworder[] = $slidesarr[$oldid]; + // If no slides are present (e.g. user has not configured the carousel), return early. + if (empty($slidesarr)) { + return; } - // Set csv of new slide order. - $config->order = implode(',', $neworder); - $configdata = base64_encode(serialize($config)); - - $DB->set_field('block_instances', 'configdata', $configdata, ['id' => $this->task->get_blockid()]); + // Syncs the order for the carousel block. + $this->update_slide_references($slidesarr); } /** @@ -132,4 +124,37 @@ protected function after_execute() { $files->close(); } + + /** + * Adjust the serialized configdata->order to the created/mapped feeds. + * + * @param array $slidesarr + */ + private function update_slide_references(array $slidesarr) { + global $DB; + + $configdata = $DB->get_field('block_instances', 'configdata', ['id' => $this->task->get_blockid()]); + $config = unserialize(base64_decode($configdata)); + if ($config === false) { + return; + } + + // If there are no slides or the order is not defined, there is nothing to update, return early. + if (empty($slidesarr) || !isset($config->order)) { + return; + } + + // Update the slide references to the new slides (preserving original order). + $oldorder = explode(',', $config->order); + $neworder = []; + foreach ($oldorder as $oldid) { + $neworder[] = $slidesarr[$oldid]; + } + + // Set csv of new slide order. + $config->order = implode(',', $neworder); + $configdata = base64_encode(serialize($config)); + + $DB->set_field('block_instances', 'configdata', $configdata, ['id' => $this->task->get_blockid()]); + } }