Show sourcecode
The following files exists in this folder. Click to view.
account.php
create_quiz.js
create_quiz.php
fetch_table.php
frontpage.php
header.php
login.php
quiz.php
quiz_answer_finished.php
quiz_creation_finished.php
signup.php
style.css
quiz_creation_finished.php
121 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
<?php
session_start();
include('../../incl/dbconnection.php');
/**
* @var PDO $dbconn
*/
$quiz_values = [];
foreach ($_POST as $key => $value) {
switch (true) {
case preg_match("/quizName/", $key):
// echo "<p>Quizzets namn är: " . $value . "</p>";
$quiz_values['quizName'] = $value;
break;
// Find all questions
case preg_match("/^question\d+\z/", $key):
// echo "<p>" . $value . " är en fråga</p>";
sscanf($key, "question%d", $questionNr);
// echo "<p>Frågan är nummer " . $questionNr . "</p>";
$quiz_values["questions"][$questionNr] = [];
$quiz_values["questions"][$questionNr]["text"] = $value;
$quiz_values["questions"][$questionNr]["choices"] = [];
break;
// Find all question choices
case preg_match("/^question\d+choice\d+\z/", $key):
// echo "<p>" . $value . " är ett svar</p>";
sscanf($key, "question%dchoice%d", $questionNr, $choiceNr);
$quiz_values["questions"][$questionNr]["choices"][$choiceNr] = ["text" => $value, "correct" => false];
break;
case preg_match("/^question\d+choice\d+Correct/", $key):
// echo "<p>" . $key . " är ett korrekt svar</p>";
sscanf($key, "question%dchoice%dCorrect", $questionNr, $choiceNr);
$quiz_values["questions"][$questionNr]["choices"][$choiceNr]["correct"] = true;
break;
default:
// echo "<p>" . $value . " är något annat</p>";
break;
}
}
$insertQuiz = "INSERT INTO quiz_quizzes (quiz_name, creator_id) values(?,?)";
$insertQuestion = "INSERT INTO quiz_questions (quiz_id, question_text, question_order) values(?,?,?)";
$insertOption = "INSERT INTO quiz_options (question_id, option_text, is_correct) values(?,?,?)";
$checkQuizExists = "SELECT 1 FROM quiz_quizzes WHERE quiz_name=? AND creator_id=?";
try {
// Begin transaction so that it wont break tables if exception occurs
$dbconn->beginTransaction();
$quizId = 0; // We will get this from $dbconn->lastInsertId()
// Check if quiz already exists on account
$checkStmt = $dbconn->prepare($checkQuizExists);
$checkStmt->execute([$quiz_values["quizName"], $_SESSION["userId"]]);
if ($checkStmt->fetch()) {
throw new Exception("Det finns redan ett quiz med detta namn på kontot!");
} else {
// Skapa ett nytt quiz
$insertQuizStmt = $dbconn->prepare($insertQuiz);
$insertQuizStmt->execute([$quiz_values["quizName"], $_SESSION["userId"]]);
$quizId = $dbconn->lastInsertId();
// Förbered insert statements
$insertQuestionStmt = $dbconn->prepare($insertQuestion);
$insertOptionStmt = $dbconn->prepare($insertOption);
foreach ($quiz_values["questions"] as $questionNr => $questionData) {
$insertQuestionStmt->execute([
$quizId,
$questionData["text"],
$questionNr // Order should be same as was made when created
]);
$questionId = $dbconn->lastInsertId();
foreach ($questionData["choices"] as $choiceNr => $choiceData) {
// [choiceNr] => ["text" => <text>, "correct" => <is_correct>]
$insertOptionStmt->execute([
$questionId,
$choiceData["text"],
(int) $choiceData["correct"]
]);
}
}
}
// Commit once everything completed correctly
$dbconn->commit();
echo "Data has been added successfully!";
} catch (Exception $e) {
$dbconn->rollBack();
echo "Exception: " . $e->getMessage();
}
// echo "<pre>";
// print_r($_SESSION);
// print_r($quiz_values);
// echo "</pre>";
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Färdigt!</title>
</head>
<body>
Nu är quizzet färdigt!
<a href="frontpage.php" style="text-decoration: none;">
<button class="big-button">Tillbaka till startsidan</button>
</a>
</body>
</html>