/**
 * This function makes all the necessary calls to {@link restore_decode_content_links_worker()}
 * function inorder to decode contents of this block from the backup 
 * format to destination site/course in order to mantain inter-activities 
 * working in the backup/restore process. 
 * 
 * This is called from {@link restore_decode_content_links()}
 * function in the restore process.  This function is called regarless of
 * the return value from {@link backuprestore_enabled()}.
 *
 * @param object $restore Standard restore object
 * @return boolean
 **/
function page_decode_format_content_links_caller($restore)
{
    global $CFG;
    $status = true;
    // Only need to run this when the restore to course is being
    // deleted and created anew or when creating a new one
    if ($restore->restoreto == 0 || $restore->restoreto == 2) {
        require_once $CFG->dirroot . '/course/format/page/lib.php';
        $pageitems = get_records_sql("SELECT i.*\n                                        FROM {$CFG->prefix}format_page p,\n                                             {$CFG->prefix}format_page_items i\n                                       WHERE p.id = i.pageid\n                                         AND p.courseid = {$restore->course_id}\n                                         AND i.blockinstance != 0");
        if (!empty($pageitems)) {
            foreach ($pageitems as $pageitem) {
                $blockinstance = backup_getid($restore->backup_unique_code, 'block_instance', $pageitem->blockinstance);
                if (!empty($blockinstance)) {
                    set_field('format_page_items', 'blockinstance', $blockinstance->new_id, 'id', $pageitem->id);
                } else {
                    // Delete page item
                    if (page_block_delete($pageitem)) {
                        if (debugging() and !defined('RESTORE_SILENTLY')) {
                            echo '<br /><hr />Failed to remap block instance ID and successfully deleted page item ID: ' . $pageitem->id . '<hr /><br />';
                        }
                    } else {
                        if (debugging() and !defined('RESTORE_SILENTLY')) {
                            echo '<br /><hr />Failed to remap block instance ID and failed to delete page item ID: ' . $pageitem->id . '<hr /><br />';
                        }
                    }
                }
            }
        }
        // Relink locks (might be activity based, etc)
        $pages = get_records_sql("SELECT p.id, p.locks\n                                    FROM {$CFG->prefix}format_page p\n                                   WHERE p.courseid = {$restore->course_id}\n                                     AND p.locks != ''");
        if (!empty($pages)) {
            foreach ($pages as $page) {
                if (!empty($page->locks)) {
                    $newlocks = format_page_lock::restore($restore, $page->locks);
                    if ($newlocks != $page->locks) {
                        set_field('format_page', 'locks', $newlocks, 'id', $page->id);
                    }
                }
            }
        }
    }
    return $status;
}