Skip to content

Instantly share code, notes, and snippets.

@no-chris
Created December 6, 2017 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save no-chris/9cb7c67b59ee89e6c95e76f218ccf367 to your computer and use it in GitHub Desktop.
Save no-chris/9cb7c67b59ee89e6c95e76f218ccf367 to your computer and use it in GitHub Desktop.
Convert QTI XML to JSON model
<?php
require_once dirname(__FILE__).'/tao/includes/raw_start.php';
function fileTypeValid($file, $type)
{
if (!$file || substr($file, -(strlen($type) + 1)) != '.'.$type) {
echo 'invalid file type : '.$file;
echo PHP_EOL;
return false;
}
if (!file_exists($file)) {
$file = dirname(__FILE__).'/'.$file;
if (!file_exists($file)) {
echo 'file not found : '.$file;
echo PHP_EOL;
return false;
}
}
return $file;
}
/**
* Sample usage :
* sudo -u www-data php taoTool.php --qti-to-json taoQtiItem/views/js/test/samples/qtiv2p1/choice_custom/qti.xml
*/
switch ($argv[1]) {
case '--decode':
echo PHP_EOL;
echo \tao_helpers_Uri::decode($argv[2]);
echo PHP_EOL;
break;
case '--encode':
echo PHP_EOL;
echo \tao_helpers_Uri::encode($argv[2]);
echo PHP_EOL;
break;
case '--qti-to-json':
$file = fileTypeValid($argv[2], 'xml');
if ($file) {
//process
$qtiParser = new oat\taoQtiItem\model\qti\Parser($file);
$item = $qtiParser->load();
$splFile = new SplFileInfo($file);
$destFile =
$splFile->getPath() .
'/' .
$splFile->getBasename('.xml') .
'.json';
file_put_contents($destFile, json_encode($item->toArray(), JSON_PRETTY_PRINT) . "\n");
//result
echo PHP_EOL;
echo json_encode($item->toArray(), JSON_PRETTY_PRINT);
echo PHP_EOL;
echo PHP_EOL;
}
break;
default:
echo 'unknown command';
echo PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment