diff --git a/test/index.php b/test/index.php index 390e1a7..2b77d11 100644 --- a/test/index.php +++ b/test/index.php @@ -17,7 +17,7 @@ include $_SERVER["DOCUMENT_ROOT"] . "/php/pages.php";

Rozpocznij test - quiz PHP

- +

Rozpocznij test

Pojedyńcze pytanie - quiz PHP
+

Rozwiąż pytanie

Wyświetlania pytań:

- Losowo
- Wg nr pytania w bazie
- + Losowo
+ Wg nr pytania w bazie
+

"; + + contentHTML += + `
+

Pytanie w bazie nr ${result.question.QID}

+

${result.question.content}

+

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

`; + if (logged) contentHTML += `Zapisać odpowiedź na pytanie?
`; + contentHTML += `

+
+ +
+

Wróc do strony głównej

`; + mainContent.innerHTML = contentHTML; + document.getElementById("questionForm").addEventListener("submit", async function (formE) { + formE.preventDefault(); + window.removeEventListener('beforeunload', beforeunloadFunc); + let formData = new FormData(formE.target); + formData.append("checkQuestion", "true"); + const response = await fetch("single.php", { + method: "POST", + body: formData, + credentials: "include", + headers: { + 'X-Requested-With': 'XMLHttpRequest' + } + }); + const result = await response.json(); + if (result.status == "OK") { + window.scrollTo({ top: 0, behavior: 'smooth' }); + const qInfo = document.getElementById(`questionInfo`); + let correctLetter = ["A", "B", "C", "D"].includes(result.answeredChar); + if (!result.correct && correctLetter) { + qInfo.innerHTML = `Nieprawidłowa odpowiedź, odpowiedziałeś ${result.answeredChar}, prawidłowa odpowiedź to ${result.correctChar}`; + qInfo.style.color = "red"; + document.getElementById(`${result.answeredChar}`).style.color = "red"; + document.getElementById(`${result.correctChar}`).style.color = "green"; + } + else if (!correctLetter) { + qInfo.innerHTML = `Nie odpowiedziałeś na pytanie, prawidłowa odpowiedź to ${result.correctChar}`; + qInfo.style.color = "red"; + document.getElementById(`${result.correctChar}`).style.color = "green"; + } else { + qInfo.innerHTML = `Odpowiedziałeś prawidłowo, prawidłowa odpowiedż to ${result.correctChar}`; + qInfo.style.color = "green"; + document.getElementById(`${result.answeredChar}`).style.color = "green"; + } + document.getElementById("end").style.display = "none"; + document.getElementById("new").innerHTML = ` +
+ Nowe pytanie: +

+ Losowo
+ Wg nr pytania w bazie
+ +

+

+

+
`; + randomRadioFunc(); + document.getElementById("startForm").addEventListener("submit", generateQuestion); + } else alert("Wystąpił błąd, spróbuj ponownie"); + }); + }, 300); } - const output = document.getElementById("info"); const mainContent = document.getElementById("content"); - const randomRadio = document.querySelectorAll("input[name='random']"); - randomRadio.forEach(function (inputRadio) { - inputRadio.addEventListener("change", function (QIDnum) { - if (document.getElementById("byQID").checked) { - QIDnum.style.visibility = "visible"; - } else { - QIDnum.style.visibility = "hidden"; - } + function randomRadioFunc() { + const QIDnum = document.getElementById("QIDnum"); + const randomRadio = document.querySelectorAll("input[name='random']"); + randomRadio.forEach(function (inputRadio) { + inputRadio.addEventListener("change", function () { + if (document.getElementById("byQID").checked) { + QIDnum.style.visibility = "visible"; + } else QIDnum.style.visibility = "hidden"; + }); }); - }); + if (!lastRandomQuestion) QIDnum.style.visibility = "visible"; + } + randomRadioFunc(); document.getElementById("startForm").addEventListener("submit", generateQuestion); }); \ No newline at end of file diff --git a/test/single/single.php b/test/single/single.php index f0e48fd..c7dad20 100644 --- a/test/single/single.php +++ b/test/single/single.php @@ -5,8 +5,61 @@ if (!checkLogin()) { exit(); } if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["generateQuestion"])) { - + $conn = connectDB(); + if (!isset($_POST["random"])) echo jsonMsg("Błąd, nie wybrano typu wyboru pytania"); + if ($_POST["random"] == "true") { + $stmt = $conn->prepare("SELECT QID FROM questions ORDER BY rand() LIMIT 1"); + $stmt->execute(); + $stmt->bind_result($selectedQID); + $stmt->fetch(); + $stmt->close(); + } else { + $selectedQID = (int) $_POST["QID"]; + $stmt = $conn->prepare("SELECT QID FROM questions WHERE QID = ?"); + $stmt->bind_param("i", $selectedQID); + $stmt->execute(); + $stmt->store_result(); + if ($stmt->num_rows != 1) + jsonMsg("Nie ma pytania w bazie o nr $selectedQID"); + $stmt->close(); + } + $stmt = $conn->prepare("SELECT QID, content, answerA, answerB, answerC, answerD FROM questions WHERE QID = ?"); + $stmt->bind_param("i", $selectedQID); + $stmt->execute(); + $stmt->bind_result($QID, $content, $answerA, $answerB, $answerC, $answerD); + $stmt->fetch(); + $result["question"] = [ + "QID" => $QID, + "content" => $content, + "answerA" => $answerA, + "answerB" => $answerB, + "answerC" => $answerC, + "answerD" => $answerD + ]; + $stmt->close(); + $conn->close(); + $result["msg"] = "Wygenerowano pytanie o nr $selectedQID"; + echo json_encode($result); } else if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["checkQuestion"])) { - + $conn = connectDB(); + $stmt = $conn->prepare("SELECT QID, answerChar FROM questions WHERE QID = ?"); + $stmt->bind_param("i", $_POST["QID"]); + $stmt->execute(); + $stmt->bind_result($QID, $correctChar); + $stmt->fetch(); + $stmt->close(); + $answerChar = $_POST["answer"]; + $correct = $_POST["answer"]==$correctChar; + $result = ["QID" => $QID, "answeredChar" => $answerChar, "correct" => $correct, "correctChar" => $correctChar]; + if (isset($_POST["save"]) && isset($_SESSION["username"])) { + $stmt = $conn->prepare("INSERT INTO questionAnswers (QID, answerChar, correct, UID) VALUES (?, ?, ?, ?)"); + $stmt->bind_param("isii", $QID, $answerChar, $correct, $_SESSION["UID"]); + $stmt->execute(); + $stmt->close(); + } + $conn->close(); + $result["status"] = "OK"; + $result["msg"] = "Ukończono pomyślnie"; + echo json_encode($result); } else jsonMsg("Nieprawidłowy request"); \ No newline at end of file