quiz-klapek/test/test.php
2025-05-31 22:08:59 +02:00

88 lines
No EOL
3.7 KiB
PHP

<?php
include $_SERVER["DOCUMENT_ROOT"] . "/php/scripts.php";
if (!checkLogin()) {
jsonMsg("Nie jesteś zalogowany");
exit();
}
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testStart"])) {
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();
$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);
$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())
$result["questions"][] = [
"QID" => $QID,
"content" => $content,
"answerA" => $answerA,
"answerB" => $answerB,
"answerC" => $answerC,
"answerD" => $answerD
];
$stmt->close();
$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] = ["answeredChar" => $value, "position" => (int)$_POST["q$key"], "correct" => 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, $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();
$result["status"] = "OK";
$result["msg"] = "Ukończono pomyślnie";
echo json_encode($result);
} else
jsonMsg("Nieprawidłowy request");