You are here

function qformat_xml::writequestion in Quiz 6.5

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

Turns question into an xml segment

Parameters

array question question array:

Return value

string xml segment

Overrides qformat_default::writequestion

File

includes/moodle/question/format/xml/format.php, line 768

Class

qformat_xml

Code

function writequestion($question) {
  global $CFG, $QTYPES;

  // initial string;
  $expout = "";

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

  // check question type
  if (!($question_type = $this
    ->get_qtype($question->qtype))) {

    // must be a plugin then, so just accept the name supplied
    $question_type = $question->qtype;
  }

  // add opening tag
  // generates specific header for Cloze and category type question
  if ($question->qtype == 'category') {
    $categorypath = $this
      ->writetext($question->category);
    $expout .= "  <question type=\"category\">\n";
    $expout .= "    <category>\n";
    $expout .= "        {$categorypath}\n";
    $expout .= "    </category>\n";
    $expout .= "  </question>\n";
    return $expout;
  }
  elseif ($question->qtype != MULTIANSWER) {

    // for all question types except Close
    $name_text = $this
      ->writetext($question->name);
    $qtformat = $this
      ->get_format($question->questiontextformat);
    $question_text = $this
      ->writetext($question->questiontext);
    $generalfeedback = $this
      ->writetext($question->generalfeedback);
    $expout .= "  <question type=\"{$question_type}\">\n";
    $expout .= "    <name>{$name_text}</name>\n";
    $expout .= "    <questiontext format=\"{$qtformat}\">\n";
    $expout .= $question_text;
    $expout .= "    </questiontext>\n";
    $expout .= "    <image>{$question->image}</image>\n";
    $expout .= $this
      ->writeimage($question->image);
    $expout .= "    <generalfeedback>\n";
    $expout .= $generalfeedback;
    $expout .= "    </generalfeedback>\n";
    $expout .= "    <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
    $expout .= "    <penalty>{$question->penalty}</penalty>\n";
    $expout .= "    <hidden>{$question->hidden}</hidden>\n";
  }
  else {

    // for Cloze type only
    $name_text = $this
      ->writetext($question->name);
    $question_text = $this
      ->writetext($question->questiontext);
    $generalfeedback = $this
      ->writetext($question->generalfeedback);
    $expout .= "  <question type=\"{$question_type}\">\n";
    $expout .= "    <name>{$name_text}</name>\n";
    $expout .= "    <questiontext>\n";
    $expout .= $question_text;
    $expout .= "    </questiontext>\n";
    $expout .= "    <generalfeedback>\n";
    $expout .= $generalfeedback;
    $expout .= "    </generalfeedback>\n";
  }
  if (!empty($question->options->shuffleanswers)) {
    $expout .= "    <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
  }
  else {
    $expout .= "    <shuffleanswers>0</shuffleanswers>\n";
  }

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

      // not a qtype really - dummy used for category switching
      break;
    case TRUEFALSE:
      foreach ($question->options->answers as $answer) {
        $fraction_pc = round($answer->fraction * 100);
        if ($answer->id == $question->options->trueanswer) {
          $answertext = 'true';
        }
        else {
          $answertext = 'false';
        }
        $expout .= "    <answer fraction=\"{$fraction_pc}\">\n";
        $expout .= $this
          ->writetext($answertext, 3) . "\n";
        $expout .= "      <feedback>\n";
        $expout .= $this
          ->writetext($answer->feedback, 4, false);
        $expout .= "      </feedback>\n";
        $expout .= "    </answer>\n";
      }
      break;
    case MULTICHOICE:
      $expout .= "    <single>" . $this
        ->get_single($question->options->single) . "</single>\n";
      $expout .= "    <shuffleanswers>" . $this
        ->get_single($question->options->shuffleanswers) . "</shuffleanswers>\n";
      $expout .= "    <correctfeedback>" . $this
        ->writetext($question->options->correctfeedback, 3) . "</correctfeedback>\n";
      $expout .= "    <partiallycorrectfeedback>" . $this
        ->writetext($question->options->partiallycorrectfeedback, 3) . "</partiallycorrectfeedback>\n";
      $expout .= "    <incorrectfeedback>" . $this
        ->writetext($question->options->incorrectfeedback, 3) . "</incorrectfeedback>\n";
      $expout .= "    <answernumbering>{$question->options->answernumbering}</answernumbering>\n";
      foreach ($question->options->answers as $answer) {
        $percent = $answer->fraction * 100;
        $expout .= "      <answer fraction=\"{$percent}\">\n";
        $expout .= $this
          ->writetext($answer->answer, 4, false);
        $expout .= "      <feedback>\n";
        $expout .= $this
          ->writetext($answer->feedback, 5, false);
        $expout .= "      </feedback>\n";
        $expout .= "    </answer>\n";
      }
      break;
    case SHORTANSWER:
      $expout .= "    <usecase>{$question->options->usecase}</usecase>\n ";
      foreach ($question->options->answers as $answer) {
        $percent = 100 * $answer->fraction;
        $expout .= "    <answer fraction=\"{$percent}\">\n";
        $expout .= $this
          ->writetext($answer->answer, 3, false);
        $expout .= "      <feedback>\n";
        $expout .= $this
          ->writetext($answer->feedback, 4, false);
        $expout .= "      </feedback>\n";
        $expout .= "    </answer>\n";
      }
      break;
    case NUMERICAL:
      foreach ($question->options->answers as $answer) {
        $tolerance = $answer->tolerance;
        $percent = 100 * $answer->fraction;
        $expout .= "<answer fraction=\"{$percent}\">\n";

        // <text> tags are an added feature, old filed won't have them
        $expout .= "    <text>{$answer->answer}</text>\n";
        $expout .= "    <tolerance>{$tolerance}</tolerance>\n";
        $expout .= "    <feedback>" . $this
          ->writetext($answer->feedback) . "</feedback>\n";

        // fraction tag is deprecated
        // $expout .= "    <fraction>{$answer->fraction}</fraction>\n";
        $expout .= "</answer>\n";
      }
      $units = $question->options->units;
      if (count($units)) {
        $expout .= "<units>\n";
        foreach ($units as $unit) {
          $expout .= "  <unit>\n";
          $expout .= "    <multiplier>{$unit->multiplier}</multiplier>\n";
          $expout .= "    <unit_name>{$unit->unit}</unit_name>\n";
          $expout .= "  </unit>\n";
        }
        $expout .= "</units>\n";
      }
      break;
    case MATCH:
      foreach ($question->options->subquestions as $subquestion) {
        $expout .= "<subquestion>\n";
        $expout .= $this
          ->writetext($subquestion->questiontext);
        $expout .= "<answer>" . $this
          ->writetext($subquestion->answertext) . "</answer>\n";
        $expout .= "</subquestion>\n";
      }
      break;
    case DESCRIPTION:

      // nothing more to do for this type
      break;
    case MULTIANSWER:
      $a_count = 1;
      foreach ($question->options->questions as $question) {
        $thispattern = addslashes("{#" . $a_count . "}");
        $thisreplace = $question->questiontext;
        $expout = ereg_replace($thispattern, $thisreplace, $expout);
        $a_count++;
      }
      break;
    case ESSAY:
      if (!empty($question->options->answers)) {
        foreach ($question->options->answers as $answer) {
          $percent = 100 * $answer->fraction;
          $expout .= "<answer fraction=\"{$percent}\">\n";
          $expout .= "    <feedback>" . $this
            ->writetext($answer->feedback) . "</feedback>\n";

          // fraction tag is deprecated
          // $expout .= "    <fraction>{$answer->fraction}</fraction>\n";
          $expout .= "</answer>\n";
        }
      }
      break;
    case CALCULATED:
      foreach ($question->options->answers as $answer) {
        $tolerance = $answer->tolerance;
        $tolerancetype = $answer->tolerancetype;
        $correctanswerlength = $answer->correctanswerlength;
        $correctanswerformat = $answer->correctanswerformat;
        $percent = 100 * $answer->fraction;
        $expout .= "<answer fraction=\"{$percent}\">\n";

        // "<text/>" tags are an added feature, old files won't have them
        $expout .= "    <text>{$answer->answer}</text>\n";
        $expout .= "    <tolerance>{$tolerance}</tolerance>\n";
        $expout .= "    <tolerancetype>{$tolerancetype}</tolerancetype>\n";
        $expout .= "    <correctanswerformat>{$correctanswerformat}</correctanswerformat>\n";
        $expout .= "    <correctanswerlength>{$correctanswerformat}</correctanswerlength>\n";
        $expout .= "    <feedback>" . $this
          ->writetext($answer->feedback) . "</feedback>\n";
        $expout .= "</answer>\n";
      }
      $units = $question->options->units;
      if (count($units)) {
        $expout .= "<units>\n";
        foreach ($units as $unit) {
          $expout .= "  <unit>\n";
          $expout .= "    <multiplier>{$unit->multiplier}</multiplier>\n";
          $expout .= "    <unit_name>{$unit->unit}</unit_name>\n";
          $expout .= "  </unit>\n";
        }
        $expout .= "</units>\n";
      }

      //echo "<pre> question calc";print_r($question);echo "</pre>";

      //First, we a new function to get all the   data itmes in the database

      //   $question_datasetdefs =$QTYPES['calculated']->get_datasets_for_export ($question);
      //    echo "<pre> question defs";print_r($question_datasetdefs);echo "</pre>";

      //If there are question_datasets
      if (isset($question->options->datasets) && count($question->options->datasets)) {

        // there should be
        $expout .= "<dataset_definitions>\n";
        foreach ($question->options->datasets as $def) {
          $expout .= "<dataset_definition>\n";
          $expout .= "    <status>" . $this
            ->writetext($def->status) . "</status>\n";
          $expout .= "    <name>" . $this
            ->writetext($def->name) . "</name>\n";
          $expout .= "    <type>calculated</type>\n";
          $expout .= "    <distribution>" . $this
            ->writetext($def->distribution) . "</distribution>\n";
          $expout .= "    <minimum>" . $this
            ->writetext($def->minimum) . "</minimum>\n";
          $expout .= "    <maximum>" . $this
            ->writetext($def->maximum) . "</maximum>\n";
          $expout .= "    <decimals>" . $this
            ->writetext($def->decimals) . "</decimals>\n";
          $expout .= "    <itemcount>{$def->itemcount}</itemcount>\n";
          if ($def->itemcount > 0) {
            $expout .= "    <dataset_items>\n";
            foreach ($def->items as $item) {
              $expout .= "        <dataset_item>\n";
              $expout .= "           <number>" . $item->itemnumber . "</number>\n";
              $expout .= "           <value>" . $item->value . "</value>\n";
              $expout .= "        </dataset_item>\n";
            }
            $expout .= "    </dataset_items>\n";
            $expout .= "    <number_of_items>" . $def->number_of_items . "</number_of_items>\n";
          }
          $expout .= "</dataset_definition>\n";
        }
        $expout .= "</dataset_definitions>\n";
      }
      break;
    default:

      // try support by optional plugin
      if (!($data = $this
        ->try_exporting_using_qtypes($question->qtype, $question))) {
        notify(get_string('unsupportedexport', 'qformat_xml', $QTYPES[$question->qtype]
          ->menu_name()));
      }
      $expout .= $data;
  }

  // close the question tag
  $expout .= "</question>\n";
  return $expout;
}