Skip to content

Commit

Permalink
fix: restore issue for an instance with empty configdata
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
keevan committed Oct 19, 2023
1 parent c4796c2 commit 8ee47df
Showing 1 changed file with 38 additions and 13 deletions.
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()]);
}
}

0 comments on commit 8ee47df

Please sign in to comment.