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/
22 filer
admin.php
create_account.php
create_quiz.php
css/
dbconnection.php
hacktest.php
highscore.php
index.php
js/
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
planering.txt
profile.php
quizzes.php
resources/
result.php
session_variable_array_check.php
create_account.php
create_quiz.php
css/
dbconnection.php
hacktest.php
highscore.php
index.php
js/
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
planering.txt
profile.php
quizzes.php
resources/
result.php
session_variable_array_check.php
create_quiz.php
148 lines ASCII Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
<?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 (!$_SESSION['quiz']['loggedIn']) {
header("Location:login.php");
}
include "dbconnection.php";
if (isset($_POST['createQuiz'])) {
$quizName = $_POST['quizName'];
$questions = [];
foreach ($_POST as $key => $value) {
if (mb_strpos($key, "qstText") !== false) { // if is question text element
$questions[(count($questions) + 1)]['text'] = $value;
}
if (mb_strpos($key, "optText") !== false) {
if (!isset($questions[count($questions)]['options']))
$questions[count($questions)]['options'] = [];
$options = $questions[count($questions)]['options'];
$options[count($options) + 1] = $value;
$questions[count($questions)]['options'] = $options;
}
if (mb_strpos($key, "correctOption") !== false) {
$questions[count($questions)]['correctOption'] = (int) $value;
}
}
// retrieve user ID for use as quiz creator ID in registering of quiz below
$creatorId = $_SESSION['quiz']['userId'];
// add quiz info into quiz_quizzes table
$sql = "INSERT INTO quiz_quizzes (creator_user_id, quiz_name, nr_of_questions) VALUES (?, ?, ?)";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$creatorId, $quizName, count($questions)]);
$quizId = (int) $dbconn->lastInsertId();
foreach ($questions as $questionNr => $questionInfo) {
// add questions into quiz_questions table
$sql = "INSERT INTO quiz_questions (quiz_id, question_nr, question_txt) VALUES (?, ?, ?)";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$quizId, $questionNr, $questionInfo['text']]);
$questionId = (int) $dbconn->lastInsertId();
// add options into quiz_options table
foreach ($questionInfo['options'] as $optionNr => $optionTxt) {
if ($optionNr === $questionInfo['correctOption'])
$correct = "correct";
else
$correct = "incorrect";
$sql = "INSERT INTO quiz_options (question_id, quiz_id, option_nr, option_txt, correct) VALUES (?, ?, ?, ?, ?)";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$questionId, $quizId, $optionNr, $optionTxt, $correct]);
}
}
$quizCreated = true;
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>Create quiz - CuriousQuizzes</title>
</head>
<body>
<?php if (isset($quizCreated)) { ?>
<div id="quizCreationSuccess">
<div id="quizCreationSuccessPopup">
<h1><a href="quizzes.php?q=<?= $quizId ?>">Quiz created! Play now!</a></h1>
<a href="index.php">or go to main menu</a>
</div>
</div>
<?php } ?>
<a href="index.php" id="logo">
CuriousQuizzes
</a>
<h1>Create quiz</h1>
<main>
<form action="" method="post">
<label id="quizNameLabel">
Quiz name<br>
<input type="text" name="quizName">
</label>
<div class="newQuestion" data-question-id="1">
<label>
Question 1<br>
<textarea class="questionText" name="qstText_1"></textarea>
</label>
<div class="btnRemoveQuestion"></div>
<div class="newOptions">
Mark the <strong>correct</strong> option using the radio buttons.<br><br>
<div class="newOption" data-option-id="1">
<label>
Option 1<br>
<textarea class="optionText" name="q1_optText_1"></textarea>
</label>
<input type="radio" name="q1_correctOption" value="1" required checked>
<button class="btnRemoveOption" type="button"></button>
</div>
<div class="newOption" data-option-id="2">
<label>
Option 2<br>
<textarea class="optionText" name="q1_optText_2"></textarea>
</label>
<input type="radio" name="q1_correctOption" value="2" required>
<button class="btnRemoveOption" type="button"></button>
</div>
<button class="btnNewOption" type="button">+ new option</button>
</div>
</div>
<button id="btnNewQuestion" type="button">+ new question</button>
<div>
<input type="submit" name="createQuiz" value="Create quiz">
</div>
</form>
</main>
<script type="text/javascript" src="js/create_quiz.js"></script>
</body>
</html>