You are here

function _questions_export_native in Quiz 6.6

Exports questions using Quiz module's native export engine.

Parameters

$collection_node:

$export_format:

Return value

unknown_type

1 call to _questions_export_native()
_questions_export_download in includes/questions_export/questions_export.admin.inc

File

includes/questions_export/questions_export.admin.inc, line 94

Code

function _questions_export_native($collection_node, $export_format) {

  // $questions is an array of quiz question node object
  $questions = _questions_in_quiz($collection_node);
  ob_start();
  $glue = $export_format === 'aiken' ? "\n" : ',';

  // if the export type is 'aiken' use new line ("\n") else use comma (",") as glue for implode
  // iterate through all the questions and generate file content.
  foreach ($questions as $question) {
    $line = array(
      $question->type,
      $question->body,
    );
    switch ($question->type) {
      case 'matching':
        foreach ($question->answer as $answer) {
          $feedback = $answer['feedback'] ? $answer['feedback'] : t('nil');

          // use 'nil' when there is no feedback
          array_push($line, $answer['question'], $answer['answer'], $feedback);
        }
        break;
      case 'choice':

        // FIXME implement 'choice'
        break;
      case 'multichoice':
        foreach ($question->answers as $answer) {
          $feedback = $answer['feedback'] ? $answer['feedback'] : t('nil');

          // use 'nil' when there is no feedback
          array_push($line, $answer['answer'], $feedback);
          $correct_answer = $answer['is_correct'] ? $answer['answer'] : '';
        }
        array_push($line, $correct_answer);
        break;
      case 'true_false':
        $feedback = $question->feedback ? $feedback : t('nil');

        // use 'nil' when there is no feedback
        array_push($line, $question->correct_answer ? 'true' : 'false', $feedback);
        break;
      case 'short_answer':
        $evaluation = array(
          'case sensitive match',
          'case insensitive match',
          'regular expression match',
          'manually score match',
        );
        array_push($line, $question->correct_answer, $question->maximum_score, $evaluation[$question->correct_answer_evaluation]);
        break;
      case 'long_answer':
        array_push($line, $question->maximum_score);
        break;
    }
    $output .= count($line) > 2 ? implode($glue, $line) . "\n\n" : '';
    $line = array();
  }
  $filename = str_replace(' ', '_', $collection_node->title) . '.txt';
  $filepath = file_directory_temp() . '/' . $filename;
  $handle = @fopen($filepath, 'w');
  fwrite($handle, $output);
  fclose($handle);
  $headers = array(
    'Content-Type: text/plain',
    'Content-Disposition: attachment; filename=' . $filename,
  );
  ob_clean();
  file_transfer($filepath, $headers);
  ob_end_clean();
}