You are here

function qformat_qti2::get_cloze_questions in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/question/format/qti2/format.php \qformat_qti2::get_cloze_questions()

gets an array with text and question arrays for the given cloze question

To make smarty processing easier, the returned text and question sub-arrays have an equal number of elements. If it is necessary to add a dummy element to the question sub-array, the question will be given an id of CLOZE_TRAILING_TEXT_ID.

Parameters

object $question:

array $answers - an array of arrays containing the question's answers:

string $allowabletags - tags not to strip out of the question text (e.g. '<i><br>'):

Return value

array with text and question arrays for the given cloze question

File

includes/moodle/question/format/qti2/format.php, line 736

Class

qformat_qti2

Code

function get_cloze_questions($question, $answers, $allowabletags) {
  $questiontext = strip_tags($question->questiontext, $allowabletags);
  if (preg_match_all('/(.*){#([0-9]+)}/U', $questiontext, $matches)) {

    // matches[1] contains the text inbetween the question blanks
    // matches[2] contains the id of the question blanks (db: question_multianswer.positionkey)
    // find any trailing text after the last {#XX} and add it to the array
    if (preg_match('/.*{#[0-9]+}(.*)$/', $questiontext, $tail)) {
      $matches[1][] = $tail[1];
      $tailadded = true;
    }
    $questions['text'] = $matches[1];
    $questions['question'] = array();
    foreach ($matches[2] as $key => $questionid) {
      foreach ($answers as $answer) {
        if ($answer['positionkey'] == $questionid) {
          $questions['question'][$key] = $answer;
          break;
        }
      }
    }
    if ($tailadded) {

      // to have a matching number of question and text array entries:
      $questions['question'][] = array(
        'id' => CLOZE_TRAILING_TEXT_ID,
        'answertype' => SHORTANSWER,
      );
    }
  }
  else {
    $questions['text'][0] = $question->questiontext;
    $questions['question'][0] = array(
      'id' => CLOZE_TRAILING_TEXT_ID,
      'answertype' => SHORTANSWER,
    );
  }
  return $questions;
}