205 lines
8.7 KiB
PHP
205 lines
8.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
ini_set('display_errors', '1');
|
|
error_reporting(E_ALL);
|
|
date_default_timezone_set('Europe/Rome');
|
|
|
|
session_start();
|
|
|
|
require_once __DIR__ . '/db/config.php';
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
$pdo = db();
|
|
$message = '';
|
|
$error = '';
|
|
$submitted_successfully = false;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
|
|
$nickname = filter_input(INPUT_POST, 'nickname', FILTER_SANITIZE_STRING);
|
|
$answers = $_POST['answers'] ?? [];
|
|
|
|
if (!$email || !$nickname || empty($answers)) {
|
|
$error = 'Per favore, compila tutti i campi e rispondi a tutte le domande.';
|
|
} else {
|
|
try {
|
|
// Check for duplicate email
|
|
$stmt = $pdo->prepare("SELECT id FROM participants WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
$error = 'Questo indirizzo email ha già partecipato. È consentito un solo invio per email.';
|
|
} else {
|
|
$pdo->beginTransaction();
|
|
|
|
// Insert participant
|
|
$stmt = $pdo->prepare("INSERT INTO participants (email, nickname) VALUES (?, ?)");
|
|
$stmt->execute([$email, $nickname]);
|
|
$participant_id = $pdo->lastInsertId();
|
|
|
|
// Insert submissions
|
|
$stmt_submission = $pdo->prepare("INSERT INTO submissions (participant_id, question_id, answer_id) VALUES (?, ?, ?)");
|
|
foreach ($answers as $question_id => $answer_id) {
|
|
$stmt_submission->execute([$participant_id, $question_id, $answer_id]);
|
|
}
|
|
|
|
$pdo->commit();
|
|
|
|
// Send confirmation email
|
|
$subject = 'Grazie per aver partecipato al nostro Quiz!';
|
|
$body = "Ciao {$nickname},<br><br>Grazie per aver inviato le tue risposte. I risultati saranno pubblicati al termine del concorso.<br><br>In bocca al lupo!";
|
|
MailService::sendMail($email, $subject, $body, strip_tags($body));
|
|
|
|
$message = 'Grazie per aver partecipato! Hai inviato con successo le tue risposte e riceverai una mail di conferma a breve.';
|
|
$submitted_successfully = true;
|
|
}
|
|
} catch (Exception $e) {
|
|
if ($pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
$error = "Si è verificato un errore durante l'invio. Riprova. Dettagli: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fetch questions and answers for the form
|
|
$questions = [];
|
|
if (!$submitted_successfully) {
|
|
try {
|
|
$stmt = $pdo->query("SELECT id, question_text, points FROM questions ORDER BY id");
|
|
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
|
$question_id = $row['id'];
|
|
$row['answers'] = [];
|
|
$stmt_answers = $pdo->prepare("SELECT id, answer_text FROM answers WHERE question_id = ? ORDER BY id");
|
|
$stmt_answers->execute([$question_id]);
|
|
$row['answers'] = $stmt_answers->fetchAll(PDO::FETCH_ASSOC);
|
|
$questions[] = $row;
|
|
}
|
|
} catch (Exception $e) {
|
|
$error = "Impossibile caricare le domande del quiz. Dettagli: " . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="it">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Quiz Eleganza Motoristica</title>
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@700&family=Roboto:wght@400;700&display=swap" rel="stylesheet">
|
|
<style>
|
|
:root {
|
|
--bg-color: #1a1a2e;
|
|
--surface-color: #16213e;
|
|
--primary-accent: #e94560;
|
|
--secondary-accent: #0f3460;
|
|
--text-color: #ffffff;
|
|
--text-muted: #a0a0a0;
|
|
--success-color: #50fa7b;
|
|
--error-color: #ff5555;
|
|
}
|
|
body {
|
|
margin: 0;
|
|
font-family: 'Roboto', sans-serif;
|
|
background-color: var(--bg-color);
|
|
color: var(--text-color);
|
|
padding: 2rem;
|
|
}
|
|
.container { max-width: 800px; margin: 0 auto; }
|
|
h1 {
|
|
font-family: 'Orbitron', sans-serif;
|
|
color: var(--primary-accent);
|
|
text-align: center;
|
|
text-shadow: 0 0 10px var(--primary-accent);
|
|
}
|
|
.header-image { width: 100%; height: 200px; background-color: var(--secondary-accent); margin-bottom: 2rem; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-style: italic; color: var(--text-muted); }
|
|
.question-card {
|
|
background-color: var(--surface-color);
|
|
border: 1px solid var(--secondary-accent);
|
|
border-radius: 12px;
|
|
padding: 1.5rem;
|
|
margin-bottom: 1.5rem;
|
|
box-shadow: 0 4px 20px rgba(0,0,0,0.3);
|
|
transition: transform 0.2s;
|
|
}
|
|
.question-card:hover { transform: translateY(-5px); }
|
|
.question-title { font-weight: 700; font-size: 1.2rem; margin-bottom: 1rem; }
|
|
.answer-option { margin-bottom: 0.5rem; }
|
|
.answer-option label { display: flex; align-items: center; cursor: pointer; padding: 0.5rem; border-radius: 6px; transition: background-color 0.2s; }
|
|
.answer-option input { margin-right: 0.8rem; }
|
|
.answer-option label:hover { background-color: var(--secondary-accent); }
|
|
.form-group { margin-bottom: 1.5rem; }
|
|
.form-group label { display: block; margin-bottom: 0.5rem; }
|
|
.form-group input[type='email'], .form-group input[type='text'] {
|
|
width: 100%;
|
|
padding: 0.8rem;
|
|
border: 1px solid var(--secondary-accent);
|
|
background-color: var(--surface-color);
|
|
color: var(--text-color);
|
|
border-radius: 6px;
|
|
}
|
|
.btn-submit {
|
|
display: block; width: 100%; padding: 1rem; font-size: 1.2rem;
|
|
font-family: 'Orbitron', sans-serif;
|
|
background-color: var(--primary-accent);
|
|
color: var(--text-color);
|
|
border: none; border-radius: 8px; cursor: pointer;
|
|
transition: background-color 0.3s, box-shadow 0.3s;
|
|
text-shadow: 0 0 5px rgba(255,255,255,0.5);
|
|
}
|
|
.btn-submit:hover { background-color: #ff5e78; box-shadow: 0 0 15px var(--primary-accent); }
|
|
.message, .error { padding: 1rem; border-radius: 8px; text-align: center; margin: 1.5rem 0; }
|
|
.message { background-color: var(--success-color); color: var(--bg-color); }
|
|
.error { background-color: var(--error-color); color: var(--text-color); }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="header-image">Spazio per logo e info</div>
|
|
<h1>Quiz The Unheard Edition 2025</h1>
|
|
|
|
<?php if ($message): ?>
|
|
<div class="message"><?= htmlspecialchars($message) ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($error): ?>
|
|
<div class="error"><?= htmlspecialchars($error) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!$submitted_successfully): ?>
|
|
<form action="index.php" method="POST">
|
|
<div class="question-card">
|
|
<h2>I tuoi dati</h2>
|
|
<div class="form-group">
|
|
<label for="email">Email</label>
|
|
<input type="email" id="email" name="email" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="nickname">Nickname</label>
|
|
<input type="text" id="nickname" name="nickname" required>
|
|
</div>
|
|
</div>
|
|
|
|
<?php foreach ($questions as $index => $q): ?>
|
|
<div class="question-card">
|
|
<p class="question-title"><?= htmlspecialchars($q['question_text']) ?> <small>(<?= $q['points'] ?> punti)</small></p>
|
|
<?php foreach ($q['answers'] as $a): ?>
|
|
<div class="answer-option">
|
|
<label>
|
|
<input type="radio" name="answers[<?= $q['id'] ?>]" value="<?= $a['id'] ?>" required>
|
|
<?= htmlspecialchars($a['answer_text']) ?>
|
|
</label>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
|
|
<?php if (!empty($questions)): ?>
|
|
<button type="submit" class="btn-submit">Invia le tue risposte</button>
|
|
<?php endif; ?>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</body>
|
|
</html>
|