You are here

function qformat_gift::writequestion in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/moodle/question/format/gift/format.php \qformat_gift::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/gift/format.php, line 531

Class

qformat_gift
@package questionbank @subpackage importexport

Code

function writequestion($question) {

  // turns question into string
  // question reflects database fields for general question and specific to type
  global $QTYPES;

  // initial string;
  $expout = "";

  // add comment
  $expout .= "// question: {$question->id}  name: {$question->name} \n";

  // get  question text format
  $textformat = $question->questiontextformat;
  $tfname = "";
  if ($textformat != FORMAT_MOODLE) {
    $tfname = text_format_name((int) $textformat);
    $tfname = "[{$tfname}]";
  }

  // output depends on question type
  switch ($question->qtype) {
    case 'category':

      // not a real question, used to insert category switch
      $expout .= "\$CATEGORY: {$question->category}\n";
      break;
    case DESCRIPTION:
      $expout .= '::' . $this
        ->repchar($question->name) . '::';
      $expout .= $tfname;
      $expout .= $this
        ->repchar($question->questiontext, $textformat);
      break;
    case ESSAY:
      $expout .= '::' . $this
        ->repchar($question->name) . '::';
      $expout .= $tfname;
      $expout .= $this
        ->repchar($question->questiontext, $textformat);
      $expout .= "{}\n";
      break;
    case TRUEFALSE:
      $trueanswer = $question->options->answers[$question->options->trueanswer];
      $falseanswer = $question->options->answers[$question->options->falseanswer];
      if ($trueanswer->fraction == 1) {
        $answertext = 'TRUE';
        $right_feedback = $trueanswer->feedback;
        $wrong_feedback = $falseanswer->feedback;
      }
      else {
        $answertext = 'FALSE';
        $right_feedback = $falseanswer->feedback;
        $wrong_feedback = $trueanswer->feedback;
      }
      $wrong_feedback = $this
        ->repchar($wrong_feedback);
      $right_feedback = $this
        ->repchar($right_feedback);
      $expout .= "::" . $this
        ->repchar($question->name) . "::" . $tfname . $this
        ->repchar($question->questiontext, $textformat) . "{" . $this
        ->repchar($answertext);
      if ($wrong_feedback) {
        $expout .= "#" . $wrong_feedback;
      }
      else {
        if ($right_feedback) {
          $expout .= "#";
        }
      }
      if ($right_feedback) {
        $expout .= "#" . $right_feedback;
      }
      $expout .= "}\n";
      break;
    case MULTICHOICE:
      $expout .= "::" . $this
        ->repchar($question->name) . "::" . $tfname . $this
        ->repchar($question->questiontext, $textformat) . "{\n";
      foreach ($question->options->answers as $answer) {
        if ($answer->fraction == 1) {
          $answertext = '=';
        }
        elseif ($answer->fraction == 0) {
          $answertext = '~';
        }
        else {
          $export_weight = $answer->fraction * 100;
          $answertext = "~%{$export_weight}%";
        }
        $expout .= "\t" . $answertext . $this
          ->repchar($answer->answer);
        if ($answer->feedback != "") {
          $expout .= "#" . $this
            ->repchar($answer->feedback);
        }
        $expout .= "\n";
      }
      $expout .= "}\n";
      break;
    case SHORTANSWER:
      $expout .= "::" . $this
        ->repchar($question->name) . "::" . $tfname . $this
        ->repchar($question->questiontext, $textformat) . "{\n";
      foreach ($question->options->answers as $answer) {
        $weight = 100 * $answer->fraction;
        $expout .= "\t=%" . $weight . "%" . $this
          ->repchar($answer->answer) . "#" . $this
          ->repchar($answer->feedback) . "\n";
      }
      $expout .= "}\n";
      break;
    case NUMERICAL:
      $expout .= "::" . $this
        ->repchar($question->name) . "::" . $tfname . $this
        ->repchar($question->questiontext, $textformat) . "{#\n";
      foreach ($question->options->answers as $answer) {
        if ($answer->answer != '') {
          $percentage = '';
          if ($answer->fraction < 1) {
            $pval = $answer->fraction * 100;
            $percentage = "%{$pval}%";
          }
          $expout .= "\t={$percentage}" . $answer->answer . ":" . (double) $answer->tolerance . "#" . $this
            ->repchar($answer->feedback) . "\n";
        }
        else {
          $expout .= "\t~#" . $this
            ->repchar($answer->feedback) . "\n";
        }
      }
      $expout .= "}\n";
      break;
    case MATCH:
      $expout .= "::" . $this
        ->repchar($question->name) . "::" . $tfname . $this
        ->repchar($question->questiontext, $textformat) . "{\n";
      foreach ($question->options->subquestions as $subquestion) {
        $expout .= "\t=" . $this
          ->repchar($subquestion->questiontext) . " -> " . $this
          ->repchar($subquestion->answertext) . "\n";
      }
      $expout .= "}\n";
      break;
    default:

      // check for plugins
      if ($out = $this
        ->try_exporting_using_qtypes($question->qtype, $question)) {
        $expout .= $out;
      }
      else {
        $expout .= "// {$question->qtype} is not supported by the GIFT format\n";
        $menuname = $QTYPES[$question->qtype]
          ->menu_name();
        notify(get_string('nohandler', 'qformat_gift', $menuname));
      }
  }

  // add empty line to delimit questions
  $expout .= "\n";
  return $expout;
}