Show sourcecode
The following files exists in this folder. Click to view.
css/
dashboard.php
inc/
index.php
js/
login.php
logout.php
profile.php
quizzes.php
register.php
results.php
dashboard.php
271 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
<?php
/* =========================
Inkludera konfiguration,
databas och säkerhet
========================= */
include("inc/config.php");
include("inc/connect_db.php");
include("inc/db_manager.php");
include("inc/login_check.php");
/* =========================
Kontrollera att användaren
är admin
========================= */
$user = getUser($conn, "id", $id);
if (!$user['admin']) {
header("Location: index.php");
exit();
}
/* =========================
Ta bort användare
========================= */
if (isset($_POST['delete_user'])) {
$id = $_POST['id'];
deleteUser($conn, $id);
header("Location: dashboard.php");
exit();
}
/* =========================
Ta bort quiz
========================= */
if (isset($_POST['delete_quiz'])) {
$id = $_POST['id'];
deleteQuiz($conn, $id);
header("Location: dashboard.php");
exit();
}
/* =========================
Skapa nytt quiz med frågor
och svarsalternativ
========================= */
if (isset($_POST['create_quiz'])) {
$title = $_POST['title'];
$description = $_POST['description'];
// Skapa quiz och få quiz-ID
$quiz_id = addQuiz($conn, $title, $description);
// Loopa igenom alla frågor
foreach ($_POST['questions'] as $question) {
$points = (int)$question['points'];
if ($points < 1) {
$points = 1;
}
$question_id = addQuestion(
$conn,
$question['question'],
$points,
$quiz_id
);
// Lägg till svarsalternativ
foreach ($question['choices'] as $choice_index => $choice_text) {
$correct_index = (int)$question['correct'];
$is_correct = ($choice_index === $correct_index) ? 1 : 0;
addChoice($conn, $choice_text, $is_correct, $question_id);
}
}
header("Location: dashboard.php");
exit();
}
$page_title = "Admin panel - Quizzeria";
?>
<!DOCTYPE html>
<html lang="sv">
<?php $js = ["dashboard"]; ?>
<?php include("inc/head.php"); ?>
<body>
<?php include("inc/header.php"); ?>
<h1>Admin panel</h1>
<!-- =========================
Hantera användare
========================= -->
<div id="admin_table_container">
<div class="table_holder">
<div class="cfg_table_wrapper">
<h2>Hantera användare</h2>
<div class="cfg_table_scrollable">
<table id="user_table">
<tr>
<th>ID</th>
<th>Användarnamn</th>
<th>Lösenord</th>
<th>Poäng</th>
<th>Registrerad</th>
<th>Senast inloggad</th>
<th></th>
<th></th>
</tr>
<?php
// Hämta och visa alla användare
$users = getUsers($conn);
foreach ($users as $user) {
echo "<tr>";
echo "<td>{$user['id']}</td>";
echo "<td>" . htmlspecialchars($user['username']) . "</td>";
echo "<td>{$user['password_hash']}</td>";
echo "<td>{$user['points']}</td>";
echo "<td>{$user['created_at']}</td>";
echo "<td>{$user['last_login']}</td>";
// Admin kan inte tas bort
if (!$user['admin']) {
echo "
<td>
<form method='POST'>
<input type='hidden' name='id' value='{$user['id']}'>
<input type='submit' name='delete_user' value='Ta bort'>
</form>
</td>";
} else {
echo "<td><i>Admin</i></td>";
}
// Visa användarens resultat
echo "
<td>
<form method='POST'>
<input type='hidden' name='id' value='{$user['id']}'>
<input type='submit' name='show_results' value='Visa resultat'>
</form>
</td>";
echo "</tr>";
}
?>
</table>
</div>
</div>
</div>
<!-- =========================
Visa resultat per användare
========================= -->
<div class="cfg_table_wrapper">
<h2>Hantera resultat</h2>
<div class="cfg_table_scrollable">
<table id="results_table" <?php if (isset($_POST['show_results'])) echo "style='display: table;'"; ?>>
<tr>
<th>Quiz</th>
<th>Användare</th>
<th>Poäng</th>
<th></th>
</tr>
<?php
if (isset($_POST['show_results'])) {
$results = getAttempts($conn, "user_id", $_POST['id']);
foreach ($results as $result) {
$quiz = getQuiz($conn, "id", $result['quiz_id']);
$user = getUser($conn, "id", $result['user_id']);
echo "<tr>";
echo "<td>{$quiz['title']}</td>";
echo "<td>" . htmlspecialchars($user['username']) . "</td>";
echo "<td>{$result['score']}</td>";
echo "<td><a href='results.php?id={$result['id']}'>Visa</a></td>";
echo "</tr>";
}
if (count($results) === 0) {
echo "<tr><td colspan='4'><i>Inga resultat hittades</i></td></tr>";
}
}
?>
</table>
</div>
</div>
</div>
<!-- =========================
Hantera quiz
========================= -->
<div id="admin_table_container">
<div class="table_holder">
<div class="cfg_table_wrapper">
<h2>Hantera quiz</h2>
<div class="cfg_table_scrollable">
<table id="quiz_table">
<tr>
<th>ID</th>
<th>Titel</th>
<th>Beskrivning</th>
<th></th>
</tr>
<?php
$quizzes = getQuizzes($conn);
foreach ($quizzes as $quiz) {
echo "<tr>";
echo "<td>{$quiz['id']}</td>";
echo "<td>{$quiz['title']}</td>";
echo "<td>{$quiz['description']}</td>";
echo "
<td>
<form method='POST'>
<input type='hidden' name='id' value='{$quiz['id']}'>
<input type='submit' name='delete_quiz' value='Ta bort'>
</form>
</td>";
echo "</tr>";
}
if (count($quizzes) === 0) {
echo "<tr><td colspan='4'><i>Inga quiz hittades</i></td></tr>";
}
?>
</table>
</div>
</div>
</div>
</div>
<!-- =========================
Skapa nytt quiz
========================= -->
<h2>Skapa quiz</h2>
<form id="create_quiz_form" method="POST">
<input type="text" name="title" placeholder="Titel" required>
<textarea name="description" placeholder="Beskrivning" required></textarea>
<p>Frågor</p>
<div id="create_question_container">
<!-- Första frågan (JS duplicerar denna) -->
<div class="create_question_holder">
<input type="text" name="questions[0][question]" placeholder="Fråga" required>
<input type="number" name="questions[0][points]" placeholder="Poäng" min="1" required>
<p>Skriv in svarsalternativ och markera rätt svar</p>
<?php for ($i = 0; $i < 4; $i++): ?>
<div class="create_choice_holder">
<input class="create_choice" type="text" name="questions[0][choices][<?= $i ?>]" placeholder="Svar <?= $i + 1 ?>" required>
<input type="radio" name="questions[0][correct]" value="<?= $i ?>" <?= $i === 0 ? "checked" : "" ?>>
</div>
<?php endfor; ?>
</div>
</div>
<div id="create_quiz_btn_holder">
<button type="button" id="add_question">Lägg till fråga</button>
<input type="submit" name="create_quiz" value="Skapa quizet">
</div>
</form>
<?php include("inc/footer.php"); ?>
</body>
</html>