Index: question/type/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/questiontype.php,v retrieving revision 1.74.2.25 diff -u -r1.74.2.25 questiontype.php --- question/type/questiontype.php 1 Apr 2009 15:48:57 -0000 1.74.2.25 +++ question/type/questiontype.php 30 Sep 2009 11:37:57 -0000 @@ -1699,6 +1699,88 @@ return $state->answer; } +/// IMPORT/EXPORT FUNCTIONS ///////////////// + + /* + * Imports question from the Moodle XML format + * + * Imports question using information from extra_question_fields function + * If some of you fields contains id's you'll need to reimplement this + */ + function import_from_xml($data, $question, $format, $extra=null) { + $question_type = $data['@']['type']; + if ($question_type != $this->name()) { + return false; + } + + $extraquestionfields = $this->extra_question_fields(); + if (!is_array($extraquestionfields)) { + return false; + } + + //omit table name + array_shift($extraquestionfields); + $qo = $format->import_headers($data); + $qo->qtype = $question_type; + + foreach ($extraquestionfields as $field) { + $qo->$field = $format->getpath($data, array('#',$field,0,'#'), $qo->$field); + } + + // run through the answers + $answers = $data['#']['answer']; + $a_count = 0; + $extraasnwersfields = $this->extra_answer_fields(); + if (is_array($extraasnwersfields)) { + //TODO import the answers, with any extra data. + } else { + foreach ($answers as $answer) { + $ans = $format->import_answer($answer); + $qo->answer[$a_count] = $ans->answer; + $qo->fraction[$a_count] = $ans->fraction; + $qo->feedback[$a_count] = $ans->feedback; + ++$a_count; + } + } + return $qo; + } + + /* + * Export question to the Moodle XML format + * + * Export question using information from extra_question_fields function + * If some of you fields contains id's you'll need to reimplement this + */ + function export_to_xml($question, $format, $extra=null) { + $extraquestionfields = $this->extra_question_fields(); + if (!is_array($extraquestionfields)) { + return false; + } + + //omit table name + array_shift($extraquestionfields); + $expout=''; + foreach ($extraquestionfields as $field) { + $expout .= " <$field>{$question->options->$field}\n"; + } + + $extraasnwersfields = $this->extra_answer_fields(); + if (is_array($extraasnwersfields)) { + //TODO export answers with any extra data + } else { + foreach ($question->options->answers as $answer) { + $percent = 100 * $answer->fraction; + $expout .= " \n"; + $expout .= $format->writetext($answer->answer, 3, false); + $expout .= " \n"; + $expout .= $format->writetext($answer->feedback, 4, false); + $expout .= " \n"; + $expout .= " \n"; + } + } + return $expout; + } + /** * Abstract function implemented by each question type. It runs all the code * required to set up and save a question of any type for testing purposes. Index: question/format.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/format.php,v retrieving revision 1.35.2.12 diff -u -r1.35.2.12 format.php --- question/format.php 18 Mar 2009 11:16:59 -0000 1.35.2.12 +++ question/format.php 30 Sep 2009 11:37:56 -0000 @@ -181,15 +181,27 @@ * @param data mixed The segment of data containing the question * @param question object processed (so far) by standard import code if appropriate * @param extra mixed any additional format specific data that may be passed by the format + * @param qtypehint hint about a question type from format * @return object question object suitable for save_options() or false if cannot handle */ - function try_importing_using_qtypes( $data, $question=null, $extra=null ) { + function try_importing_using_qtypes( $data, $question=null, $extra=null, $qtypehint='') { global $QTYPES; // work out what format we are using - $formatname = substr( get_class( $this ), strlen('qformat_')); + $formatname = substr(get_class($this), strlen('qformat_')); $methodname = "import_from_$formatname"; + //first try importing using a hint from format + if (!empty($qtypehint)) { + $qtype = $QTYPES[$qtypehint]; + if (is_object($qtype) && method_exists($qtype, $methodname)) { + $question = $qtype->$methodname($data, $question, $this, $extra); + if ($question) { + return $question; + } + } + } + // loop through installed questiontypes checking for // function to handle this question foreach ($QTYPES as $qtype) { Index: question/format/xml/format.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/format/xml/format.php,v retrieving revision 1.41.2.10 diff -u -r1.41.2.10 format.php --- question/format/xml/format.php 4 Sep 2009 02:10:28 -0000 1.41.2.10 +++ question/format/xml/format.php 30 Sep 2009 11:37:57 -0000 @@ -568,7 +568,7 @@ // try for plugin support // no default question, as the plugin can call // import_headers() itself if it wants to - if (!$qo=$this->try_importing_using_qtypes( $question )) { + if (!$qo = $this->try_importing_using_qtypes( $question, null, null, $question_type)) { $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type ); $this->error( $notsupported ); $qo = null;