public function import_from_xml($data, $question, qformat_xml $format, $extra = null)
 {
     if (!isset($data['@']['type']) || $data['@']['type'] != 'gapselect') {
         return false;
     }
     $question = $format->import_headers($data);
     $question->qtype = 'gapselect';
     $question->shuffleanswers = $format->trans_single($format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
     if (!empty($data['#']['selectoption'])) {
         // Modern XML format.
         $selectoptions = $data['#']['selectoption'];
         $question->answer = array();
         $question->selectgroup = array();
         foreach ($data['#']['selectoption'] as $selectoptionxml) {
             $question->choices[] = array('answer' => $format->getpath($selectoptionxml, array('#', 'text', 0, '#'), '', true), 'choicegroup' => $format->getpath($selectoptionxml, array('#', 'group', 0, '#'), 1));
         }
     } else {
         // Legacy format containing PHP serialisation.
         foreach ($data['#']['answer'] as $answerxml) {
             $ans = $format->import_answer($answerxml);
             $question->choices[] = array('answer' => $ans->answer, 'choicegroup' => $ans->feedback);
         }
     }
     $format->import_combined_feedback($question, $data, true);
     $format->import_hints($question, $data, true, false, $format->get_format($question->questiontextformat));
     return $question;
 }
示例#2
0
 public function import_from_xml($data, $question, qformat_xml $format, $extra = null)
 {
     if (!isset($data['@']['type']) || $data['@']['type'] != 'ddwtos') {
         return false;
     }
     $question = $format->import_headers($data);
     $question->qtype = 'ddwtos';
     $question->shuffleanswers = $format->trans_single($format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
     if (!empty($data['#']['dragbox'])) {
         // Modern XML format.
         $dragboxes = $data['#']['dragbox'];
         $question->answer = array();
         $question->draggroup = array();
         $question->infinite = array();
         foreach ($data['#']['dragbox'] as $dragboxxml) {
             $question->choices[] = array('answer' => $format->getpath($dragboxxml, array('#', 'text', 0, '#'), '', true), 'choicegroup' => $format->getpath($dragboxxml, array('#', 'group', 0, '#'), 1), 'infinite' => array_key_exists('infinite', $dragboxxml['#']));
         }
     } else {
         // Legacy format containing PHP serialisation.
         foreach ($data['#']['answer'] as $answerxml) {
             $ans = $format->import_answer($answerxml);
             $options = unserialize(stripslashes($ans->feedback['text']));
             $question->choices[] = array('answer' => $ans->answer, 'choicegroup' => $options->draggroup, 'infinite' => $options->infinite);
         }
     }
     $format->import_combined_feedback($question, $data, true);
     $format->import_hints($question, $data, true, false, $format->get_format($question->questiontextformat));
     return $question;
 }
示例#3
0
    public function test_import_no_hints_no_error() {
        $xml = <<<END
<question>
</question>
END;

        $questionxml = xmlize($xml);
        $qo = new stdClass();

        $importer = new qformat_xml();
        $importer->import_hints($qo, $questionxml['question'], 'html');

        $this->assertFalse(isset($qo->hint));
    }
示例#4
0
 /**
  * Imports the question from Moodle XML format.
  *
  * @param array $xml structure containing the XML data
  * @param object $fromform question object to fill: ignored by this function (assumed to be null)
  * @param qformat_xml $format format class exporting the question
  * @param object $extra extra information (not required for importing this question in this format)
  * @return object question object
  */
 public function import_from_xml($xml, $fromform, qformat_xml $format, $extra = null)
 {
     // Return if data type is not our own one.
     if (!isset($xml['@']['type']) || $xml['@']['type'] != $this->name()) {
         return false;
     }
     // Import the common question headers and set the corresponding field.
     $fromform = $format->import_headers($xml);
     $fromform->qtype = $this->name();
     $format->import_combined_feedback($fromform, $xml, true);
     $format->import_hints($fromform, $xml, true);
     $extras = $this->extra_question_fields();
     array_shift($extras);
     foreach ($extras as $extra) {
         $fromform->{$extra} = $format->getpath($xml, array('#', $extra, 0, '#'), '', true);
     }
     return $fromform;
 }
 /**
  * Provide import functionality for xml format
  * @param data mixed the segment of data containing the question
  * @param question object question object processed (so far) by standard import code
  * @param format object the format object so that helper methods can be used (in particular error())
  * @param extra mixed any additional format specific data that may be passed by the format (see format code for info)
  * @return object question object suitable for save_options() call or false if cannot handle
  */
 public function import_from_xml($data, $question, qformat_xml $format, $extra = null)
 {
     // Check question is for us.
     if (!isset($data['@']['type']) || $data['@']['type'] != 'multichoiceset') {
         return false;
     }
     $question = $format->import_headers($data);
     $question->qtype = 'multichoiceset';
     $question->shuffleanswers = $format->trans_single($format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
     $question->answernumbering = $format->getpath($data, array('#', 'answernumbering', 0, '#'), 'abc');
     $question->correctfeedback = array();
     $question->correctfeedback['text'] = $format->getpath($data, array('#', 'correctfeedback', 0, '#', 'text', 0, '#'), '', true);
     $question->correctfeedback['format'] = $format->trans_format($format->getpath($data, array('#', 'correctfeedback', 0, '@', 'format'), $format->get_format($question->questiontextformat)));
     $question->correctfeedback['files'] = array();
     // Restore files in correctfeedback.
     $files = $format->getpath($data, array('#', 'correctfeedback', 0, '#', 'file'), array(), false);
     foreach ($files as $file) {
         $filesdata = new stdclass();
         $filesdata->content = $file['#'];
         $filesdata->encoding = $file['@']['encoding'];
         $filesdata->name = $file['@']['name'];
         $question->correctfeedback['files'][] = $filesdata;
     }
     $question->incorrectfeedback = array();
     $question->incorrectfeedback['text'] = $format->getpath($data, array('#', 'incorrectfeedback', 0, '#', 'text', 0, '#'), '', true);
     $question->incorrectfeedback['format'] = $format->trans_format($format->getpath($data, array('#', 'incorrectfeedback', 0, '@', 'format'), $format->get_format($question->questiontextformat)));
     $question->incorrectfeedback['files'] = array();
     // Restore files in incorrectfeedback.
     $files = $format->getpath($data, array('#', 'incorrectfeedback', 0, '#', 'file'), array(), false);
     foreach ($files as $file) {
         $filesdata = new stdclass();
         $filesdata->content = $file['#'];
         $filesdata->encoding = $file['@']['encoding'];
         $filesdata->name = $file['@']['name'];
         $question->incorrectfeedback['files'][] = $filesdata;
     }
     $question->shownumcorrect = array_key_exists('shownumcorrect', $data['#']);
     // Run through the answers.
     $answers = $data['#']['answer'];
     foreach ($answers as $answer) {
         $ans = $format->import_answer($answer, true, $format->get_format($question->questiontextformat));
         $question->answer[] = $ans->answer;
         $question->correctanswer[] = !empty($ans->fraction);
         $question->feedback[] = $ans->feedback;
         // Backwards compatibility.
         if (array_key_exists('correctanswer', $answer['#'])) {
             $key = end(array_keys($question->correctanswer));
             $question->correctanswer[$key] = $format->getpath($answer, array('#', 'correctanswer', 0, '#'), 0);
         }
     }
     $format->import_hints($question, $data, true, true, $format->get_format($question->questiontextformat));
     // Get extra choicefeedback setting from each hint.
     if (!empty($question->hintoptions)) {
         foreach ($question->hintoptions as $key => $options) {
             $question->hintshowchoicefeedback[$key] = !empty($options);
         }
     }
     return $question;
 }
示例#6
0
 public function import_from_xml($data, $question, qformat_xml $format, $extra = null)
 {
     if (!isset($data['@']['type']) || $data['@']['type'] != 'ddimageortext') {
         return false;
     }
     $question = $format->import_headers($data);
     $question->qtype = 'ddimageortext';
     $question->shuffleanswers = array_key_exists('shuffleanswers', $format->getpath($data, array('#'), array()));
     $filexml = $format->getpath($data, array('#', 'file'), array());
     $question->bgimage = $format->import_files_as_draft($filexml);
     $drags = $data['#']['drag'];
     $question->drags = array();
     foreach ($drags as $dragxml) {
         $dragno = $format->getpath($dragxml, array('#', 'no', 0, '#'), 0);
         $dragindex = $dragno - 1;
         $question->drags[$dragindex] = array();
         $question->draglabel[$dragindex] = $format->getpath($dragxml, array('#', 'text', 0, '#'), '', true);
         $question->drags[$dragindex]['infinite'] = array_key_exists('infinite', $dragxml['#']);
         $question->drags[$dragindex]['draggroup'] = $format->getpath($dragxml, array('#', 'draggroup', 0, '#'), 1);
         $filexml = $format->getpath($dragxml, array('#', 'file'), array());
         $question->dragitem[$dragindex] = $format->import_files_as_draft($filexml);
         if (count($filexml)) {
             $question->drags[$dragindex]['dragitemtype'] = 'image';
         } else {
             $question->drags[$dragindex]['dragitemtype'] = 'word';
         }
     }
     $drops = $data['#']['drop'];
     $question->drops = array();
     foreach ($drops as $dropxml) {
         $dropno = $format->getpath($dropxml, array('#', 'no', 0, '#'), 0);
         $dropindex = $dropno - 1;
         $question->drops[$dropindex] = array();
         $question->drops[$dropindex]['choice'] = $format->getpath($dropxml, array('#', 'choice', 0, '#'), 0);
         $question->drops[$dropindex]['droplabel'] = $format->getpath($dropxml, array('#', 'text', 0, '#'), '', true);
         $question->drops[$dropindex]['xleft'] = $format->getpath($dropxml, array('#', 'xleft', 0, '#'), '');
         $question->drops[$dropindex]['ytop'] = $format->getpath($dropxml, array('#', 'ytop', 0, '#'), '');
     }
     $format->import_combined_feedback($question, $data, true);
     $format->import_hints($question, $data, true, false, $format->get_format($question->questiontextformat));
     return $question;
 }
 /**
  * Provide import functionality for xml format
  * @param $xml mixed the segment of data containing the question
  * @param $fromform object question object processed (so far) by standard import code
  * @param $format object the format object so that helper methods can be used (in particular error() )
  * @param $extra mixed any additional format specific data that may be passed by the format (see format code for info)
  * @return object question object suitable for save_options() call or false if cannot handle
  */
 public function import_from_xml($xml, $fromform, qformat_xml $format, $extra = null)
 {
     // Check question is for us.
     $qtype = $xml['@']['type'];
     if ($qtype == 'ddmatch') {
         $fromform = $format->import_headers($xml);
         // Header parts particular to ddmatch qtype.
         $fromform->qtype = $this->name();
         $fromform->shuffleanswers = $format->getpath($xml, array('#', 'shuffleanswers', 0, '#'), 1);
         // Run through subquestions.
         $fromform->subquestions = array();
         $fromform->subanswers = array();
         foreach ($xml['#']['subquestion'] as $subqxml) {
             $fromform->subquestions[] = $format->import_text_with_files($subqxml, array(), '', $format->get_format($fromform->questiontextformat));
             $answers = $format->getpath($subqxml, array('#', 'answer', 0), array());
             $fromform->subanswers[] = $format->import_text_with_files($answers, array(), '', $format->get_format($fromform->questiontextformat));
         }
         $format->import_combined_feedback($fromform, $xml, true);
         $format->import_hints($fromform, $xml, true);
         return $fromform;
     } else {
         return false;
     }
 }
示例#8
0
 public function import_from_xml($data, $question, qformat_xml $format, $extra = null)
 {
     if (!isset($data['@']['type']) || $data['@']['type'] != 'ddmarker') {
         return false;
     }
     $question = $format->import_headers($data);
     $question->qtype = 'ddmarker';
     $question->shuffleanswers = array_key_exists('shuffleanswers', $format->getpath($data, array('#'), array()));
     $question->showmisplaced = array_key_exists('showmisplaced', $format->getpath($data, array('#'), array()));
     $filexml = $format->getpath($data, array('#', 'file'), array());
     $question->bgimage = $format->import_files_as_draft($filexml);
     $drags = $data['#']['drag'];
     $question->drags = array();
     foreach ($drags as $dragxml) {
         $dragno = $format->getpath($dragxml, array('#', 'no', 0, '#'), 0);
         $dragindex = $dragno - 1;
         $question->drags[$dragindex] = array();
         $question->drags[$dragindex]['label'] = $format->getpath($dragxml, array('#', 'text', 0, '#'), '', true);
         if (array_key_exists('infinite', $dragxml['#'])) {
             $question->drags[$dragindex]['noofdrags'] = 0;
             // Means infinite in the form.
         } else {
             // Defaults to 1 if 'noofdrags' not set.
             $question->drags[$dragindex]['noofdrags'] = $format->getpath($dragxml, array('#', 'noofdrags', 0, '#'), 1);
         }
     }
     $drops = $data['#']['drop'];
     $question->drops = array();
     foreach ($drops as $dropxml) {
         $dropno = $format->getpath($dropxml, array('#', 'no', 0, '#'), 0);
         $dropindex = $dropno - 1;
         $question->drops[$dropindex] = array();
         $question->drops[$dropindex]['choice'] = $format->getpath($dropxml, array('#', 'choice', 0, '#'), 0);
         $question->drops[$dropindex]['shape'] = $format->getpath($dropxml, array('#', 'shape', 0, '#'), '');
         $question->drops[$dropindex]['coords'] = $format->getpath($dropxml, array('#', 'coords', 0, '#'), '');
     }
     $format->import_combined_feedback($question, $data, true);
     $format->import_hints($question, $data, true, true, $format->get_format($question->questiontextformat));
     return $question;
 }
 public function import_from_xml($data, $question, qformat_xml $format, $extra = null)
 {
     global $CFG;
     if (!array_key_exists('@', $data)) {
         return false;
     }
     if (!array_key_exists('type', $data['@'])) {
         return false;
     }
     if ($data['@']['type'] == 'jme') {
         // Get common parts.
         $question = $format->import_headers($data);
         // Header parts particular to jme.
         $question->qtype = 'jme';
         $question->jmeoptions = $format->getpath($data, array('#', 'jmeoptions', 0, '#'), $CFG->qtype_jme_options);
         $question->width = $format->getpath($data, array('#', 'width', 0, '#'), QTYPE_JME_APPLET_WIDTH);
         $question->height = $format->getpath($data, array('#', 'height', 0, '#'), QTYPE_JME_APPLET_HEIGHT);
         // Run through the answers.
         $answers = $data['#']['answer'];
         $anscount = 0;
         foreach ($answers as $answer) {
             $ans = $format->import_answer($answer);
             $question->answer[$anscount] = $ans->answer['text'];
             $question->fraction[$anscount] = $ans->fraction;
             $question->feedback[$anscount] = $ans->feedback;
             ++$anscount;
         }
         $format->import_hints($question, $data);
         return $question;
     }
     return false;
 }