Index: 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 --- format.php 18 Mar 2009 11:16:59 -0000 1.35.2.12 +++ format.php 30 Aug 2009 21:35:20 -0000 @@ -181,15 +181,24 @@ * @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_')); $methodname = "import_from_$formatname"; + //first try importing using a hint from format + $qtype = $QTYPES[$qtypehint]; + if(is_object($qtype) && method_exists($qtype, $methodname)) { + if ($question = $qtype->$methodname( $data, $question, $this, $extra )) { + return $question; + } + } + // loop through installed questiontypes checking for // function to handle this question foreach ($QTYPES as $qtype) { Index: format/xml/format.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/format/xml/format.php,v retrieving revision 1.41.2.9 diff -u -r1.41.2.9 format.php --- format/xml/format.php 16 Mar 2009 14:20:24 -0000 1.41.2.9 +++ format/xml/format.php 31 Aug 2009 21:23: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; Index: 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 --- type/questiontype.php 1 Apr 2009 15:48:57 -0000 1.74.2.25 +++ type/questiontype.php 31 Aug 2009 21:41:08 -0000 @@ -1699,6 +1699,89 @@ 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']; + //ECHO 'import_from_xml is called for '.$this->name().'with qtype '.$question_type.'\n'; + if($question_type == $this->name()) { + $extraquestionfields = $this->extra_question_fields(); + if (is_array($extraquestionfields)) { + //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; + } else { + return false; + } + } else { + return false; + } + } + + /* + * 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)) { + //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; + } else { + return false; + } + } + /** * 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.