test finished
This commit is contained in:
parent
d7262080f8
commit
d699e85c2b
5 changed files with 87 additions and 23 deletions
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
include $_SERVER["DOCUMENT_ROOT"] . "/php/functions.php";
|
||||
// include $_SERVER["DOCUMENT_ROOT"] . "/php/functions.php";
|
||||
// $conn = connectDB();
|
||||
|
||||
// $file = fopen("insert3.sql", "r");
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
create table users (UID int PRIMARY KEY AUTO_INCREMENT, username TEXT UNIQUE NOT NULL, password TEXT NOT NULL);
|
||||
create table questions (QID int PRIMARY KEY AUTO_INCREMENT, content TEXT, answerA TEXT, answerB TEXT, answerC TEXT, answerD TEXT, answerChar CHAR(1));
|
||||
create table questions (QID int PRIMARY KEY AUTO_INCREMENT, content TEXT, answerA TEXT, answerB TEXT, answerC TEXT, answerD TEXT, answerChar CHAR(1));
|
||||
create table tests (TID int PRIMARY KEY AUTO_INCREMENT, rank boolean, correctAnswers int, testLength int, UID int, testTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (UID) REFERENCES users(UID));
|
||||
create table testAnswers (TAID int PRIMARY KEY AUTO_INCREMENT, QID int, position int, answeredChar CHAR(1), correct boolean, TID int, FOREIGN KEY (QID) REFERENCES questions(QID), FOREIGN KEY (TID) REFERENCES tests(TID));
|
||||
create table questionAnswers (QAID int PRIMARY KEY AUTO_INCREMENT, QID int, answerChar CHAR(1), correct boolean, UID int, questionTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (UID) REFERENCES users(UID), FOREIGN KEY (QID) REFERENCES questions(QID));
|
||||
|
|
|
@ -35,4 +35,4 @@ main form p#info {
|
|||
display: none;
|
||||
color: red;
|
||||
font-size: smaller;
|
||||
}
|
||||
}
|
||||
|
|
57
test/test.js
57
test/test.js
|
@ -17,7 +17,8 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||
output.innerHTML = result.msg;
|
||||
output.style.display = "block";
|
||||
if (result.questions) setTimeout(function () {
|
||||
window.addEventListener('beforeunload', function (e) { e.preventDefault(); });
|
||||
function beforeunloadFunc(e) { e.preventDefault(); }
|
||||
window.addEventListener('beforeunload', beforeunloadFunc);
|
||||
contentHTML = "<form id='test'>";
|
||||
i = 0;
|
||||
testLength = result.questions.length;
|
||||
|
@ -26,25 +27,36 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||
`<div class="question">
|
||||
<h4>Pytanie ${++i}/${testLength} <sub>w bazie nr ${question.QID}</sub></h4>
|
||||
<h3>${question.content}</h3>
|
||||
<p id="q${question.QID}"></p>
|
||||
<div class="answer">
|
||||
<input type="hidden" name="${"q"+question.QID}" value="${i}"/>
|
||||
<input type="hidden" name="${"q" + question.QID}" value="${i}"/>
|
||||
<input type="hidden" name="${question.QID}" value="-"/>
|
||||
<input type="radio" name="${question.QID}" value="A"/><b>A.</b> ${question.answerA}<br/>
|
||||
<input type="radio" name="${question.QID}" value="B"/><b>B.</b> ${question.answerB}<br/>
|
||||
<input type="radio" name="${question.QID}" value="C"/><b>C.</b> ${question.answerC}<br/>
|
||||
<input type="radio" name="${question.QID}" value="D"/><b>D.</b> ${question.answerD}<br/>
|
||||
<span id="A${question.QID}"><input type="radio" name="${question.QID}" value="A"/><b>A.</b> ${question.answerA}<br/></span>
|
||||
<span id="B${question.QID}"><input type="radio" name="${question.QID}" value="B"/><b>B.</b> ${question.answerB}<br/></span>
|
||||
<span id="C${question.QID}"><input type="radio" name="${question.QID}" value="C"/><b>C.</b> ${question.answerC}<br/></span>
|
||||
<span id="D${question.QID}"><input type="radio" name="${question.QID}" value="D"/><b>D.</b> ${question.answerD}<br/></span>
|
||||
</div>
|
||||
</div>`;
|
||||
});
|
||||
contentHTML += `<p>`;
|
||||
if (logged) contentHTML += `Zapisać wynik? <input type="checkbox" name="save"/><br/>`;
|
||||
if (logged && !own) contentHTML += `Zapisać w rankingu? <input type="checkbox" name="rank"/>`;
|
||||
contentHTML += `<div id="end"><p>`;
|
||||
if (logged) contentHTML += `Zapisać wynik? <input type="checkbox" name="save" id="save"/><br/>`;
|
||||
if (logged && !own) contentHTML += `<span style="visibility: hidden;" id="rank">Zapisać w rankingu? <input type="checkbox" name="rank"/></span>`;
|
||||
contentHTML += `</p>
|
||||
<input type="submit" value="Zakończ Test"/>
|
||||
<input type="submit" value="Zakończ Test"/></div>
|
||||
<p><a href="/">Wróc do strony głównej</a></p>
|
||||
</form>`;
|
||||
mainContent.innerHTML = contentHTML;
|
||||
document.getElementById("save").addEventListener("change", function (event) {
|
||||
rank = document.getElementById("rank");
|
||||
if (event.target.checked) {
|
||||
rank.style.visibility = "visible";
|
||||
} else {
|
||||
rank.style.visibility = "hidden";
|
||||
}
|
||||
});
|
||||
document.getElementById("test").addEventListener("submit", async function (formE) {
|
||||
formE.preventDefault();
|
||||
window.removeEventListener('beforeunload', beforeunloadFunc);
|
||||
formData = new FormData(formE.target);
|
||||
formData.append("testEnd", "true");
|
||||
const response = await fetch("test.php", {
|
||||
|
@ -56,7 +68,30 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||
}
|
||||
});
|
||||
const result = await response.json();
|
||||
console.log(result);
|
||||
if (result.status == "OK") {
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||||
console.log(result.answers); // DEBUG
|
||||
Object.entries(result.answers).forEach(function ([key, arr]) {
|
||||
console.log(key, arr);
|
||||
if (!arr.correct && arr.answeredChar != "-") {
|
||||
document.getElementById(`q${key}`).innerHTML = `Nieprawidłowa odpowiedź, odpowiedziałeś ${arr.answeredChar}, prawidłowa odpowiedź to ${arr.correctChar}`;
|
||||
document.getElementById(`q${key}`).style.color = "red";
|
||||
document.getElementById(`${arr.answeredChar}${key}`).style.color = "red";
|
||||
document.getElementById(`${arr.correctChar}${key}`).style.color = "green";
|
||||
}
|
||||
else if (arr.answeredChar == "-") {
|
||||
document.getElementById(`q${key}`).innerHTML = `Nie odpowiedziałeś na pytanie, prawidłowa odpowiedź to ${arr.correctChar}`;
|
||||
document.getElementById(`q${key}`).style.color = "red";
|
||||
document.getElementById(`${arr.correctChar}${key}`).style.color = "green";
|
||||
} else {
|
||||
document.getElementById(`q${key}`).innerHTML = `Odpowiedziałeś prawidłowo, prawidłowa odpowiedż to ${arr.correctChar}`;
|
||||
document.getElementById(`q${key}`).style.color = "green";
|
||||
document.getElementById(`${arr.answeredChar}${key}`).style.color = "green";
|
||||
}
|
||||
|
||||
});
|
||||
document.getElementById("end").style.display = "none";
|
||||
} else alert("Wystąpił błąd, spróbuj ponownie");
|
||||
});
|
||||
}, 300);
|
||||
});
|
||||
|
|
|
@ -5,8 +5,8 @@ if (!checkLogin()) {
|
|||
exit();
|
||||
}
|
||||
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testStart"])) {
|
||||
if ($_POST["testLength"] > 100)
|
||||
jsonMsg("Test nie może być dłuższy niż 100 pytań");
|
||||
if ($_POST["testLength"] > 100 || $_POST["testLength"] < 2)
|
||||
jsonMsg("Test nie może być dłuższy niż 100 pytań ani krótszy niż 2");
|
||||
$conn = connectDB();
|
||||
$stmt = $conn->prepare("SELECT QID FROM questions");
|
||||
$stmt->execute();
|
||||
|
@ -17,7 +17,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testStart"])) {
|
|||
$stmt->close();
|
||||
shuffle($allQIDs);
|
||||
$questionsIDs = array_slice($allQIDs, 0, $_POST["testLength"]);
|
||||
|
||||
|
||||
$filler = implode(",", array_fill(0, count($questionsIDs), "?"));
|
||||
$stmt = $conn->prepare("SELECT QID, content, answerA, answerB, answerC, answerD FROM questions WHERE QID IN ($filler) ORDER BY FIELD(QID, $filler)");
|
||||
$types = str_repeat("i", count($questionsIDs) * 2);
|
||||
|
@ -43,9 +43,10 @@ if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testStart"])) {
|
|||
$allTestAnswers = [];
|
||||
$answersQIDs = [];
|
||||
foreach ($_POST as $key => $value) {
|
||||
if (in_array($key, ["save", "rank", "testEnd", "testLenght"]) || !is_numeric($key)) continue;
|
||||
$allTestAnswers[(int)$key] = ["ans" => $value, "pos" => $_POST["q$key"], "cor" => false];
|
||||
$answersQIDs[] = (int)$key;
|
||||
if (in_array($key, ["save", "rank", "testEnd", "testLenght"]) || !is_numeric($key))
|
||||
continue;
|
||||
$allTestAnswers[(int) $key] = ["answeredChar" => $value, "position" => (int)$_POST["q$key"], "correct" => false];
|
||||
$answersQIDs[] = (int) $key;
|
||||
}
|
||||
$conn = connectDB();
|
||||
$filler = implode(",", array_fill(0, count($allTestAnswers), "?"));
|
||||
|
@ -53,10 +54,35 @@ if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testStart"])) {
|
|||
$types = str_repeat("i", count($allTestAnswers));
|
||||
$stmt->bind_param($types, ...$answersQIDs);
|
||||
$stmt->execute();
|
||||
$stmt->bind_result($QID, $answerChar);
|
||||
while ($stmt->fetch()) if ($answerChar==$allTestAnswers[$QID]["ans"]) $allTestAnswers[$QID]["cor"] = true;
|
||||
$stmt->bind_result($QID, $correctChar);
|
||||
$correctAnswers = 0;
|
||||
while ($stmt->fetch()) {
|
||||
$allTestAnswers[$QID]["correctChar"] = $correctChar;
|
||||
if ($correctChar == $allTestAnswers[$QID]["answeredChar"]) {
|
||||
$allTestAnswers[$QID]["correct"] = true;
|
||||
$correctAnswers += 1;
|
||||
}
|
||||
}
|
||||
$stmt->close();
|
||||
$result = ["answers" => $allTestAnswers, "correctAnswers" => $correctAnswers, "testLength" => count($allTestAnswers)];
|
||||
if (isset($_POST["save"]) && isset($_SESSION["username"])) {
|
||||
$testLength = count($allTestAnswers);
|
||||
$rank = isset($_POST["rank"]) && $testLength == 20;
|
||||
$stmt = $conn->prepare("INSERT INTO tests (rank, correctAnswers, testLength, UID) VALUES (?, ?, ?, ?)");
|
||||
$stmt->bind_param("iiii", $rank, $correctAnswers, $testLength, $_SESSION["UID"]);
|
||||
$stmt->execute();
|
||||
$TID = $stmt->insert_id;
|
||||
$stmt->close();
|
||||
$stmt = $conn->prepare("INSERT INTO testAnswers (QID, position, answeredChar, correct, TID) VALUES (?, ?, ?, ?, ?)");
|
||||
foreach ($allTestAnswers as $QID => $arr) {
|
||||
$stmt->bind_param("iisii", $QID, $arr["position"], $arr["answeredChar"], $arr['correct'], $TID);
|
||||
$stmt->execute();
|
||||
}
|
||||
$stmt->close();
|
||||
}
|
||||
$conn->close();
|
||||
echo json_encode($allTestAnswers);
|
||||
$result["status"] = "OK";
|
||||
$result["msg"] = "Ukończono pomyślnie";
|
||||
echo json_encode($result);
|
||||
} else
|
||||
jsonMsg("Nieprawidłowy request");
|
Loading…
Add table
Reference in a new issue