/** * 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; }
/** * Helper method used by {@link export_to_xml()}. * @param array $xml the XML to extract the data from. * @param string $field the name of the sub-tag in the XML to load the data from. * @param qformat_xml $format the importer/exporter object. * @param int $defaultformat Dfeault text format, if it is not given in the file. * @return array with fields text, format and files. */ protected function import_xml_text($xml, $field, qformat_xml $format, $defaultformat) { $text = array(); $text['text'] = $format->getpath($xml, array('#', $field, 0, '#', 'text', 0, '#'), '', true); $text['format'] = $format->trans_format($format->getpath($xml, array('#', $field, 0, '@', 'format'), $format->get_format($defaultformat))); $text['files'] = $format->import_files($format->getpath($xml, array('#', $field, 0, '#', 'file'), array(), false)); return $text; }
public function import_from_xml($data, $question, qformat_xml $format, $extra = null) { global $CFG; $question_type = "poodllrecording"; //omit table name $qo = $format->import_headers($data); $qo->qtype = $question_type; $q = $data; $qo->responseformat = $format->getpath($q, array('#', 'responseformat', 0, '#'), 'picture'); $qo->responsefieldlines = $format->getpath($q, array('#', 'responsefieldlines', 0, '#'), 15); $qo->attachments = $format->getpath($q, array('#', 'attachments', 0, '#'), 0); //older versions handled files diff. SeeM DL-39-57 if ($CFG->version < 2013051400) { $qo->graderinfo['text'] = $format->getpath($q, array('#', 'graderinfo', 0, '#', 'text', 0, '#'), '', true); $qo->graderinfo['format'] = $format->trans_format($format->getpath($q, array('#', 'graderinfo', 0, '@', 'format'), $format->get_format($qo->questiontextformat))); $qo->graderinfo['files'] = $format->import_files($format->getpath($q, array('#', 'graderinfo', '0', '#', 'file'), array())); $qo->backimage = $format->import_files($format->getpath($q, array('#', 'backimage', '0', '#', 'file'), array())); } else { $qo->graderinfo = $format->import_text_with_files($q, array('#', 'graderinfo', 0), '', $qo->questiontextformat); $qo->backimage = $format->import_files_as_draft($format->getpath($q, array('#', 'backimage', '0', '#', 'file'), array())); } $qo->boardsize = $format->getpath($q, array('#', 'boardsize', 0, '#'), '320x320'); $qo->timelimit = $format->getpath($q, array('#', 'timelimit', 0, '#'), 0); return $qo; }
public function import_from_xml($data, $question, qformat_xml $format, $extra = null) { $question_type = $format->getpath($data, array('@', 'type'), ''); if ($question_type != 'ordering') { return false; } $newquestion = $format->import_headers($data); $newquestion->qtype = $question_type; // fix empty or long question name $newquestion->name = $this->fix_questionname($newquestion->name, $newquestion->questiontext); // extra fields fields fields - "selecttype" and "selectcount" // (these fields used to be called "logical" and "studentsee") if (isset($data['#']['selecttype'])) { $selecttype = 'selecttype'; $selectcount = 'selectcount'; } else { $selecttype = 'logical'; $selectcount = 'studentsee'; } $selecttype = $format->getpath($data, array('#', $selecttype, 0, '#'), 'RANDOM'); $selectcount = $format->getpath($data, array('#', $selectcount, 0, '#'), 6); $this->set_count_and_type($newquestion, $selectcount, $selecttype); $newquestion->answer = array(); $newquestion->answerformat = array(); $newquestion->fraction = array(); $newquestion->feedback = array(); $newquestion->feedbackformat = array(); $i = 0; while ($answer = $format->getpath($data, array('#', 'answer', $i), '')) { if ($text = $format->getpath($answer, array('#', 'text', 0, '#'), '')) { $newquestion->answer[] = $text; $answerformat = $format->getpath($answer, array('@', 'format'), 'moodle_auto_format'); $newquestion->answerformat[] = $format->trans_format($answerformat); $newquestion->fraction[] = 1; // will be reset later in save_question_options() $newquestion->feedback[] = $format->getpath($answer, array('#', 'feedback', 0, '#', 'text', 0, '#'), ''); $feedbackformat = $format->getpath($answer, array('#', 'format', 0, '@', 'format'), 'moodle_auto_format'); $newquestion->feedbackformat[] = $format->trans_format($feedbackformat); } $i++; } return $newquestion; }
public function import_from_xml($data, $question, qformat_xml $format, $extra = null) { $question_type = "poodllrecording"; //omit table name $qo = $format->import_headers($data); $qo->qtype = $question_type; $q = $data; $qo->responseformat = $format->getpath($q, array('#', 'responseformat', 0, '#'), 'picture'); $qo->responsefieldlines = $format->getpath($q, array('#', 'responsefieldlines', 0, '#'), 15); $qo->attachments = $format->getpath($q, array('#', 'attachments', 0, '#'), 0); $qo->graderinfo['text'] = $format->getpath($q, array('#', 'graderinfo', 0, '#', 'text', 0, '#'), '', true); $qo->graderinfo['format'] = $format->trans_format($format->getpath($q, array('#', 'graderinfo', 0, '@', 'format'), $format->get_format($qo->questiontextformat))); $qo->graderinfo['files'] = $format->import_files($format->getpath($q, array('#', 'graderinfo', '0', '#', 'file'), array())); $qo->backimage['files'] = $format->import_files($format->getpath($q, array('#', 'backimage', '0', '#', 'file'), array())); $qo->boardsize = $format->getpath($q, array('#', 'boardsize', 0, '#'), '320x320'); $qo->timelimit = $format->getpath($q, array('#', 'timelimit', 0, '#'), 0); return $qo; }