| Server IP : 172.67.187.206 / Your IP : 172.71.28.156 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : E:/Inetpub/www/training/user/ |
Upload File : |
<?php
include("../session.php");
if ($role != 'user') {
header("Location: ../index.php");
exit;
}
include("../db.php");
// ตรวจสอบว่ามีการส่งพารามิเตอร์ที่ต้องการหรือไม่
if (!isset($_GET['exam'])) {
header("Location: dashboard.php");
exit;
}
// ถอดรหัส subject ID
$subject_id = base64_decode($_GET['exam']);
// ตรวจสอบว่าผู้ใช้ได้สอบผ่านวิชานี้แล้วหรือไม่
$user_id = $_SESSION['user_id'];
$check_result = mysqli_query($conn, "SELECT * FROM exam_results WHERE user_id = $user_id AND subject_id = $subject_id AND passed = 1");
if (mysqli_num_rows($check_result) > 0) {
header("Location: dashboard.php");
exit;
}
// ดึงข้อมูลวิชา
$subject_query = mysqli_query($conn, "SELECT * FROM subjects WHERE id = $subject_id");
if (!$subject = mysqli_fetch_assoc($subject_query)) {
header("Location: dashboard.php");
exit;
}
// ดึงข้อสอบทั้งหมดในวิชานี้
$questions_query = mysqli_query($conn, "SELECT * FROM questions WHERE subject_id = $subject_id ORDER BY id ASC");
$questions = [];
$total_score = 0;
while ($row = mysqli_fetch_assoc($questions_query)) {
$questions[] = $row;
$total_score += $row['score'];
}
// ถ้าไม่มีข้อสอบในวิชานี้
if (count($questions) == 0) {
$error_message = "ขออภัย วิชานี้ยังไม่มีข้อสอบ";
}
// ตรวจสอบว่ามีการส่งคำตอบหรือไม่
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit_exam'])) {
$user_score = 0;
$answers = [];
// ตรวจคำตอบ
foreach ($questions as $question) {
$question_id = $question['id'];
// ตรวจว่าผู้ใช้ตอบข้อนี้หรือไม่
if (isset($_POST['answer'][$question_id])) {
$user_answer = $_POST['answer'][$question_id];
$correct_answer = $question['correct_choice'];
// เก็บข้อมูลการตอบ
$answers[$question_id] = [
'user_answer' => $user_answer,
'correct_answer' => $correct_answer,
'is_correct' => ($user_answer == $correct_answer)
];
// ถ้าตอบถูก เพิ่มคะแนน
if ($user_answer == $correct_answer) {
$user_score += $question['score'];
}
}
}
// คำนวณเปอร์เซ็นต์ที่ได้
$percentage = ($total_score > 0) ? ($user_score / $total_score) * 100 : 0;
$percentage = round($percentage, 2);
// ตรวจสอบว่าผ่านหรือไม่
$passed = ($percentage >= $subject['pass_percentage']) ? 1 : 0;
// บันทึกผลสอบลงฐานข้อมูล
$insert_result = mysqli_query($conn, "INSERT INTO exam_results
(user_id, subject_id, score, percentage, passed)
VALUES ($user_id, $subject_id, $user_score, $percentage, $passed)");
// ส่งไปยังหน้าแสดงผลสอบ
header("Location: exam_result.php?result=" . base64_encode(mysqli_insert_id($conn)));
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ทำข้อสอบ - <?php echo htmlspecialchars($subject['name']); ?></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style>
.question-box {
margin-bottom: 30px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
}
.choice-list {
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<h3 class="text-center">ทำข้อสอบวิชา: <?php echo htmlspecialchars($subject['name']); ?></h3>
<p class="text-right">
<a href="dashboard.php" class="btn btn-default btn-sm">← กลับหน้าหลัก</a>
</p>
<?php if (isset($error_message)): ?>
<div class="alert alert-warning">
<p><?php echo $error_message; ?></p>
<p><a href="dashboard.php" class="btn btn-primary">กลับหน้าหลัก</a></p>
</div>
<?php else: ?>
<div class="panel panel-default">
<div class="panel-heading">
<h4>คำชี้แจง:</h4>
<ul>
<li>ข้อสอบมีทั้งหมด <?php echo count($questions); ?> ข้อ</li>
<li>คะแนนเต็ม <?php echo $total_score; ?> คะแนน</li>
<li>ต้องได้คะแนนอย่างน้อย <?php echo $subject['pass_percentage']; ?>% จึงจะผ่าน</li>
</ul>
</div>
<div class="panel-body">
<form method="post" id="exam-form">
<?php $question_num = 1; ?>
<?php foreach ($questions as $question): ?>
<div class="question-box">
<h4>ข้อที่ <?php echo $question_num; ?> (<?php echo $question['score']; ?> คะแนน)</h4>
<div class="question-text">
<?php echo htmlspecialchars($question['question']); ?>
</div>
<div class="choice-list">
<?php
$choices = [
1 => $question['choice1'],
2 => $question['choice2'],
3 => $question['choice3'],
4 => $question['choice4']
];
// กรองตัวเลือกที่ว่าง
$choices = array_filter($choices);
?>
<?php foreach ($choices as $choice_num => $choice_text): ?>
<div class="radio">
<label>
<input type="radio" name="answer[<?php echo $question['id']; ?>]" value="<?php echo $choice_num; ?>" required>
<?php echo htmlspecialchars($choice_text); ?>
</label>
</div>
<?php endforeach; ?>
</div>
</div>
<?php $question_num++; ?>
<?php endforeach; ?>
<div class="text-center">
<button type="submit" name="submit_exam" class="btn btn-primary btn-lg">ส่งคำตอบ</button>
</div>
</form>
</div>
</div>
<?php endif; ?>
</div>
</body>
</html>