You are here

function _questions_export_moodle in Quiz 6.6

Exports questions using the Moodle export engine.

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

File

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

Code

function _questions_export_moodle($collection_node, $format) {
  global $user;
  ob_start();
  $drupal_questions = _questions_in_quiz($collection_node);
  $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;

  // 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;

  // 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
}