You are here

function questions_export_submit_moodle in Quiz 6.5

Exports questions to a GIFT file.

1 call to questions_export_submit_moodle()
questions_export_form_submit in includes/questions_export/questions_export.admin.inc
This generic submit handler calls specific export functions

File

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

Code

function questions_export_submit_moodle($format, &$form, &$form_state) {
  global $user;
  ob_start();
  $quiz_nid = $form_state['values']['quiz_node'];
  $drupal_questions = _questions_in_quiz($quiz_nid);
  $moodle_questions = _as_moodle_questions($drupal_questions);
  $file_name = tempnam(variable_get('file_directory_temp', file_directory_temp()), 'quiz');
  $fhandle = @fopen($file_name, 'w');

  // The @ suppresses errors.
  global $CFG;
  require_once drupal_get_path('module', 'quiz') . "/includes/moodle/question/format/{$format}/format.php";
  $classname = "qformat_{$format}";
  $qformat = new $classname();
  $qformat
    ->set_can_access_backupdata(false);

  // not available here
  $qformat->filename = $quiz->title;

  /*
    // import individually
    foreach ($moodle_questions as $question) {
      $informat = $qformat->writequestion($question);
      fwrite($fhandle, $informat);
    }
    $dlname = "$quiz->title.gift.txt";
    $headers = array("Content-Type: text/plain",
                     "Content-Disposition: attachment; filename=\"$dlname\"" );
    // fwrite($fhandle, print_r($drupal_questions,true));  // debug
  */

  // process using moodle
  $qformat
    ->setQuestions($moodle_questions);
  if (!$qformat
    ->exportpreprocess()) {

    // Do anything before that we need to
    print_error('exporterror', 'quiz', $thispageurl
      ->out());
  }
  if (!$qformat
    ->exportprocess()) {

    // Process the export data
    print_error('exporterror', 'quiz', $thispageurl
      ->out());
  }
  if (!$qformat
    ->exportpostprocess()) {

    // In case anything needs to be done after
    print_error('exporterror', 'quiz', $thispageurl
      ->out());
  }

  // send the finished file
  // FIXME this isn't very robust, but it's what Moodle's question/export.php does
  $filename = $qformat->filename . $qformat
    ->export_file_extension();
  $filepath = $qformat
    ->question_get_export_dir() . '/' . $filename;

  // print $filepath;exit;
  // TODO set Content-Type based on export format.  wish that Moodle included that in its formats.
  switch ($qformat
    ->export_file_extension()) {
    case 'zip':
      $content_type = 'application/zip';
      break;
    case 'xml':
      $content_type = 'text/xml';
      break;
    case 'txt':
      $content_type = 'text/plain';
      break;
  }
  $headers = array(
    "Content-Type: {$content_type}",
    "Content-Disposition: attachment; filename=\"{$filename}\"",
  );
  ob_clean();
  file_transfer($filepath, $headers);
  ob_end_clean();
  $url = file_create_url($filepath);

  // for future reference
}