improved things
This commit is contained in:
parent
80435eac89
commit
a37350b112
8 changed files with 30 additions and 26 deletions
|
@ -15,8 +15,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
output.innerHTML = result.msg;
|
output.innerHTML = result.msg;
|
||||||
output.style.display = "block";
|
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 () {
|
document.getElementById("guest").addEventListener("click", async function () {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
@ -32,7 +31,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
output.innerHTML = result.msg;
|
output.innerHTML = result.msg;
|
||||||
output.style.display = "block";
|
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");
|
passwordI = document.getElementById("password");
|
||||||
document.getElementById("showPassword").addEventListener("mousedown", function () { passwordI.type = "text"; });
|
document.getElementById("showPassword").addEventListener("mousedown", function () { passwordI.type = "text"; });
|
||||||
|
|
|
@ -22,7 +22,6 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["username"]) && isset($
|
||||||
$conn->close();
|
$conn->close();
|
||||||
} elseif ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["guest"])) {
|
} elseif ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["guest"])) {
|
||||||
$_SESSION["UID"] = 0;
|
$_SESSION["UID"] = 0;
|
||||||
header('Content-Type: application/json');
|
|
||||||
echo json_encode(["redirect" => "/", "msg" => "Zalogowano jako gość"]);
|
echo json_encode(["redirect" => "/", "msg" => "Zalogowano jako gość"]);
|
||||||
} else
|
} else
|
||||||
jsonMsg("Nieprawidłowy request");
|
jsonMsg("Nieprawidłowy request");
|
||||||
|
|
|
@ -19,7 +19,7 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||||
});
|
});
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
msg(result.msg);
|
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");
|
} else msg("Hasła nie są identyczne");
|
||||||
});
|
});
|
||||||
|
|
|
@ -23,7 +23,6 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["username"]) && isset($
|
||||||
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
||||||
$stmt->bind_param("ss", $username, $password);
|
$stmt->bind_param("ss", $username, $password);
|
||||||
if ($stmt->execute()) {
|
if ($stmt->execute()) {
|
||||||
header('Content-Type: application/json');
|
|
||||||
echo json_encode(["redirect" => "/login/", "msg" => "Utworzono użytkownika $username"]);
|
echo json_encode(["redirect" => "/login/", "msg" => "Utworzono użytkownika $username"]);
|
||||||
} else
|
} else
|
||||||
jsonMsg("Nie udało się utworzyć użytkownika");
|
jsonMsg("Nie udało się utworzyć użytkownika");
|
||||||
|
|
|
@ -10,7 +10,6 @@ function connectDB() {
|
||||||
return $conn;
|
return $conn;
|
||||||
}
|
}
|
||||||
function jsonMsg($messsage) {
|
function jsonMsg($messsage) {
|
||||||
header('Content-Type: application/json');
|
|
||||||
echo json_encode(["msg" => $messsage]);
|
echo json_encode(["msg" => $messsage]);
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,3 +4,5 @@ if (!isset($_SERVER["HTTP_X_REQUESTED_WITH"]) || strtolower($_SERVER["HTTP_X_REQ
|
||||||
http_response_code(403);
|
http_response_code(403);
|
||||||
exit('Odmowa dostępu');
|
exit('Odmowa dostępu');
|
||||||
}
|
}
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
|
13
test/test.js
13
test/test.js
|
@ -1,4 +1,6 @@
|
||||||
document.addEventListener("DOMContentLoaded", function () {
|
document.addEventListener("DOMContentLoaded", function () {
|
||||||
|
const output = document.getElementById("info");
|
||||||
|
const mainContent = document.getElementById("content");
|
||||||
document.getElementById("startForm").addEventListener("submit", async function (formE) {
|
document.getElementById("startForm").addEventListener("submit", async function (formE) {
|
||||||
formE.preventDefault();
|
formE.preventDefault();
|
||||||
formData = new FormData(formE.target);
|
formData = new FormData(formE.target);
|
||||||
|
@ -11,6 +13,15 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const result = await response.json();
|
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);
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -5,31 +5,28 @@ if (!checkLogin()) {
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testLength"])) {
|
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();
|
$conn = connectDB();
|
||||||
$stmt = $conn->prepare("SELECT count(*) FROM questions LIMIT 1");
|
$stmt = $conn->prepare("SELECT QID FROM questions");
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$stmt->bind_result($questionsCount);
|
$stmt->bind_result($QID);
|
||||||
$stmt->fetch();
|
$allQIDs = [];
|
||||||
$questionsIDs = [];
|
while ($stmt->fetch())
|
||||||
{$i = 0;
|
$allQIDs[] = $QID;
|
||||||
while ($i != $_POST["testLength"]) {
|
|
||||||
$randomNum = rand(0, $questionsCount);
|
|
||||||
if (!in_array($randomNum, $questionsIDs)) {
|
|
||||||
$questionsIDs[] = $randomNum;
|
|
||||||
$i++;
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
|
shuffle($allQIDs);
|
||||||
|
$questionsIDs = array_slice($allQIDs, 0, $_POST["testLength"]);
|
||||||
|
|
||||||
$filler = implode(",", array_fill(0, count($questionsIDs), "?"));
|
$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)");
|
$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);
|
$arrMerged = array_merge($questionsIDs, $questionsIDs);
|
||||||
$stmt->bind_param($types, ...$arrMerged);
|
$stmt->bind_param($types, ...$arrMerged);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$stmt->bind_result($QID, $content, $answerA, $answerB, $answerC, $answerD);
|
$stmt->bind_result($QID, $content, $answerA, $answerB, $answerC, $answerD);
|
||||||
$result = [];
|
$result = [];
|
||||||
while ($stmt->fetch()) {
|
while ($stmt->fetch())
|
||||||
$result["questions"][] = [
|
$result["questions"][] = [
|
||||||
"QID" => $QID,
|
"QID" => $QID,
|
||||||
"content" => $content,
|
"content" => $content,
|
||||||
|
@ -38,11 +35,9 @@ if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testLength"])) {
|
||||||
"answerC" => $answerC,
|
"answerC" => $answerC,
|
||||||
"answerD" => $answerD
|
"answerD" => $answerD
|
||||||
];
|
];
|
||||||
}
|
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
$conn->close();
|
$conn->close();
|
||||||
$result["msg"] = "Wygenerowano ". count($result["questions"]). " pytań";
|
$result["msg"] = "Wygenerowano " . count($result["questions"]) . " pytań";
|
||||||
header('Content-Type: application/json');
|
|
||||||
echo json_encode($result);
|
echo json_encode($result);
|
||||||
} else
|
} else
|
||||||
jsonMsg("Nieprawidłowy request");
|
jsonMsg("Nieprawidłowy request");
|
Loading…
Add table
Reference in a new issue