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_answer_finished.php
124 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
<?php
session_start();
include('../../incl/dbconnection.php');
/**
* @var PDO $dbconn
*/
$questionAnswers = [];
$correctAnswers = [];
$questionNrToIdMap = [];
$score = 0;
// Spara fråga => val
foreach ($_POST as $key => $value) {
switch (true) {
case preg_match("/^q\d+c\d+\z/", $key):
echo $key;
sscanf($key, "q%dc%d", $questionNr, $choiceNr);
$questionAnswers[$questionNr] = ["text" => $value, "choiceNr" => $choiceNr];
break;
default:
echo "<p>" . $value . " är något annat</p>";
break;
}
}
$insertView = "INSERT INTO quiz_views (quiz_id) values(?)";
$insertResults = "INSERT INTO quiz_quiz_results (user_id, quiz_id, score) values(?,?, ?)";
$insertAnswers = "INSERT INTO quiz_answers (quiz_result_id, question_id, option_id) values(?,?,?)";
$selectQuestions = "SELECT question_id, question_order FROM quiz_questions WHERE quiz_id=?";
$getOption = "SELECT option_id, is_correct FROM quiz_options WHERE question_id=? AND option_text=?";
$updateViews = "UPDATE quiz_quizzes SET total_views=total_views+1 WHERE quiz_id=?";
try {
if (isset($_GET["success"]) && $_GET["success"] == 1) {
?>
<div>
Grattis och sånt, ditt svar är inskickat!
<a href="./frontpage.php">
<button>
Tillbaka till startsidan
</button>
</a>
</div>
<?php
} else {
// Begin transaction so that it wont break tables if exception occurs
$dbconn->beginTransaction();
$quizId = $_POST["quiz_id"];
$insertViewStmt = $dbconn->prepare($insertView);
$insertViewStmt->execute([$quizId]);
$updateViewsStmt = $dbconn->prepare($updateViews);
$updateViewsStmt->execute([$quizId]);
$questionStmt = $dbconn->prepare($selectQuestions);
$questionStmt->execute([$quizId]);
// Find correct answers
foreach ($questionStmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$qNr = $row["question_order"];
$qId = $row["question_id"];
echo "\n";
print_r($row);
$questionNrToIdMap[$qNr] = $qId;
$getOptionStmt = $dbconn->prepare($getOption);
$getOptionStmt->execute([$qId, $questionAnswers[$qNr]["text"]]);
$option = $getOptionStmt->fetch(PDO::FETCH_ASSOC);
echo "<br>";
print_r($option);
$questionAnswers[$qNr]["choiceId"] = $option["option_id"];
if ($option && $option["is_correct"]) {
$correctAnswers[] = $questionAnswers[$qNr]["text"];
$score++;
}
}
// Insert quiz results
$insertResultsStmt = $dbconn->prepare($insertResults);
$insertResultsStmt->execute([$_SESSION["userId"], $quizId, $score]);
$resultId = $dbconn->lastInsertId();
// Insert Answers
$insertAnswersStmt = $dbconn->prepare($insertAnswers);
foreach ($questionAnswers as $qNr => $answer) {
$insertAnswersStmt->execute([$resultId, $questionNrToIdMap[$qNr], $answer["choiceId"]]);
}
// // Commit once everything completed correctly
$dbconn->commit();
echo "Data has been added successfully!";
echo "<pre>";
print_r($_SESSION);
echo "qAnswers \n";
print_r($questionAnswers);
echo "score \n";
print_r($score);
echo "\n correctAnswers \n";
print_r($correctAnswers);
echo "</pre>";
header("Location: " . $_SERVER['PHP_SELF'] . "?success=1");
}
} catch (Exception $e) {
$dbconn->rollBack();
echo "Exception: " . $e->getMessage();
}
?>
<!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>
</body>
</html>