You are here

function qformat_qti2::writequestion in Quiz 6.5

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

Creates the export text for a question

@todo handle in-line media (specified in the question/subquestion/answer text) for course-level exports

Parameters

object $question:

boolean $shuffleanswers whether or not to shuffle the answers:

boolean $courselevel whether or not this is a course-level export:

string $path provide the path to copy question media files to, if $courselevel == true:

Return value

string containing export text

Overrides qformat_default::writequestion

2 calls to qformat_qti2::writequestion()
qformat_qti2::exportprocess in includes/moodle/question/format/qti2/format.php
exports the questions in a question category to the given location
qformat_qti2::questions_with_export_info in includes/moodle/question/format/qti2/format.php
adds exporttext property to the questions

File

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

Class

qformat_qti2

Code

function writequestion($question, $shuffleanswers = null, $courselevel = false, $path = '') {

  // turns question into string
  // question reflects database fields for general question and specific to type
  global $CFG;
  $expout = '';

  //need to unencode the html entities in the questiontext field.

  // the whole question object was earlier run throught htmlspecialchars in xml_entitize().
  $question->questiontext = html_entity_decode($question->questiontext, ENT_COMPAT);
  $hasimage = empty($question->image) ? 0 : 1;
  $hassize = empty($question->mediax) ? 0 : 1;
  $allowedtags = '<a><br><b><h1><h2><h3><h4><i><img><li><ol><strong><table><tr><td><th><u><ul><object>';

  // all other tags will be stripped from question text
  $smarty =& $this
    ->init_smarty();
  $assesmentitemid = $this
    ->get_assesment_item_id($question);
  $question_type = $this
    ->get_qtype($question->qtype);
  $questionid = "question{$question->id}{$question_type}";
  $smarty
    ->assign('question_has_image', $hasimage);
  $smarty
    ->assign('hassize', $hassize);
  $smarty
    ->assign('questionid', $questionid);
  $smarty
    ->assign('assessmentitemidentifier', $assesmentitemid);
  $smarty
    ->assign('assessmentitemtitle', $question->name);
  $smarty
    ->assign('courselevelexport', $courselevel);
  if ($question->qtype == MULTIANSWER) {
    $question->questiontext = strip_tags($question->questiontext, $allowedtags . '<intro>');
    $smarty
      ->assign('questionText', $this
      ->get_cloze_intro($question->questiontext));
  }
  else {
    $smarty
      ->assign('questionText', strip_tags($question->questiontext, $allowedtags));
  }
  $smarty
    ->assign('question', $question);

  // the following two are left for compatibility; the templates should be changed, though, to make object tags for the questions

  //$smarty->assign('questionimage', $question->image);

  //$smarty->assign('questionimagealt', "image: $question->image");

  // output depends on question type
  switch ($question->qtype) {
    case TRUEFALSE:
      $qanswers = $question->options->answers;
      $answers[0] = (array) $qanswers['true'];
      $answers[0]['answer'] = get_string("true", "quiz");
      $answers[1] = (array) $qanswers['false'];
      $answers[1]['answer'] = get_string("false", "quiz");
      if (!empty($shuffleanswers)) {
        $answers = $this
          ->shuffle_things($answers);
      }
      if (isset($question->response)) {
        $correctresponseid = $question->response[$questionid];
        if ($answers[0]['id'] == $correctresponseid) {
          $correctresponse = $answers[0];
        }
        else {
          $correctresponse = $answers[1];
        }
      }
      else {
        $correctresponse = '';
      }
      $smarty
        ->assign('correctresponse', $correctresponse);
      $smarty
        ->assign('answers', $answers);
      $expout = $smarty
        ->fetch('choice.tpl');
      break;
    case MULTICHOICE:
      $answers = $this
        ->objects_to_array($question->options->answers);
      $correctresponses = $this
        ->get_correct_answers($answers);
      $correctcount = count($correctresponses);
      $smarty
        ->assign('responsedeclarationcardinality', $question->options->single ? 'single' : 'multiple');
      $smarty
        ->assign('operator', $question->options->single ? 'match' : 'member');
      $smarty
        ->assign('correctresponses', $correctresponses);
      $smarty
        ->assign('answers', $answers);
      $smarty
        ->assign('maxChoices', $question->options->single ? '1' : count($answers));
      $smarty
        ->assign('maxChoices', $question->options->single ? '1' : count($answers));
      $smarty
        ->assign('shuffle', empty($shuffleanswers) ? 'false' : 'true');
      $smarty
        ->assign('generalfeedback', $question->generalfeedback);
      $smarty
        ->assign('correctfeedback', $question->options->correctfeedback);
      $smarty
        ->assign('partiallycorrectfeedback', $question->options->partiallycorrectfeedback);
      $smarty
        ->assign('incorrectfeedback', $question->options->incorrectfeedback);
      $expout = $smarty
        ->fetch('choiceMultiple.tpl');
      break;
    case SHORTANSWER:
      $answers = $this
        ->objects_to_array($question->options->answers);
      if (!empty($shuffleanswers)) {
        $answers = $this
          ->shuffle_things($answers);
      }
      $correctresponses = $this
        ->get_correct_answers($answers);
      $correctcount = count($correctresponses);
      $smarty
        ->assign('responsedeclarationcardinality', $correctcount > 1 ? 'multiple' : 'single');
      $smarty
        ->assign('correctresponses', $correctresponses);
      $smarty
        ->assign('answers', $answers);
      $expout = $smarty
        ->fetch('textEntry.tpl');
      break;
    case NUMERICAL:
      $qanswer = array_pop($question->options->answers);
      $smarty
        ->assign('lowerbound', $qanswer->answer - $qanswer->tolerance);
      $smarty
        ->assign('upperbound', $qanswer->answer + $qanswer->tolerance);
      $smarty
        ->assign('answer', $qanswer->answer);
      $expout = $smarty
        ->fetch('numerical.tpl');
      break;
    case MATCH:
      $this
        ->xml_entitize($question->options->subquestions);
      $subquestions = $this
        ->objects_to_array($question->options->subquestions);
      if (!empty($shuffleanswers)) {
        $subquestions = $this
          ->shuffle_things($subquestions);
      }
      $setcount = count($subquestions);
      $smarty
        ->assign('setcount', $setcount);
      $smarty
        ->assign('matchsets', $subquestions);
      $expout = $smarty
        ->fetch('match.tpl');
      break;
    case DESCRIPTION:
      $expout = $smarty
        ->fetch('extendedText.tpl');
      break;

    // loss of get_answers() from quiz_embedded_close_qtype class during
    // Gustav's refactor breaks MULTIANSWER badly - one for another day!!

    /*
            case MULTIANSWER:
                $answers = $this->get_cloze_answers_array($question);
                $questions = $this->get_cloze_questions($question, $answers, $allowedtags);

                $smarty->assign('cloze_trailing_text_id', CLOZE_TRAILING_TEXT_ID);
                $smarty->assign('answers', $answers);
                $smarty->assign('questions', $questions);
                $expout = $smarty->fetch('composite.tpl');
                break; */
    default:
      $smarty
        ->assign('questionText', "This question type (Unknown: type {$question_type})  has not yet been implemented");
      $expout = $smarty
        ->fetch('notimplemented.tpl');
  }

  // run through xml tidy function

  //$tidy_expout = $this->indent_xhtml( $expout, '    ' ) . "\n\n";

  //return $tidy_expout;
  return $expout;
}