2009-11-04 11:57:52 +00:00
|
|
|
<?php
|
2010-08-05 13:11:27 +00:00
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2006-02-24 15:14:09 +00:00
|
|
|
//
|
2010-08-05 13:11:27 +00:00
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2006-02-24 15:14:09 +00:00
|
|
|
//
|
2010-08-05 13:11:27 +00:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Code for exporting questions as Moodle XML.
|
2010-08-05 13:11:27 +00:00
|
|
|
*
|
2011-02-23 13:38:16 +00:00
|
|
|
* @package qformat
|
2011-01-17 18:01:49 +00:00
|
|
|
* @subpackage xml
|
2010-08-05 13:11:27 +00:00
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
2011-02-23 13:38:16 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2010-08-05 13:11:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-02-23 15:03:35 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
require_once($CFG->libdir . '/xmlize.php');
|
2011-06-27 13:59:57 +01:00
|
|
|
if (!class_exists('qformat_default')) {
|
|
|
|
// This is ugly, but this class is also (ab)used by mod/lesson, which defines
|
|
|
|
// a different base class in mod/lesson/format.php. Thefore, we can only
|
|
|
|
// include the proper base class conditionally like this. (We have to include
|
|
|
|
// the base class like this, otherwise it breaks third-party question types.)
|
|
|
|
// This may be reviewd, and a better fix found one day.
|
|
|
|
require_once($CFG->dirroot . '/question/format.php');
|
|
|
|
}
|
2011-01-17 18:01:49 +00:00
|
|
|
|
|
|
|
|
2007-03-20 02:10:26 +00:00
|
|
|
/**
|
2010-08-05 13:11:27 +00:00
|
|
|
* Importer for Moodle XML question format.
|
|
|
|
*
|
|
|
|
* See http://docs.moodle.org/en/Moodle_XML_format for a description of the format.
|
|
|
|
*
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
2011-02-23 13:38:16 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2007-03-20 02:10:26 +00:00
|
|
|
*/
|
2006-03-01 07:36:09 +00:00
|
|
|
class qformat_xml extends qformat_default {
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2011-03-23 16:22:25 +00:00
|
|
|
public function provide_import() {
|
2006-02-24 15:14:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-23 16:22:25 +00:00
|
|
|
public function provide_export() {
|
2006-02-24 15:14:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-23 16:22:25 +00:00
|
|
|
public function mime_type() {
|
2010-11-08 15:51:10 +00:00
|
|
|
return 'application/xml';
|
|
|
|
}
|
|
|
|
|
2006-02-24 15:14:09 +00:00
|
|
|
// IMPORT FUNCTIONS START HERE
|
|
|
|
|
2009-09-30 13:38:36 +00:00
|
|
|
/**
|
2006-08-23 14:57:40 +00:00
|
|
|
* Translate human readable format name
|
|
|
|
* into internal Moodle code number
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param string name format name from xml file
|
2011-02-23 16:25:25 +00:00
|
|
|
* @return int Moodle format code
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
protected function trans_format($name) {
|
2009-09-30 13:38:36 +00:00
|
|
|
$name = trim($name);
|
|
|
|
|
2010-11-08 17:25:53 +00:00
|
|
|
if ($name == 'moodle_auto_format') {
|
2011-01-17 18:01:49 +00:00
|
|
|
return FORMAT_MOODLE;
|
2010-11-08 17:25:53 +00:00
|
|
|
} else if ($name == 'html') {
|
2011-01-17 18:01:49 +00:00
|
|
|
return FORMAT_HTML;
|
2010-11-08 17:25:53 +00:00
|
|
|
} else if ($name == 'plain_text') {
|
2011-01-17 18:01:49 +00:00
|
|
|
return FORMAT_PLAIN;
|
2010-11-08 17:25:53 +00:00
|
|
|
} else if ($name == 'wiki_like') {
|
2011-01-17 18:01:49 +00:00
|
|
|
return FORMAT_WIKI;
|
2010-11-08 17:25:53 +00:00
|
|
|
} else if ($name == 'markdown') {
|
2011-01-17 18:01:49 +00:00
|
|
|
return FORMAT_MARKDOWN;
|
2010-11-08 17:25:53 +00:00
|
|
|
} else {
|
2011-01-17 18:01:49 +00:00
|
|
|
return 0; // or maybe warning required
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2006-08-23 14:57:40 +00:00
|
|
|
* Translate human readable single answer option
|
|
|
|
* to internal code number
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param string name true/false
|
2011-02-23 16:25:25 +00:00
|
|
|
* @return int internal code number
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function trans_single($name) {
|
2006-10-03 14:01:38 +00:00
|
|
|
$name = trim($name);
|
|
|
|
if ($name == "false" || !$name) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2006-08-23 14:57:40 +00:00
|
|
|
* process text string from xml file
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param array $text bit of xml tree after ['text']
|
2011-01-17 18:01:49 +00:00
|
|
|
* @return string processed text.
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function import_text($text) {
|
2007-03-21 11:18:28 +00:00
|
|
|
// quick sanity check
|
|
|
|
if (empty($text)) {
|
|
|
|
return '';
|
|
|
|
}
|
2006-02-24 15:14:09 +00:00
|
|
|
$data = $text[0]['#'];
|
2008-06-09 16:53:30 +00:00
|
|
|
return trim($data);
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
|
2007-05-08 14:24:13 +00:00
|
|
|
/**
|
|
|
|
* return the value of a node, given a path to the node
|
|
|
|
* if it doesn't exist return the default value
|
|
|
|
* @param array xml data to read
|
2009-09-30 13:38:36 +00:00
|
|
|
* @param array path path to node expressed as array
|
|
|
|
* @param mixed default
|
2011-02-23 16:25:25 +00:00
|
|
|
* @param bool istext process as text
|
2007-05-08 14:24:13 +00:00
|
|
|
* @param string error if set value must exist, return false and issue message if not
|
|
|
|
* @return mixed value
|
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function getpath($xml, $path, $default, $istext=false, $error='') {
|
2007-05-08 14:24:13 +00:00
|
|
|
foreach ($path as $index) {
|
2007-08-10 07:17:46 +00:00
|
|
|
if (!isset($xml[$index])) {
|
2007-05-08 14:24:13 +00:00
|
|
|
if (!empty($error)) {
|
2011-01-17 18:01:49 +00:00
|
|
|
$this->error($error);
|
2007-05-08 14:24:13 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
}
|
2011-01-17 18:01:49 +00:00
|
|
|
|
|
|
|
$xml = $xml[$index];
|
2007-05-08 14:24:13 +00:00
|
|
|
}
|
2011-01-17 18:01:49 +00:00
|
|
|
|
2007-05-08 14:24:13 +00:00
|
|
|
if ($istext) {
|
2008-02-12 11:57:39 +00:00
|
|
|
if (!is_string($xml)) {
|
2011-01-17 18:01:49 +00:00
|
|
|
$this->error(get_string('invalidxml', 'qformat_xml'));
|
2008-02-12 11:57:39 +00:00
|
|
|
}
|
2010-11-05 06:34:00 +00:00
|
|
|
$xml = trim($xml);
|
2007-05-08 14:24:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $xml;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2006-08-23 14:57:40 +00:00
|
|
|
* import parts of question common to all types
|
2008-07-07 11:54:39 +00:00
|
|
|
* @param $question array question question array from xml tree
|
2006-08-24 10:16:54 +00:00
|
|
|
* @return object question object
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function import_headers($question) {
|
2010-11-19 12:33:48 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2007-05-08 14:24:13 +00:00
|
|
|
// get some error strings
|
2011-06-14 17:06:55 +01:00
|
|
|
$error_noname = get_string('xmlimportnoname', 'qformat_xml');
|
|
|
|
$error_noquestion = get_string('xmlimportnoquestion', 'qformat_xml');
|
2007-05-08 14:24:13 +00:00
|
|
|
|
2006-02-24 15:14:09 +00:00
|
|
|
// this routine initialises the question object
|
2007-01-16 12:12:26 +00:00
|
|
|
$qo = $this->defaultquestion();
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
// Question name
|
|
|
|
$qo->name = $this->getpath($question,
|
|
|
|
array('#', 'name', 0, '#', 'text', 0, '#'), '', true,
|
2011-02-23 18:53:50 +00:00
|
|
|
get_string('xmlimportnoname', 'qformat_xml'));
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo->questiontext = $this->getpath($question,
|
|
|
|
array('#', 'questiontext', 0, '#', 'text', 0, '#'), '', true);
|
|
|
|
$qo->questiontextformat = $this->trans_format($this->getpath(
|
|
|
|
$question, array('#', 'questiontext', 0, '@', 'format'), ''));
|
2010-11-05 06:34:00 +00:00
|
|
|
|
2011-03-23 14:27:22 +00:00
|
|
|
$qo->questiontextfiles = $this->import_files($this->getpath($question,
|
|
|
|
array('#', 'questiontext', 0, '#', 'file'), array(), false));
|
2010-11-05 06:34:00 +00:00
|
|
|
|
2010-11-19 14:24:18 +00:00
|
|
|
// Backwards compatibility, deal with the old image tag.
|
|
|
|
$filedata = $this->getpath($question, array('#', 'image_base64', '0', '#'), null, false);
|
|
|
|
$filename = $this->getpath($question, array('#', 'image', '0', '#'), null, false);
|
|
|
|
if ($filedata && $filename) {
|
2011-02-21 18:10:19 +00:00
|
|
|
$data = new stdClass();
|
2010-11-19 14:24:18 +00:00
|
|
|
$data->content = $filedata;
|
|
|
|
$data->encoding = 'base64';
|
|
|
|
$data->name = $filename;
|
|
|
|
$qo->questiontextfiles[] = $data;
|
|
|
|
$qo->questiontext .= ' <img src="@@PLUGINFILE@@/' . $filename . '" />';
|
|
|
|
}
|
|
|
|
|
2010-11-05 06:34:00 +00:00
|
|
|
// restore files in generalfeedback
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo->generalfeedback = $this->getpath($question,
|
|
|
|
array('#', 'generalfeedback', 0, '#', 'text', 0, '#'), $qo->generalfeedback, true);
|
2010-11-05 06:34:00 +00:00
|
|
|
$qo->generalfeedbackfiles = array();
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->generalfeedbackformat = $this->trans_format($this->getpath($question,
|
|
|
|
array('#', 'generalfeedback', 0, '@', 'format'), 'moodle_auto_format'));
|
2011-03-23 14:27:22 +00:00
|
|
|
$qo->generalfeedbackfiles = $this->import_files($this->getpath($question,
|
|
|
|
array('#', 'generalfeedback', 0, '#', 'file'), array(), false));
|
2010-11-05 06:34:00 +00:00
|
|
|
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->defaultmark = $this->getpath($question,
|
|
|
|
array('#', 'defaultgrade', 0, '#'), $qo->defaultmark);
|
|
|
|
$qo->penalty = $this->getpath($question,
|
|
|
|
array('#', 'penalty', 0, '#'), $qo->penalty);
|
2011-01-17 18:01:49 +00:00
|
|
|
|
|
|
|
// Fix problematic rounding from old files:
|
|
|
|
if (abs($qo->penalty - 0.3333333) < 0.005) {
|
|
|
|
$qo->penalty = 0.3333333;
|
|
|
|
}
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2010-11-19 12:33:48 +00:00
|
|
|
// Read the question tags.
|
|
|
|
if (!empty($CFG->usetags) && array_key_exists('tags', $question['#'])
|
|
|
|
&& !empty($question['#']['tags'][0]['#']['tag'])) {
|
|
|
|
require_once($CFG->dirroot.'/tag/lib.php');
|
|
|
|
$qo->tags = array();
|
|
|
|
foreach ($question['#']['tags'][0]['#']['tag'] as $tagdata) {
|
|
|
|
$qo->tags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-24 15:14:09 +00:00
|
|
|
return $qo;
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Import the common parts of a single answer
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param array answer xml tree for single answer
|
|
|
|
* @return object answer object
|
2009-09-30 13:38:36 +00:00
|
|
|
*/
|
2011-06-16 20:24:44 +01:00
|
|
|
public function import_answer($answer, $withanswerfiles = false) {
|
|
|
|
$ans = new stdClass();
|
|
|
|
|
|
|
|
$ans->answer = array();
|
|
|
|
$ans->answer['text'] = $this->getpath($answer, array('#', 'text', 0, '#'), '', true);
|
|
|
|
$ans->answer['format'] = $this->trans_format($this->getpath($answer,
|
2011-03-23 14:27:22 +00:00
|
|
|
array('@', 'format'), 'moodle_auto_format'));
|
2011-06-16 20:24:44 +01:00
|
|
|
if ($withanswerfiles) {
|
|
|
|
$ans->answer['files'] = $this->import_files($this->getpath($answer,
|
|
|
|
array('#', 'file'), array()));
|
|
|
|
}
|
2010-11-05 06:34:00 +00:00
|
|
|
|
2011-06-16 20:24:44 +01:00
|
|
|
$ans->feedback = array();
|
|
|
|
$ans->feedback['text'] = $this->getpath($answer,
|
2011-06-14 17:06:55 +01:00
|
|
|
array('#', 'feedback', 0, '#', 'text', 0, '#'), '', true);
|
2011-06-16 20:24:44 +01:00
|
|
|
$ans->feedback['format'] = $this->trans_format($this->getpath($answer,
|
2010-11-08 17:25:53 +00:00
|
|
|
array('#', 'feedback', 0, '@', 'format'), 'moodle_auto_format'));
|
2011-06-16 20:24:44 +01:00
|
|
|
$ans->feedback['files'] = $this->import_files($this->getpath($answer,
|
2011-03-23 14:27:22 +00:00
|
|
|
array('#', 'feedback', 0, '#', 'file'), array()));
|
2010-11-05 06:34:00 +00:00
|
|
|
|
2011-06-16 20:24:44 +01:00
|
|
|
$ans->fraction = $this->getpath($answer, array('@', 'fraction'), 0) / 100;
|
2006-02-24 15:14:09 +00:00
|
|
|
|
|
|
|
return $ans;
|
|
|
|
}
|
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
/**
|
|
|
|
* Import the common overall feedback fields.
|
|
|
|
* @param object $question the part of the XML relating to this question.
|
|
|
|
* @param object $qo the question data to add the fields to.
|
2011-02-23 16:25:25 +00:00
|
|
|
* @param bool $withshownumpartscorrect include the shownumcorrect field.
|
2011-01-17 18:01:49 +00:00
|
|
|
*/
|
|
|
|
public function import_combined_feedback($qo, $questionxml, $withshownumpartscorrect = false) {
|
2011-06-14 17:06:55 +01:00
|
|
|
$fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
|
|
|
|
foreach ($fields as $field) {
|
2011-01-18 16:06:05 +00:00
|
|
|
$text = array();
|
|
|
|
$text['text'] = $this->getpath($questionxml,
|
|
|
|
array('#', $field, 0, '#', 'text', 0, '#'), '', true);
|
|
|
|
$text['format'] = $this->trans_format($this->getpath($questionxml,
|
|
|
|
array('#', $field, 0, '@', 'format'), 'moodle_auto_format'));
|
2011-03-23 14:27:22 +00:00
|
|
|
$text['files'] = $this->import_files($this->getpath($questionxml,
|
|
|
|
array('#', $field, 0, '#', 'file'), array(), false));
|
2011-01-18 16:06:05 +00:00
|
|
|
|
|
|
|
$qo->$field = $text;
|
|
|
|
}
|
2011-01-17 18:01:49 +00:00
|
|
|
|
|
|
|
if ($withshownumpartscorrect) {
|
|
|
|
$qo->shownumcorrect = array_key_exists('shownumcorrect', $questionxml['#']);
|
|
|
|
|
|
|
|
// Backwards compatibility:
|
|
|
|
if (array_key_exists('correctresponsesfeedback', $questionxml['#'])) {
|
|
|
|
$qo->shownumcorrect = $this->trans_single($this->getpath($questionxml,
|
|
|
|
array('#', 'correctresponsesfeedback', 0, '#'), 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import a question hint
|
|
|
|
* @param array $hintxml hint xml fragment.
|
|
|
|
* @return object hint for storing in the database.
|
|
|
|
*/
|
|
|
|
public function import_hint($hintxml) {
|
|
|
|
if (array_key_exists('hintcontent', $hintxml['#'])) {
|
|
|
|
// Backwards compatibility:
|
|
|
|
|
2011-02-21 18:10:19 +00:00
|
|
|
$hint = new stdClass();
|
2011-03-23 14:27:22 +00:00
|
|
|
$hint->hint = array('format' => FORMAT_HTML, 'files' => array());
|
|
|
|
$hint->hint['text'] = $this->getpath($hintxml,
|
2011-06-14 17:06:55 +01:00
|
|
|
array('#', 'hintcontent', 0, '#', 'text', 0, '#'), '', true);
|
2011-01-17 18:01:49 +00:00
|
|
|
$hint->shownumcorrect = $this->getpath($hintxml,
|
|
|
|
array('#', 'statenumberofcorrectresponses', 0, '#'), 0);
|
|
|
|
$hint->clearwrong = $this->getpath($hintxml,
|
|
|
|
array('#', 'clearincorrectresponses', 0, '#'), 0);
|
|
|
|
$hint->options = $this->getpath($hintxml,
|
|
|
|
array('#', 'showfeedbacktoresponses', 0, '#'), 0);
|
|
|
|
|
|
|
|
return $hint;
|
|
|
|
}
|
|
|
|
|
2011-06-16 20:24:44 +01:00
|
|
|
$hint = new stdClass();
|
|
|
|
$hint->hint['text'] = $this->getpath($hintxml,
|
2011-03-23 14:27:22 +00:00
|
|
|
array('#', 'text', 0, '#'), '', true);
|
2011-06-16 20:24:44 +01:00
|
|
|
$hint->hint['format'] = $this->trans_format($this->getpath($hintxml,
|
2011-03-23 14:27:22 +00:00
|
|
|
array('@', 'format'), 'moodle_auto_format'));
|
2011-06-16 20:24:44 +01:00
|
|
|
$hint->hint['files'] = $this->import_files($this->getpath($hintxml,
|
2011-03-23 14:27:22 +00:00
|
|
|
array('#', 'file'), array(), false));
|
2011-01-17 18:01:49 +00:00
|
|
|
$hint->shownumcorrect = array_key_exists('shownumcorrect', $hintxml['#']);
|
|
|
|
$hint->clearwrong = array_key_exists('clearwrong', $hintxml['#']);
|
2011-06-14 17:06:55 +01:00
|
|
|
$hint->options = $this->getpath($hintxml, array('#', 'options', 0, '#'), '', true);
|
2011-01-17 18:01:49 +00:00
|
|
|
|
|
|
|
return $hint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import all the question hints
|
|
|
|
*
|
|
|
|
* @param object $qo the question data that is being constructed.
|
|
|
|
* @param array $hintsxml hints xml fragment.
|
|
|
|
*/
|
|
|
|
public function import_hints($qo, $questionxml, $withparts = false, $withoptions = false) {
|
|
|
|
if (!isset($questionxml['#']['hint'])) {
|
|
|
|
return;
|
|
|
|
}
|
2011-01-18 16:06:05 +00:00
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
foreach ($questionxml['#']['hint'] as $hintxml) {
|
|
|
|
$hint = $this->import_hint($hintxml);
|
|
|
|
$qo->hint[] = $hint->hint;
|
|
|
|
|
|
|
|
if ($withparts) {
|
|
|
|
$qo->hintshownumcorrect[] = $hint->shownumcorrect;
|
|
|
|
$qo->hintclearwrong[] = $hint->clearwrong;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($withoptions) {
|
|
|
|
$qo->hintoptions[] = $hint->options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 14:27:22 +00:00
|
|
|
/**
|
|
|
|
* Import files from a node in the XML.
|
|
|
|
* @param array $xml an array of <file> nodes from the the parsed XML.
|
|
|
|
* @return array of things representing files - in the form that save_question expects.
|
|
|
|
*/
|
|
|
|
public function import_files($xml) {
|
|
|
|
$files = array();
|
|
|
|
foreach ($xml as $file) {
|
|
|
|
$data = new stdClass();
|
|
|
|
$data->content = $file['#'];
|
|
|
|
$data->encoding = $file['@']['encoding'];
|
|
|
|
$data->name = $file['@']['name'];
|
|
|
|
$files[] = $data;
|
|
|
|
}
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2009-09-30 13:38:36 +00:00
|
|
|
* import multiple choice question
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param array question question array from xml tree
|
|
|
|
* @return object question object
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function import_multichoice($question) {
|
2006-02-24 15:14:09 +00:00
|
|
|
// get common parts
|
2010-11-05 06:34:00 +00:00
|
|
|
$qo = $this->import_headers($question);
|
2006-02-24 15:14:09 +00:00
|
|
|
|
|
|
|
// 'header' parts particular to multichoice
|
|
|
|
$qo->qtype = MULTICHOICE;
|
2011-01-17 18:01:49 +00:00
|
|
|
$single = $this->getpath($question, array('#', 'single', 0, '#'), 'true');
|
|
|
|
$qo->single = $this->trans_single($single);
|
2011-06-14 17:06:55 +01:00
|
|
|
$shuffleanswers = $this->getpath($question,
|
|
|
|
array('#', 'shuffleanswers', 0, '#'), 'false');
|
|
|
|
$qo->answernumbering = $this->getpath($question,
|
|
|
|
array('#', 'answernumbering', 0, '#'), 'abc');
|
2006-10-03 14:01:38 +00:00
|
|
|
$qo->shuffleanswers = $this->trans_single($shuffleanswers);
|
2010-11-05 06:34:00 +00:00
|
|
|
|
2011-06-14 17:06:55 +01:00
|
|
|
// There was a time on the 1.8 branch when it could output an empty
|
|
|
|
// answernumbering tag, so fix up any found.
|
2008-05-27 13:42:27 +00:00
|
|
|
if (empty($qo->answernumbering)) {
|
|
|
|
$qo->answernumbering = 'abc';
|
|
|
|
}
|
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
// Run through the answers
|
2009-09-30 13:38:36 +00:00
|
|
|
$answers = $question['#']['answer'];
|
2011-01-17 18:01:49 +00:00
|
|
|
$acount = 0;
|
2006-02-24 15:14:09 +00:00
|
|
|
foreach ($answers as $answer) {
|
2011-06-16 20:24:44 +01:00
|
|
|
$ans = $this->import_answer($answer, true);
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo->answer[$acount] = $ans->answer;
|
|
|
|
$qo->fraction[$acount] = $ans->fraction;
|
|
|
|
$qo->feedback[$acount] = $ans->feedback;
|
|
|
|
++$acount;
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
2010-11-05 06:34:00 +00:00
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
$this->import_combined_feedback($qo, $question, true);
|
|
|
|
$this->import_hints($qo, $question, true);
|
|
|
|
|
2006-02-24 15:14:09 +00:00
|
|
|
return $qo;
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Import cloze type question
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param array question question array from xml tree
|
|
|
|
* @return object question object
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-06-14 17:06:55 +01:00
|
|
|
public function import_multianswer($questions) {
|
2011-06-15 12:27:45 +01:00
|
|
|
question_bank::get_qtype('multianswer');
|
|
|
|
|
2010-11-09 04:07:37 +00:00
|
|
|
$questiontext = array();
|
|
|
|
$questiontext['text'] = $this->import_text($questions['#']['questiontext'][0]['#']['text']);
|
|
|
|
$questiontext['format'] = '1';
|
2011-02-10 20:44:47 +00:00
|
|
|
$questiontext['itemid'] = '';
|
2010-11-09 04:07:37 +00:00
|
|
|
$qo = qtype_multianswer_extract_question($questiontext);
|
2006-06-02 09:45:19 +00:00
|
|
|
|
|
|
|
// 'header' parts particular to multianswer
|
|
|
|
$qo->qtype = MULTIANSWER;
|
|
|
|
$qo->course = $this->course;
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->generalfeedback = '';
|
2010-11-09 04:07:37 +00:00
|
|
|
// restore files in generalfeedback
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->generalfeedback = $this->getpath($questions,
|
|
|
|
array('#', 'generalfeedback', 0, '#', 'text', 0, '#'), $qo->generalfeedback, true);
|
|
|
|
$qo->generalfeedbackformat = $this->trans_format($this->getpath($questions,
|
|
|
|
array('#', 'generalfeedback', 0, '@', 'format'), 'moodle_auto_format'));
|
2011-03-23 14:27:22 +00:00
|
|
|
$qo->generalfeedbackfiles = $this->import_files($this->getpath($questions,
|
|
|
|
array('#', 'generalfeedback', 0, '#', 'file'), array(), false));
|
|
|
|
|
2006-07-05 12:39:35 +00:00
|
|
|
if (!empty($questions)) {
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo->name = $this->import_text($questions['#']['name'][0]['#']['text']);
|
2006-06-02 09:45:19 +00:00
|
|
|
}
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->questiontext = $qo->questiontext['text'];
|
|
|
|
$qo->questiontextformat = '';
|
2006-06-02 09:45:19 +00:00
|
|
|
|
2011-06-15 12:27:45 +01:00
|
|
|
$this->import_hints($qo, $questions, true);
|
2011-01-17 18:01:49 +00:00
|
|
|
|
2006-06-02 09:45:19 +00:00
|
|
|
return $qo;
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Import true/false type question
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param array question question array from xml tree
|
|
|
|
* @return object question object
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function import_truefalse($question) {
|
2006-02-24 15:14:09 +00:00
|
|
|
// get common parts
|
2009-08-18 05:19:00 +00:00
|
|
|
global $OUTPUT;
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo = $this->import_headers($question);
|
2006-02-24 15:14:09 +00:00
|
|
|
|
|
|
|
// 'header' parts particular to true/false
|
|
|
|
$qo->qtype = TRUEFALSE;
|
|
|
|
|
2006-12-18 18:07:49 +00:00
|
|
|
// In the past, it used to be assumed that the two answers were in the file
|
|
|
|
// true first, then false. Howevever that was not always true. Now, we
|
|
|
|
// try to match on the answer text, but in old exports, this will be a localised
|
|
|
|
// string, so if we don't find true or false, we fall back to the old system.
|
|
|
|
$first = true;
|
|
|
|
$warning = false;
|
|
|
|
foreach ($question['#']['answer'] as $answer) {
|
2011-06-14 17:06:55 +01:00
|
|
|
$answertext = $this->getpath($answer,
|
|
|
|
array('#', 'text', 0, '#'), '', true);
|
|
|
|
$feedback = $this->getpath($answer,
|
|
|
|
array('#', 'feedback', 0, '#', 'text', 0, '#'), '', true);
|
|
|
|
$feedbackformat = $this->getpath($answer,
|
|
|
|
array('#', 'feedback', 0, '@', 'format'), 'moodle_auto_format');
|
|
|
|
$feedbackfiles = $this->getpath($answer,
|
|
|
|
array('#', 'feedback', 0, '#', 'file'), array());
|
2010-11-05 06:34:00 +00:00
|
|
|
$files = array();
|
|
|
|
foreach ($feedbackfiles as $file) {
|
2011-02-21 18:10:19 +00:00
|
|
|
$data = new stdClass();
|
2010-11-05 06:34:00 +00:00
|
|
|
$data->content = $file['#'];
|
|
|
|
$data->encoding = $file['@']['encoding'];
|
|
|
|
$data->name = $file['@']['name'];
|
|
|
|
$files[] = $data;
|
|
|
|
}
|
2006-12-18 18:07:49 +00:00
|
|
|
if ($answertext != 'true' && $answertext != 'false') {
|
2011-01-17 18:01:49 +00:00
|
|
|
// Old style file, assume order is true/false.
|
2006-12-18 18:07:49 +00:00
|
|
|
$warning = true;
|
2011-01-17 18:01:49 +00:00
|
|
|
if ($first) {
|
|
|
|
$answertext = 'true';
|
|
|
|
} else {
|
|
|
|
$answertext = 'false';
|
|
|
|
}
|
2009-09-30 13:38:36 +00:00
|
|
|
}
|
2011-01-17 18:01:49 +00:00
|
|
|
|
2006-12-18 18:07:49 +00:00
|
|
|
if ($answertext == 'true') {
|
|
|
|
$qo->answer = ($answer['@']['fraction'] == 100);
|
2007-02-21 14:20:46 +00:00
|
|
|
$qo->correctanswer = $qo->answer;
|
2010-11-05 06:34:00 +00:00
|
|
|
$qo->feedbacktrue = array();
|
|
|
|
$qo->feedbacktrue['text'] = $feedback;
|
|
|
|
$qo->feedbacktrue['format'] = $this->trans_format($feedbackformat);
|
2011-01-18 16:06:05 +00:00
|
|
|
$qo->feedbacktrue['files'] = $files;
|
2006-12-18 18:07:49 +00:00
|
|
|
} else {
|
|
|
|
$qo->answer = ($answer['@']['fraction'] != 100);
|
2007-02-21 14:20:46 +00:00
|
|
|
$qo->correctanswer = $qo->answer;
|
2010-11-05 06:34:00 +00:00
|
|
|
$qo->feedbackfalse = array();
|
|
|
|
$qo->feedbackfalse['text'] = $feedback;
|
|
|
|
$qo->feedbackfalse['format'] = $this->trans_format($feedbackformat);
|
2011-01-18 16:06:05 +00:00
|
|
|
$qo->feedbackfalse['files'] = $files;
|
2006-12-18 18:07:49 +00:00
|
|
|
}
|
|
|
|
$first = false;
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
2006-12-18 18:07:49 +00:00
|
|
|
|
|
|
|
if ($warning) {
|
2011-02-21 18:10:19 +00:00
|
|
|
$a = new stdClass();
|
2007-03-12 16:32:52 +00:00
|
|
|
$a->questiontext = $qo->questiontext;
|
2011-02-23 18:53:50 +00:00
|
|
|
$a->answer = get_string($qo->correctanswer ? 'true' : 'false', 'qtype_truefalse');
|
|
|
|
echo $OUTPUT->notification(get_string('truefalseimporterror', 'qformat_xml', $a));
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
2011-01-17 18:01:49 +00:00
|
|
|
|
|
|
|
$this->import_hints($qo, $question);
|
|
|
|
|
2006-02-24 15:14:09 +00:00
|
|
|
return $qo;
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Import short answer type question
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param array question question array from xml tree
|
|
|
|
* @return object question object
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function import_shortanswer($question) {
|
2006-02-24 15:14:09 +00:00
|
|
|
// get common parts
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo = $this->import_headers($question);
|
2006-02-24 15:14:09 +00:00
|
|
|
|
|
|
|
// header parts particular to shortanswer
|
|
|
|
$qo->qtype = SHORTANSWER;
|
|
|
|
|
|
|
|
// get usecase
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo->usecase = $this->getpath($question, array('#', 'usecase', 0, '#'), $qo->usecase);
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
// Run through the answers
|
2009-09-30 13:38:36 +00:00
|
|
|
$answers = $question['#']['answer'];
|
2011-01-17 18:01:49 +00:00
|
|
|
$acount = 0;
|
2006-02-24 15:14:09 +00:00
|
|
|
foreach ($answers as $answer) {
|
2010-11-12 17:11:34 +00:00
|
|
|
$ans = $this->import_answer($answer);
|
2011-01-18 16:06:05 +00:00
|
|
|
$qo->answer[$acount] = $ans->answer['text'];
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo->fraction[$acount] = $ans->fraction;
|
|
|
|
$qo->feedback[$acount] = $ans->feedback;
|
|
|
|
++$acount;
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
$this->import_hints($qo, $question);
|
|
|
|
|
2006-02-24 15:14:09 +00:00
|
|
|
return $qo;
|
|
|
|
}
|
2009-09-30 13:38:36 +00:00
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Import description type question
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param array question question array from xml tree
|
|
|
|
* @return object question object
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function import_description($question) {
|
2006-06-02 09:45:19 +00:00
|
|
|
// get common parts
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo = $this->import_headers($question);
|
2006-06-02 09:45:19 +00:00
|
|
|
// header parts particular to shortanswer
|
|
|
|
$qo->qtype = DESCRIPTION;
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo->defaultmark = 0;
|
2008-05-01 12:21:55 +00:00
|
|
|
$qo->length = 0;
|
2006-06-02 09:45:19 +00:00
|
|
|
return $qo;
|
|
|
|
}
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Import numerical type question
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param array question question array from xml tree
|
|
|
|
* @return object question object
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function import_numerical($question) {
|
2006-02-24 15:14:09 +00:00
|
|
|
// get common parts
|
2010-11-08 17:25:53 +00:00
|
|
|
$qo = $this->import_headers($question);
|
2006-02-24 15:14:09 +00:00
|
|
|
|
|
|
|
// header parts particular to numerical
|
|
|
|
$qo->qtype = NUMERICAL;
|
|
|
|
|
|
|
|
// get answers array
|
|
|
|
$answers = $question['#']['answer'];
|
|
|
|
$qo->answer = array();
|
|
|
|
$qo->feedback = array();
|
|
|
|
$qo->fraction = array();
|
|
|
|
$qo->tolerance = array();
|
|
|
|
foreach ($answers as $answer) {
|
2007-03-12 16:32:52 +00:00
|
|
|
// answer outside of <text> is deprecated
|
2010-11-05 06:34:00 +00:00
|
|
|
$obj = $this->import_answer($answer);
|
2010-11-12 17:11:34 +00:00
|
|
|
$qo->answer[] = $obj->answer['text'];
|
2007-05-23 12:52:33 +00:00
|
|
|
if (empty($qo->answer)) {
|
|
|
|
$qo->answer = '*';
|
2007-03-12 16:32:52 +00:00
|
|
|
}
|
2010-11-08 17:25:53 +00:00
|
|
|
$qo->feedback[] = $obj->feedback;
|
|
|
|
$qo->tolerance[] = $this->getpath($answer, array('#', 'tolerance', 0, '#'), 0);
|
2007-03-12 16:32:52 +00:00
|
|
|
|
|
|
|
// fraction as a tag is deprecated
|
2010-11-08 17:25:53 +00:00
|
|
|
$fraction = $this->getpath($answer, array('@', 'fraction'), 0) / 100;
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->fraction[] = $this->getpath($answer,
|
|
|
|
array('#', 'fraction', 0, '#'), $fraction); // deprecated
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
// Get the units array
|
2006-02-24 15:14:09 +00:00
|
|
|
$qo->unit = array();
|
2011-01-17 18:01:49 +00:00
|
|
|
$units = $this->getpath($question, array('#', 'units', 0, '#', 'unit'), array());
|
2007-05-23 12:52:33 +00:00
|
|
|
if (!empty($units)) {
|
2006-06-01 10:13:57 +00:00
|
|
|
$qo->multiplier = array();
|
|
|
|
foreach ($units as $unit) {
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->multiplier[] = $this->getpath($unit, array('#', 'multiplier', 0, '#'), 1);
|
|
|
|
$qo->unit[] = $this->getpath($unit, array('#', 'unit_name', 0, '#'), '', true);
|
2006-06-01 10:13:57 +00:00
|
|
|
}
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->unitgradingtype = $this->getpath($question, array('#', 'unitgradingtype', 0, '#'), 0);
|
|
|
|
$qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), 0);
|
|
|
|
$qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), 0);
|
|
|
|
$qo->unitsleft = $this->getpath($question, array('#', 'unitsleft', 0, '#'), 0);
|
2010-11-12 17:11:34 +00:00
|
|
|
$qo->instructions['text'] = '';
|
|
|
|
$qo->instructions['format'] = FORMAT_HTML;
|
2010-11-05 06:34:00 +00:00
|
|
|
$instructions = $this->getpath($question, array('#', 'instructions'), array());
|
|
|
|
if (!empty($instructions)) {
|
|
|
|
$qo->instructions = array();
|
2010-11-08 17:25:53 +00:00
|
|
|
$qo->instructions['text'] = $this->getpath($instructions,
|
|
|
|
array('0', '#', 'text', '0', '#'), '', true);
|
2010-11-08 20:58:32 +00:00
|
|
|
$qo->instructions['format'] = $this->trans_format($this->getpath($instructions,
|
2010-11-08 17:25:53 +00:00
|
|
|
array('0', '@', 'format'), 'moodle_auto_format'));
|
2011-03-23 14:27:22 +00:00
|
|
|
$qo->instructions['files'] = $this->import_files($this->getpath(
|
|
|
|
$instructions, array('0', '#', 'file'), array()));
|
2010-11-05 06:34:00 +00:00
|
|
|
}
|
2011-01-17 18:01:49 +00:00
|
|
|
|
|
|
|
$this->import_hints($qo, $question);
|
|
|
|
|
2006-02-24 15:14:09 +00:00
|
|
|
return $qo;
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Import matching type question
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param array question question array from xml tree
|
|
|
|
* @return object question object
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function import_matching($question) {
|
2006-03-23 11:13:50 +00:00
|
|
|
// get common parts
|
2010-11-08 17:25:53 +00:00
|
|
|
$qo = $this->import_headers($question);
|
2006-03-23 11:13:50 +00:00
|
|
|
|
|
|
|
// header parts particular to matching
|
|
|
|
$qo->qtype = MATCH;
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo->shuffleanswers = $this->trans_single($this->getpath($question,
|
|
|
|
array('#', 'shuffleanswers', 0, '#'), 1));
|
2006-03-23 11:13:50 +00:00
|
|
|
|
2011-01-18 16:06:05 +00:00
|
|
|
// run through subquestions
|
2006-03-23 11:13:50 +00:00
|
|
|
$qo->subquestions = array();
|
|
|
|
$qo->subanswers = array();
|
2011-01-18 16:06:05 +00:00
|
|
|
foreach ($question['#']['subquestion'] as $subqxml) {
|
|
|
|
$subquestion = array();
|
|
|
|
$subquestion['text'] = $this->getpath($subqxml, array('#', 'text', 0, '#'), '', true);
|
2011-06-14 17:06:55 +01:00
|
|
|
$subquestion['format'] = $this->trans_format($this->getpath($subqxml,
|
|
|
|
array('@', 'format'), 'moodle_auto_format'));
|
2011-03-23 14:27:22 +00:00
|
|
|
$subquestion['files'] = $this->import_files($this->getpath($subqxml,
|
|
|
|
array('#', 'file'), array()));
|
2011-01-18 16:06:05 +00:00
|
|
|
|
|
|
|
$qo->subquestions[] = $subquestion;
|
|
|
|
$answers = $this->getpath($subqxml, array('#', 'answer'), array());
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->subanswers[] = $this->getpath($subqxml,
|
|
|
|
array('#', 'answer', 0, '#', 'text', 0, '#'), '', true);
|
2006-03-23 11:13:50 +00:00
|
|
|
}
|
2011-01-17 18:01:49 +00:00
|
|
|
|
|
|
|
$this->import_combined_feedback($qo, $question, true);
|
|
|
|
$this->import_hints($qo, $question, true);
|
|
|
|
|
2006-03-23 11:13:50 +00:00
|
|
|
return $qo;
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Import essay type question
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param array question question array from xml tree
|
|
|
|
* @return object question object
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function import_essay($question) {
|
2006-08-23 14:57:40 +00:00
|
|
|
// get common parts
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo = $this->import_headers($question);
|
2006-08-23 14:57:40 +00:00
|
|
|
|
|
|
|
// header parts particular to essay
|
|
|
|
$qo->qtype = ESSAY;
|
|
|
|
|
2011-06-14 17:36:11 +01:00
|
|
|
$qo->responseformat = $this->getpath($question,
|
|
|
|
array('#', 'responseformat', 0, '#'), 'editor');
|
|
|
|
$qo->responsefieldlines = $this->getpath($question,
|
|
|
|
array('#', 'responsefieldlines', 0, '#'), 15);
|
|
|
|
$qo->attachments = $this->getpath($question,
|
|
|
|
array('#', 'attachments', 0, '#'), 0);
|
|
|
|
$qo->graderinfo['text'] = $this->getpath($question,
|
|
|
|
array('#', 'graderinfo', 0, '#', 'text', 0, '#'), '', true);
|
|
|
|
$qo->graderinfo['format'] = $this->trans_format($this->getpath($question,
|
|
|
|
array('#', 'graderinfo', 0, '@', 'format'), 'moodle_auto_format'));
|
2006-08-23 14:57:40 +00:00
|
|
|
|
|
|
|
return $qo;
|
|
|
|
}
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2011-06-14 17:06:55 +01:00
|
|
|
/**
|
|
|
|
* Import a calculated question
|
|
|
|
* @param object $question the imported XML data.
|
|
|
|
*/
|
|
|
|
public function import_calculated($question) {
|
2007-03-28 05:00:36 +00:00
|
|
|
|
|
|
|
// get common parts
|
2011-01-17 18:01:49 +00:00
|
|
|
$qo = $this->import_headers($question);
|
2007-03-28 05:00:36 +00:00
|
|
|
|
2010-04-23 23:10:11 +00:00
|
|
|
// header parts particular to calculated
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->qtype = CALCULATED;
|
|
|
|
$qo->synchronize = $this->getpath($question, array('#', 'synchronize', 0, '#'), 0);
|
|
|
|
$single = $this->getpath($question, array('#', 'single', 0, '#'), 'true');
|
|
|
|
$qo->single = $this->trans_single($single);
|
|
|
|
$shuffleanswers = $this->getpath($question, array('#', 'shuffleanswers', 0, '#'), 'false');
|
|
|
|
$qo->answernumbering = $this->getpath($question,
|
|
|
|
array('#', 'answernumbering', 0, '#'), 'abc');
|
2010-04-23 23:10:11 +00:00
|
|
|
$qo->shuffleanswers = $this->trans_single($shuffleanswers);
|
2010-11-05 06:34:00 +00:00
|
|
|
|
|
|
|
$qo->correctfeedback = array();
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->correctfeedback['text'] = $this->getpath(
|
|
|
|
$question, array('#', 'correctfeedback', 0, '#', 'text', 0, '#'), '', true);
|
2010-11-08 17:25:53 +00:00
|
|
|
$qo->correctfeedback['format'] = $this->trans_format($this->getpath(
|
2011-06-14 17:36:11 +01:00
|
|
|
$question, array('#', 'correctfeedback', 0, '@', 'format'), 'moodle_auto_format'));
|
2011-03-23 14:27:22 +00:00
|
|
|
$qo->correctfeedback['files'] = $this->import_files($this->getpath(
|
|
|
|
$question, array('#', 'correctfeedback', '0', '#', 'file'), array()));
|
2010-11-05 06:34:00 +00:00
|
|
|
|
|
|
|
$qo->partiallycorrectfeedback = array();
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->partiallycorrectfeedback['text'] = $this->getpath($question,
|
|
|
|
array('#', 'partiallycorrectfeedback', 0, '#', 'text', 0, '#'), '', true);
|
2010-11-08 17:25:53 +00:00
|
|
|
$qo->partiallycorrectfeedback['format'] = $this->trans_format(
|
2011-06-14 17:06:55 +01:00
|
|
|
$this->getpath($question, array('#', 'partiallycorrectfeedback', 0, '@', 'format'),
|
|
|
|
'moodle_auto_format'));
|
2011-03-23 14:27:22 +00:00
|
|
|
$qo->partiallycorrectfeedback['files'] = $this->import_files($this->getpath(
|
|
|
|
$question, array('#', 'partiallycorrectfeedback', '0', '#', 'file'), array()));
|
2010-11-05 06:34:00 +00:00
|
|
|
|
|
|
|
$qo->incorrectfeedback = array();
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->incorrectfeedback['text'] = $this->getpath($question,
|
|
|
|
array('#', 'incorrectfeedback', 0, '#', 'text', 0, '#'), '', true);
|
|
|
|
$qo->incorrectfeedback['format'] = $this->trans_format($this->getpath($question,
|
|
|
|
array('#', 'incorrectfeedback', 0, '@', 'format'), 'moodle_auto_format'));
|
|
|
|
$qo->incorrectfeedback['files'] = $this->import_files($this->getpath($question,
|
|
|
|
array('#', 'incorrectfeedback', '0', '#', 'file'), array()));
|
|
|
|
|
|
|
|
$qo->unitgradingtype = $this->getpath($question,
|
|
|
|
array('#', 'unitgradingtype', 0, '#'), 0);
|
|
|
|
$qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), 0);
|
|
|
|
$qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), 0);
|
|
|
|
$qo->unitsleft = $this->getpath($question, array('#', 'unitsleft', 0, '#'), 0);
|
|
|
|
$qo->instructions = $this->getpath($question,
|
|
|
|
array('#', 'instructions', 0, '#', 'text', 0, '#'), '', true);
|
2010-11-06 02:16:07 +00:00
|
|
|
if (!empty($instructions)) {
|
|
|
|
$qo->instructions = array();
|
2010-11-08 17:25:53 +00:00
|
|
|
$qo->instructions['text'] = $this->getpath($instructions,
|
|
|
|
array('0', '#', 'text', '0', '#'), '', true);
|
|
|
|
$qo->instructions['format'] = $this->trans_format($this->getpath($instructions,
|
|
|
|
array('0', '@', 'format'), 'moodle_auto_format'));
|
2011-03-23 14:27:22 +00:00
|
|
|
$qo->instructions['files'] = $this->import_files($this->getpath($instructions,
|
|
|
|
array('0', '#', 'file'), array()));
|
2010-11-06 02:16:07 +00:00
|
|
|
}
|
2010-11-05 06:34:00 +00:00
|
|
|
|
2007-03-28 05:00:36 +00:00
|
|
|
// get answers array
|
|
|
|
$answers = $question['#']['answer'];
|
|
|
|
$qo->answers = array();
|
|
|
|
$qo->feedback = array();
|
|
|
|
$qo->fraction = array();
|
|
|
|
$qo->tolerance = array();
|
|
|
|
$qo->tolerancetype = array();
|
|
|
|
$qo->correctanswerformat = array();
|
|
|
|
$qo->correctanswerlength = array();
|
|
|
|
$qo->feedback = array();
|
|
|
|
foreach ($answers as $answer) {
|
2011-06-16 20:24:44 +01:00
|
|
|
$ans = $this->import_answer($answer, true);
|
2007-03-28 05:00:36 +00:00
|
|
|
// answer outside of <text> is deprecated
|
2010-11-05 06:34:00 +00:00
|
|
|
if (empty($ans->answer['text'])) {
|
|
|
|
$ans->answer['text'] = '*';
|
2007-03-28 05:00:36 +00:00
|
|
|
}
|
2010-11-05 06:34:00 +00:00
|
|
|
$qo->answers[] = $ans->answer;
|
|
|
|
$qo->feedback[] = $ans->feedback;
|
2007-03-28 05:00:36 +00:00
|
|
|
$qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
|
|
|
|
// fraction as a tag is deprecated
|
|
|
|
if (!empty($answer['#']['fraction'][0]['#'])) {
|
|
|
|
$qo->fraction[] = $answer['#']['fraction'][0]['#'];
|
2010-11-05 06:34:00 +00:00
|
|
|
} else {
|
2007-03-28 05:00:36 +00:00
|
|
|
$qo->fraction[] = $answer['@']['fraction'] / 100;
|
|
|
|
}
|
|
|
|
$qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
|
|
|
|
$qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
|
|
|
|
$qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
|
|
|
|
}
|
|
|
|
// get units array
|
|
|
|
$qo->unit = array();
|
|
|
|
if (isset($question['#']['units'][0]['#']['unit'])) {
|
|
|
|
$units = $question['#']['units'][0]['#']['unit'];
|
|
|
|
$qo->multiplier = array();
|
|
|
|
foreach ($units as $unit) {
|
|
|
|
$qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
|
|
|
|
$qo->unit[] = $unit['#']['unit_name'][0]['#'];
|
|
|
|
}
|
|
|
|
}
|
2010-11-05 06:34:00 +00:00
|
|
|
$instructions = $this->getpath($question, array('#', 'instructions'), array());
|
|
|
|
if (!empty($instructions)) {
|
|
|
|
$qo->instructions = array();
|
2010-11-08 17:25:53 +00:00
|
|
|
$qo->instructions['text'] = $this->getpath($instructions,
|
|
|
|
array('0', '#', 'text', '0', '#'), '', true);
|
|
|
|
$qo->instructions['format'] = $this->trans_format($this->getpath($instructions,
|
|
|
|
array('0', '@', 'format'), 'moodle_auto_format'));
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->instructions['files'] = $this->import_files($this->getpath($instructions,
|
2011-03-23 14:27:22 +00:00
|
|
|
array('0', '#', 'file'), array()));
|
2010-11-05 06:34:00 +00:00
|
|
|
}
|
|
|
|
$datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
|
|
|
|
$qo->dataset = array();
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->datasetindex= 0;
|
2007-03-28 05:00:36 +00:00
|
|
|
foreach ($datasets as $dataset) {
|
|
|
|
$qo->datasetindex++;
|
|
|
|
$qo->dataset[$qo->datasetindex] = new stdClass();
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->dataset[$qo->datasetindex]->status =
|
|
|
|
$this->import_text($dataset['#']['status'][0]['#']['text']);
|
|
|
|
$qo->dataset[$qo->datasetindex]->name =
|
|
|
|
$this->import_text($dataset['#']['name'][0]['#']['text']);
|
|
|
|
$qo->dataset[$qo->datasetindex]->type =
|
|
|
|
$dataset['#']['type'][0]['#'];
|
|
|
|
$qo->dataset[$qo->datasetindex]->distribution =
|
|
|
|
$this->import_text($dataset['#']['distribution'][0]['#']['text']);
|
|
|
|
$qo->dataset[$qo->datasetindex]->max =
|
|
|
|
$this->import_text($dataset['#']['maximum'][0]['#']['text']);
|
|
|
|
$qo->dataset[$qo->datasetindex]->min =
|
|
|
|
$this->import_text($dataset['#']['minimum'][0]['#']['text']);
|
|
|
|
$qo->dataset[$qo->datasetindex]->length =
|
|
|
|
$this->import_text($dataset['#']['decimals'][0]['#']['text']);
|
|
|
|
$qo->dataset[$qo->datasetindex]->distribution =
|
|
|
|
$this->import_text($dataset['#']['distribution'][0]['#']['text']);
|
2007-03-28 05:00:36 +00:00
|
|
|
$qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
|
|
|
|
$qo->dataset[$qo->datasetindex]->datasetitem = array();
|
|
|
|
$qo->dataset[$qo->datasetindex]->itemindex = 0;
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->dataset[$qo->datasetindex]->number_of_items =
|
|
|
|
$dataset['#']['number_of_items'][0]['#'];
|
2007-03-28 05:00:36 +00:00
|
|
|
$datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
|
|
|
|
foreach ($datasetitems as $datasetitem) {
|
|
|
|
$qo->dataset[$qo->datasetindex]->itemindex++;
|
2011-06-14 17:06:55 +01:00
|
|
|
$qo->dataset[$qo->datasetindex]->datasetitem[
|
|
|
|
$qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
|
|
|
|
$qo->dataset[$qo->datasetindex]->datasetitem[
|
|
|
|
$qo->dataset[$qo->datasetindex]->itemindex]->itemnumber =
|
|
|
|
$datasetitem['#']['number'][0]['#'];
|
|
|
|
$qo->dataset[$qo->datasetindex]->datasetitem[
|
|
|
|
$qo->dataset[$qo->datasetindex]->itemindex]->value =
|
|
|
|
$datasetitem['#']['value'][0]['#'];
|
2010-11-05 06:34:00 +00:00
|
|
|
}
|
2007-03-28 05:00:36 +00:00
|
|
|
}
|
2009-09-30 13:38:36 +00:00
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
$this->import_hints($qo, $question);
|
|
|
|
|
2007-03-28 05:00:36 +00:00
|
|
|
return $qo;
|
|
|
|
}
|
|
|
|
|
2006-11-29 14:27:48 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* This is not a real question type. It's a dummy type used to specify the
|
|
|
|
* import category. The format is:
|
2006-11-29 14:27:48 +00:00
|
|
|
* <question type="category">
|
|
|
|
* <category>tom/dick/harry</category>
|
|
|
|
* </question>
|
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
protected function import_category($question) {
|
2011-02-21 18:10:19 +00:00
|
|
|
$qo = new stdClass();
|
2006-11-29 14:27:48 +00:00
|
|
|
$qo->qtype = 'category';
|
2008-01-15 14:52:49 +00:00
|
|
|
$qo->category = $this->import_text($question['#']['category'][0]['#']['text']);
|
2006-11-29 14:27:48 +00:00
|
|
|
return $qo;
|
|
|
|
}
|
|
|
|
|
2006-08-23 14:57:40 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Parse the array of lines into an array of questions
|
2006-08-23 14:57:40 +00:00
|
|
|
* this *could* burn memory - but it won't happen that much
|
|
|
|
* so fingers crossed!
|
2011-01-17 18:01:49 +00:00
|
|
|
* @param array of lines from the input file.
|
|
|
|
* @return array (of objects) question objects.
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
protected function readquestions($lines) {
|
2011-01-17 18:01:49 +00:00
|
|
|
// We just need it as one big string
|
|
|
|
$text = implode($lines, ' ');
|
2010-11-05 06:34:00 +00:00
|
|
|
unset($lines);
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
// This converts xml to big nasty data structure
|
2006-02-24 15:14:09 +00:00
|
|
|
// the 0 means keep white space as it is (important for markdown format)
|
2011-01-17 18:01:49 +00:00
|
|
|
try {
|
|
|
|
$xml = xmlize($text, 0, 'UTF-8', true);
|
2011-06-14 17:06:55 +01:00
|
|
|
} catch (xml_format_exception $e) {
|
2011-01-17 18:01:49 +00:00
|
|
|
$this->error($e->getMessage(), '');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Set up array to hold all our questions
|
2006-02-24 15:14:09 +00:00
|
|
|
$questions = array();
|
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
// Iterate through questions
|
2006-02-24 15:14:09 +00:00
|
|
|
foreach ($xml['quiz']['#']['question'] as $question) {
|
2011-01-17 18:01:49 +00:00
|
|
|
$questiontype = $question['@']['type'];
|
|
|
|
|
|
|
|
if ($questiontype == 'multichoice') {
|
|
|
|
$qo = $this->import_multichoice($question);
|
|
|
|
} else if ($questiontype == 'truefalse') {
|
|
|
|
$qo = $this->import_truefalse($question);
|
|
|
|
} else if ($questiontype == 'shortanswer') {
|
|
|
|
$qo = $this->import_shortanswer($question);
|
|
|
|
} else if ($questiontype == 'numerical') {
|
|
|
|
$qo = $this->import_numerical($question);
|
|
|
|
} else if ($questiontype == 'description') {
|
|
|
|
$qo = $this->import_description($question);
|
|
|
|
} else if ($questiontype == 'matching') {
|
|
|
|
$qo = $this->import_matching($question);
|
|
|
|
} else if ($questiontype == 'cloze') {
|
|
|
|
$qo = $this->import_multianswer($question);
|
|
|
|
} else if ($questiontype == 'essay') {
|
|
|
|
$qo = $this->import_essay($question);
|
|
|
|
} else if ($questiontype == 'calculated') {
|
|
|
|
$qo = $this->import_calculated($question);
|
2011-06-15 12:27:45 +01:00
|
|
|
} else if ($questiontype == 'calculatedsimple') {
|
|
|
|
$qo = $this->import_calculated($question);
|
|
|
|
$qo->qtype = 'calculatedsimple';
|
|
|
|
} else if ($questiontype == 'calculatedmulti') {
|
|
|
|
$qo = $this->import_calculated($question);
|
|
|
|
$qo->qtype = 'calculatedmulti';
|
2011-01-17 18:01:49 +00:00
|
|
|
} else if ($questiontype == 'category') {
|
|
|
|
$qo = $this->import_category($question);
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
} else {
|
|
|
|
// Not a type we handle ourselves. See if the question type wants
|
|
|
|
// to handle it.
|
|
|
|
if (!$qo = $this->try_importing_using_qtypes(
|
|
|
|
$question, null, null, $questiontype)) {
|
2011-02-23 18:53:50 +00:00
|
|
|
$this->error(get_string('xmltypeunsupported', 'qformat_xml', $questiontype));
|
2007-08-08 10:45:09 +00:00
|
|
|
$qo = null;
|
|
|
|
}
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
// Stick the result in the $questions array
|
2006-02-24 15:14:09 +00:00
|
|
|
if ($qo) {
|
|
|
|
$questions[] = $qo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $questions;
|
|
|
|
}
|
|
|
|
|
|
|
|
// EXPORT FUNCTIONS START HERE
|
|
|
|
|
2011-03-23 16:22:25 +00:00
|
|
|
public function export_file_extension() {
|
2010-11-08 15:51:10 +00:00
|
|
|
return '.xml';
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
|
2006-08-23 14:57:40 +00:00
|
|
|
/**
|
|
|
|
* Turn the internal question code into a human readable form
|
|
|
|
* (The code used to be numeric, but this remains as some of
|
|
|
|
* the names don't match the new internal format)
|
2011-01-17 18:01:49 +00:00
|
|
|
* @param mixed $typeid Internal code
|
2006-08-24 10:16:54 +00:00
|
|
|
* @return string question type string
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
protected function get_qtype($typeid) {
|
2011-01-17 18:01:49 +00:00
|
|
|
switch($typeid) {
|
|
|
|
case TRUEFALSE:
|
|
|
|
return 'truefalse';
|
|
|
|
case MULTICHOICE:
|
|
|
|
return 'multichoice';
|
|
|
|
case SHORTANSWER:
|
|
|
|
return 'shortanswer';
|
|
|
|
case NUMERICAL:
|
|
|
|
return 'numerical';
|
|
|
|
case MATCH:
|
|
|
|
return 'matching';
|
|
|
|
case DESCRIPTION:
|
|
|
|
return 'description';
|
|
|
|
case MULTIANSWER:
|
|
|
|
return 'cloze';
|
|
|
|
case ESSAY:
|
|
|
|
return 'essay';
|
|
|
|
case CALCULATED:
|
|
|
|
return 'calculated';
|
|
|
|
default:
|
|
|
|
return false;
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2006-08-23 14:57:40 +00:00
|
|
|
* Convert internal Moodle text format code into
|
|
|
|
* human readable form
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param int id internal code
|
|
|
|
* @return string format text
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
protected function get_format($id) {
|
2011-01-17 18:01:49 +00:00
|
|
|
switch($id) {
|
|
|
|
case FORMAT_MOODLE:
|
|
|
|
return 'moodle_auto_format';
|
|
|
|
case FORMAT_HTML:
|
|
|
|
return 'html';
|
|
|
|
case FORMAT_PLAIN:
|
|
|
|
return 'plain_text';
|
|
|
|
case FORMAT_WIKI:
|
|
|
|
return 'wiki_like';
|
|
|
|
case FORMAT_MARKDOWN:
|
|
|
|
return 'markdown';
|
|
|
|
default:
|
|
|
|
return 'unknown';
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2009-09-30 13:38:36 +00:00
|
|
|
* Convert internal single question code into
|
2006-08-23 14:57:40 +00:00
|
|
|
* human readable form
|
2006-08-24 10:16:54 +00:00
|
|
|
* @param int id single question code
|
|
|
|
* @return string single question string
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function get_single($id) {
|
2011-01-17 18:01:49 +00:00
|
|
|
switch($id) {
|
|
|
|
case 0:
|
|
|
|
return 'false';
|
|
|
|
case 1:
|
|
|
|
return 'true';
|
|
|
|
default:
|
|
|
|
return 'unknown';
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2011-01-17 18:01:49 +00:00
|
|
|
* Generates <text></text> tags, processing raw text therein
|
|
|
|
* @param string $raw the content to output.
|
|
|
|
* @param int $indent the current indent level.
|
2011-02-23 16:25:25 +00:00
|
|
|
* @param bool $short stick it on one line.
|
2011-01-17 18:01:49 +00:00
|
|
|
* @return string formatted text.
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function writetext($raw, $indent = 0, $short = true) {
|
2011-01-17 18:01:49 +00:00
|
|
|
$indent = str_repeat(' ', $indent);
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2008-09-19 10:01:47 +00:00
|
|
|
// if required add CDATA tags
|
2011-01-17 18:01:49 +00:00
|
|
|
if (!empty($raw) && htmlspecialchars($raw) != $raw) {
|
2008-09-19 10:01:47 +00:00
|
|
|
$raw = "<![CDATA[$raw]]>";
|
|
|
|
}
|
2006-02-24 15:14:09 +00:00
|
|
|
|
|
|
|
if ($short) {
|
2011-01-17 18:01:49 +00:00
|
|
|
$xml = "$indent<text>$raw</text>\n";
|
2010-11-19 12:33:48 +00:00
|
|
|
} else {
|
2006-02-24 15:14:09 +00:00
|
|
|
$xml = "$indent<text>\n$raw\n$indent</text>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $xml;
|
|
|
|
}
|
2009-09-30 13:38:36 +00:00
|
|
|
|
2011-03-23 16:22:25 +00:00
|
|
|
protected function presave_process($content) {
|
2011-01-17 18:01:49 +00:00
|
|
|
// Override to allow us to add xml headers and footers
|
|
|
|
return '<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<quiz>
|
|
|
|
' . $content . '</quiz>';
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
|
2006-08-24 10:16:54 +00:00
|
|
|
/**
|
2006-08-23 14:57:40 +00:00
|
|
|
* Turns question into an xml segment
|
2011-01-17 18:01:49 +00:00
|
|
|
* @param object $question the question data.
|
2006-08-24 10:16:54 +00:00
|
|
|
* @return string xml segment
|
2006-08-23 14:57:40 +00:00
|
|
|
*/
|
2011-03-23 16:22:25 +00:00
|
|
|
public function writequestion($question) {
|
2011-02-24 15:28:25 +00:00
|
|
|
global $CFG, $OUTPUT;
|
2010-11-05 06:34:00 +00:00
|
|
|
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$contextid = $question->contextid;
|
2011-01-18 16:06:05 +00:00
|
|
|
// Get files used by the questiontext.
|
|
|
|
$question->questiontextfiles = $fs->get_area_files(
|
|
|
|
$contextid, 'question', 'questiontext', $question->id);
|
|
|
|
// Get files used by the generalfeedback.
|
|
|
|
$question->generalfeedbackfiles = $fs->get_area_files(
|
|
|
|
$contextid, 'question', 'generalfeedback', $question->id);
|
2011-01-17 18:01:49 +00:00
|
|
|
if (!empty($question->options->answers)) {
|
|
|
|
foreach ($question->options->answers as $answer) {
|
2011-06-16 20:24:44 +01:00
|
|
|
$answer->answerfiles = $fs->get_area_files(
|
|
|
|
$contextid, 'question', 'answer', $answer->id);
|
2011-01-18 16:06:05 +00:00
|
|
|
$answer->feedbackfiles = $fs->get_area_files(
|
|
|
|
$contextid, 'question', 'answerfeedback', $answer->id);
|
2011-01-17 18:01:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$expout = '';
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
// Add a comment linking this to the original question id.
|
|
|
|
$expout .= "<!-- question: $question->id -->\n";
|
2006-02-24 15:14:09 +00:00
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
// Check question type
|
|
|
|
if (!$questiontype = $this->get_qtype($question->qtype)) {
|
2007-08-08 10:45:09 +00:00
|
|
|
// must be a plugin then, so just accept the name supplied
|
2011-01-17 18:01:49 +00:00
|
|
|
$questiontype = $question->qtype;
|
2007-05-08 14:24:13 +00:00
|
|
|
}
|
|
|
|
|
2006-02-24 15:14:09 +00:00
|
|
|
// add opening tag
|
2007-02-07 10:29:53 +00:00
|
|
|
// generates specific header for Cloze and category type question
|
|
|
|
if ($question->qtype == 'category') {
|
2011-01-17 18:01:49 +00:00
|
|
|
$categorypath = $this->writetext($question->category);
|
2007-02-07 10:29:53 +00:00
|
|
|
$expout .= " <question type=\"category\">\n";
|
|
|
|
$expout .= " <category>\n";
|
2007-10-09 10:16:01 +00:00
|
|
|
$expout .= " $categorypath\n";
|
2007-02-07 10:29:53 +00:00
|
|
|
$expout .= " </category>\n";
|
|
|
|
$expout .= " </question>\n";
|
|
|
|
return $expout;
|
2011-01-17 18:01:49 +00:00
|
|
|
|
|
|
|
} else if ($question->qtype != MULTIANSWER) {
|
2006-06-02 09:45:19 +00:00
|
|
|
// for all question types except Close
|
2011-01-17 18:01:49 +00:00
|
|
|
$name_text = $this->writetext($question->name, 3);
|
2010-11-05 06:34:00 +00:00
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
$expout .= " <question type=\"$questiontype\">\n";
|
|
|
|
$expout .= " <name>\n";
|
|
|
|
$expout .= $name_text;
|
|
|
|
$expout .= " </name>\n";
|
2011-01-18 14:36:40 +00:00
|
|
|
$expout .= " <questiontext {$this->format($question->questiontextformat)}>\n";
|
2011-06-14 16:34:14 +01:00
|
|
|
$expout .= $this->writetext($question->questiontext, 3);
|
|
|
|
$expout .= $this->writefiles($question->questiontextfiles);
|
2009-09-30 13:38:36 +00:00
|
|
|
$expout .= " </questiontext>\n";
|
2011-01-18 14:36:40 +00:00
|
|
|
$expout .= " <generalfeedback {$this->format($question->generalfeedbackformat)}>\n";
|
2011-06-14 16:34:14 +01:00
|
|
|
$expout .= $this->writetext($question->generalfeedback, 3);
|
|
|
|
$expout .= $this->writefiles($question->generalfeedbackfiles);
|
2006-09-19 13:35:42 +00:00
|
|
|
$expout .= " </generalfeedback>\n";
|
2011-01-17 18:01:49 +00:00
|
|
|
$expout .= " <defaultgrade>{$question->defaultmark}</defaultgrade>\n";
|
2006-06-02 09:45:19 +00:00
|
|
|
$expout .= " <penalty>{$question->penalty}</penalty>\n";
|
|
|
|
$expout .= " <hidden>{$question->hidden}</hidden>\n";
|
2011-01-17 18:01:49 +00:00
|
|
|
|
2010-11-05 06:34:00 +00:00
|
|
|
} else {
|
2006-06-02 09:45:19 +00:00
|
|
|
// for Cloze type only
|
2011-06-14 17:06:55 +01:00
|
|
|
$name_text = $this->writetext($question->name);
|
|
|
|
$question_text = $this->writetext($question->questiontext);
|
|
|
|
$generalfeedback = $this->writetext($question->generalfeedback);
|
2011-06-14 16:34:14 +01:00
|
|
|
$expout .= " <question type=\"$questiontype\">\n";
|
|
|
|
$expout .= " <name>\n";
|
|
|
|
$expout .= $name_text;
|
|
|
|
$expout .= " </name>\n";
|
2006-06-02 09:45:19 +00:00
|
|
|
$expout .= " <questiontext>\n";
|
2011-06-14 16:34:14 +01:00
|
|
|
$expout .= $this->writetext($question->questiontext, 3);
|
|
|
|
$expout .= $this->writefiles($question->questiontextfiles);
|
2006-06-02 09:45:19 +00:00
|
|
|
$expout .= " </questiontext>\n";
|
2007-09-24 14:17:23 +00:00
|
|
|
$expout .= " <generalfeedback>\n";
|
2011-06-14 16:34:14 +01:00
|
|
|
$expout .= $this->writetext($question->generalfeedback, 3);
|
|
|
|
$expout .= $this->writefiles($question->generalfeedbackfiles);
|
2007-09-24 14:17:23 +00:00
|
|
|
$expout .= " </generalfeedback>\n";
|
2006-06-02 09:45:19 +00:00
|
|
|
}
|
|
|
|
|
2006-02-24 15:14:09 +00:00
|
|
|
// output depends on question type
|
|
|
|
switch($question->qtype) {
|
2011-06-14 17:06:55 +01:00
|
|
|
case 'category':
|
|
|
|
// not a qtype really - dummy used for category switching
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'truefalse':
|
|
|
|
$trueanswer = $question->options->answers[$question->options->trueanswer];
|
|
|
|
$trueanswer->answer = 'true';
|
|
|
|
$expout .= $this->write_answer($trueanswer);
|
|
|
|
|
|
|
|
$falseanswer = $question->options->answers[$question->options->falseanswer];
|
|
|
|
$falseanswer->answer = 'false';
|
|
|
|
$expout .= $this->write_answer($falseanswer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'multichoice':
|
|
|
|
$expout .= " <single>" . $this->get_single($question->options->single) .
|
|
|
|
"</single>\n";
|
|
|
|
$expout .= " <shuffleanswers>" .
|
|
|
|
$this->get_single($question->options->shuffleanswers) .
|
|
|
|
"</shuffleanswers>\n";
|
|
|
|
$expout .= " <answernumbering>" . $question->options->answernumbering .
|
|
|
|
"</answernumbering>\n";
|
|
|
|
$expout .= $this->write_combined_feedback($question->options);
|
|
|
|
$expout .= $this->write_answers($question->options->answers);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'shortanswer':
|
|
|
|
$expout .= " <usecase>{$question->options->usecase}</usecase>\n";
|
|
|
|
$expout .= $this->write_answers($question->options->answers);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'numerical':
|
|
|
|
foreach ($question->options->answers as $answer) {
|
|
|
|
$expout .= $this->write_answer($answer,
|
|
|
|
" <tolerance>$answer->tolerance</tolerance>\n");
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
2010-11-05 06:34:00 +00:00
|
|
|
|
|
|
|
$units = $question->options->units;
|
|
|
|
if (count($units)) {
|
|
|
|
$expout .= "<units>\n";
|
|
|
|
foreach ($units as $unit) {
|
|
|
|
$expout .= " <unit>\n";
|
|
|
|
$expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
|
|
|
|
$expout .= " <unit_name>{$unit->unit}</unit_name>\n";
|
|
|
|
$expout .= " </unit>\n";
|
|
|
|
}
|
|
|
|
$expout .= "</units>\n";
|
2007-03-28 05:00:36 +00:00
|
|
|
}
|
2011-06-14 17:06:55 +01:00
|
|
|
if (isset($question->options->unitgradingtype)) {
|
|
|
|
$expout .= " <unitgradingtype>" . $question->options->unitgradingtype .
|
|
|
|
"</unitgradingtype>\n";
|
|
|
|
}
|
|
|
|
if (isset($question->options->unitpenalty)) {
|
|
|
|
$expout .= " <unitpenalty>{$question->options->unitpenalty}</unitpenalty>\n";
|
|
|
|
}
|
|
|
|
if (isset($question->options->showunits)) {
|
|
|
|
$expout .= " <showunits>{$question->options->showunits}</showunits>\n";
|
|
|
|
}
|
|
|
|
if (isset($question->options->unitsleft)) {
|
|
|
|
$expout .= " <unitsleft>{$question->options->unitsleft}</unitsleft>\n";
|
|
|
|
}
|
|
|
|
if (!empty($question->options->instructionsformat)) {
|
|
|
|
$files = $fs->get_area_files($contextid, 'qtype_numerical',
|
|
|
|
'instruction', $question->id);
|
|
|
|
$expout .= " <instructions " .
|
|
|
|
$this->format($question->options->instructionsformat) . ">\n";
|
|
|
|
$expout .= $this->writetext($question->options->instructions, 3);
|
|
|
|
$expout .= $this->writefiles($files);
|
|
|
|
$expout .= " </instructions>\n";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'match':
|
|
|
|
$expout .= " <shuffleanswers>" .
|
|
|
|
$this->get_single($question->options->shuffleanswers) .
|
|
|
|
"</shuffleanswers>\n";
|
|
|
|
$expout .= $this->write_combined_feedback($question->options);
|
|
|
|
foreach ($question->options->subquestions as $subquestion) {
|
|
|
|
$files = $fs->get_area_files($contextid, 'qtype_match',
|
|
|
|
'subquestion', $subquestion->id);
|
|
|
|
$expout .= " <subquestion " .
|
|
|
|
$this->format($subquestion->questiontextformat) . ">\n";
|
|
|
|
$expout .= $this->writetext($subquestion->questiontext, 3);
|
|
|
|
$expout .= $this->writefiles($files);
|
|
|
|
$expout .= " <answer>\n";
|
|
|
|
$expout .= $this->writetext($subquestion->answertext, 4);
|
|
|
|
$expout .= " </answer>\n";
|
|
|
|
$expout .= " </subquestion>\n";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'description':
|
|
|
|
// Nothing else to do.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'multianswer':
|
|
|
|
$acount = 1;
|
|
|
|
foreach ($question->options->questions as $question) {
|
|
|
|
$thispattern = "{#".$acount."}";
|
|
|
|
$thisreplace = $question->questiontext;
|
|
|
|
$expout = preg_replace("~$thispattern~", $thisreplace, $expout);
|
|
|
|
$acount++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'essay':
|
2011-06-14 17:36:11 +01:00
|
|
|
$expout .= " <responseformat>" . $question->options->responseformat .
|
|
|
|
"</responseformat>\n";
|
|
|
|
$expout .= " <responsefieldlines>" . $question->options->responsefieldlines .
|
|
|
|
"</responsefieldlines>\n";
|
|
|
|
$expout .= " <attachments>" . $question->options->attachments .
|
|
|
|
"</attachments>\n";
|
|
|
|
$expout .= " <graderinfo " .
|
|
|
|
$this->format($question->options->graderinfoformat) . ">\n";
|
|
|
|
$expout .= $this->writetext($question->options->graderinfo, 3);
|
|
|
|
$expout .= " </graderinfo>\n";
|
2011-06-14 17:06:55 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'calculated':
|
|
|
|
case 'calculatedsimple':
|
|
|
|
case 'calculatedmulti':
|
|
|
|
$expout .= " <synchronize>{$question->options->synchronize}</synchronize>\n";
|
|
|
|
$expout .= " <single>{$question->options->single}</single>\n";
|
|
|
|
$expout .= " <answernumbering>" . $question->options->answernumbering .
|
|
|
|
"</answernumbering>\n";
|
2011-06-15 12:27:45 +01:00
|
|
|
$expout .= " <shuffleanswers>" . $question->options->shuffleanswers .
|
2011-06-14 17:06:55 +01:00
|
|
|
"</shuffleanswers>\n";
|
|
|
|
|
|
|
|
$component = 'qtype_' . $question->qtype;
|
|
|
|
$files = $fs->get_area_files($contextid, $component,
|
|
|
|
'correctfeedback', $question->id);
|
|
|
|
$expout .= " <correctfeedback>\n";
|
|
|
|
$expout .= $this->writetext($question->options->correctfeedback, 3);
|
|
|
|
$expout .= $this->writefiles($files);
|
|
|
|
$expout .= " </correctfeedback>\n";
|
|
|
|
|
|
|
|
$files = $fs->get_area_files($contextid, $component,
|
|
|
|
'partiallycorrectfeedback', $question->id);
|
|
|
|
$expout .= " <partiallycorrectfeedback>\n";
|
|
|
|
$expout .= $this->writetext($question->options->partiallycorrectfeedback, 3);
|
|
|
|
$expout .= $this->writefiles($files);
|
|
|
|
$expout .= " </partiallycorrectfeedback>\n";
|
|
|
|
|
|
|
|
$files = $fs->get_area_files($contextid, $component,
|
|
|
|
'incorrectfeedback', $question->id);
|
|
|
|
$expout .= " <incorrectfeedback>\n";
|
|
|
|
$expout .= $this->writetext($question->options->incorrectfeedback, 3);
|
|
|
|
$expout .= $this->writefiles($files);
|
|
|
|
$expout .= " </incorrectfeedback>\n";
|
|
|
|
|
|
|
|
foreach ($question->options->answers as $answer) {
|
|
|
|
$percent = 100 * $answer->fraction;
|
|
|
|
$expout .= "<answer fraction=\"$percent\">\n";
|
|
|
|
// "<text/>" tags are an added feature, old files won't have them
|
|
|
|
$expout .= " <text>{$answer->answer}</text>\n";
|
|
|
|
$expout .= " <tolerance>{$answer->tolerance}</tolerance>\n";
|
|
|
|
$expout .= " <tolerancetype>{$answer->tolerancetype}</tolerancetype>\n";
|
|
|
|
$expout .= " <correctanswerformat>" .
|
|
|
|
$answer->correctanswerformat . "</correctanswerformat>\n";
|
|
|
|
$expout .= " <correctanswerlength>" .
|
|
|
|
$answer->correctanswerlength . "</correctanswerlength>\n";
|
|
|
|
$expout .= " <feedback {$this->format($answer->feedbackformat)}>\n";
|
|
|
|
$files = $fs->get_area_files($contextid, $component,
|
|
|
|
'instruction', $question->id);
|
|
|
|
$expout .= $this->writetext($answer->feedback);
|
|
|
|
$expout .= $this->writefiles($answer->feedbackfiles);
|
|
|
|
$expout .= " </feedback>\n";
|
|
|
|
$expout .= "</answer>\n";
|
|
|
|
}
|
|
|
|
if (isset($question->options->unitgradingtype)) {
|
|
|
|
$expout .= " <unitgradingtype>" .
|
|
|
|
$question->options->unitgradingtype . "</unitgradingtype>\n";
|
|
|
|
}
|
|
|
|
if (isset($question->options->unitpenalty)) {
|
|
|
|
$expout .= " <unitpenalty>" .
|
|
|
|
$question->options->unitpenalty . "</unitpenalty>\n";
|
|
|
|
}
|
|
|
|
if (isset($question->options->showunits)) {
|
|
|
|
$expout .= " <showunits>{$question->options->showunits}</showunits>\n";
|
|
|
|
}
|
|
|
|
if (isset($question->options->unitsleft)) {
|
|
|
|
$expout .= " <unitsleft>{$question->options->unitsleft}</unitsleft>\n";
|
|
|
|
}
|
2011-02-24 15:28:25 +00:00
|
|
|
|
2011-06-14 17:06:55 +01:00
|
|
|
if (isset($question->options->instructionsformat)) {
|
|
|
|
$files = $fs->get_area_files($contextid, $component,
|
|
|
|
'instruction', $question->id);
|
|
|
|
$expout .= " <instructions " .
|
|
|
|
$this->format($question->options->instructionsformat) . ">\n";
|
|
|
|
$expout .= $this->writetext($question->options->instructions, 3);
|
|
|
|
$expout .= $this->writefiles($files);
|
|
|
|
$expout .= " </instructions>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($question->options->units)) {
|
|
|
|
$units = $question->options->units;
|
|
|
|
if (count($units)) {
|
|
|
|
$expout .= "<units>\n";
|
|
|
|
foreach ($units as $unit) {
|
|
|
|
$expout .= " <unit>\n";
|
|
|
|
$expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
|
|
|
|
$expout .= " <unit_name>{$unit->unit}</unit_name>\n";
|
|
|
|
$expout .= " </unit>\n";
|
|
|
|
}
|
|
|
|
$expout .= "</units>\n";
|
2009-06-02 17:12:27 +00:00
|
|
|
}
|
2011-06-14 17:06:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The tag $question->export_process has been set so we get all the
|
|
|
|
// data items in the database from the function
|
|
|
|
// qtype_calculated::get_question_options calculatedsimple defaults
|
|
|
|
// to calculated
|
|
|
|
if (isset($question->options->datasets) && count($question->options->datasets)) {
|
|
|
|
$expout .= "<dataset_definitions>\n";
|
|
|
|
foreach ($question->options->datasets as $def) {
|
|
|
|
$expout .= "<dataset_definition>\n";
|
|
|
|
$expout .= " <status>".$this->writetext($def->status)."</status>\n";
|
|
|
|
$expout .= " <name>".$this->writetext($def->name)."</name>\n";
|
|
|
|
if ($question->qtype == CALCULATED) {
|
|
|
|
$expout .= " <type>calculated</type>\n";
|
|
|
|
} else {
|
|
|
|
$expout .= " <type>calculatedsimple</type>\n";
|
|
|
|
}
|
|
|
|
$expout .= " <distribution>" . $this->writetext($def->distribution) .
|
|
|
|
"</distribution>\n";
|
|
|
|
$expout .= " <minimum>" . $this->writetext($def->minimum) .
|
|
|
|
"</minimum>\n";
|
|
|
|
$expout .= " <maximum>" . $this->writetext($def->maximum) .
|
|
|
|
"</maximum>\n";
|
|
|
|
$expout .= " <decimals>" . $this->writetext($def->decimals) .
|
|
|
|
"</decimals>\n";
|
|
|
|
$expout .= " <itemcount>$def->itemcount</itemcount>\n";
|
|
|
|
if ($def->itemcount > 0) {
|
|
|
|
$expout .= " <dataset_items>\n";
|
|
|
|
foreach ($def->items as $item) {
|
|
|
|
$expout .= " <dataset_item>\n";
|
|
|
|
$expout .= " <number>".$item->itemnumber."</number>\n";
|
|
|
|
$expout .= " <value>".$item->value."</value>\n";
|
|
|
|
$expout .= " </dataset_item>\n";
|
|
|
|
}
|
|
|
|
$expout .= " </dataset_items>\n";
|
|
|
|
$expout .= " <number_of_items>" . $def->number_of_items .
|
|
|
|
"</number_of_items>\n";
|
2009-09-30 13:38:36 +00:00
|
|
|
}
|
2011-06-14 17:06:55 +01:00
|
|
|
$expout .= "</dataset_definition>\n";
|
|
|
|
}
|
|
|
|
$expout .= "</dataset_definitions>\n";
|
2009-09-30 13:38:36 +00:00
|
|
|
}
|
2011-06-14 17:06:55 +01:00
|
|
|
break;
|
2011-01-17 18:01:49 +00:00
|
|
|
|
2011-06-14 17:06:55 +01:00
|
|
|
default:
|
|
|
|
// try support by optional plugin
|
|
|
|
if (!$data = $this->try_exporting_using_qtypes($question->qtype, $question)) {
|
|
|
|
notify(get_string('unsupportedexport', 'qformat_xml', $question->qtype));
|
|
|
|
}
|
|
|
|
$expout .= $data;
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
// Output any hints.
|
|
|
|
$expout .= $this->write_hints($question);
|
|
|
|
|
2010-11-19 12:33:48 +00:00
|
|
|
// Write the question tags.
|
|
|
|
if (!empty($CFG->usetags)) {
|
|
|
|
require_once($CFG->dirroot.'/tag/lib.php');
|
|
|
|
$tags = tag_get_tags_array('question', $question->id);
|
|
|
|
if (!empty($tags)) {
|
|
|
|
$expout .= " <tags>\n";
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
$expout .= " <tag>" . $this->writetext($tag, 0, true) . "</tag>\n";
|
|
|
|
}
|
|
|
|
$expout .= " </tags>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-24 15:14:09 +00:00
|
|
|
// close the question tag
|
2011-01-17 18:01:49 +00:00
|
|
|
$expout .= " </question>\n";
|
2006-02-24 15:14:09 +00:00
|
|
|
|
|
|
|
return $expout;
|
|
|
|
}
|
2011-01-17 18:01:49 +00:00
|
|
|
|
|
|
|
public function write_answers($answers) {
|
|
|
|
if (empty($answers)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$output = '';
|
|
|
|
foreach ($answers as $answer) {
|
|
|
|
$output .= $this->write_answer($answer);
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function write_answer($answer, $extra = '') {
|
|
|
|
$percent = $answer->fraction * 100;
|
|
|
|
$output = '';
|
2011-03-23 14:27:22 +00:00
|
|
|
$output .= " <answer fraction=\"$percent\" {$this->format($answer->answerformat)}>\n";
|
2011-01-17 18:01:49 +00:00
|
|
|
$output .= $this->writetext($answer->answer, 3);
|
2011-06-16 20:24:44 +01:00
|
|
|
$output .= $this->writefiles($answer->answerfiles);
|
2011-01-18 14:36:40 +00:00
|
|
|
$output .= " <feedback {$this->format($answer->feedbackformat)}>\n";
|
2011-01-17 18:01:49 +00:00
|
|
|
$output .= $this->writetext($answer->feedback, 4);
|
2011-01-18 14:36:40 +00:00
|
|
|
$output .= $this->writefiles($answer->feedbackfiles);
|
2011-01-17 18:01:49 +00:00
|
|
|
$output .= " </feedback>\n";
|
|
|
|
$output .= $extra;
|
|
|
|
$output .= " </answer>\n";
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function write_hints($question) {
|
|
|
|
if (empty($question->hints)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = '';
|
|
|
|
foreach ($question->hints as $hint) {
|
|
|
|
$output .= $this->write_hint($hint);
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2011-01-18 14:36:40 +00:00
|
|
|
/**
|
|
|
|
* @param unknown_type $format a FORMAT_... constant.
|
|
|
|
* @return string the attribute to add to an XML tag.
|
|
|
|
*/
|
|
|
|
protected function format($format) {
|
|
|
|
return 'format="' . $this->get_format($format) . '"';
|
|
|
|
}
|
|
|
|
|
2011-01-17 18:01:49 +00:00
|
|
|
public function write_hint($hint) {
|
|
|
|
$output = '';
|
2011-01-18 14:36:40 +00:00
|
|
|
$output .= " <hint {$this->format($hint->hintformat)}>\n";
|
2011-01-17 18:01:49 +00:00
|
|
|
$output .= ' ' . $this->writetext($hint->hint);
|
|
|
|
if (!empty($hint->shownumcorrect)) {
|
|
|
|
$output .= " <shownumcorrect/>\n";
|
|
|
|
}
|
|
|
|
if (!empty($hint->clearwrong)) {
|
|
|
|
$output .= " <clearwrong/>\n";
|
|
|
|
}
|
|
|
|
if (!empty($hint->options)) {
|
|
|
|
$output .= ' <options>' . htmlspecialchars($hint->options) . "</options>\n";
|
|
|
|
}
|
|
|
|
$output .= " </hint>\n";
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function write_combined_feedback($questionoptions) {
|
2011-01-18 14:36:40 +00:00
|
|
|
$output = " <correctfeedback {$this->format($questionoptions->correctfeedbackformat)}>
|
2011-01-17 18:01:49 +00:00
|
|
|
{$this->writetext($questionoptions->correctfeedback)} </correctfeedback>
|
2011-01-18 14:36:40 +00:00
|
|
|
<partiallycorrectfeedback {$this->format($questionoptions->partiallycorrectfeedbackformat)}>
|
2011-01-17 18:01:49 +00:00
|
|
|
{$this->writetext($questionoptions->partiallycorrectfeedback)} </partiallycorrectfeedback>
|
2011-01-18 14:36:40 +00:00
|
|
|
<incorrectfeedback {$this->format($questionoptions->incorrectfeedbackformat)}>
|
2011-01-17 18:01:49 +00:00
|
|
|
{$this->writetext($questionoptions->incorrectfeedback)} </incorrectfeedback>\n";
|
|
|
|
if (!empty($questionoptions->shownumcorrect)) {
|
|
|
|
$output .= " <shownumcorrect/>\n";
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
2006-02-24 15:14:09 +00:00
|
|
|
}
|