From dcd4464ea34b3df76ba9578cd3945d181f067575 Mon Sep 17 00:00:00 2001
From: Eoin Campbell
Date: Wed, 5 Aug 2015 19:11:11 +0100
Subject: [PATCH] MDL-50642 qformat_xml: fix import of tags in Cloze questions
---
question/format/xml/format.php | 32 ++++++++++++++------
question/format/xml/tests/xmlformat_test.php | 28 +++++++++++++++++
2 files changed, 51 insertions(+), 9 deletions(-)
diff --git a/question/format/xml/format.php b/question/format/xml/format.php
index 4ddff96d502..8bcada5abd7 100644
--- a/question/format/xml/format.php
+++ b/question/format/xml/format.php
@@ -198,7 +198,7 @@ class qformat_xml extends qformat_default {
* @return object question object
*/
public function import_headers($question) {
- global $CFG, $USER;
+ global $USER;
// This routine initialises the question object.
$qo = $this->defaultquestion();
@@ -255,14 +255,7 @@ class qformat_xml extends qformat_default {
}
// 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);
- }
- }
+ $this->import_question_tags($qo, $question);
return $qo;
}
@@ -380,6 +373,26 @@ class qformat_xml extends qformat_default {
}
}
+ /**
+ * Import all the question tags
+ *
+ * @param object $qo the question data that is being constructed.
+ * @param array $questionxml The xml representing the question.
+ * @return array of objects representing the tags in the file.
+ */
+ public function import_question_tags($qo, $questionxml) {
+ global $CFG;
+
+ if (!empty($CFG->usetags) && array_key_exists('tags', $questionxml['#'])
+ && !empty($questionxml['#']['tags'][0]['#']['tag'])) {
+ require_once($CFG->dirroot.'/tag/lib.php');
+ $qo->tags = array();
+ foreach ($questionxml['#']['tags'][0]['#']['tag'] as $tagdata) {
+ $qo->tags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true);
+ }
+ }
+ }
+
/**
* Import files from a node in the XML.
* @param array $xml an array of nodes from the the parsed XML.
@@ -505,6 +518,7 @@ class qformat_xml extends qformat_default {
}
$this->import_hints($qo, $question, true, false, $this->get_format($qo->questiontextformat));
+ $this->import_question_tags($qo, $question);
return $qo;
}
diff --git a/question/format/xml/tests/xmlformat_test.php b/question/format/xml/tests/xmlformat_test.php
index fbaa93d00c6..acd73bbfd36 100644
--- a/question/format/xml/tests/xmlformat_test.php
+++ b/question/format/xml/tests/xmlformat_test.php
@@ -29,6 +29,7 @@ global $CFG;
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/format/xml/format.php');
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
+require_once($CFG->dirroot . '/tag/lib.php');
/**
@@ -302,6 +303,10 @@ END;
0
0
0
+
+ tagDescription
+ tagTest
+
';
$xmldata = xmlize($xml);
@@ -317,6 +322,7 @@ END;
$expectedq->defaultmark = 0;
$expectedq->length = 0;
$expectedq->penalty = 0;
+ $expectedq->tags = array('tagDescription', 'tagTest');
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
@@ -373,6 +379,11 @@ END;
1
0
0
+
+ tagEssay
+ tagEssay20
+ tagTest
+
';
$xmldata = xmlize($xml);
@@ -397,6 +408,7 @@ END;
$expectedq->graderinfo['format'] = FORMAT_MOODLE;
$expectedq->responsetemplate['text'] = '';
$expectedq->responsetemplate['format'] = FORMAT_MOODLE;
+ $expectedq->tags = array('tagEssay', 'tagEssay20', 'tagTest');
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
@@ -426,6 +438,11 @@ END;
Here is something really interesting.
]]>
+
+ tagEssay
+ tagEssay21
+ tagTest
+
';
$xmldata = xmlize($xml);
@@ -450,6 +467,7 @@ END;
$expectedq->graderinfo['format'] = FORMAT_HTML;
$expectedq->responsetemplate['text'] = 'Here is something really interesting.
';
$expectedq->responsetemplate['format'] = FORMAT_HTML;
+ $expectedq->tags = array('tagEssay', 'tagEssay21', 'tagTest');
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
@@ -571,6 +589,10 @@ END;
+
+ tagMatching
+ tagTest
+
';
$xmldata = xmlize($xml);
@@ -607,6 +629,7 @@ END;
);
$expectedq->hintshownumcorrect = array(true, true);
$expectedq->hintclearwrong = array(false, true);
+ $expectedq->tags = array('tagMatching', 'tagTest');
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
@@ -1343,6 +1366,10 @@ END;
Hint 2
+
+ tagCloze
+ tagTest
+
';
$xmldata = xmlize($xml);
@@ -1419,6 +1446,7 @@ END;
1 => $sa,
2 => $mc,
);
+ $expectedqa->tags = array('tagCloze', 'tagTest');
$this->assertEquals($expectedqa->hint, $q->hint);
$this->assertEquals($expectedqa->options->questions[1], $q->options->questions[1]);
--
2.43.0