improved things

This commit is contained in:
Yarcio 2025-05-29 17:52:22 +02:00
parent 80435eac89
commit a37350b112
8 changed files with 30 additions and 26 deletions

View file

@ -15,8 +15,7 @@ document.addEventListener('DOMContentLoaded', function () {
const result = await response.json();
output.innerHTML = result.msg;
output.style.display = "block";
setTimeout(function () { if (result.redirect) window.location.replace(result.redirect); }, 300);
if (result.redirect) setTimeout(function () { window.location.replace(result.redirect); }, 300);
});
document.getElementById("guest").addEventListener("click", async function () {
const formData = new FormData();
@ -32,7 +31,7 @@ document.addEventListener('DOMContentLoaded', function () {
const result = await response.json();
output.innerHTML = result.msg;
output.style.display = "block";
setTimeout(function () { if (result.redirect) window.location.replace(result.redirect); }, 300);
if (result.redirect) setTimeout(function () { window.location.replace(result.redirect); }, 300);
});
passwordI = document.getElementById("password");
document.getElementById("showPassword").addEventListener("mousedown", function () { passwordI.type = "text"; });

View file

@ -22,7 +22,6 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["username"]) && isset($
$conn->close();
} elseif ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["guest"])) {
$_SESSION["UID"] = 0;
header('Content-Type: application/json');
echo json_encode(["redirect" => "/", "msg" => "Zalogowano jako gość"]);
} else
jsonMsg("Nieprawidłowy request");

View file

@ -19,7 +19,7 @@ document.addEventListener("DOMContentLoaded", function () {
});
const result = await response.json();
msg(result.msg);
setTimeout(function () { if (result.redirect) window.location.replace(result.redirect); }, 300);
if (result.redirect) setTimeout(function () { window.location.replace(result.redirect); }, 300);
}
} else msg("Hasła nie są identyczne");
});

View file

@ -23,7 +23,6 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["username"]) && isset($
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $password);
if ($stmt->execute()) {
header('Content-Type: application/json');
echo json_encode(["redirect" => "/login/", "msg" => "Utworzono użytkownika $username"]);
} else
jsonMsg("Nie udało się utworzyć użytkownika");

View file

@ -10,7 +10,6 @@ function connectDB() {
return $conn;
}
function jsonMsg($messsage) {
header('Content-Type: application/json');
echo json_encode(["msg" => $messsage]);
exit();
}

View file

@ -4,3 +4,5 @@ if (!isset($_SERVER["HTTP_X_REQUESTED_WITH"]) || strtolower($_SERVER["HTTP_X_REQ
http_response_code(403);
exit('Odmowa dostępu');
}
header('Content-Type: application/json');

View file

@ -1,4 +1,6 @@
document.addEventListener("DOMContentLoaded", function () {
const output = document.getElementById("info");
const mainContent = document.getElementById("content");
document.getElementById("startForm").addEventListener("submit", async function (formE) {
formE.preventDefault();
formData = new FormData(formE.target);
@ -11,6 +13,15 @@ document.addEventListener("DOMContentLoaded", function () {
}
});
const result = await response.json();
document.getElementById("content").innerHTML = result.msg;
output.innerHTML = result.msg;
output.style.display = "block";
if (result.questions) setTimeout(function () {
mainContent.innerHTML = "";
console.log(result.questions); // DEBUG
result.questions.forEach(question => {
mainContent.innerHTML += question.QID;
mainContent.innerHTML += ";";
});
}, 300);
});
});

View file

@ -5,31 +5,28 @@ if (!checkLogin()) {
exit();
}
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testLength"])) {
if ($_POST["testLength"]>100) jsonMsg("Test nie może być dłuższy niż 100 pytań");
if ($_POST["testLength"] > 100)
jsonMsg("Test nie może być dłuższy niż 100 pytań");
$conn = connectDB();
$stmt = $conn->prepare("SELECT count(*) FROM questions LIMIT 1");
$stmt = $conn->prepare("SELECT QID FROM questions");
$stmt->execute();
$stmt->bind_result($questionsCount);
$stmt->fetch();
$questionsIDs = [];
{$i = 0;
while ($i != $_POST["testLength"]) {
$randomNum = rand(0, $questionsCount);
if (!in_array($randomNum, $questionsIDs)) {
$questionsIDs[] = $randomNum;
$i++;
}
}}
$stmt->bind_result($QID);
$allQIDs = [];
while ($stmt->fetch())
$allQIDs[] = $QID;
$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);
$types = str_repeat("i", count($questionsIDs) * 2);
$arrMerged = array_merge($questionsIDs, $questionsIDs);
$stmt->bind_param($types, ...$arrMerged);
$stmt->execute();
$stmt->bind_result($QID, $content, $answerA, $answerB, $answerC, $answerD);
$result = [];
while ($stmt->fetch()) {
while ($stmt->fetch())
$result["questions"][] = [
"QID" => $QID,
"content" => $content,
@ -38,11 +35,9 @@ if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testLength"])) {
"answerC" => $answerC,
"answerD" => $answerD
];
}
$stmt->close();
$conn->close();
$result["msg"] = "Wygenerowano ". count($result["questions"]). " pytań";
header('Content-Type: application/json');
$result["msg"] = "Wygenerowano " . count($result["questions"]) . " pytań";
echo json_encode($result);
} else
jsonMsg("Nieprawidłowy request");