You are here

function qformat_xhtml::writequestion in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/moodle/question/format/xhtml/format.php \qformat_xhtml::writequestion()

convert a single question object into text output in the given format. This must be overriden

Parameters

object question question object:

Return value

mixed question export text or null if not implemented

Overrides qformat_default::writequestion

File

includes/moodle/question/format/xhtml/format.php, line 21

Class

qformat_xhtml
@package questionbank @subpackage importexport

Code

function writequestion($question) {

  // turns question into string
  // question reflects database fields for general question and specific to type
  // if a category switch, just ignore
  if ($question->qtype == 'category') {
    return '';
  }

  // initial string;
  $expout = "";
  $id = $question->id;

  // add comment and div tags
  $expout .= "<!-- question: {$id}  name: {$question->name} -->\n";
  $expout .= "<div class=\"question\">\n";

  // add header
  $expout .= "<h3>{$question->name}</h3>\n";

  // format and add question text
  $questiontext = $question->questiontext;
  $format = $question->questiontextformat;
  $formatted_text = format_text($questiontext, $format);
  $expout .= "<p class=\"questiontext\">{$formatted_text}</p>\n";

  // selection depends on question type
  switch ($question->qtype) {
    case TRUEFALSE:
      $st_true = get_string('true', 'quiz');
      $st_false = get_string('false', 'quiz');
      $expout .= "<ul class=\"truefalse\">\n";
      $expout .= "  <li><input name=\"quest_{$id}\" type=\"radio\" value=\"{$st_true}\" />{$st_true}</li>\n";
      $expout .= "  <li><input name=\"quest_{$id}\" type=\"radio\" value=\"{$st_false}\" />{$st_false}</li>\n";
      $expout .= "</ul>\n";
      break;
    case MULTICHOICE:
      $expout .= "<ul class=\"multichoice\">\n";
      foreach ($question->options->answers as $answer) {
        $ans_text = $this
          ->repchar($answer->answer);
        if ($question->options->single) {
          $expout .= "  <li><input name=\"quest_{$id}\" type=\"radio\" value=\"{$ans_text}\" />{$ans_text}</li>\n";
        }
        else {
          $expout .= "  <li><input name=\"quest_{$id}\" type=\"checkbox\" value=\"{$ans_text}\" />{$ans_text}</li>\n";
        }
      }
      $expout .= "</ul>\n";
      break;
    case SHORTANSWER:
      $expout .= "<ul class=\"shortanswer\">\n";
      $expout .= "  <li><input name=\"quest_{$id}\" type=\"text\" /></li>\n";
      $expout .= "</ul>\n";
      break;
    case NUMERICAL:
      $expout .= "<ul class=\"numerical\">\n";
      $expout .= "  <li><input name=\"quest_{$id}\" type=\"text\" /></li>\n";
      $expout .= "</ul>\n";
      break;
    case MATCH:
      $expout .= "<ul class=\"match\">\n";

      // build answer list
      $ans_list = array();
      foreach ($question->options->subquestions as $subquestion) {
        $ans_list[] = $this
          ->repchar($subquestion->answertext);
      }
      shuffle($ans_list);

      // random display order
      // build drop down for answers
      $dropdown = "<select name=\"quest_{$id}\">\n";
      foreach ($ans_list as $ans) {
        $dropdown .= "<option value=\"{$ans}\">{$ans}</option>\n";
      }
      $dropdown .= "</select>\n";

      // finally display
      foreach ($question->options->subquestions as $subquestion) {
        $quest_text = $this
          ->repchar($subquestion->questiontext);
        $expout .= "  <li>{$quest_text}</li>\n";
        $expout .= $dropdown;
      }
      $expout .= "</ul>\n";
      break;
    case DESCRIPTION:
      break;
    case MULTIANSWER:
      $expout .= "<!-- CLOZE type is not supported  -->\n";
      break;
    default:
      notify("No handler for qtype {$question->qtype} for GIFT export");
  }

  // close off div
  $expout .= "</div>\n\n\n";
  return $expout;
}