This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 19
Trivia Questions
darkalchemy edited this page Aug 1, 2019
·
2 revisions
This is what I use to bulk import questions. The source I have has many duplicates so this is the way that I have to do it ensure no duplicates are imported. You may have a better or easier way, this is mine.
<?php
require_once '/var/www/master/include/bittorrent.php';
global $fluent, $pdo;
$db_questions = $fluent->from('triviaq')
->select(null)
->select('question')
->select('answer1')
->select('answer2')
->select('answer3')
->select('answer4')
->select('answer5')
->select('canswer')
->fetchAll();
$questions = // source for questions in json format
$array = json_decode($questions, true);
$to_add = [];
foreach ($array['results'] as $item) {
$answers = [];
if (!empty($item['question'])) {
$question = clean_text($item['question']);
$answers[] = clean_text($item['correct_answer']);
foreach ($item['incorrect_answers'] as $wrong) {
$answers[] = $wrong;
}
for ($i= 0; $i <= 10; $i++) {
shuffle($answers);
}
$i = 1;
$canswer = '';
foreach($answers as $answer) {
${'answer'.$i} = $answer;
if ($answer === $item['correct_answer']) {
$canswer = 'answer'.$i;
}
if ($canswer === 'answer5' && $answer5 === '') {
echo $canswer . "\n";
echo $item['correct_answer'] . "\n";
var_dump($answers);
exit;
}
$i++;
}
$list['question'] = $question;
$list['answer1'] = !empty($answers[0]) ? $answers[0] : '';
$list['answer2'] = !empty($answers[1]) ? $answers[1] : '';
$list['answer3'] = !empty($answers[2]) ? $answers[2] : '';
$list['answer4'] = !empty($answers[3]) ? $answers[3] : '';
$list['answer5'] = !empty($answers[4]) ? $answers[4] : '';
$list['canswer'] = $canswer;
$to_add[] = $list;
}
}
$to_clean = array_merge($db_questions, $to_add);
$all_questions = $results = [];
foreach ($to_clean as $question) {
$question['question'] = clean_text($question['question']);
$question['answer1'] = clean_text($question['answer1']);
$question['answer2'] = clean_text($question['answer2']);
$question['answer3'] = clean_text($question['answer3']);
$question['answer4'] = clean_text($question['answer4']);
$question['answer5'] = clean_text($question['answer5']);
$question['hash'] = hash('sha256', $question['question']);
if (!in_array($question['hash'], $all_questions)) {
$all_questions[] = $question['hash'];
$results[] = $question;
}
}
echo count($results) . " questions \n";
$update = [
'asked' => 0,
];
$pdo->query('ALTER TABLE triviausers DROP FOREIGN KEY `triviausers_ibfk_2`');
$pdo->query('TRUNCATE triviaq');
$pdo->query('TRUNCATE triviausers');
$fluent->insertInto('triviaq', $results)
->onDuplicateKeyUpdate($update)
->execute();
function clean_text($text) {
$text = trim($text);
$text = preg_replace('/&/', "&", $text);
$text = preg_replace('/"/', "'", $text);
$text = preg_replace('/'/', "'", $text);
$text = preg_replace('/"/', "'", $text);
$text = preg_replace('/É/', 'É', $text);
return $text;
}
$pdo->query('ALTER TABLE triviausers ADD CONSTRAINT triviausers_ibfk_2 FOREIGN KEY (qid) REFERENCES triviaq (qid) ON DELETE CASCADE ON UPDATE CASCADE;');```