/**
 * リストアの実行
 * 
 * @
 */
function project_restore_execute(&$restore, $info, $course_header, &$errorstr)
{
    global $CFG, $USER;
    $status = true;
    //Localtion of the xml file
    $xml_file = $CFG->dataroot . "/temp/backup/" . $restore->backup_unique_code . "/moodle.xml";
    if ($status) {
        //If we are deleting and bringing into a course or making a new course, same situation
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('courseformatdata');
        }
        if (!($status = restore_set_format_data($restore, $xml_file))) {
            $error = "Error while setting the course format data";
            if (!defined('RESTORE_SILENTLY')) {
                notify($error);
            } else {
                $errorstr = $error;
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    // リストア先のフォルダがバックアップデータと重複する場合
    // フォルダ内のファイル一覧リストを作成する
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('getcurrentdirectorylist', 'format_project');
        }
        if (!($status = project_restore_create_directory_list($restore))) {
            $error = "Error while getting directory list";
            if (!defined('RESTORE_SILENTLY')) {
                notify($error);
            } else {
                $errorstr = $error;
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //    //Checks for the required files/functions to restore every module
    //    //and include them
    //    if ($allmods = get_records("modules") ) {
    //        foreach ($allmods as $mod) {
    //            $modname = $mod->name;
    //            $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
    //
    //            //If file exists and we have selected to restore that type of module
    //            if ((file_exists($modfile)) and !empty($restore->mods[$modname]) and ($restore->mods[$modname]->restore)) {
    //                include_once($modfile);
    //            }
    //        }
    //    }
    if (!defined('RESTORE_SILENTLY')) {
        //Start the main table
        echo "<table cellpadding=\"5\">";
        echo "<tr><td>";
        //Start the main ul
        echo "<ul>";
    }
    if (file_exists($shared_lib = $CFG->dirroot . '/blocks/sharing_cart/shared/SharingCart_Restore.php')) {
        require_once $shared_lib;
    } else {
        require_once dirname(__FILE__) . '/shared/SharingCart_Restore.php';
    }
    $course_id = $restore->course_id;
    $section_i = $restore->section;
    //$prev_restore = clone $restore;
    $worker = new SharingCart_Restore($course_id, $section_i);
    $worker->beginPreferences();
    //$restore = $prev_restore;
    if ($file = optional_param('file')) {
        $worker->setZipDir($CFG->dataroot . '/' . dirname($file));
    } else {
        $worker->setZipDir($CFG->dataroot . '/' . $course_id . '/backupdata');
    }
    $worker->setZipName($info->backup_name);
    $worker->setRestoreSectionStatus(TRUE);
    $worker->setRestoreModulesStatus(TRUE);
    $worker->endPreferences();
    $worker->execute();
    //    // 復旧先のディレクトリを削除
    //    if ($status) {
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo "<li>".get_string("deletingolddata").'</li>';
    //        }
    //        if (!$status = project_remove_section_contents($restore)) {
    //            if (!defined('RESTORE_SILENTLY')) {
    //                notify("An error occurred while deleting some of the course contents.");
    //            } else {
    //                $errrostr = "An error occurred while deleting some of the course contents.";
    //                return false;
    //            }
    //        }
    //    }
    //
    //    //Now create the course_sections and their associated course_modules
    //    //we have to do this after groups and groupings are restored, because we need the new groupings id
    //    if ($status) {
    //        //Into new course
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo "<li>".get_string("checkingsections");
    //        }
    //        if (!$status = project_restore_section($restore,$xml_file)) {
    //            if (!defined('RESTORE_SILENTLY')) {
    //                notify("Error creating sections in the existing course.");
    //            } else {
    //                $errorstr = "Error creating sections in the existing course.";
    //                return false;
    //            }
    //        }
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo '</li>';
    //        }
    //    }
    //
    //    //Now create categories and questions as needed
    //    if ($status) {
    //        include_once("$CFG->dirroot/question/restorelib.php");
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo "<li>".get_string("creatingcategoriesandquestions");
    //            echo "<ul>";
    //        }
    //        if (!$status = restore_create_questions($restore,$xml_file)) {
    //            if (!defined('RESTORE_SILENTLY')) {
    //                notify("Could not restore categories and questions!");
    //            } else {
    //                $errorstr = "Could not restore categories and questions!";
    //                return false;
    //            }
    //        }
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo "</ul></li>";
    //        }
    //    }
    //
    //    //Now create course files as needed
    //    if ($status and ($restore->course_files)) {
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo "<li>".get_string("copyingcoursefiles");
    //        }
    //        if (!$status = project_restore_course_files($restore)) {
    //            if (empty($status)) {
    //                notify("Could not restore course files!");
    //            } else {
    //                $errorstr = "Could not restore course files!";
    //                return false;
    //            }
    //        }
    //        //If all is ok (and we have a counter)
    //        if ($status and ($status !== true)) {
    //            //Inform about user dirs created from backup
    //            if (!defined('RESTORE_SILENTLY')) {
    //                echo "<ul>";
    //                echo "<li>".get_string("filesfolders").": ".$status.'</li>';
    //                echo "</ul>";
    //            }
    //        }
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo "</li>";
    //        }
    //    }
    //
    //    //Now create course modules as needed
    //    if ($status) {
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo "<li>".get_string("creatingcoursemodules");
    //        }
    //
    //        if (!$status = restore_create_modules($restore,$xml_file)) {
    //            if (!defined('RESTORE_SILENTLY')) {
    //                notify("Could not restore modules!");
    //            } else {
    //                $errorstr = "Could not restore modules!";
    //                return false;
    //            }
    //        }
    //
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo '</li>';
    //        }
    //    }
    //
    //    //Now, if all is OK, adjust the instance field in course_modules !!
    //    if ($status) {
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo "<li>".get_string("checkinginstances");
    //        }
    //        if (!$status = restore_check_instances($restore)) {
    //            if (!defined('RESTORE_SILENTLY')) {
    //                notify("Could not adjust instances in course_modules!");
    //            } else {
    //                $errorstr = "Could not adjust instances in course_modules!";
    //                return false;
    //            }
    //        }
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo '</li>';
    //        }
    //    }
    //
    //    //Now, if all is OK, adjust inter-activity links
    //    if ($status) {
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo "<li>".get_string("decodinginternallinks");
    //        }
    //        if (!$status = restore_decode_content_links($restore)) {
    //            if (!defined('RESTORE_SILENTLY')) {
    //                notify("Could not decode content links!");
    //            } else {
    //                $errorstr = "Could not decode content links!";
    //                return false;
    //            }
    //        }
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo '</li>';
    //        }
    //    }
    //
    //    // リストア先のフォルダがバックアップデータと重複する場合
    //    // フォルダ内のファイル一覧リストを作成する
    //    if ($status) {
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo '<li>'.get_string('removenewdirectorylist','format_project');
    //        }
    //        if (!$status = project_restore_remove_diff_files($restore)) {
    //            $error = "Error while getting directory list";
    //            if (!defined('RESTORE_SILENTLY')) {
    //                notify($error);
    //            } else {
    //                $errorstr=$error;
    //                return false;
    //            }
    //        }
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo '</li>';
    //        }
    //    }
    //
    //    //Cleanup temps (files and db)
    //    if ($status) {
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo "<li>".get_string("cleaningtempdata");
    //        }
    //        if (!$status = clean_temp_data ($restore)) {
    //            if (!defined('RESTORE_SILENTLY')) {
    //                notify("Could not clean up temporary data from files and database");
    //            } else {
    //                $errorstr = "Could not clean up temporary data from files and database";
    //                return false;
    //            }
    //        }
    //        if (!defined('RESTORE_SILENTLY')) {
    //            echo '</li>';
    //        }
    //    }
    if (!defined('RESTORE_SILENTLY')) {
        //End the main ul
        echo "</ul>";
        //End the main table
        echo "</td></tr>";
        echo "</table>";
    }
    return $status;
}
//error_reporting(E_ALL);
require_once './shared/SharingCart_Restore.php';
require_once './sharing_cart_table.php';
$sc_id = required_param('id', PARAM_INT);
$course_id = required_param('course', PARAM_INT);
$section_id = required_param('section', PARAM_INT);
$return_to = urldecode(required_param('return'));
// 共有アイテムが存在するかチェック
$sharing_cart = sharing_cart_table::get_record_by_id($sc_id) or print_error('err_shared_id', 'block_sharing_cart', $return_to);
// 自分が所有する共有アイテムかチェック
$sharing_cart->user == $USER->id or print_error('err_capability', 'block_sharing_cart', $return_to);
// ZIPファイル名取得
$zip_name = $sharing_cart->file;
try {
    // リストアオブジェクト (※ $restore は Moodle グローバル変数として予約されているので使用不可)
    $worker = new SharingCart_Restore($course_id, $section_id);
    // サイレントモード
    $worker->setSilent();
    // 設定開始
    $worker->beginPreferences();
    // ZIPファイル名設定
    $worker->setZipName($zip_name);
    // 設定完了
    $worker->endPreferences();
    // リストア実行
    $worker->execute();
} catch (SharingCart_CourseException $e) {
    //print_error('err_course_id', 'block_sharing_cart', $return_to);
    error((string) $e);
    // デバッグ用に詳細メッセージを表示
} catch (SharingCart_SectionException $e) {