You are here

function qformat_missingword::readquestion in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/question/format/missingword/format.php \qformat_missingword::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

File

includes/moodle/question/format/missingword/format.php, line 38

Class

qformat_missingword
@package questionbank @subpackage importexport

Code

function readquestion($lines) {

  /// Given an array of lines known to define a question in

  /// this format, this function converts it into a question

  /// object suitable for processing and insertion into Moodle.
  $question = $this
    ->defaultquestion();

  ///$comment added by T Robb
  $comment = NULL;
  $text = implode(" ", $lines);

  /// Find answer section
  $answerstart = strpos($text, "{");
  if ($answerstart === false) {
    if ($this->displayerrors) {
      echo "<p>{$text}<p>Could not find a {";
    }
    return false;
  }
  $answerfinish = strpos($text, "}");
  if ($answerfinish === false) {
    if ($this->displayerrors) {
      echo "<p>{$text}<p>Could not find a }";
    }
    return false;
  }
  $answerlength = $answerfinish - $answerstart;
  $answertext = substr($text, $answerstart + 1, $answerlength - 1);

  /// Save the new question text
  $question->questiontext = addslashes(substr_replace($text, "_____", $answerstart, $answerlength + 1));
  $question->name = $question->questiontext;

  /// Parse the answers
  $answertext = str_replace("=", "~=", $answertext);
  $answers = explode("~", $answertext);
  if (isset($answers[0])) {
    $answers[0] = trim($answers[0]);
  }
  if (empty($answers[0])) {
    array_shift($answers);
  }
  $countanswers = count($answers);
  switch ($countanswers) {
    case 0:

      // invalid question
      if ($this->displayerrors) {
        echo "<p>No answers found in {$answertext}";
      }
      return false;
    case 1:
      $question->qtype = SHORTANSWER;
      $answer = trim($answers[0]);
      if ($answer[0] == "=") {
        $answer = substr($answer, 1);
      }
      $question->answer[] = addslashes($answer);
      $question->fraction[] = 1;
      $question->feedback[] = "";
      return $question;
    default:
      $question->qtype = MULTICHOICE;
      foreach ($answers as $key => $answer) {
        $answer = trim($answer);

        // Tom's addition starts here
        $answeight = 0;
        if (strspn($answer, "1234567890%") > 0) {

          //Make sure that the percent sign is the last in the span
          if (strpos($answer, "%") == strspn($answer, "1234567890%") - 1) {
            $answeight0 = substr($answer, 0, strspn($answer, "1234567890%"));
            $answeight = round($answeight0 / 100, 2);
            $answer = substr($answer, strspn($answer, "1234567890%"));
          }
        }
        if ($answer[0] == "=") {
          $answeight = 1;
        }

        //remove the protective underscore for leading numbers in answers
        if ($answer[0] == "_") {
          $answer = substr($answer, 1);
        }
        $answer = trim($answer);
        if (strpos($answer, "#") > 0) {
          $hashpos = strpos($answer, "#");
          $comment = addslashes(substr($answer, $hashpos + 1));
          $answer = substr($answer, 0, $hashpos);
        }
        else {
          $comment = " ";
        }

        // End of Tom's addition
        if ($answer[0] == "=") {

          #                       $question->fraction[$key] = 1;
          $question->fraction[$key] = $answeight;
          $answer = substr($answer, 1);
        }
        else {

          #                       $question->fraction[$key] = 0;
          $question->fraction[$key] = $answeight;
        }
        $question->answer[$key] = addslashes($answer);
        $question->feedback[$key] = $comment;
      }
      return $question;
  }
}