diff --git a/admin_panel.php b/admin_panel.php
new file mode 100644
index 0000000..5daf619
--- /dev/null
+++ b/admin_panel.php
@@ -0,0 +1,60 @@
+query("
+ SELECT u.username, t.title, ut.score, ut.completed_at
+ FROM user_tests ut
+ JOIN users u ON ut.user_id = u.id
+ JOIN tests t ON ut.test_id = t.id
+ ORDER BY ut.completed_at DESC
+");
+$results = $stmt->fetchAll();
+
+require_once 'includes/header.php';
+?>
+
+
-
+
+
+
diff --git a/retake_test.php b/retake_test.php
new file mode 100644
index 0000000..f81d8a3
--- /dev/null
+++ b/retake_test.php
@@ -0,0 +1,52 @@
+beginTransaction();
+
+ // Delete from user_answers
+ $stmt_answers = $db->prepare("DELETE FROM user_answers WHERE user_id = :user_id AND question_id IN (SELECT id FROM questions WHERE test_id = :test_id)");
+ $stmt_answers->execute(['user_id' => $user_id, 'test_id' => $test_id]);
+
+ // Delete from user_tests
+ $stmt_tests = $db->prepare("DELETE FROM user_tests WHERE user_id = :user_id AND test_id = :test_id");
+ $stmt_tests->execute(['user_id' => $user_id, 'test_id' => $test_id]);
+
+ // Commit the transaction
+ $db->commit();
+
+ // Redirect to the test page
+ header('Location: take_test.php?test_id=' . $test_id);
+ exit();
+
+} catch (PDOException $e) {
+ // Rollback the transaction if something failed
+ if ($db->inTransaction()) {
+ $db->rollBack();
+ }
+ // You might want to log this error instead of showing it to the user
+ die("Database error while trying to reset the test. Please try again.");
+}
+
+// No need for footer as we are redirecting
+?>
diff --git a/submit_test.php b/submit_test.php
index 8098516..5d47790 100644
--- a/submit_test.php
+++ b/submit_test.php
@@ -32,13 +32,24 @@ try {
// Begin transaction
$db->beginTransaction();
+ // Prepare statement for inserting or updating user answers
+ $answerStmt = $db->prepare(
+ "INSERT INTO user_answers (user_id, question_id, option_id) VALUES (:user_id, :question_id, :option_id)
+ ON DUPLICATE KEY UPDATE option_id = VALUES(option_id)"
+ );
-
- // Calculate total score
+ // Calculate total score and insert individual answers
foreach ($answers as $question_id => $option_id) {
if (isset($scores[$option_id])) {
$total_score += $scores[$option_id];
}
+
+ // Insert the answer
+ $answerStmt->execute([
+ 'user_id' => $user_id,
+ 'question_id' => $question_id,
+ 'option_id' => $option_id
+ ]);
}
// Save the final score in the user_tests table
diff --git a/take_test.php b/take_test.php
index 2befdb7..eade55c 100644
--- a/take_test.php
+++ b/take_test.php
@@ -20,17 +20,16 @@ $user_id = $_SESSION['user_id'];
try {
$db = db();
-
+
// Check if user has already taken this test
$checkStmt = $db->prepare("SELECT COUNT(*) FROM user_tests WHERE user_id = :user_id AND test_id = :test_id");
$checkStmt->execute(['user_id' => $user_id, 'test_id' => $test_id]);
if ($checkStmt->fetchColumn() > 0) {
- echo "
";
- require_once 'includes/footer.php';
- exit();
+ echo "
";
+ require_once 'includes/footer.php';
+ exit();
}
-
// Fetch test details
$stmt = $db->prepare("SELECT title, description FROM tests WHERE id = :id");
$stmt->execute(['id' => $test_id]);
@@ -52,12 +51,33 @@ try {
");
$questionsStmt->execute(['test_id' => $test_id]);
$questionsData = $questionsStmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC);
-
+ $question_keys = array_keys($questionsData);
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
-
?>
+
@@ -67,31 +87,98 @@ try {
+
This test has no questions yet.
-
+
+
diff --git a/test_results.php b/test_results.php
index 75510cf..31787d9 100644
--- a/test_results.php
+++ b/test_results.php
@@ -58,7 +58,8 @@ try {