Show sourcecode
The following files exists in this folder. Click to view.
create_test.php
create_test_fuckingtrasig.php
hash.php
index.php
login.php
main.php
personal_results.php
setup.php
sign_up.php
test_correcting.php
tests.php
user_administration.php
verification.php
test_correcting.php
194 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
<?php
session_start(["gc_maxlifetime" => 86400]);
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rättning</title>
<style>
.correct {
background-color: lightgreen;
}
.wrong {
background-color: lightcoral;
}
.question_cont {
border: 2px solid black;
margin: 1px;
}
p {
margin: 0;
}
/* Script för att printa stapeldiagram */
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
</head>
<body>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (!isset($_POST["test_id"])) {
header("Location: main.php"); // Skickar tillbaka en om det är konstigt.
}
try {
/** @var PDO $dbconn */
include("../databaser/dbconnection.php");
include("verification.php");
$test_id = $_POST["test_id"];
$total_questions = 0; // Incrementeras
$correct_questions = 0; // Incrementeras
$answers_arr = []; // Läggs in allteftersom
$sql = "SELECT name FROM quiz_tests WHERE id=?";
$stmt = $dbconn->prepare($sql);
$data = [$test_id];
$stmt->execute($data);
$quiz_name = $stmt->fetch(PDO::FETCH_ASSOC)["name"];
echo ("<h1>$quiz_name</h1><p>Rätt svar markerat i grönt. Om ditt svar ej var korrekt markeras ditt svar med rött och korrekt med grönt</p><br>");
$sql = "SELECT * FROM quiz_questions WHERE test_id=? ORDER BY id";
$stmt = $dbconn->prepare($sql);
$data = [$test_id]; // Samma som den var men för tydlighet
$stmt->execute($data);
while ($question = $stmt->fetch(PDO::FETCH_ASSOC)) {
$total_questions += 1;
$sql = "SELECT * FROM quiz_answers WHERE question_id =?";
$answer_stmt = $dbconn->prepare($sql); // Nytt stmt för att inte sabba loopen
$data = [$question["id"]];
$answer_stmt->execute($data);
$input_name = $total_questions; // Total questions är ännu ej totala antalet frågor utan hur många som hanterats hittils.
// Då även händelsevis namnet på inputsen
$user_answer = $_POST[$input_name];
$answers_arr[] = $user_answer;
if ($user_answer == $question["correct"]) {
$correct_questions += 1;
}
echo ('<div class="question_cont"><h2>' . $question["text"] . '</h2>'); // Frågan och en container.
while ($answer = $answer_stmt->fetch(PDO::FETCH_ASSOC)) {
$is_correct = ($answer["answer_num"] == $question["correct"]);
$is_user_answer = ($answer["answer_num"] == $user_answer);
if ($is_correct) {
echo ('<p class="correct">');
} else if ($is_user_answer) {
echo ('<p class="wrong">'); // Användarens svar om det inte är rätt
} else {
echo ("<p>"); // Varken användarsvar eller rätt
}
// Slutet av p-elementet och
echo ($answer["text"] . '</p>');
}
echo ("</div>");
}
echo ('<b>Ditt resultat: ' . $correct_questions . '/' . $total_questions . ' poäng</b><br>');
// Lägger in ens resultat och svar i databasen
$sql = "INSERT INTO quiz_results (test_id, user_id, result, answers) VALUES(?,?,?,?)";
$stmt = $dbconn->prepare($sql);
$answers_str = implode(",", $answers_arr); // Gör om till rätt format
$data = [$test_id, $_SESSION["user_id"], $correct_questions, $answers_str];
$stmt->execute($data);
echo ("<hr><hr>"); // Här under kommer alla andra resultat att stå.
echo ('<h2>Jämför resultat</h2>');
$global_results_arr = []; // Antal rätt som index och hur många som fick så många som värde
$total_points = 0;
$total_test_takers = 0;
$sql = "SELECT COUNT(*) FROM quiz_results WHERE test_id = ? AND result = ?";
$stmt = $dbconn->prepare($sql);
for ($i = 0; $i <= $total_questions; $i++) {
$data = [$test_id, $i];
$stmt->execute($data);
$count = $stmt->fetchColumn(); // Lägger till i arrayen
$global_results_arr[] = $count;
$total_points += $count * $i;
$total_test_takers += $count;
}
$avr_result = round($total_points / $total_test_takers, 2);
} catch (PDOException $e) {
echo ($e->getMessage());
}
?>
<!-- För stapeldiagrammet -->
<canvas id="myChart" style="width:100%;max-width:900px;position:relative"></canvas>
<br><br>
<script>
var xValues = [<?php
for ($i = 0; $i <= $total_questions; $i++) {
echo ('"' . $i . ' p"');
if ($i != $total_questions) {
echo (', ');
}
}
?>];
var yValues = [<?php
for ($i = 0; $i <= $total_questions; $i++) {
echo ($global_results_arr[$i]);
if ($i != $total_questions) {
echo (', ');
}
}
?>];;
var barColors = [<?php
for ($i = 0; $i <= $total_questions; $i++) {
echo ('"green"');
if ($i != $total_questions) {
echo (', ');
}
}
?>];;
new Chart("myChart", {
type: "bar",
data: {
labels: xValues,
datasets: [{
backgroundColor: barColors,
data: yValues
}]
},
options: {
legend: {
display: false
},
title: {
display: true,
text: <?php echo ('"Andras resultat (och ditt) med antal personer som har det det resultatet på y-axeln. Snittpoäng: ' . $avr_result . '"'); ?>
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
stepSize: 1 // viktigt om du vill se små staplar
}
}]
}
}
});
</script>
</body>
</html>