Skip to content

Commit

Permalink
MDL-50642 qformat_xml: fix import of tags in Cloze questions
Browse files Browse the repository at this point in the history
  • Loading branch information
ecampbell committed Aug 5, 2015
1 parent 032a4fe commit dcd4464
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
32 changes: 23 additions & 9 deletions question/format/xml/format.php
Expand Up @@ -198,7 +198,7 @@ public function import_files_as_draft($xml) {
* @return object question object
*/
public function import_headers($question) {
global $CFG, $USER;
global $USER;

// This routine initialises the question object.
$qo = $this->defaultquestion();
Expand Down Expand Up @@ -255,14 +255,7 @@ public function import_headers($question) {
}

// 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;
}
Expand Down Expand Up @@ -380,6 +373,26 @@ public function import_hints($qo, $questionxml, $withparts = false,
}
}

/**
* 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 <file> nodes from the the parsed XML.
Expand Down Expand Up @@ -505,6 +518,7 @@ public function import_multianswer($question) {
}

$this->import_hints($qo, $question, true, false, $this->get_format($qo->questiontextformat));
$this->import_question_tags($qo, $question);

return $qo;
}
Expand Down
28 changes: 28 additions & 0 deletions question/format/xml/tests/xmlformat_test.php
Expand Up @@ -29,6 +29,7 @@
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');


/**
Expand Down Expand Up @@ -302,6 +303,10 @@ public function test_import_description() {
<defaultgrade>0</defaultgrade>
<penalty>0</penalty>
<hidden>0</hidden>
<tags>
<tag><text>tagDescription</text></tag>
<tag><text>tagTest</text></tag>
</tags>
</question>';
$xmldata = xmlize($xml);

Expand All @@ -317,6 +322,7 @@ public function test_import_description() {
$expectedq->defaultmark = 0;
$expectedq->length = 0;
$expectedq->penalty = 0;
$expectedq->tags = array('tagDescription', 'tagTest');

$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
Expand Down Expand Up @@ -373,6 +379,11 @@ public function test_import_essay_20() {
<defaultgrade>1</defaultgrade>
<penalty>0</penalty>
<hidden>0</hidden>
<tags>
<tag><text>tagEssay</text></tag>
<tag><text>tagEssay20</text></tag>
<tag><text>tagTest</text></tag>
</tags>
</question>';
$xmldata = xmlize($xml);

Expand All @@ -397,6 +408,7 @@ public function test_import_essay_20() {
$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);
}
Expand Down Expand Up @@ -426,6 +438,11 @@ public function test_import_essay_21() {
<responsetemplate format="html">
<text><![CDATA[<p>Here is something <b>really</b> interesting.</p>]]></text>
</responsetemplate>
<tags>
<tag><text>tagEssay</text></tag>
<tag><text>tagEssay21</text></tag>
<tag><text>tagTest</text></tag>
</tags>
</question>';
$xmldata = xmlize($xml);

Expand All @@ -450,6 +467,7 @@ public function test_import_essay_21() {
$expectedq->graderinfo['format'] = FORMAT_HTML;
$expectedq->responsetemplate['text'] = '<p>Here is something <b>really</b> interesting.</p>';
$expectedq->responsetemplate['format'] = FORMAT_HTML;
$expectedq->tags = array('tagEssay', 'tagEssay21', 'tagTest');

$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
Expand Down Expand Up @@ -571,6 +589,10 @@ public function test_import_match_19() {
<shownumcorrect />
<clearwrong />
</hint>
<tags>
<tag><text>tagMatching</text></tag>
<tag><text>tagTest</text></tag>
</tags>
</question>';
$xmldata = xmlize($xml);

Expand Down Expand Up @@ -607,6 +629,7 @@ public function test_import_match_19() {
);
$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);
}
Expand Down Expand Up @@ -1343,6 +1366,10 @@ public function test_import_multianswer() {
<hint format="html">
<text>Hint 2</text>
</hint>
<tags>
<tag><text>tagCloze</text></tag>
<tag><text>tagTest</text></tag>
</tags>
</question>
';
$xmldata = xmlize($xml);
Expand Down Expand Up @@ -1419,6 +1446,7 @@ public function test_import_multianswer() {
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]);
Expand Down

0 comments on commit dcd4464

Please sign in to comment.