Last active
December 15, 2015 01:49
-
-
Save Cacodaimon/5182931 to your computer and use it in GitHub Desktop.
Converts the questions from the "OCA Java SE 7 Programmer 1 Study Guide (Exam 1Z0-803)" book CD into a readable JSON format.
The dBASE files from the book disc have to be exported to a csv file first.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Converts the questions from the "OCA Java SE 7 Programmer 1 Study Guide (Exam 1Z0-803)" book CD into a readable JSON format. | |
* The dBASE files from the book disc have to be exported to a csv file first. | |
* | |
* @autor Guido Krömer <[email protected]> | |
*/ | |
$fileQuestions = 'Questions.csv'; | |
$fileAnswers = 'Answers.csv'; | |
function getAnswers ($fileAnswers, $delimiter = ',') { | |
$handle = fopen($fileAnswers, 'r'); | |
$numToAlpha = range('A', 'Z'); | |
$answers = []; | |
while ($data = fgetcsv($handle, 0, $delimiter)) { | |
$numAnswer = $data[1] - 1; | |
$answers[$data[0]][$numAnswer] = utf8_encode($numToAlpha[$numAnswer] . ': ' . $data[2]); | |
} | |
fclose($handle); | |
return $answers; | |
} | |
function getRightAnswers($data, $answers) { | |
$id = $data[1]; | |
$answerNumber = intval($data[6]); | |
$countPossibleAnswers = count($answers[$id]); | |
$returnVal = []; | |
$returnVal = array_fill(0, $countPossibleAnswers, false); | |
if ($answerNumber < $countPossibleAnswers) { | |
$returnVal[$answerNumber -1] = true; | |
return $returnVal; | |
} | |
$bit = 1; | |
for ($i = 0; $i < $countPossibleAnswers; ++$i) { | |
$returnVal[$i] = ($answerNumber & $bit) > 0; | |
$bit *= 2; | |
} | |
return $returnVal; | |
} | |
function getQuestions ($fileQuestions, array $answers, $delimiter = ',') { | |
$handle = fopen($fileQuestions, 'r'); | |
$questions = []; | |
while ($data = fgetcsv($handle, 0, $delimiter)) { | |
$questions[] = [ | |
'title' => utf8_encode($data[3]), | |
'text' => utf8_encode($data[4]), | |
'options' => $answers[$data[1]], | |
'rightAnswers' => getRightAnswers($data, $answers), | |
'hint' => utf8_encode($data[7]), | |
'answerDescription' => utf8_encode($data[8]), | |
]; | |
} | |
fclose($handle); | |
return $questions; | |
} | |
echo json_encode([ | |
'title' => 'OCJA 7', | |
'description' => '', | |
'questions' => getQuestions($fileQuestions, getAnswers($fileAnswers)), | |
], JSON_PRETTY_PRINT); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment