You are here

function qformat_learnwise::readquestion in Quiz 6.5

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

Given the data known to define a question in this format, this function converts it into a question object suitable for processing and insertion into Moodle.

If your format does not use blank lines to delimit questions (e.g. an XML format) you must override 'readquestions' too

Parameters

$lines mixed data that represents question:

Return value

object question object

Overrides qformat_default::readquestion

1 call to qformat_learnwise::readquestion()
qformat_learnwise::readquestions in includes/moodle/question/format/learnwise/format.php
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.

File

includes/moodle/question/format/learnwise/format.php, line 35

Class

qformat_learnwise
@package questionbank @subpackage importexport

Code

function readquestion($lines) {
  $text = implode(' ', $lines);
  $text = str_replace(array(
    '\\t',
    '\\n',
    '\\r',
    '\'',
  ), array(
    '',
    '',
    '',
    '\\\'',
  ), $text);
  $startpos = strpos($text, '<question type');
  $endpos = strpos($text, '</question>');
  if ($startpos === false || $endpos === false) {
    return false;
  }
  preg_match("/<question type=[\"\\']([^\"\\']+)[\"\\']>/i", $text, $matches);
  $type = strtolower($matches[1]);

  // multichoice or multianswerchoice
  $questiontext = $this
    ->unhtmlentities($this
    ->stringbetween($text, '<text>', '</text>'));
  $questionhint = $this
    ->unhtmlentities($this
    ->stringbetween($text, '<hint>', '</hint>'));
  $questionaward = $this
    ->stringbetween($text, '<award>', '</award>');
  $optionlist = $this
    ->stringbetween($text, '<answer>', '</answer>');
  $optionlist = explode('<option', $optionlist);
  $n = 0;
  $optionscorrect = array();
  $optionstext = array();
  if ($type == 'multichoice') {
    foreach ($optionlist as $option) {
      $correct = $this
        ->stringbetween($option, ' correct="', '">');
      $answer = $this
        ->stringbetween($option, '">', '</option>');
      $optionscorrect[$n] = $correct;
      $optionstext[$n] = $this
        ->unhtmlentities($answer);
      ++$n;
    }
  }
  else {
    if ($type == 'multianswerchoice') {
      $numcorrect = 0;
      $totalaward = 0;
      $optionsaward = array();
      foreach ($optionlist as $option) {
        preg_match("/correct=\"([^\"]*)\"/i", $option, $correctmatch);
        preg_match("/award=\"([^\"]*)\"/i", $option, $awardmatch);
        $correct = $correctmatch[1];
        $award = $awardmatch[1];
        if ($correct == 'yes') {
          $totalaward += $award;
          ++$numcorrect;
        }
        $answer = $this
          ->stringbetween($option, '">', '</option>');
        $optionscorrect[$n] = $correct;
        $optionstext[$n] = $this
          ->unhtmlentities($answer);
        $optionsaward[$n] = $award;
        ++$n;
      }
    }
    else {
      echo "<p>I don't understand this question type (type = <strong>{$type}</strong>).</p>\n";
    }
  }
  $question = $this
    ->defaultquestion();
  $question->qtype = MULTICHOICE;
  $question->name = substr($questiontext, 0, 30);
  if (strlen($questiontext) > 30) {
    $question->name .= '...';
  }
  $question->questiontext = $questiontext;
  $question->single = $type == 'multichoice' ? 1 : 0;
  $question->feedback[] = '';
  $question->fraction = array();
  $question->answer = array();
  for ($n = 0; $n < count($optionstext); ++$n) {
    if ($optionstext[$n]) {
      if (!isset($numcorrect)) {

        // single answer
        if ($optionscorrect[$n] == 'yes') {
          $fraction = (int) $questionaward;
        }
        else {
          $fraction = 0;
        }
      }
      else {

        // mulitple answers
        if ($optionscorrect[$n] == 'yes') {
          $fraction = $optionsaward[$n] / $totalaward;
        }
        else {
          $fraction = -$optionsaward[$n] / count($optionstext);
        }
      }
      $question->fraction[] = $fraction;
      $question->answer[] = $optionstext[$n];
      $question->feedback[] = '';

      // no feedback in this type
    }
  }
  return $question;
}