Källkod
Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.
webbserverprogrammering/projects/quiz-extended/
26 filer
admin.php
confirm_account.php
create_account.php
create_quiz.php
css/
dbconnection.php
highscore.php
index.php
js/
logbook.php
login.php
mysql_create_table_options.php
mysql_create_table_questions.php
mysql_create_table_quizzes.php
mysql_create_table_results.php
mysql_create_table_submits.php
mysql_create_table_users.php
plan.php
planering.txt
profile.php
projektrapport.txt
quizzes.php
resources/
result.php
send_email.php
session_variable_array_check.php
confirm_account.php
create_account.php
create_quiz.php
css/
dbconnection.php
highscore.php
index.php
js/
logbook.php
login.php
mysql_create_table_options.php
mysql_create_table_questions.php
mysql_create_table_quizzes.php
mysql_create_table_results.php
mysql_create_table_submits.php
mysql_create_table_users.php
plan.php
planering.txt
profile.php
projektrapport.txt
quizzes.php
resources/
result.php
send_email.php
session_variable_array_check.php
result.php
194 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
<?php
error_reporting(-1); // Report all type of errors
ini_set('display_errors', 1); // Display all errors
ini_set('output_buffering', 0); // Do not buffer outputs, write directly
function dump($dump) {
echo "<pre>";
var_dump($dump);
echo "</pre>";
}
session_start();
include "session_variable_array_check.php";
if (!isset($_POST['submitQuiz']) && !isset($_GET['r']))
header("Location:quizzes.php");
include "dbconnection.php";
$usingGet = true;
$success = true;
$answers = [];
if (isset($_POST['submitQuiz'])) {
$usingGet = false;
$quizId = (int) $_POST['quizId'];
$displayname = $_SESSION['quizExtended']['displayname']; // empty if not logged in
// retrieves user's answered option for each question
foreach ($_POST as $questionId => $answeredOptionId) {
if (is_int($questionId)) {
$answers[$questionId] = $answeredOptionId;
}
}
// retrieves array of the correct options for this quiz. Index is question id
$sql = "SELECT question_id, option_id FROM quizext_options WHERE quiz_id=? AND correct=?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$quizId, "correct"]);
$correctOptions = $stmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC | PDO::FETCH_UNIQUE);
$userPoints = 0;
$maxPoints = count($correctOptions);
foreach ($correctOptions as $questionId => $correctOption) {
if ($answers[$questionId] == $correctOption['option_id']) {
// answered correct for question $questionId
$userPoints++;
}
}
$sql = "SELECT quiz_name FROM quizext_quizzes WHERE quiz_id=?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$quizId]);
$quizInfo = $stmt->fetch(PDO::FETCH_ASSOC);
$quizName = $quizInfo['quiz_name'];
// registers result information into database only if logged in
if ($_SESSION['quizExtended']['loggedIn']) {
$userId = $_SESSION['quizExtended']['userId'];
// for one quiz try
$sql = "INSERT INTO quizext_results (user_id, quiz_id, user_points, max_points) VALUES (?, ?, ?, ?)";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$userId, $quizId, $userPoints, $maxPoints]);
$resultId = (int) $dbconn->lastInsertId();
// for each question/answer submitted
foreach ($answers as $questionId => $optionId) {
$sql = "INSERT INTO quizext_submits (user_id, quiz_id, result_id, question_id, option_id) VALUES (?, ?, ?, ?, ?)";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$userId, $quizId, $resultId, $questionId, ((int) $optionId)]);
}
}
} else {
$resultId = $_GET['r'];
$sql = "SELECT question_id, option_id FROM quizext_submits WHERE result_id=?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$resultId]);
$answersLayered = $stmt->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_UNIQUE | PDO::FETCH_GROUP);
// if $answersLayered is empty, means that provided result id is invalid
if (!empty($answersLayered)) {
// means result id was functional, thus can now be used safely
foreach ($answersLayered as $questionId => $answerInfo) {
$answers[$questionId] = $answerInfo['option_id'];
}
$sql = "
SELECT
quizext_users.displayname,
quizext_results.quiz_id,
quizext_quizzes.quiz_name,
user_points,
max_points
FROM quizext_results
INNER JOIN quizext_users ON quizext_results.user_id = quizext_users.user_id
INNER JOIN quizext_quizzes ON quizext_results.quiz_id = quizext_quizzes.quiz_id
WHERE result_id=?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$resultId]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$displayname = $result['displayname'];
$quizId = $result['quiz_id'];
$quizName = $result['quiz_name'];
$userPoints = $result['user_points'];
$maxPoints = $result['max_points'];
} else {
$success = false;
}
}
if ($success) {
// all options for quiz (for printout)
$sql = "SELECT question_id, option_id, option_txt, correct FROM quizext_options WHERE quiz_id=?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$quizId]);
$allOptions = $stmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC);
// all questions for quiz (for printout)
$sql = "SELECT question_id, question_nr, question_txt FROM quizext_questions WHERE quiz_id=?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$quizId]);
$questions = $stmt->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_UNIQUE);
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Result - CuriousQuizzes</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<a href="index.php" id="logo">
CuriousQuizzes
</a> <?php
if ($success) {
if ($usingGet) { ?>
<h1>You are looking at an old result:</h1>
<?php } ?>
<h1>Result for user <?= $displayname ?> for quiz <span class="cursive"><?= $quizName ?></span></h1>
<main>
<?php
if (!$usingGet) { ?>
<a href="quizzes.php">return to quiz list</a>
<?php } ?>
<h3 class="pointsResult"><?= $userPoints ?> out of <?= $maxPoints ?> correct!</h3>
<?php
foreach ($questions as $questionId => $question) {
?><h4><?= "Question {$question['question_nr']}" ?></h4>
<p class="correctedQuestion"><?= $question['question_txt'] ?></p><?php
foreach ($allOptions[$questionId] as $option) {
$classList = ["correctedOption"];
if ($answers[$questionId] == $option['option_id']) // user answered this option
$classList[] = "answered"; // set background to red
if ($option['correct'] === "correct") // this option is correct
$classList[] = "correct"; // set background to green
?><p class="<?= join(" ", $classList) ?>"><input type="radio" disabled><?= $option['option_txt'] ?></p><?php
}
} ?>
</main>
<?php } else { ?>
<h1>Invalid argument. Broken link!</h1>
<h1>¯\_(ツ)_/¯</h1>
<main>
bringing you back in 5 seconds...<br><br>
</main>
<script type="text/javascript">
setTimeout(() => window.history.back(), 5000);
</script>
<?php } ?>
</body>
</html>