MDL-61132 Questions: Adapt logic on export/import

Modified import/export to adapt with "Top" categories.
This commit is contained in:
Shamim Rezaie 2018-01-30 14:17:08 +11:00
parent 8df402e401
commit f86f8c85c1
2 changed files with 15 additions and 2 deletions

View File

@ -69,7 +69,8 @@ class question_export_form extends moodleform {
// Export options.
$mform->addElement('header', 'general', get_string('general', 'form'));
$mform->addElement('questioncategory', 'category', get_string('exportcategory', 'question'), compact('contexts'));
$mform->addElement('questioncategory', 'category', get_string('exportcategory', 'question'),
array('contexts' => $contexts, 'top' => true));
$mform->setDefault('category', $defaultcategory);
$mform->addHelpButton('category', 'exportcategory', 'question');

View File

@ -499,6 +499,12 @@ class qformat_default {
$contextid = false;
}
// Before 3.5, question categories could be created at top level.
// From 3.5 onwards, all question categories should be a child of a special category called the "top" category.
if (isset($catnames[0]) && (($catnames[0] != 'top') || (count($catnames) < 3))) {
array_unshift($catnames, 'top');
}
if ($this->contextfromfile && $contextid !== false) {
$context = context::instance_by_id($contextid);
require_capability('moodle/question:add', $context);
@ -509,9 +515,15 @@ class qformat_default {
// Now create any categories that need to be created.
foreach ($catnames as $catname) {
if ($category = $DB->get_record('question_categories',
if ($parent == 0) {
$category = question_get_top_category($context->id, true);
$parent = $category->id;
} else if ($category = $DB->get_record('question_categories',
array('name' => $catname, 'contextid' => $context->id, 'parent' => $parent))) {
$parent = $category->id;
} else if ($parent == 0) {
$category = question_get_top_category($context->id, true);
$parent = $category->id;
} else {
require_capability('moodle/question:managecategory', $context);
// create the new category