new features

This commit is contained in:
torsten 2026-10-21 20:22:20 +02:00
parent 9fcd228eb2
commit a73358d2f1
9 changed files with 538 additions and 2 deletions

View file

@ -0,0 +1,174 @@
<?php
namespace App\Models;
use App\Core\Database;
/**
* Fragen der Spieler an die Admins inklusive der Antworten.
* Beantwortete Fragen werden für den fragenden Spieler, offene Fragen für die
* Admins als "neu" markiert, bis sie auf der Fragenseite angesehen wurden.
*/
class Question {
/** Fragen und Antworten werden nach so vielen Jahren automatisch gelöscht */
const RETENTION_YEARS = 1;
/**
* Legt eine neue Frage an.
*
* @param int $userId
* @param string $question
* @return int ID
*/
public static function create($userId, $question) {
$db = Database::getConnection();
$stmt = $db->prepare(
"INSERT INTO questions (user_id, question, question_date, created_at, updated_at)
VALUES (?, ?, NOW(), NOW(), NOW())"
);
$stmt->execute([(int)$userId, $question]);
return (int)$db->lastInsertId();
}
/**
* Alle Fragen eines Spielers, neueste zuerst.
*
* @param int $userId
* @return array
*/
public static function findByUser($userId) {
$db = Database::getConnection();
$stmt = $db->prepare(
"SELECT * FROM questions WHERE user_id = ? ORDER BY question_date DESC, id DESC"
);
$stmt->execute([(int)$userId]);
return $stmt->fetchAll();
}
/**
* Alle Fragen für die Admin-Ansicht (offene zuerst), mit Name des Fragestellers.
*
* @return array
*/
public static function findAllForAdmin() {
$db = Database::getConnection();
$stmt = $db->prepare(
"SELECT q.*, COALESCE(u.name, '?') AS user_name
FROM questions q
LEFT JOIN users u ON u.id = q.user_id
ORDER BY (q.answer IS NULL) DESC, q.question_date DESC, q.id DESC"
);
$stmt->execute();
return $stmt->fetchAll();
}
/**
* @param int $id
* @return array|false
*/
public static function findById($id) {
$db = Database::getConnection();
$stmt = $db->prepare(
"SELECT q.*, COALESCE(u.name, '?') AS user_name
FROM questions q
LEFT JOIN users u ON u.id = q.user_id
WHERE q.id = ?"
);
$stmt->execute([(int)$id]);
return $stmt->fetch();
}
/**
* Beantwortet eine Frage und markiert die Antwort für den Spieler als neu.
*
* @param int $id
* @param string $answer
* @param int $adminId
* @return bool
*/
public static function answer($id, $answer, $adminId) {
$db = Database::getConnection();
$stmt = $db->prepare(
"UPDATE questions
SET answer = ?, answer_date = NOW(), answered_by = ?, answer_seen = 0, updated_at = NOW()
WHERE id = ?"
);
return $stmt->execute([$answer, (int)$adminId, (int)$id]);
}
/**
* Gibt es für einen Spieler eine noch nicht angesehene Antwort?
*
* @param int $userId
* @return bool
*/
public static function hasNewAnswer($userId) {
$db = Database::getConnection();
$stmt = $db->prepare(
"SELECT COUNT(*) FROM questions WHERE user_id = ? AND answer IS NOT NULL AND answer_seen = 0"
);
$stmt->execute([(int)$userId]);
return (int)$stmt->fetchColumn() > 0;
}
/**
* Gibt es eine noch nicht von einem Admin angesehene offene Frage?
*
* @return bool
*/
public static function hasNewQuestion() {
$db = Database::getConnection();
$stmt = $db->prepare(
"SELECT COUNT(*) FROM questions WHERE answer IS NULL AND question_seen = 0"
);
$stmt->execute();
return (int)$stmt->fetchColumn() > 0;
}
/**
* Markiert alle beantworteten Fragen eines Spielers als angesehen.
*
* @param int $userId
* @return int
*/
public static function markAnswersSeen($userId) {
$db = Database::getConnection();
$stmt = $db->prepare(
"UPDATE questions SET answer_seen = 1, updated_at = NOW()
WHERE user_id = ? AND answer IS NOT NULL AND answer_seen = 0"
);
$stmt->execute([(int)$userId]);
return $stmt->rowCount();
}
/**
* Markiert alle offenen Fragen als von Admins angesehen.
*
* @return int
*/
public static function markQuestionsSeen() {
$db = Database::getConnection();
$stmt = $db->prepare(
"UPDATE questions SET question_seen = 1, updated_at = NOW()
WHERE answer IS NULL AND question_seen = 0"
);
$stmt->execute();
return $stmt->rowCount();
}
/**
* Löscht Fragen (und damit ihre Antworten), die älter als RETENTION_YEARS Jahre sind.
*
* @return int Anzahl gelöschter Fragen
*/
public static function deleteOld() {
$db = Database::getConnection();
$stmt = $db->prepare(
"DELETE FROM questions
WHERE question_date < CURDATE() - INTERVAL " . (int)self::RETENTION_YEARS . " YEAR"
);
$stmt->execute();
return $stmt->rowCount();
}
}

