forked from Shalini-Majumdar/Quizzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit_quiz.php
More file actions
66 lines (54 loc) · 1.66 KB
/
submit_quiz.php
File metadata and controls
66 lines (54 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
session_start();
include 'db_connect.php';
// Make sure the user is logged in
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
// Make sure a quiz ID was posted
if (!isset($_POST['quiz_id']) || !is_numeric($_POST['quiz_id'])) {
die("Invalid quiz.");
}
$quiz_id = intval($_POST['quiz_id']);
$user_id = $_SESSION['user_id'];
// Fetch all questions for the quiz
$stmt = $conn->prepare("SELECT id, correct_option FROM questions WHERE quiz_id = ?");
$stmt->bind_param("i", $quiz_id);
$stmt->execute();
$result = $stmt->get_result();
$score = 0;
$responses = [];
// Evaluate each question
while ($q = $result->fetch_assoc()) {
$qid = $q['id'];
$correct = $q['correct_option'];
if (isset($_POST["q$qid"])) {
$selected = $_POST["q$qid"];
// Save the response
$responses[] = [
'question_id' => $qid,
'selected' => $selected
];
if ($selected === $correct) {
$score++;
}
}
}
$stmt->close();
// Save the attempt
$stmt = $conn->prepare("INSERT INTO attempts (user_id, quiz_id, score) VALUES (?, ?, ?)");
$stmt->bind_param("iii", $user_id, $quiz_id, $score);
$stmt->execute();
$attempt_id = $stmt->insert_id;
$stmt->close();
// Save responses
$stmt = $conn->prepare("INSERT INTO responses (attempt_id, question_id, selected_option) VALUES (?, ?, ?)");
foreach ($responses as $r) {
$stmt->bind_param("iis", $attempt_id, $r['question_id'], $r['selected']);
$stmt->execute();
}
$stmt->close();
// Redirect to result page
header("Location: result.php?attempt_id=$attempt_id");
exit;