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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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