You are here

function qformat_aiken::readquestions in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/question/format/aiken/format.php \qformat_aiken::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/aiken/format.php, line 54

Class

qformat_aiken
Aiken format - a simple format for creating multiple choice questions (with only one correct choice, and no feedback).

Code

function readquestions($lines) {
  $questions = array();
  $question = $this
    ->defaultquestion();
  $endchar = chr(13);
  foreach ($lines as $line) {
    $stp = strpos($line, $endchar, 0);
    $newlines = explode($endchar, $line);
    $foundQ = 0;
    $linescount = count($newlines);
    for ($i = 0; $i < $linescount; $i++) {
      $nowline = addslashes(trim($newlines[$i]));

      // Go through the array and build an object called $question
      // When done, add $question to $questions
      if (strlen($nowline) < 2) {
        continue;
      }
      if (preg_match('/^[A-Z][).][ \\t]/', $nowline)) {

        // A choice. Trim off the label and space, then save
        $question->answer[] = htmlspecialchars(trim(substr($nowline, 2)), ENT_NOQUOTES);
        $question->fraction[] = 0;
        $question->feedback[] = '';
        continue;
      }
      if (preg_match('/^ANSWER:/', $nowline)) {

        // The line that indicates the correct answer. This question is finised.
        $ans = trim(substr($nowline, strpos($nowline, ':') + 1));
        $ans = substr($ans, 0, 1);

        // We want to map A to 0, B to 1, etc.
        $rightans = ord($ans) - ord('A');
        $question->fraction[$rightans] = 1;
        $questions[] = $question;

        // Clear array for next question set
        $question = $this
          ->defaultquestion();
        continue;
      }
      else {

        // Must be the first line of a new question, since no recognised prefix.
        $question->qtype = MULTICHOICE;
        $question->name = htmlspecialchars(substr($nowline, 0, 50));
        $question->questiontext = htmlspecialchars($nowline);
        $question->single = 1;
        $question->feedback[] = '';
      }
    }
  }
  return $questions;
}