36915-vm/db/seed.php
Flatlogic Bot 1b18b92970 0.1
2025-12-13 17:39:27 +00:00

98 lines
3.8 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
try {
$pdo = db();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Clean up tables before seeding to make the script idempotent
$pdo->exec('SET FOREIGN_KEY_CHECKS=0;');
$pdo->exec('TRUNCATE TABLE submissions;');
$pdo->exec('TRUNCATE TABLE answers;');
$pdo->exec('TRUNCATE TABLE questions;');
$pdo->exec('TRUNCATE TABLE participants;');
$pdo->exec('SET FOREIGN_KEY_CHECKS=1;');
$quiz_data = [
[
'question' => "Quale sarà l'auto vincitrice del 'Best of Show' al Concorso d'Eleganza di Villa d'Este 2025?",
'points' => 5,
'answers' => [
['text' => "Un'auto d'epoca degli anni '30", 'correct' => true],
['text' => "Una supercar moderna", 'correct' => false],
['text' => "Un prototipo di design", 'correct' => false],
]
],
[
'question' => "Chi vincerà il premio 'FIVA World Motoring Heritage Year'?",
'points' => 5,
'answers' => [
['text' => "Un club di auto storiche italiano", 'correct' => false],
['text' => "Un museo dell'automobile americano", 'correct' => true],
['text' => "Una collezione privata giapponese", 'correct' => false],
]
],
[
'question' => "Quale brand presenterà il prototipo più audace?",
'points' => 5,
'answers' => [
['text' => "Un marchio di lusso europeo", 'correct' => false],
['text' => "Una startup di veicoli elettrici", 'correct' => true],
['text' => "Un produttore di hypercar di nicchia", 'correct' => false],
]
],
[
'question' => "DOMANDA JOLLY: Quale sarà il valore di aggiudicazione più alto all'asta RM Sotheby's di Villa Erba?",
'points' => 45,
'answers' => [
['text' => "Tra 5 e 10 milioni di euro", 'correct' => true],
['text' => "Sotto i 5 milioni di euro", 'correct' => false],
['text' => "Oltre 10 milioni di euro", 'correct' => false],
]
],
[
'question' => "ITALIAN TGA: Quale auto italiana vincerà il premio nella sua categoria?",
'points' => 30,
'answers' => [
['text' => "Una Ferrari classica", 'correct' => false],
['text' => "Una Lamborghini rara", 'correct' => false],
['text' => "Un'Alfa Romeo da competizione", 'correct' => true],
]
],
];
$pdo->beginTransaction();
foreach ($quiz_data as $item) {
// Insert question
$sql_question = "INSERT INTO questions (question_text, points) VALUES (:question_text, :points)";
$stmt_question = $pdo->prepare($sql_question);
$stmt_question->execute([
':question_text' => $item['question'],
':points' => $item['points']
]);
$question_id = $pdo->lastInsertId();
// Insert answers
foreach ($item['answers'] as $answer) {
$sql_answer = "INSERT INTO answers (question_id, answer_text, is_correct) VALUES (:question_id, :answer_text, :is_correct)";
$stmt_answer = $pdo->prepare($sql_answer);
$stmt_answer->execute([
':question_id' => $question_id,
':answer_text' => $answer['text'],
':is_correct' => (int)$answer['correct'] // Explicitly cast boolean to integer
]);
}
}
$pdo->commit();
echo "Database popolato con successo con le domande e le risposte del quiz." . PHP_EOL;
} catch (PDOException $e) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
die("Errore durante il popolamento del database: " . $e->getMessage());
}