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; }
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; }
/** * 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; }