I give up for now

This commit is contained in:
Daniel Mason 2016-12-31 17:01:14 +13:00
parent 9985eeed2a
commit 9f6e348e05
3 changed files with 202 additions and 38 deletions

View file

@ -1,11 +1,12 @@
<?php
require_once("includes/include.php");
$questionArray = [];
$files = glob("files/*");
foreach($files as $file) {
$questions = [];
$fileOpen = file_get_contents($file);
$fileOpen = implode("\n", array_slice(explode("\n", $fileOpen), 6));
$fileOpen = substr($fileOpen, 1);
$questions = [];
$data = explode("%", $fileOpen);
$data = array_map('trim', $data);
foreach ($data as $i => $d) {
@ -21,7 +22,7 @@ foreach($files as $file) {
preg_match('/".*?"/', $splitNewLine[0], $matches);
$questions[$i]['image'] = strtoupper($matches[0]);
if (strpos($splitNewLine[1], ':') !== false || !empty(trim($splitNewLine[2]))) {
if (strpos($splitNewLine[1], ':') !== false && !empty(trim($splitNewLine[2]))) {
$twoLineQuestion = true;
}
$cutOutImageShit = explode(">", $splitNewLine[1]);
@ -47,7 +48,7 @@ foreach($files as $file) {
}
$x = $b = 1;
foreach ($splitNewLine as $a => $line) {
//Don't ask what this witchcraft is.
//Don't ask what this witchcraft is. I don't know either.
if ($a == 0) continue;
if (($twoLineQuestion || $imageFirstLine) && $a == 1) continue;
if ($threeLineQuestion || ($twoLineQuestion && $imageFirstLine) && $a == 2) continue;
@ -67,39 +68,51 @@ foreach($files as $file) {
$questions[$i - 1]['correctAnswer'] = substr($d, 3);
}
}
$i = $b = $d = $a = 0; //Clear the useless stuff from above.
if (isset($_REQUEST['insert']) && $_REQUEST['insert'] == 1 && count($questions)) {
$count = 0;
foreach ($questions as $q) {
$db = new db();
$db->query("INSERT INTO question(question_time, questiondata_number, questiondata_content, questiondata_image)
VALUES(:qTime, :qNumber, :qContent, :qImage)");
$db->bind("qTime", time());
$db->bind("qNumber", $q['questionNumber'] ?: 0);
$db->bind("qContent", $q['question']);
$db->bind("qImage", $q['image'] ?: "");
$db->execute();
$lastRow = $db->lastInsertId();
$db->kill(); //IS THIS EVEN NEEDED?
$row = 1;
foreach ($q['answers'] as $a) {
$db = new db();
$db->query("INSERT INTO answer(answer_time, answerdata_content, answerdata_question, answerdata_correct)
VALUES(:aTime, :aContent, :aQuestion, :aCorrect)");
$db->bind("aTime", time());
$db->bind("aContent", $a);
$db->bind("aQuestion", $lastRow);
$db->bind("aCorrect", ($q['correctAnswer'] == $row ? "1" : "0"));
$db->execute();
$db->kill();
$row++;
}
$db = null;
$count++;
}
echo "Inserted " . $count . " questions.";
} else {
var_dump($questions);
//CHECK ALL THE STUFF IS CORRECT.
foreach($questions as $q) {
$bad = false;
if(count($q['answers']) != 4) $bad = true;
if(!$q['correctAnswer']) $bad = true;
if(!$q['questionNumber']) $bad = true;
if(empty(trim($q['question']))) $bad = true;
foreach($q['answers'] as $a) {
if(empty(trim($a))) $bad = true;
}
if($bad) var_dump($q);
}
}
$questionArray = array_merge($questionArray, $questions);
}
exit();
$i = $b = $d = $a = $count = 0; //Clear the useless stuff from above.
foreach ($questionArray as $q) {
$db = new db();
$db->query("INSERT INTO question(question_time, questiondata_number, questiondata_content, questiondata_image)
VALUES(:qTime, :qNumber, :qContent, :qImage)");
$db->bind("qTime", time());
$db->bind("qNumber", $q['questionNumber'] ?: 0);
$db->bind("qContent", $q['question']);
$db->bind("qImage", $q['image'] ?: "");
$db->execute();
$lastRow = $db->lastInsertId();
$db->kill(); //IS THIS EVEN NEEDED?
$row = 1;
foreach ($q['answers'] as $a) {
$db = new db();
$db->query("INSERT INTO answer(answer_time, answerdata_content, answerdata_question, answerdata_correct)
VALUES(:aTime, :aContent, :aQuestion, :aCorrect)");
$db->bind("aTime", time());
$db->bind("aContent", $a);
$db->bind("aQuestion", $lastRow);
$db->bind("aCorrect", ($q['correctAnswer'] == $row ? "1" : "0"));
$db->execute();
$db->kill();
$row++;
}
$db = null;
$count++;
}
echo "Inserted " . $count . " questions.";