Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

ramverket/exercises/quiz/

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)
<?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) ? 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 === "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>