<?php
// セッションの開始
if (!session_id()) {
session_start();
}
global $wpdb;
$table_question = $wpdb->prefix . 'question';
$table_answer = $wpdb->prefix . 'answer';
// セッションでランダムな問題IDを管理
if (!isset($_SESSION['random_questions'])) {
// ランダムに10個の問題IDを取得
$random_questions = $wpdb->get_col(
"SELECT question_id FROM $table_question ORDER BY RAND() LIMIT 5"
);
$_SESSION['random_questions'] = $random_questions;
$_SESSION['current_index'] = 0;
}
$current_index = $_SESSION['current_index'];
$random_questions = $_SESSION['random_questions'];
// 現在の問題IDを取得
$current_question_id = isset($random_questions[$current_index]) ? $random_questions[$current_index] : null;
// 現在の問題を取得
$question = $wpdb->get_row(
$wpdb->prepare("SELECT * FROM $table_question WHERE question_id = %d", $current_question_id)
);
// 解答処理部分
if (isset($_POST['submit_answer']) && $question) {
if ($_POST['input_answer'] == $question->correct_answer) {
$accuracy = 1;
} else {
$accuracy = 0;
}
$user_data = array(
'user_name' => wp_get_current_user()->user_login,
'user_answer' => sanitize_text_field($_POST['input_answer']),
'question_id' => $question->question_id,
'accuracy' => $accuracy,
'created_at' => date('Y-m-d H:i:s'),
);
$format = array('%s', '%s', '%d', '%d', '%s');
$inserted = $wpdb->insert($table_answer, $user_data, $format);
if ($inserted === false) {
echo 'データの保存に失敗しました: ' . $wpdb->last_error;
}
// 次の問題のインデックスへ
$_SESSION['current_index']++;
wp_redirect(get_permalink());
exit();
}
?>
<!DOCTYPE html>
<html lang="ja">
<head prefix="og: http://ogp.me/ns#">
省略
</head>
<body <?php body_class(); ?> >
<header>
</header>
<main>
<?php
// 結果表示部分の修正
if (!$question) {
echo '<p class="result_good_job">全ての問題が終了しました。お疲れ様でした。</p>';
echo '<a class="result_to_home" href="' . site_url() . '">トップページに戻る</a>';
// 問題数と正解数
$question_count = count($random_questions);
$accuracy_count = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $table_answer
WHERE user_name = %s
AND accuracy = 1
AND question_id IN (" . implode(',', $random_questions) . ")",
wp_get_current_user()->user_login
)
);
?>
<div class='result_accuracy'>
<span><?php echo $question_count ?></span>問中
<span><?php echo $accuracy_count ?></span>問正解
</div>
<?php
// 各問題の結果表示
foreach ($random_questions as $q_id) {
$quiz = $wpdb->get_row(
$wpdb->prepare("SELECT * FROM $table_question WHERE question_id = %d", $q_id)
);
$your_answer = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM $table_answer
WHERE user_name = %s AND question_id = %d",
wp_get_current_user()->user_login,
$q_id
)
);
?>
<section class='result_question'>
<h2><?php echo esc_html($quiz->question_text) ?></h2>
<ul>
<li><?php echo esc_html($quiz->option_a) ?></li>
<li><?php echo esc_html($quiz->option_b) ?></li>
<li><?php echo esc_html($quiz->option_c) ?></li>
<li><?php echo esc_html($quiz->option_d) ?></li>
</ul>
<div>
<p>正解は<?php echo esc_html($quiz->correct_answer) ?></p>
<p>あなたの答えは<?php echo esc_html($your_answer->user_answer) ?></p>
</div>
</section>
<?php
}
// セッションをクリア
unset($_SESSION['random_questions']);
unset($_SESSION['current_index']);
echo '<a class="result_to_home" href="' . site_url() . '">トップページに戻る</a>';
return;
}
?>
<form method="post" action="" class="form_question">
<h2><?php echo esc_html($question->question_text) ?></h2>
<ul>
<li>
<label>
<input type="radio" name="input_answer" value="a" />
<?php echo esc_html($question->option_a) ?>
</label>
</li>
<li>
<label>
<input type="radio" name="input_answer" value="b" />
<?php echo esc_html($question->option_b) ?>
</label>
</li>
<li>
<label>
<input type="radio" name="input_answer" value="c" />
<?php echo esc_html($question->option_c) ?>
</label>
</li>
<li>
<label>
<input type="radio" name="input_answer" value="d" />
<?php echo esc_html($question->option_d) ?>
</label>
</li>
</ul>
<input type='submit' name='submit_answer' value='次の問題へ' />
</form>
</main>
<?php get_footer(); ?>