From d7262080f84478769a32dbc730c966639c164655 Mon Sep 17 00:00:00 2001 From: Yarcio Date: Sat, 31 May 2025 17:39:35 +0200 Subject: [PATCH] style update + server checking answers --- js/functions.js | 8 +++++++ login/index.php | 1 - login/register/index.php | 1 - single/index.php | 1 - sql/index.php | 24 +++++++++++++++++++++ styles/main.css | 1 + styles/quiz.css | 11 +++++++++- template.html | 1 - test/index.php | 10 ++++----- test/test.js | 45 +++++++++++++++++++++++++++++----------- test/test.php | 21 ++++++++++++++++++- 11 files changed, 101 insertions(+), 23 deletions(-) create mode 100644 js/functions.js create mode 100644 sql/index.php diff --git a/js/functions.js b/js/functions.js new file mode 100644 index 0000000..4578583 --- /dev/null +++ b/js/functions.js @@ -0,0 +1,8 @@ +//msg +export function msg(message) { + const output = document.getElementById("info"); + output.innerHTML = result.msg; + output.style.display = "block"; +} + +//password show diff --git a/login/index.php b/login/index.php index e31a547..37762ef 100644 --- a/login/index.php +++ b/login/index.php @@ -31,7 +31,6 @@ session_destroy(); Zarejestruj się -

\ No newline at end of file diff --git a/login/register/index.php b/login/register/index.php index 0aaee0b..23092bd 100644 --- a/login/register/index.php +++ b/login/register/index.php @@ -31,7 +31,6 @@ session_destroy();

Powróć do strony logowania

-

\ No newline at end of file diff --git a/single/index.php b/single/index.php index c5bb561..48d3931 100644 --- a/single/index.php +++ b/single/index.php @@ -19,7 +19,6 @@ include $_SERVER["DOCUMENT_ROOT"] . "/php/pages.php"; -

\ No newline at end of file diff --git a/sql/index.php b/sql/index.php new file mode 100644 index 0000000..8526ce3 --- /dev/null +++ b/sql/index.php @@ -0,0 +1,24 @@ +multi_query($sql)) { +// do { +// if ($result = $conn->store_result()) { +// $result->free(); +// } +// } while ($conn->more_results() && $conn->next_result()); +// } else { +// echo "SQL Error: " . $conn->error; +// } + +// $conn->close(); +exit("DISABLED!!!"); diff --git a/styles/main.css b/styles/main.css index da14f92..5df116b 100644 --- a/styles/main.css +++ b/styles/main.css @@ -14,6 +14,7 @@ body { display: flex; flex-direction: column; min-height: 100vh; + padding-bottom: 60px; } main { diff --git a/styles/quiz.css b/styles/quiz.css index 61226e5..7e89385 100644 --- a/styles/quiz.css +++ b/styles/quiz.css @@ -2,4 +2,13 @@ main form p span.bigger { font-size: larger; -} \ No newline at end of file +} +main div.question { + margin: 50px auto; + border: 1px solid var(--border-basic); + width: 90%; +} +main div.question div.answer { + text-align: left; + padding: 0 20px 20px 20px; +} diff --git a/template.html b/template.html index aadddbe..4a2f9e3 100644 --- a/template.html +++ b/template.html @@ -16,7 +16,6 @@ -

\ No newline at end of file diff --git a/test/index.php b/test/index.php index 2be6fc3..c9e8a22 100644 --- a/test/index.php +++ b/test/index.php @@ -21,23 +21,23 @@ include $_SERVER["DOCUMENT_ROOT"] . "/php/pages.php";
Długość testu:
pytań

"; + echo "

Długość testu:
pytań

"; else - echo ""; + echo ""; ?>

Nie jesteś zalogowany,
więc żaden postęp nie zostanie zapisany!

"; + echo "

Nie jesteś zalogowany,
więc żaden postęp nie zostanie zapisany!

"; else - echo "

" + echo "

" ?>

Jednak nie chcesz zacząć?

Powrót do strony głównej

-

+ \ No newline at end of file diff --git a/test/test.js b/test/test.js index 463e5cc..73f81bc 100644 --- a/test/test.js +++ b/test/test.js @@ -4,6 +4,7 @@ document.addEventListener("DOMContentLoaded", function () { document.getElementById("startForm").addEventListener("submit", async function (formE) { formE.preventDefault(); formData = new FormData(formE.target); + formData.append("testStart", "true") const response = await fetch("test.php", { method: "POST", body: formData, @@ -16,27 +17,47 @@ document.addEventListener("DOMContentLoaded", function () { output.innerHTML = result.msg; output.style.display = "block"; if (result.questions) setTimeout(function () { + window.addEventListener('beforeunload', function (e) { e.preventDefault(); }); contentHTML = "
"; - console.log(result.questions); // DEBUG i = 0; - testLenght = result.questions.length; + testLength = result.questions.length; result.questions.forEach(question => { - contentHTML += - `
-

Pytanie ${++i}/${testLenght} w bazie nr ${question.QID}

+ contentHTML += + `
+

Pytanie ${++i}/${testLength} w bazie nr ${question.QID}

${question.content}

- ${question.answerA}
- ${question.answerB}
- ${question.answerC}
- ${question.answerD}
+ + + A. ${question.answerA}
+ B. ${question.answerB}
+ C. ${question.answerC}
+ D. ${question.answerD}
`; }); - contentHTML += ` -

Zapisać wynik?

+ contentHTML += `

`; + if (logged) contentHTML += `Zapisać wynik?
`; + if (logged && !own) contentHTML += `Zapisać w rankingu? `; + contentHTML += `

+ `; - mainContent.innerHTML = contentHTML + mainContent.innerHTML = contentHTML; + document.getElementById("test").addEventListener("submit", async function (formE) { + formE.preventDefault(); + formData = new FormData(formE.target); + formData.append("testEnd", "true"); + const response = await fetch("test.php", { + method: "POST", + body: formData, + credentials: "include", + headers: { + 'X-Requested-With': 'XMLHttpRequest' + } + }); + const result = await response.json(); + console.log(result); + }); }, 300); }); }); \ No newline at end of file diff --git a/test/test.php b/test/test.php index ca7d177..8425413 100644 --- a/test/test.php +++ b/test/test.php @@ -4,7 +4,7 @@ if (!checkLogin()) { jsonMsg("Nie jesteś zalogowany"); exit(); } -if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testLength"])) { +if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testStart"])) { if ($_POST["testLength"] > 100) jsonMsg("Test nie może być dłuższy niż 100 pytań"); $conn = connectDB(); @@ -39,5 +39,24 @@ if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testLength"])) { $conn->close(); $result["msg"] = "Wygenerowano " . count($result["questions"]) . " pytań"; echo json_encode($result); +} else if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testEnd"])) { + $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; + } + $conn = connectDB(); + $filler = implode(",", array_fill(0, count($allTestAnswers), "?")); + $stmt = $conn->prepare("SELECT QID, answerChar FROM questions WHERE QID IN ($filler)"); + $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->close(); + $conn->close(); + echo json_encode($allTestAnswers); } else jsonMsg("Nieprawidłowy request"); \ No newline at end of file