View file

@ -42,4 +42,39 @@ class Setting {
$stmt = $db->prepare("UPDATE settings SET registration_open = ?, updated_at = NOW() WHERE id = ?");
return $stmt->execute([$open ? 1 : 0, $settingsId]);
}
/**
* Aktuelle Mitteilung für die Startseite (leer = keine Mitteilung).
*
* @return string
*/
public static function getMessage() {
$db = Database::getConnection();
$value = $db->query("SELECT message FROM settings ORDER BY id LIMIT 1")->fetchColumn();
return $value === false ? '' : (string)$value;
}
/**
* Setzt (oder löscht) die aktuelle Mitteilung für die Startseite.
*
* @param string $message
* @return bool
*/
public static function setMessage($message) {
$db = Database::getConnection();
$message = trim((string)$message);
$settingsId = $db->query("SELECT id FROM settings ORDER BY id LIMIT 1")->fetchColumn();
if ($settingsId === false) {
$current = Season::getCurrent();
$stmt = $db->prepare(
"INSERT INTO settings (current_season_id, registration_open, message, created_at, updated_at)
VALUES (?, ?, ?, NOW(), NOW())"
);
return $stmt->execute([$current ? (int)$current['id'] : 0, 1, $message]);
}
$stmt = $db->prepare("UPDATE settings SET message = ?, updated_at = NOW() WHERE id = ?");
return $stmt->execute([$message, $settingsId]);
}
}

View file

@ -0,0 +1,34 @@
<?php
$pageTitle = 'Aktuelle Mitteilung';
require __DIR__ . '/../partials/header.php';
?>
<h1>Aktuelle Mitteilung</h1>
<?php if (!empty($flashMessage)): ?>
<p style="color: green;"><?php echo htmlspecialchars($flashMessage); ?></p>
<?php endif; ?>
<?php if (!empty($error)): ?>
<p style="color: red;"><?php echo htmlspecialchars($error); ?></p>
<?php endif; ?>
<p>Die Mitteilung erscheint in großer Schrift oben auf der Startseite. Leer lassen und speichern entfernt die Mitteilung.</p>
<form method="post" action="/index.php?page=admin_message" class="message-form">
<div class="form-group">
<label for="message">Mitteilung:</label>
<textarea id="message" name="message" rows="8"><?php echo htmlspecialchars($currentMessage); ?></textarea>
</div>
<div class="form-group">
<button type="submit">Speichern</button>
</div>
</form>
<style>
.message-form { max-width: 760px; }
.message-form .form-group { margin-bottom: 1rem; }
.message-form label { display: block; font-weight: bold; margin-bottom: 0.3rem; }
.message-form textarea { width: 100%; box-sizing: border-box; font-size: 1.2em; }
</style>
<?php require __DIR__ . '/../partials/footer.php'; ?>

View file

