You are here

function qformat_default::exportprocess in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/question/format.php \qformat_default::exportprocess()

Do the export For most types this should not need to be overrided

Return value

boolean success

1 method overrides qformat_default::exportprocess()
qformat_qti2::exportprocess in includes/moodle/question/format/qti2/format.php
exports the questions in a question category to the given location

File

includes/moodle/question/format.php, line 643

Class

qformat_default
Base class for question import and export formats.

Code

function exportprocess() {
  global $CFG;
  $export_dir = make_upload_directory($this
    ->question_get_export_dir());

  // create a directory for the exports (if not already existing)
  if (!($export_dir = make_upload_directory($this
    ->question_get_export_dir()))) {
    error(get_string('cannotcreatepath', 'quiz', $export_dir));
  }
  $path = $CFG->dataroot . '/' . $this
    ->question_get_export_dir();

  // get the questions (from database) in this category
  // only get q's with no parents (no cloze subquestions specifically)
  if ($this->category) {
    $questions = get_questions_category($this->category, true);
  }
  else {
    $questions = $this->questions;
  }
  notify(get_string('exportingquestions', 'quiz'));
  $count = 0;

  // results are first written into string (and then to a file)
  // so create/initialize the string here
  $expout = "";

  // track which category questions are in
  // if it changes we will record the category change in the output
  // file if selected. 0 means that it will get printed before the 1st question
  $trackcategory = 0;

  // iterate through questions
  foreach ($questions as $question) {
    if (module_exists('devel')) {
      print "exporting:";
      dprint_r($question);
    }

    // do not export hidden questions
    if (!empty($question->hidden)) {
      continue;
    }

    // do not export random questions
    if ($question->qtype == RANDOM) {
      continue;
    }

    // check if we need to record category change
    if ($this->cattofile) {
      if ($question->category != $trackcategory) {
        $trackcategory = $question->category;
        $categoryname = $this
          ->get_category_path($trackcategory, '/', $this->contexttofile);

        // create 'dummy' question for category export
        $dummyquestion = new object();
        $dummyquestion->qtype = 'category';
        $dummyquestion->category = $categoryname;
        $dummyquestion->name = "switch category to {$categoryname}";
        $dummyquestion->id = 0;
        $dummyquestion->questiontextformat = '';
        $expout .= $this
          ->writequestion($dummyquestion) . "\n";
      }
    }

    // export the question displaying message
    $count++;
    echo "<hr /><p><b>{$count}</b>. " . $this
      ->format_question_text($question) . "</p>";
    if (question_has_capability_on($question, 'view', $question->category)) {
      $expout .= $this
        ->writequestion($question) . "\n";
    }
  }

  // continue path for following error checks
  $course = $this->course;
  $continuepath = "{$CFG->wwwroot}/question/export.php?courseid={$course->id}";

  // did we actually process anything
  if ($count == 0) {
    print_error('noquestions', 'quiz', $continuepath);
  }

  // final pre-process on exported data
  $expout = $this
    ->presave_process($expout);

  // write file
  $filepath = $path . "/" . $this->filename . $this
    ->export_file_extension();
  if (!($fh = fopen($filepath, "w"))) {
    print_error('cannotopen', 'quiz', $continuepath, $filepath);
  }
  if (!fwrite($fh, $expout, strlen($expout))) {
    print_error('cannotwrite', 'quiz', $continuepath, $filepath);
  }
  fclose($fh);
  return true;
}