You are here

function qformat_xml::readquestions in Quiz 6.5

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

parse the array of lines into an array of questions this *could* burn memory - but it won't happen that much so fingers crossed!

Parameters

array lines array of lines from the input file:

Return value

array (of objects) question objects

Overrides qformat_default::readquestions

File

includes/moodle/question/format/xml/format.php, line 520

Class

qformat_xml

Code

function readquestions($lines) {

  // we just need it as one big string
  $text = implode($lines, " ");
  unset($lines);

  // this converts xml to big nasty data structure
  // the 0 means keep white space as it is (important for markdown format)
  // print_r it if you want to see what it looks like!
  $xml = xmlize($text, 0);

  // set up array to hold all our questions
  $questions = array();

  // iterate through questions
  foreach ($xml['quiz']['#']['question'] as $question) {
    $question_type = $question['@']['type'];
    $questiontype = get_string('questiontype', 'quiz', $question_type);
    if ($question_type == 'multichoice') {
      $qo = $this
        ->import_multichoice($question);
    }
    elseif ($question_type == 'truefalse') {
      $qo = $this
        ->import_truefalse($question);
    }
    elseif ($question_type == 'shortanswer') {
      $qo = $this
        ->import_shortanswer($question);
    }
    elseif ($question_type == 'numerical') {
      $qo = $this
        ->import_numerical($question);
    }
    elseif ($question_type == 'description') {
      $qo = $this
        ->import_description($question);
    }
    elseif ($question_type == 'matching') {
      $qo = $this
        ->import_matching($question);
    }
    elseif ($question_type == 'cloze') {
      $qo = $this
        ->import_multianswer($question);
    }
    elseif ($question_type == 'essay') {
      $qo = $this
        ->import_essay($question);
    }
    elseif ($question_type == 'calculated') {
      $qo = $this
        ->import_calculated($question);
    }
    elseif ($question_type == 'category') {
      $qo = $this
        ->import_category($question);
    }
    else {

      // try for plugin support
      // no default question, as the plugin can call
      // import_headers() itself if it wants to
      if (!($qo = $this
        ->try_importing_using_qtypes($question))) {
        $notsupported = get_string('xmltypeunsupported', 'quiz', $question_type);
        $this
          ->error($notsupported);
        $qo = null;
      }
    }

    // stick the result in the $questions array
    if ($qo) {
      $questions[] = $qo;
    }
  }
  return $questions;
}