This needs a bit more work, but I basically needed a way to create a qset file from an instructor's document. I decided to create a task to convert a CSV into a qset yaml file, however this will only work for short answer questions, and currently is somewhat hardcoded for Flash Cards
// Accepts a csv file and turns it into a qset yaml file.
// Only works for Question-Answer qsets (not multiple choice)
// Requires CSV in the following format:
// -------------------------------------
// "Q","A"
// "Your first question","Your first answer"
// "Your second question","Your second answer"
// ...
// -------------------------------------
// Additionally the file should have UNIX line endings
// (If using Sublime Text, go to View->Line Endings->Unix)
// Reading of the CSV file can be touchy, so you may
// want to strip out non-printable characters in the CSV
// Sed can do this:
// sed 's/[^[:print:]]|[^[\n]]//g' in.csv > out.csv
public static function convert_csv_to_qset_yaml($csv_file)
{
// read in CSV
$file_area = \File::forge(['basedir' => null]);
$csv_data = \Format::forge($file_area->read($csv_file, true), 'csv')->to_array();
// \Cli::write(print_r($csv_data, true));
// go through each question and convert to a qset structure
$items = [];
foreach($csv_data as $q)
{
$item = [
'materiaType' => 'question',
'type' => 'QA',
'id' => 0,
'questions' => [ ['text' => $q['Q']] ],
'assets' => [],
'answers' => [[
'value' => '100',
'text' => $q['A'],
'id' => 0
]]
];
$item['assets'][] = false;
$item['assets'][] = false;
$items[] = $item;
}
// create yaml
// $qset = ['items' => $items ];
$qset = [
'rand' => false,
'items' => [],
'assets' => [],
'options' => [],
'name' => ''
];
$qset['items'][] = ['items' => $items ];
$yaml = \Format::forge($qset)->to_yaml();
// write yaml
$output_file = $csv_file.'_qset.yaml';
if (file_exists($output_file))
{
$output_file = $csv_file.'_'.mt_rand().'_qset.yaml';
}
$file_area = \File::forge(['basedir' => null]);
if ($file_area->create(DOCROOT, $output_file, $yaml))
{
\Cli::write($output_file.' created', 'green');
self::quit();
}
\Cli::write(print_r($yaml, true));
}