MINT-Projekt/wahl.php

188 lines
No EOL
6.4 KiB
PHP

<?php
/*
* Copyright (C) 2021 Eichehome
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
error_reporting(E_ALL); //Debuging
require_once 'include/DBConnection.inc.php';
function autoloader($classname)
{
include 'class/' . $classname . '.php';
}
spl_autoload_register('autoloader');
$session = new Session();
$db = new Datenbank($dbname, $username, $password, $servername);
/*===============================*/
function convertName(string $name) {
$suchmuster = array('/([A-Za-z]+)\.([A-Za-z]+)/');
$ersetzungen = array('<span style="text-transform: capitalize;">\1 \2</span>');
$convertedName = preg_replace($suchmuster, $ersetzungen, $name);
return $convertedName;
}
function convertUmlauteFrom(string $text) {
$suchmuster = array();
$suchmuster[0] = '/ae/';
$suchmuster[1] = '/ue/';
$suchmuster[2] = '/oe/';
$ersetzungen = array();
$ersetzungen[2] = 'ä';
$ersetzungen[1] = 'ü';
$ersetzungen[0] = 'ö';
$replacedtext = preg_replace($suchmuster, $ersetzungen, $text);
return $replacedtext;
}
/*===============================*/
if (isset($_SESSION["name"])) {
$nutzername = $_SESSION["name"];
}
$check = filter_input(INPUT_POST, "check", FILTER_SANITIZE_STRING);
if (isset($_SESSION["login"]) && $session->get("login") == 1) {
if (isset($_SESSION["status"]) && $session->get("status") == "ok") {
$message = $session->get("message");
}
} else {
$session->set("status", "error");
$session->set("message", "Nicht eingeloggt");
$url = "index.php";
header("Location: $url");
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
$wahl = array();
for ($i = 0; $i < (count($_POST) - 1); $i++) { //-1 Wegen hidden-Feld
$wahl[$i] = filter_input(INPUT_POST, "wahl" . $i);
}
if ($check) {
if (count($wahl) == 3) {
if ($db->hasAlredySelected($nutzername)) {
$message = "";
$session->set("status", "error");
$session->set("message", "Schon eingetragen!");
$url = "index.php";
header("Location: $url");
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
} else {
if ($db->insertSelection($nutzername, $wahl)){
$session->set("status", "ok");
$session->set("message", "Du bist jetzt eingetragen!<br>Du wirst in 3 Sekunden wietergeleitet.");
$url = "check.php";
header("Location: $url");
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
} else {
$session->set("status", "error");
$session->set("message", "Beim Eintragen ist etwas schief gelaufen, versuche es später nochmal oder wende dich an den Admin.");
$url = "index.php";
header("Location: $url");
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}
} else {
$message = "";
$errorMessage = "Fehlerhafte Auswahl!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sportwahlen | Wahl</title>
<link rel="stylesheet" href="assets/css/main.css">
</head>
<body>
<?php if (isset($errorMessage) && $errorMessage != ""): ?>
<div class="modal">
<div class="message error">
<!-- Ausgabe einer eventuellen Fehlermeldung -->
<p><?php echo $errorMessage; ?></p>
</div>
</div>
<?php endif;?>
<?php if (isset($message) && $message != ""): ?>
<div class="modal">
<div class="message sucsses">
<!-- Ausgabe einer eventuellen Statusmeldung -->
<p><?php echo $message; ?></p>
</div>
</div>
<?php endif;?>
<?php
$kurse = $db->selectKurse();
echo '<form action="wahl.php" method="post">';
?>
<p class="name"><?php echo 'Hallo, ' . convertName(convertUmlauteFrom($nutzername)) . ', bitte auswählen:'; ?></p>
<?php
echo '<table border="1"><tr><td></td>';
for ($i = 0; $i < count($kurse); $i++) {
echo "<td>" . convertUmlauteFrom($kurse[$i]["name"]) . "</td>";
}
for ($j = 0; $j < 3; $j++) {
echo "<tr>";
echo "<td>Wahl " . ($j + 1) . "</td>";
for ($k = 0; $k < count($kurse); $k++) {
echo "<td>";
echo '<input type="radio" id="' . $j . '/' . $k . '" name="wahl' . $j . '" value="' . convertUmlauteFrom($kurse[$k]["name"]) . ';' . $kurse[$k]["stunde"] . '">';
echo '<label for="' . $j . '/' . $k . '">' . $kurse[$k]["stunde"] . '</label>';
echo "</td>";
}
echo "</tr>";
}
echo '<tr><td colspan="' . (count($kurse) + 1) . '"><input type="submit" value="Wählen"></td></tr></table>';
?>
<input type="hidden" name="check" value="ja">
<?php
echo "</form>";
?>
<noscript>
<p>
JavaScript wird für Annehmlichkeiten verwendet, um diese nutzen zu können, sollten Sie dies auch aktivieren.
</p>
</noscript>
<script src="assets/js/main.js"></script>
</body>
</html>