Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/projects/anton-quiz/admin/
create_answer.php
create_question.php
create_quiz.php
dashboard.php
edit_quiz.php
show_results.php
view_details.php
edit_quiz.php
112 lines UTF-8 Windows (CRLF)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
session_start();
// Kontrollera att man är admin
if (!$_SESSION["is_admin"]) {
header('Location: ../index.php');
die();
}
require_once __DIR__ . '/../dbconnect.php';
$test_id = $_GET["test_id"] ?? 0;
// 1) Hämta testets information
$stmt = $conn->prepare("SELECT id, test_name, is_enabled FROM quizdb_tests WHERE id=?");
$stmt->bind_param("i", $test_id);
$stmt->execute();
$test = $stmt->get_result()->fetch_assoc();
if (!$test) {
echo "Testet hittades inte.";
die();
}
$test_name = $test["test_name"];
$is_enabled = $test["is_enabled"];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Redigera test</title>
</head>
<body>
<h1>Redigera test: <?php echo htmlspecialchars($test_name); ?></h1>
<!-- FORM för att uppdatera is_enabled -->
<form action="../endpoints/admin/update_enabled.php" method="POST">
<input type="hidden" name="test_id" value="<?php echo $test["id"]; ?>">
<h3>Testet är aktivt?</h3>
<label>
<input type="radio" name="is_enabled" value="1"
<?php echo ($is_enabled == 1) ? 'checked' : ''; ?>>
Ja
</label>
<label>
<input type="radio" name="is_enabled" value="0"
<?php echo ($is_enabled == 0) ? 'checked' : ''; ?>>
Nej
</label>
<br><br>
<input type="submit" value="Uppdatera status">
</form>
<hr>
<h2>Frågor i testet</h2>
<?php
// 2) Hämta samtliga frågor som hör till test_id
$stmt = $conn->prepare("SELECT id, question_text FROM quizdb_questions WHERE test_id = ?");
$stmt->bind_param("i", $test_id);
$stmt->execute();
$questionsDbResult = $stmt->get_result();
if ($questionsDbResult->num_rows === 0) {
echo "<p>Inga frågor har lagts till i detta test ännu.</p>";
} else {
while ($question = $questionsDbResult->fetch_assoc()) {
?>
<div style="margin-bottom: 20px;">
<h3>Fråga: <?php echo htmlspecialchars($question["question_text"]); ?></h3>
<?php
// 3) Hämta de svarsalternativ som hör till frågan
$stmtAnswers = $conn->prepare("
SELECT id, answer_text, is_correct, is_enabled
FROM quizdb_answers
WHERE question_id = ?
");
$stmtAnswers->bind_param("i", $question["id"]);
$stmtAnswers->execute();
$answersDbResult = $stmtAnswers->get_result();
if ($answersDbResult->num_rows === 0) {
echo "<p style='color: grey;'>Inga svarsalternativ skapade ännu.</p>";
} else {
echo "<ul>";
while ($answer = $answersDbResult->fetch_assoc()) {
// Visa texten för svaret, markera om det är rätt
$correctText = $answer["is_correct"] ? " (Rätt svar)" : "";
$enabledText = $answer["is_enabled"] ? "" : " [Inaktivt]";
echo "<li>"
. htmlspecialchars($answer["answer_text"])
. $correctText
. $enabledText
. "</li>";
}
echo "</ul>";
}
?>
</div>
<?php
}
}
?>
<a href="dashboard.php">Tillbaka</a>
</body>
</html>