You are here

function qformat_hotpot::readquestions in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/moodle/question/format/hotpot/format.php \qformat_hotpot::readquestions()

Parses an array of lines into an array of questions, where each item is a question object as defined by readquestion(). Questions are defined as anything between blank lines.

If your format does not use blank lines as a delimiter then you will need to override this method. Even then try to use readquestion for each question

Parameters

array lines array of lines from readdata:

Return value

array array of question objects

Overrides qformat_default::readquestions

File

includes/moodle/question/format/hotpot/format.php, line 22

Class

qformat_hotpot
@package questionbank @subpackage importexport

Code

function readquestions($lines) {

  /// Parses an array of lines into an array of questions,

  /// where each item is a question object as defined by

  /// readquestion().

  // set courseid and baseurl
  global $CFG, $COURSE, $course;
  switch (true) {
    case isset($this->course->id):

      // import to quiz module
      $courseid = $this->course->id;
      break;
    case isset($course->id):

      // import to lesson module
      $courseid = $course->id;
      break;
    case isset($COURSE->id):

      // last resort
      $courseid = $COURSE->id;
      break;
    default:

      // shouldn't happen !!
      $courseid = 0;
  }
  require_once $CFG->libdir . '/filelib.php';
  $baseurl = get_file_url($courseid) . '/';

  // get import file name
  global $params;
  if (isset($params) && !empty($params->choosefile)) {

    // course file (Moodle >=1.6+)
    $filename = $params->choosefile;
  }
  else {

    // uploaded file (all Moodles)
    $filename = basename($_FILES['newfile']['tmp_name']);
  }

  // get hotpot file source
  $source = implode($lines, " ");
  $source = hotpot_convert_relative_urls($source, $baseurl, $filename);

  // create xml tree for this hotpot
  $xml = new hotpot_xml_tree($source);

  // determine the quiz type
  $xml->quiztype = '';
  $keys = array_keys($xml->xml);
  foreach ($keys as $key) {
    if (preg_match('/^(hotpot|textoys)-(\\w+)-file$/i', $key, $matches)) {
      $xml->quiztype = strtolower($matches[2]);
      $xml->xml_root = "['{$key}']['#']";
      break;
    }
  }

  // convert xml to questions array
  $questions = array();
  switch ($xml->quiztype) {
    case 'jcloze':
      $this
        ->process_jcloze($xml, $questions);
      break;
    case 'jcross':
      $this
        ->process_jcross($xml, $questions);
      break;
    case 'jmatch':
      $this
        ->process_jmatch($xml, $questions);
      break;
    case 'jmix':
      $this
        ->process_jmix($xml, $questions);
      break;
    case 'jbc':
    case 'jquiz':
      $this
        ->process_jquiz($xml, $questions);
      break;
    default:
      if (empty($xml->quiztype)) {
        notice("Input file not recognized as a Hot Potatoes XML file");
      }
      else {
        notice("Unknown quiz type '{$xml->quiztype}'");
      }
  }

  // end switch
  return $questions;
}