throw new SharingCart_RepositoryException('Repository URL was missing');
    }
    if (empty($config[$repo_id]->instance)) {
        throw new SharingCart_RepositoryException('Repository course ID was missing');
    }
    if (empty($config[$repo_id]->username)) {
        throw new SharingCart_RepositoryException('Repository username was missing');
    }
    $zip_path = make_user_directory($USER->id, true) . '/' . $sharing_cart->file;
    $zip_data = file_get_contents($zip_path);
    $form = new MoodleQuickForm('upload', 'post', $config[$repo_id]->url . '/course/format/repository/material.php');
    $form->addElement('hidden', 'mode', 'upload');
    $form->addElement('hidden', 'id', $config[$repo_id]->instance);
    $form->addElement('hidden', 'username', $config[$repo_id]->username);
    $form->addElement('hidden', 'password', $config[$repo_id]->password);
    $form->addElement('hidden', 'icon', $sharing_cart->icon);
    $form->addElement('hidden', 'type', $sharing_cart->name);
    $form->addElement('hidden', 'title', $sharing_cart->text);
    $form->addElement('hidden', 'file', base64_encode($zip_data));
    $form->addElement('hidden', 'sha1', sha1($zip_data));
    $form->addElement('hidden', 'usersite', $CFG->wwwroot);
    $form->addElement('hidden', 'sitename', $SITE->fullname);
    $icon = sharing_cart_lib::get_icon($sharing_cart->name, $sharing_cart->icon);
    $text = '<span class="icon">' . $icon . '</span><span>' . $sharing_cart->text . '</span>';
    $form->addElement('static', NULL, '', $text);
    $form->addElement('static', NULL, '', SharingCart_Repository::getString('confirm_upload'));
    $form->addElement('submit', 'upload', SharingCart_Repository::getString('upload'));
    SharingCart_Repository::printForm($form, SharingCart_Repository::getString('upload_to_repository'), $course_id);
} catch (Exception $e) {
    error((string) $e);
}
		</div>';
    echo '
		<div style="text-align:center; width:100%; margin:1em;">
			<form id="downloadform" method="POST"
			 action="' . $config[$repo_id]->url . '/course/format/repository/material.php">
			<div style="display:hidden;">';
    foreach ($params as $k => $v) {
        echo '
				<input type="hidden" name="' . $k . '" value="' . $v . '" />';
    }
    echo '
			</div>
			<div>
				<input type="submit" value="' . SharingCart_Repository::getString('download') . '" />
			</div>
			</form>
		</div>';
    // 自動でリポジトリのダウンロードページへジャンプ
    echo '
		<script type="text/javascript">
		//<![CDATA[
			setTimeout(function ()
			{
				document.forms["downloadform"].submit();
			}, 100);
		//]]>
		</script>';
    SharingCart_Repository::printFooter();
} catch (Exception $e) {
    error((string) $e);
}
		<form action="transfer.php" method="post">
		<div style="display:none;">
			<input type="hidden" name="repository" value="' . $repository . '" />
			<input type="hidden" name="usercourse" value="' . $usercourse . '" />
			<input type="hidden" name="sessionkey" value="' . $sessionkey . '" />
		</div>
		<table>';
    foreach ($materials as $id => $fields) {
        // フィールドを分解
        // @see /course/format/repository/RepositoryMaterial.php # download()
        list($sha1, $type, $icon, $text) = explode('|', $fields, 4);
        // ダウンロードボタンがクリックされたら、教材ファイルをHTTP通信により取得
        if (!empty($downloads[$id])) {
            $user_dir = make_user_directory($USER->id);
            $temp_dir = make_upload_directory('temp/download', false);
            $zip_name = SharingCart_Repository::getDownloadName($id);
            if (!is_dir($user_dir) || !is_dir($temp_dir)) {
                throw new SharingCart_RepositoryException('Directory creation failure');
            }
            if (is_file($user_dir . '/' . $zip_name)) {
                throw new SharingCart_RepositoryException('File already exists');
            }
            $response_header = FileTransfer::downloadFile($temp_dir . '/' . $zip_name, $repository_wwwroot . '/course/format/repository/material.php', array('mode' => 'transfer', 'material' => $id, 'sessionkey' => $sessionkey));
            // ダウンロードしたファイルのハッシュを比較してダウンロード成功かチェック
            if (sha1_file($temp_dir . '/' . $zip_name) == $sha1) {
                if (!rename($temp_dir . '/' . $zip_name, $user_dir . '/' . $zip_name)) {
                    throw new SharingCart_RepositoryException('File rename failure');
                }
                $sharing_cart = new stdClass();
                $sharing_cart->user = $USER->id;
                $sharing_cart->name = $type;
    $forms = array();
    foreach ($config as $id => $info) {
        if ($id == $update_id) {
            if (!empty($succeeded)) {
                $message = get_string('updated', NULL, SharingCart_Repository::getString('settings'));
            } elseif (!empty($forbidden)) {
                $message = SharingCart_Repository::getString('auth_fail');
            } else {
                $message = SharingCart_Repository::getString('not_found');
            }
            $forms[] = new SharingCart_Repository_Form_Settings($course_id, $id, $info, $message);
        } else {
            $forms[] = new SharingCart_Repository_Form_Settings($course_id, $id, $info);
        }
    }
    //* (暫定) UL/DL機能が複数設定に対応していないので機能隠蔽
    if (!optional_param('add')) {
        class add_repository_form extends moodleform
        {
            public function definition()
            {
                $this->_form->addElement('submit', 'add', get_string('add'));
            }
        }
        $forms[] = new add_repository_form();
    }
    //*/
    SharingCart_Repository::printForm($forms, SharingCart_Repository::getString('settings'), $course_id, $return_to);
} catch (Exception $e) {
    error((string) $e);
}
    private static function getScriptHeader()
    {
        if (self::$script_header_defined) {
            return '';
        }
        self::$script_header_defined = TRUE;
        return '
			function sharing_cart_repository_add_url_desc(i)
			{
				var item = document.getElementById("id_url" + i).parentNode;
				var desc = document.createElement("div");
				desc.className = "dimmed_text";
				desc.appendChild(document.createTextNode("' . addslashes(SharingCart_Repository::getString('repo_url_desc')) . '"));
				item.appendChild(desc);
			}';
    }