diff --git a/question/format.php b/question/format.php index 5e82530a454..0177abf03e4 100644 --- a/question/format.php +++ b/question/format.php @@ -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; } diff --git a/question/format/xml/format.php b/question/format/xml/format.php index 98fe6d2c725..d2323d00afd 100644 --- a/question/format/xml/format.php +++ b/question/format/xml/format.php @@ -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 .= " \n"; $expout .= " {$categoryinfo}"; $expout .= " \n"; + $expout .= " {$question->idnumber}\n"; $expout .= " \n"; return $expout; } diff --git a/question/format/xml/tests/fixtures/category_with_description.xml b/question/format/xml/tests/fixtures/category_with_description.xml index 5c8ff8331af..674457bab42 100644 --- a/question/format/xml/tests/fixtures/category_with_description.xml +++ b/question/format/xml/tests/fixtures/category_with_description.xml @@ -8,6 +8,7 @@ This is Alpha category for test + alpha-idnumber diff --git a/question/format/xml/tests/fixtures/export_category.xml b/question/format/xml/tests/fixtures/export_category.xml index 42c9f37e57f..63087cf7264 100644 --- a/question/format/xml/tests/fixtures/export_category.xml +++ b/question/format/xml/tests/fixtures/export_category.xml @@ -8,6 +8,7 @@ This is Alpha category for test + alpha-idnumber diff --git a/question/format/xml/tests/fixtures/nested_categories.xml b/question/format/xml/tests/fixtures/nested_categories.xml index 4bf5c2cfca6..ac6db64d315 100644 --- a/question/format/xml/tests/fixtures/nested_categories.xml +++ b/question/format/xml/tests/fixtures/nested_categories.xml @@ -8,6 +8,7 @@ This is Delta category for test + @@ -18,6 +19,7 @@ This is Epsilon category for test + @@ -28,6 +30,7 @@ This is Zeta category for test + diff --git a/question/format/xml/tests/fixtures/nested_categories_with_questions.xml b/question/format/xml/tests/fixtures/nested_categories_with_questions.xml index db729e99dc9..8350c16c4f8 100644 --- a/question/format/xml/tests/fixtures/nested_categories_with_questions.xml +++ b/question/format/xml/tests/fixtures/nested_categories_with_questions.xml @@ -8,6 +8,7 @@ This is Iota category for test + @@ -47,6 +48,7 @@ This is Kappa category for test + @@ -114,6 +116,7 @@ This is Lambda category for test + @@ -153,6 +156,7 @@ This is Mu category for test + diff --git a/question/format/xml/tests/qformat_xml_import_export_test.php b/question/format/xml/tests/qformat_xml_import_export_test.php index a0af0afd865..4926bea1040 100644 --- a/question/format/xml/tests/qformat_xml_import_export_test.php +++ b/question/format/xml/tests/qformat_xml_import_export_test.php @@ -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']); diff --git a/question/format/xml/tests/xmlformat_test.php b/question/format/xml/tests/xmlformat_test.php index c85fff02b26..c8cd52c41f8 100644 --- a/question/format/xml/tests/xmlformat_test.php +++ b/question/format/xml/tests/xmlformat_test.php @@ -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);