You are here

function qformat_default::importprocess in Quiz 6.5

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

Process the file This method should not normally be overidden

Return value

boolean success

1 method overrides qformat_default::importprocess()
qformat_coursetestmanager::importprocess in includes/moodle/question/format/coursetestmanager/format.php
Process the file This method should not normally be overidden

File

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

Class

qformat_default
Base class for question import and export formats.

Code

function importprocess() {
  global $USER;

  // reset the timer in case file upload was slow
  @set_time_limit();

  // STAGE 1: Parse the file
  notify(get_string('parsingquestions', 'quiz'));
  if (!($lines = $this
    ->readdata($this->filename))) {
    notify(get_string('cannotread', 'quiz'));
    return false;
  }
  if (!($questions = $this
    ->readquestions($lines))) {

    // Extract all the questions
    notify(get_string('noquestionsinfile', 'quiz'));
    return false;
  }

  // STAGE 2: Write data to database
  notify(get_string('importingquestions', 'quiz', $this
    ->count_questions($questions)));

  // check for errors before we continue
  if ($this->stoponerror and $this->importerrors > 0) {
    notify(get_string('importparseerror', 'quiz'));
    return true;
  }

  // get list of valid answer grades
  $grades = get_grade_options();
  $gradeoptionsfull = $grades->gradeoptionsfull;

  // check answer grades are valid
  // (now need to do this here because of 'stop on error': MDL-10689)
  $gradeerrors = 0;
  $goodquestions = array();
  foreach ($questions as $question) {
    if (!empty($question->fraction) and is_array($question->fraction)) {
      $fractions = $question->fraction;
      $answersvalid = true;

      // in case they are!
      foreach ($fractions as $key => $fraction) {
        $newfraction = match_grade_options($gradeoptionsfull, $fraction, $this->matchgrades);
        if ($newfraction === false) {
          $answersvalid = false;
        }
        else {
          $fractions[$key] = $newfraction;
        }
      }
      if (!$answersvalid) {
        notify(get_string('matcherror', 'quiz'));
        ++$gradeerrors;
        continue;
      }
      else {
        $question->fraction = $fractions;
      }
    }
    $goodquestions[] = $question;
  }
  $questions = $goodquestions;

  // check for errors before we continue
  if ($this->stoponerror and $gradeerrors > 0) {
    return false;
  }

  // count number of questions processed
  $count = 0;
  foreach ($questions as $question) {

    // Process and store each question
    // reset the php timeout
    @set_time_limit();

    // check for category modifiers
    if ($question->qtype == 'category') {
      if ($this->catfromfile) {

        // find/create category object
        $catpath = $question->category;
        $newcategory = $this
          ->create_category_path($catpath, '/');
        if (!empty($newcategory)) {
          $this->category = $newcategory;
        }
      }
      continue;
    }
    $count++;
    echo "<hr /><p><b>{$count}</b>. " . $this
      ->format_question_text($question) . "</p>";
    $question->category = $this->category->id;
    $question->stamp = make_unique_id_code();

    // Set the unique code (not to be changed)
    $question->createdby = $USER->id;
    $question->timecreated = time();
    if (!($question->id = insert_record("question", $question))) {
      error(get_string('cannotinsert', 'quiz'));
    }
    $this->questionids[] = $question->id;

    // Now to save all the answers and type-specific options
    global $QTYPES;
    $result = $QTYPES[$question->qtype]
      ->save_question_options($question);
    if (!empty($result->error)) {
      notify($result->error);
      return false;
    }
    if (!empty($result->notice)) {
      notify($result->notice);
      return true;
    }

    // Give the question a unique version stamp determined by question_hash()
    set_field('question', 'version', question_hash($question), 'id', $question->id);
  }
  return true;
}