$r[$k] = backup_to_restore_object($v);
                    }
                    return $r;
                }
                return $backup;
            }
            $restore = backup_to_restore_object($SESSION->import_preferences);
            $restore->restoreto = 1;
            $restore->course_id = $id;
            $restore->importing = 1;
            // magic variable so we know that we're importing rather than just restoring.
            $SESSION->restore = $restore;
            redirect($CFG->wwwroot . '/course/format/project/restore.php?file=' . $filename . '&id=' . $id . '&section=' . $tosection . '&newdirectoryname=' . $newdirectoryname);
        } else {
            // 既存セクションタイトルのチェック
            if (project_format_check_directoryname($id, $newdirectoryname)) {
                redirect($CFG->wwwroot . '/course/format/project/backup.php?id=' . $fromcourse . '&section=' . $fromsection . '&to=' . $course->id . '&tosection=' . $tosection . '&newdirectoryname=' . $newdirectoryname);
            } else {
                $isexistingdirectory = true;
            }
        }
    }
}
// HTMLヘッダの生成
$navlinks = array();
$navlinks[] = array('name' => get_string('import'), 'link' => "{$CFG->wwwroot}/course/import.php?id={$course->id}", 'type' => 'misc');
$navlinks[] = array('name' => $strimportsection, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header("{$course->shortname}: {$strimportsection}", $course->fullname, $navigation);
if (!defined('MOODLE_INTERNAL')) {
    die('Direct access to this script is forbidden.');
/**
 * Moodleから呼び出されるリストアモジュール
 * 
 * @param object $restore
 * @param array $data : XMLパース
 * @return bool
 */
function project_restore_format_data($restore, $data)
{
    global $CFG;
    $status = true;
    if (!defined('RESTORE_SILENTLY')) {
        echo "<ul>";
    }
    // $dataをパースする
    foreach ($data["FORMATDATA"]["#"]["SECTIONTITLES"]["0"]["#"]["SECTIONTITLE"] as $sectiontitle) {
        // XMLデータから旧セクションIDを取得
        $oldsectionid = $sectiontitle["#"]["SECTIONID"]["0"]["#"];
        // 新しく登録するタイトルデータを構築
        $newtitle = new StdClass();
        if ($restore->newdirectoryname) {
            // 新しいディレクトリ名がある場合
            $newtitle->directoryname = $restore->newdirectoryname;
            $restore->olddirectoryname = backup_todb($sectiontitle["#"]["DIRECTORYNAME"]["0"]["#"]);
        } else {
            $newtitle->directoryname = backup_todb($sectiontitle["#"]["DIRECTORYNAME"]["0"]["#"]);
        }
        // 新しいタイトルのレコードを登録する
        if (empty($restore->section)) {
            // 旧セクションIDから新セクションIDを取得する
            $sec = backup_getid($restore->backup_unique_code, "course_sections", $oldsectionid);
            $newtitle->sectionid = $sec->new_id;
            // セクション指定がない場合は新規でセクションを追加する
            if (!($newtitle->id = insert_record('course_project_title', $newtitle))) {
                $status = false;
            } else {
                // ログ出力
                if (!defined('RESTORE_SILENTLY')) {
                    echo "<li>" . get_string('sectiontitle', 'format_project') . ' "' . $newtitle->title . '"</li>';
                }
            }
        } else {
            // ディレクトリ名重複チェック
            if (!project_format_check_directoryname($restore->course_id, $newtitle->directoryname, $restore->section)) {
                // 重複していた
                error(get_string('directoryalreadyexist', 'format_project', $newtitle->directoryname), $CFG->wwwroot . '/course/view.php?id=' . $restore->course_id);
                $status = false;
            } else {
                // セクションタイトルを指定したセクションに上書き
                if (!defined('RESTORE_SILENTLY')) {
                    echo "<li>" . get_string("updatesection", 'format_project') . "</li>";
                }
                // 対象のコース情報の取得
                if (!($course = get_record("course", "id", $restore->course_id))) {
                    error("Course ID was incorrect (can't find it)");
                }
                // 対象のセクション情報の取得
                if (!($section = get_course_section($restore->section, $restore->course_id))) {
                    error("Section data was incorrect (can't find it)");
                }
                // 現在のセクションのディレクトリ名を取得
                if (!($sectiontitle = project_format_get_title($course, $section->id))) {
                    error("Section directory was incorrect");
                }
                // 現在のディレクトリをストアする
                $restore->currentdirectoryname = $sectiontitle->directoryname;
                // タイトルIDを取得
                $newtitle->id = $sectiontitle->id;
                $status = update_record("course_project_title", $newtitle);
            }
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        echo "</ul>";
    }
    return $status;
}
    error("Could not find the course!");
}
require_login($course->id);
require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
// ここからフォームの表示
$stredit = get_string('edittitle', 'format_project');
$sectionname = get_string("name{$course->format}", 'format_project');
$strsummaryof = get_string('summaryof', '', " {$sectionname} {$section->section}");
print_header_simple($stredit, '', build_navigation(array(array('name' => $stredit, 'link' => null, 'type' => 'misc'))), 'theform.summary');
print_heading($strsummaryof);
print_simple_box_start('center');
// フォームからデータが送信されたときの処理
if ($form = data_submitted() and confirm_sesskey()) {
    $directoryname = required_param('directoryname', PARAM_CLEANFILE);
    $timenow = time();
    if ($form->olddirectoryname != $directoryname && !project_format_check_directoryname($course->id, $directoryname)) {
        error(get_string('directoryalreadyexist', 'format_project', $directoryname), "edittitle.php?id={$id}");
    }
    /* ディレクトリ名の変更 */
    // ディレクトリ名の取得
    if (!($basedir = make_upload_directory("{$course->id}"))) {
        error("The site administrator needs to fix the file permissions");
    }
    // ファイル名のチェック
    if ($form->olddirectoryname != $directoryname) {
        // 古いディレクトリが存在しない
        if (!file_exists($basedir . "/" . $form->olddirectoryname)) {
            // DB内のディレクトリ名を変更
            if (!set_field("course_project_title", "directoryname", $directoryname, "id", $form->id)) {
                error("Could not update the directory name!");
            }