function questions_import_submit_aiken in Quiz 6.6
Same name and namespace in other branches
- 6.3 includes/questions_import/questions_import.admin.inc \questions_import_submit_aiken()
@function This function imports questions from Moodle Aiken format file.
Return value
Return the number of questions successfully imported.
File
- includes/
questions_import/ questions_import.admin.inc, line 667 - Administration file for Questions Import module
Code
function questions_import_submit_aiken($destination_node, $form, $form_state, $import_id) {
global $user;
$row = 0;
$output = '';
$line = array();
$file = $form_state['values']['validated_file'];
$lines = file($file->filepath);
// while not empty of file content
while (!empty($lines)) {
while ($current_line = questions_import_get_next_aiken_field($lines)) {
// get the whole question fields to an array
if (empty($current_line)) {
// No more question fields. we have reached the end, exit from this loop.
break;
}
if ($current_line[0] === '#') {
// ho its a comment line, just ignore it
continue;
}
$line[] = $current_line;
}
if (empty($line)) {
// this line is empty. Get the next line
continue;
}
// now $line is an array holding question fields.
$type = questions_import_get_next_aiken_field($line);
if (!in_array($type, quiz_get_questions_type('enabled'))) {
drupal_set_message(t('Unable to import @type type question. You may need to enable @type module.', array(
'@type' => $type,
)), 'error');
// oops you have miss spelled question type or you are trying to import question whose module is disabled.
// set an error message and read the next question.
$line = array();
//empty the $line to load the next question
continue;
}
$node = new stdClass();
$node->type = $type;
$node->format = $form_state['values']['input_format'];
$node->quiz_id = $destination_node->nid;
$node->quiz_vid = $destination_node->vid;
$node->type = $type;
$node->title = $node->body = $node->teaser = questions_import_get_next_aiken_field($line);
$options = array();
// clear the options array for a new question
switch ($node->type) {
case 'multichoice':
$options = array();
$answer = array_pop($line);
// now $line is left only with choices which looks like A) Moodle B) ATutor C) Claroline D) Blackboard etc
while (!empty($line)) {
$l = questions_import_get_next_aiken_field($line);
$option = explode($l[1], $l);
$options[trim($option[0])]['choice'] = trim($option[1]);
$feedback = questions_import_get_next_aiken_field($line);
$options[trim($option[0])]['feedback'] = $feedback == 'nil' ? '' : $feedback;
}
$correct = substr(trim($answer), '-1');
$answer = $options[$correct]['choice'];
$line = array();
// empty the $line and load the next question fields into it.
$node->num_answers = count($options);
$node->answers = array();
foreach ($options as $option) {
$node->answers[] = array(
'correct' => trim($answer) == trim($option['choice']) ? 1 : 0,
'answer' => trim($option['choice']),
'feedback' => trim($option['feedback']),
);
}
break;
case 'true_false':
$node->correct_answer = questions_import_get_next_aiken_field($line) == 'true' ? 1 : 0;
$feedback = questions_import_get_next_aiken_field($line);
$node->feedback = $feedback === 'nil' ? '' : $feedback;
break;
case 'matching':
$node->match = array();
while (!empty($line)) {
$row = explode(',', questions_import_get_next_aiken_field($line));
$node->match[] = array(
'question' => $row[0],
'answer' => $row[1],
'feedback' => $row[2] == 'nil' ? '' : $row[2],
);
}
break;
case 'short_answer':
$evaluation_type = array(
'case sensitive match',
'case insensitive match',
'regular expression match',
'manually score match',
);
$node->correct_answer = questions_import_get_next_aiken_field($line);
$node->maximum_score = (int) questions_import_get_next_aiken_field($line);
$node->correct_answer_evaluation = (int) array_search(questions_import_get_next_aiken_field($line), $evaluation_type);
// if array_search return FALSE we convert it to int 0 and take the default format 'case sensitive match'
break;
case 'long_answer':
$node->maximum_score = questions_import_get_next_aiken_field($line);
break;
case 'quiz_directions':
break;
}
node_save(questions_import_node_save_static_data($node));
db_query("INSERT INTO {quiz_questions_import_items} VALUES (%d, %d, %d)", $import_id, $node->nid, $row);
++$row;
}
return $row;
}