@ -7,6 +7,10 @@ require __DIR__ . '/partials/header.php';
<h1>Willkommen bei Tippspiel 2027</h1>
<?php if (!empty($message)): ?>
<div class="announcement"><?php echo nl2br(htmlspecialchars($message)); ?></div>
<?php endif; ?>
<h2>Abgeschlossene Runden:</h2>
<?php if ($currentSeason): ?>
<p>Saison: <strong><?php echo htmlspecialchars($currentSeason['name']); ?></strong></p>

View file

@ -1,4 +1,16 @@
<?php $pageTitle = $pageTitle ?? 'Tippspiel 2027'; ?>
<?php
$pageTitle = $pageTitle ?? 'Tippspiel 2027';
// Leucht-Hinweis am Menüpunkt "Fragen", wenn eine neue Antwort oder (für
// Admins) eine neue Frage vorliegt.
$questionsGlow = false;
if (App\Core\Auth::check()) {
$questionsGlow = App\Models\Question::hasNewAnswer((int)App\Core\Auth::id());
if (App\Core\Auth::isAdmin()) {
$questionsGlow = $questionsGlow || App\Models\Question::hasNewQuestion();
}
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
@ -14,9 +26,10 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" integrity="sha512-SnH5WK+bZxgPHs44uWIX+LLJAJ9/2PkPKZ5QiAj6Ta86w+fsb2TkcmfRyVX3pBnMFcV7oQPJkl9QevSCWr3W6A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body class="<?php echo htmlspecialchars($bodyClass ?? ''); ?>">
<?php if (App\Core\Auth::check()): ?>
<nav class="site-nav">
<a href="/" class="brand"><span class="brand-ball">⚽</span> Tippspiel <span class="brand-year">2027</span></a>
<a href="/">Start</a>
<a href="/index.php?page=questions"<?php if ($questionsGlow): ?> class="is-glowing"<?php endif; ?>>Fragen</a>
<?php if (App\Core\Auth::isAdmin()): $registrationOpen = App\Models\Setting::isRegistrationOpen(); ?>
<span class="nav-dropdown">
<a href="#" onclick="return false;">Administration</a>
@ -26,6 +39,7 @@
<a href="/index.php?page=admin_rounds">Spiele &amp; Ergebnisse eintragen</a>
<a href="/index.php?page=admin_roundmails">Runden-E-Mails versenden</a>
<a href="/index.php?page=admin_accounts">Kontoliste</a>
<a href="/index.php?page=admin_message">Aktuelle Mitteilung</a>
<form method="post" action="/index.php?page=admin" class="nav-dropdown-form">
<input type="hidden" name="action" value="set_registration">
<input type="hidden" name="registration_open" value="<?php echo $registrationOpen ? '0' : '1'; ?>">
@ -65,4 +79,5 @@
<?php endif; ?>
</span>
</nav>
<?php endif; ?>
<main class="page">

View file

@ -0,0 +1,138 @@
<?php
$pageTitle = 'Fragen';
require __DIR__ . '/partials/header.php';
?>
<h1>Fragen</h1>
<?php if (!empty($message)): ?>
<p class="flash flash-ok"><?php echo htmlspecialchars($message); ?></p>
<?php endif; ?>
<?php if (!empty($error)): ?>
<p class="flash flash-err"><?php echo htmlspecialchars($error); ?></p>
<?php endif; ?>
<?php if ($answerQuestion): ?>
<h2>Frage beantworten</h2>
<div class="question-entry">
<p class="question-meta">
Von <?php echo htmlspecialchars($answerQuestion['user_name']); ?>
· <?php echo date('d.m.Y H:i', strtotime($answerQuestion['question_date'])); ?>
</p>
<p class="question-text"><?php echo nl2br(htmlspecialchars($answerQuestion['question'])); ?></p>
</div>
<form method="post" action="/index.php?page=questions" class="questions-form">
<input type="hidden" name="action" value="answer">
<input type="hidden" name="id" value="<?php echo (int)$answerQuestion['id']; ?>">
<div class="form-group">
<label for="answer">Antwort:</label>
<textarea id="answer" name="answer" rows="6" required></textarea>
</div>
<div class="form-group buttons">
<button type="submit">Antwort speichern</button>
<a href="/index.php?page=questions">Abbrechen</a>
</div>
</form>
<?php else: ?>
<?php if ($isAdmin): ?>
<p>Hier siehst du alle Fragen der Spieler. Offene Fragen kannst du direkt beantworten.</p>
<?php else: ?>
<p>Hier siehst du deine Fragen an die Admins und deren Antworten.</p>
<?php endif; ?>
<h2>Neue Frage stellen</h2>
<form method="post" action="/index.php?page=questions" class="questions-form">
<input type="hidden" name="action" value="ask">
<div class="form-group">
<label for="question">Deine Frage:</label>
<textarea id="question" name="question" rows="4" required maxlength="5000"></textarea>
</div>
<div class="form-group buttons">
<button type="submit">Absenden</button>
</div>
</form>
<h2><?php echo $isAdmin ? 'Alle Fragen' : 'Deine Fragen'; ?></h2>
<?php if (!$questions): ?>
<p><?php echo $isAdmin ? 'Noch keine Fragen vorhanden.' : 'Du hast noch keine Fragen gestellt.'; ?></p>
<?php else: ?>
<?php foreach ($questions as $q): ?>
<div class="question-entry<?php echo $q['answer'] === null ? ' is-open' : ''; ?>">
<p class="question-meta">
<?php if ($isAdmin): ?>
Von <?php echo htmlspecialchars($q['user_name']); ?>
<?php endif; ?>
· <?php echo date('d.m.Y H:i', strtotime($q['question_date'])); ?>
</p>
<p class="question-text"><?php echo nl2br(htmlspecialchars($q['question'])); ?></p>
<?php if ($q['answer'] === null): ?>
<p class="question-status open">Noch nicht beantwortet</p>
<?php if ($isAdmin): ?>
<p><a class="answer-link" href="/index.php?page=questions&amp;answer_id=<?php echo (int)$q['id']; ?>">Antworten</a></p>
<?php endif; ?>
<?php else: ?>
<div class="answer-box">
<p class="question-meta">Antwort · <?php echo date('d.m.Y H:i', strtotime($q['answer_date'])); ?></p>
<p class="question-text"><?php echo nl2br(htmlspecialchars($q['answer'])); ?></p>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>
<style>
.flash { padding: 0.6em 0.9em; border-radius: 6px; }
.flash-ok { background: #e8f3ec; border-left: 4px solid var(--pitch); color: var(--pitch-dark); }
.flash-err { background: #fdecea; border-left: 4px solid #d62d2d; color: #a32626; }
.questions-form { max-width: 760px; margin-bottom: 2em; }
.questions-form .form-group { margin-bottom: 1rem; }
.questions-form label { display: block; font-weight: bold; margin-bottom: 0.3rem; }
.questions-form textarea { width: 100%; box-sizing: border-box; }
.questions-form .buttons a { margin-left: 1em; }
.question-entry {
max-width: 760px;
margin: 1em 0;
padding: 0.8em 1em;
border: 1px solid var(--line);
border-left: 5px solid var(--pitch);
border-radius: 8px;
background: #fff;
}
.question-entry.is-open { border-left-color: var(--gold); background: #fffdf3; }
.question-meta { color: var(--muted); font-size: 0.85em; margin: 0 0 0.35em; }
.question-text { margin: 0.35em 0; white-space: normal; }
.question-status { font-size: 0.85em; font-weight: 700; margin: 0.5em 0; }
.question-status.open { color: var(--col-joker-exact); }
.answer-link {
display: inline-block;
font-family: var(--font-display);
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.05em;
padding: 0.35em 1.1em;
border-radius: 999px;
color: #fff;
background: linear-gradient(180deg, #1b7a3b, var(--pitch));
text-decoration: none;
box-shadow: 0 2px 0 var(--pitch-dark);
}
.answer-link:hover { background: var(--pitch-dark); color: var(--gold); }
.answer-box {
margin-top: 0.6em;
padding: 0.6em 0.9em;
background: var(--pitch-light);
border-radius: 6px;
}
</style>
<?php require __DIR__ . '/partials/footer.php'; ?>