MDL-66801 questions: XML import/export should include category idnumbers

This commit is contained in:
Tim Hunt 2019-09-30 18:21:18 +01:00
parent 9528b1ff5b
commit e247068d72
8 changed files with 48 additions and 19 deletions

View File

@ -575,14 +575,22 @@ class qformat_default {
$parent = $category->id;
} else if ($category = $DB->get_record('question_categories',
array('name' => $catname, 'contextid' => $context->id, 'parent' => $parent))) {
// Do nothing unless the child category appears before the parent category
// in the imported xml file. Because the parent was created without info being available
// at that time, this allows the info to be added from the xml data.
if ($key == (count($catnames) - 1) && $lastcategoryinfo && isset($lastcategoryinfo->info) &&
$lastcategoryinfo->info !== '' && $category->info === '') {
$category->info = $lastcategoryinfo->info;
if (isset($lastcategoryinfo->infoformat) && $lastcategoryinfo->infoformat !== '') {
$category->infoformat = $lastcategoryinfo->infoformat;
// If this category is now the last one in the path we are processing ...
if ($key == (count($catnames) - 1) && $lastcategoryinfo) {
// Do nothing unless the child category appears before the parent category
// in the imported xml file. Because the parent was created without info being available
// at that time, this allows the info to be added from the xml data.
if (isset($lastcategoryinfo->info) && $lastcategoryinfo->info !== ''
&& $category->info === '') {
$category->info = $lastcategoryinfo->info;
if (isset($lastcategoryinfo->infoformat) && $lastcategoryinfo->infoformat !== '') {
$category->infoformat = $lastcategoryinfo->infoformat;
}
}
// Same for idnumber.
if (isset($lastcategoryinfo->idnumber) && $lastcategoryinfo->idnumber !== ''
&& $category->idnumber === '') {
$category->idnumber = $lastcategoryinfo->idnumber;
}
$DB->update_record('question_categories', $category);
}
@ -603,11 +611,16 @@ class qformat_default {
$category->name = $catname;
$category->info = '';
// Only add info (category description) for the final category in the catpath.
if ($key == (count($catnames) - 1) && $lastcategoryinfo && isset($lastcategoryinfo->info) &&
$lastcategoryinfo->info !== '') {
$category->info = $lastcategoryinfo->info;
if (isset($lastcategoryinfo->infoformat) && $lastcategoryinfo->infoformat !== '') {
$category->infoformat = $lastcategoryinfo->infoformat;
if ($key == (count($catnames) - 1) && $lastcategoryinfo) {
if (isset($lastcategoryinfo->info) && $lastcategoryinfo->info !== '') {
$category->info = $lastcategoryinfo->info;
if (isset($lastcategoryinfo->infoformat) && $lastcategoryinfo->infoformat !== '') {
$category->infoformat = $lastcategoryinfo->infoformat;
}
}
// Same for idnumber.
if (isset($lastcategoryinfo->idnumber) && $lastcategoryinfo->idnumber !== '') {
$category->idnumber = $lastcategoryinfo->idnumber;
}
}
$category->parent = $parent;
@ -924,7 +937,7 @@ class qformat_default {
if (!count($DB->get_records('question', array('category' => $trackcategoryparent)))) {
$categoryname = $this->get_category_path($trackcategoryparent, $this->contexttofile);
$categoryinfo = $DB->get_record('question_categories', array('id' => $trackcategoryparent),
'name, info, infoformat', MUST_EXIST);
'name, info, infoformat, idnumber', MUST_EXIST);
if ($categoryinfo->name != 'top') {
// Create 'dummy' question for parent category.
$dummyquestion = $this->create_dummy_question_representing_category($categoryname, $categoryinfo);
@ -937,7 +950,7 @@ class qformat_default {
if ($addnewcat && !in_array($trackcategory, $writtencategories)) {
$categoryname = $this->get_category_path($trackcategory, $this->contexttofile);
$categoryinfo = $DB->get_record('question_categories', array('id' => $trackcategory),
'info, infoformat', MUST_EXIST);
'info, infoformat, idnumber', MUST_EXIST);
// Create 'dummy' question for category.
$dummyquestion = $this->create_dummy_question_representing_category($categoryname, $categoryinfo);
$expout .= $this->writequestion($dummyquestion) . "\n";
@ -986,6 +999,7 @@ class qformat_default {
$dummyquestion->contextid = 0;
$dummyquestion->info = $categoryinfo->info;
$dummyquestion->infoformat = $categoryinfo->infoformat;
$dummyquestion->idnumber = $categoryinfo->idnumber;
$dummyquestion->name = 'Switch category to ' . $categoryname;
return $dummyquestion;
}

View File

@ -935,6 +935,7 @@ class qformat_xml extends qformat_default {
// The import should have the format in human readable form, so translate to machine readable format.
$qo->infoformat = $this->trans_format($question['#']['info'][0]['@']['format']);
}
$qo->idnumber = $this->getpath($question, array('#', 'idnumber', 0, '#'), null);
return $qo;
}
@ -1205,6 +1206,7 @@ class qformat_xml extends qformat_default {
$expout .= " <info {$infoformat}>\n";
$expout .= " {$categoryinfo}";
$expout .= " </info>\n";
$expout .= " <idnumber>{$question->idnumber}</idnumber>\n";
$expout .= " </question>\n";
return $expout;
}

View File

@ -8,6 +8,7 @@
<info format="moodle_auto_format">
<text>This is Alpha category for test</text>
</info>
<idnumber>alpha-idnumber</idnumber>
</question>
<!-- question: 91 -->

View File

@ -8,6 +8,7 @@
<info format="moodle_auto_format">
<text>This is Alpha category for test</text>
</info>
<idnumber>alpha-idnumber</idnumber>
</question>
<!-- question: 91 -->

View File

@ -8,6 +8,7 @@
<info format="plain_text">
<text>This is Delta category for test</text>
</info>
<idnumber></idnumber>
</question>
<!-- question: 0 -->
@ -18,6 +19,7 @@
<info format="markdown">
<text>This is Epsilon category for test</text>
</info>
<idnumber></idnumber>
</question>
<!-- question: 0 -->
@ -28,6 +30,7 @@
<info format="moodle_auto_format">
<text>This is Zeta category for test</text>
</info>
<idnumber></idnumber>
</question>
<!-- question: 93 -->

View File

@ -8,6 +8,7 @@
<info format="plain_text">
<text>This is Iota category for test</text>
</info>
<idnumber></idnumber>
</question>
<!-- question: 96 -->
@ -47,6 +48,7 @@
<info format="markdown">
<text>This is Kappa category for test</text>
</info>
<idnumber></idnumber>
</question>
<!-- question: 106 -->
@ -114,6 +116,7 @@
<info format="moodle_auto_format">
<text>This is Lambda category for test</text>
</info>
<idnumber></idnumber>
</question>
<!-- question: 98 -->
@ -153,6 +156,7 @@
<info format="moodle_auto_format">
<text>This is Mu category for test</text>
</info>
<idnumber></idnumber>
</question>
<!-- question: 99 -->

View File

@ -38,8 +38,8 @@ class qformat_xml_import_export_test extends advanced_testcase {
/**
* Create object qformat_xml for test.
* @param string $filename with name for testing file.
* @param object $course
* @return object qformat_xml.
* @param stdClass $course
* @return qformat_xml XML question format object.
*/
public function create_qformat($filename, $course) {
global $DB;
@ -99,11 +99,12 @@ class qformat_xml_import_export_test extends advanced_testcase {
* @param string $info imported category info field (description of category).
* @param int $infoformat imported category info field format.
*/
public function assert_category_imported($name, $info, $infoformat) {
public function assert_category_imported($name, $info, $infoformat, $idnumber = null) {
global $DB;
$category = $DB->get_record('question_categories', ['name' => $name], '*', MUST_EXIST);
$this->assertEquals($info, $category->info);
$this->assertEquals($infoformat, $category->infoformat);
$this->assertSame($idnumber, $category->idnumber);
}
/**
@ -146,7 +147,8 @@ class qformat_xml_import_export_test extends advanced_testcase {
$qformat = $this->create_qformat('category_with_description.xml', $course);
$imported = $qformat->importprocess();
$this->assertTrue($imported);
$this->assert_category_imported('Alpha', 'This is Alpha category for test', FORMAT_MOODLE);
$this->assert_category_imported('Alpha',
'This is Alpha category for test', FORMAT_MOODLE, 'alpha-idnumber');
$this->assert_category_has_parent('Alpha', 'top');
}
@ -246,6 +248,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
'contextid' => '2',
'info' => 'This is Alpha category for test',
'infoformat' => '0',
'idnumber' => 'alpha-idnumber',
'stamp' => make_unique_id_code(),
'parent' => '0',
'sortorder' => '999']);

View File

@ -1721,6 +1721,7 @@ END;
$categoryinfo = new stdClass();
$categoryinfo->info = 'info1';
$categoryinfo->infoformat = 'infoformat1';
$categoryinfo->idnumber = null;
$dummyquestion = $testobject->mock_create_dummy_question_representing_category($categoryname, $categoryinfo);
$this->assertEquals('category', $dummyquestion->qtype);