diff --git a/sql/index.php b/sql/index.php
index 8526ce3..170ec13 100644
--- a/sql/index.php
+++ b/sql/index.php
@@ -1,5 +1,5 @@
";
i = 0;
testLength = result.questions.length;
@@ -26,25 +27,36 @@ document.addEventListener("DOMContentLoaded", function () {
`
`;
});
- contentHTML += ``;
- if (logged) contentHTML += `Zapisać wynik? `;
- if (logged && !own) contentHTML += `Zapisać w rankingu? `;
+ contentHTML += `
+ Wróc do strony głównej
`;
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);
});
diff --git a/test/test.php b/test/test.php
index 8425413..9eb67b9 100644
--- a/test/test.php
+++ b/test/test.php
@@ -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");
\ No newline at end of file