Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore issue for an instance with empty configdata #51

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions backup/moodle2/restore_carousel_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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()]);
}
}