Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-39230 Question bank - refactor xml import

Allow for reuse of code to read xml questions.
  • Loading branch information...
commit 397acdfda21852f23c3edbb83716db7260120b6b 1 parent cf5a329
@jamiepratt jamiepratt authored damyon committed
Showing with 56 additions and 40 deletions.
  1. +56 −40 question/format/xml/format.php
View
96 question/format/xml/format.php
@@ -911,50 +911,19 @@ protected function readquestions($lines) {
return false;
}
unset($lines); // No need to keep this in memory.
+ return $this->import_questions($xml['quiz']['#']['question']);
+ }
- // Set up array to hold all our questions
+ /**
+ * @param array $xml the xmlized xml
+ * @return stdClass[] question objects to pass to question type save_question_options
+ */
+ public function import_questions($xml) {
$questions = array();
// Iterate through questions
- foreach ($xml['quiz']['#']['question'] as $question) {
- $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' || $questiontype == 'match') {
- $qo = $this->import_match($question);
- } else if ($questiontype == 'cloze' || $questiontype == 'multianswer') {
- $qo = $this->import_multianswer($question);
- } else if ($questiontype == 'essay') {
- $qo = $this->import_essay($question);
- } else if ($questiontype == 'calculated') {
- $qo = $this->import_calculated($question);
- } else if ($questiontype == 'calculatedsimple') {
- $qo = $this->import_calculated($question);
- $qo->qtype = 'calculatedsimple';
- } else if ($questiontype == 'calculatedmulti') {
- $qo = $this->import_calculated($question);
- $qo->qtype = 'calculatedmulti';
- } else if ($questiontype == 'category') {
- $qo = $this->import_category($question);
-
- } 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)) {
- $this->error(get_string('xmltypeunsupported', 'qformat_xml', $questiontype));
- $qo = null;
- }
- }
+ foreach ($xml as $questionxml) {
+ $qo = $this->import_question($questionxml);
// Stick the result in the $questions array
if ($qo) {
@@ -964,6 +933,53 @@ protected function readquestions($lines) {
return $questions;
}
+ /**
+ * @param array $questionxml xml describing the question
+ * @return null|stdClass an object with data to be fed to question type save_question_options
+ */
+ protected function import_question($questionxml) {
+ $questiontype = $questionxml['@']['type'];
+
+ if ($questiontype == 'multichoice') {
+ return $this->import_multichoice($questionxml);
+ } else if ($questiontype == 'truefalse') {
+ return $this->import_truefalse($questionxml);
+ } else if ($questiontype == 'shortanswer') {
+ return $this->import_shortanswer($questionxml);
+ } else if ($questiontype == 'numerical') {
+ return $this->import_numerical($questionxml);
+ } else if ($questiontype == 'description') {
+ return $this->import_description($questionxml);
+ } else if ($questiontype == 'matching' || $questiontype == 'match') {
+ return $this->import_match($questionxml);
+ } else if ($questiontype == 'cloze' || $questiontype == 'multianswer') {
+ return $this->import_multianswer($questionxml);
+ } else if ($questiontype == 'essay') {
+ return $this->import_essay($questionxml);
+ } else if ($questiontype == 'calculated') {
+ return $this->import_calculated($questionxml);
+ } else if ($questiontype == 'calculatedsimple') {
+ $qo = $this->import_calculated($questionxml);
+ $qo->qtype = 'calculatedsimple';
+ return $qo;
+ } else if ($questiontype == 'calculatedmulti') {
+ $qo = $this->import_calculated($questionxml);
+ $qo->qtype = 'calculatedmulti';
+ return $qo;
+ } else if ($questiontype == 'category') {
+ return $this->import_category($questionxml);
+
+ } else {
+ // Not a type we handle ourselves. See if the question type wants
+ // to handle it.
+ if (!$qo = $this->try_importing_using_qtypes($questionxml, null, null, $questiontype)) {
+ $this->error(get_string('xmltypeunsupported', 'qformat_xml', $questiontype));
+ return null;
+ }
+ return $qo;
+ }
+ }
+
// EXPORT FUNCTIONS START HERE
public function export_file_extension() {
Please sign in to comment.
Something went wrong with that request. Please try again.