Skip to content

Commit

Permalink
MDL-61132 Questions: Adapt logic on export/import
Browse files Browse the repository at this point in the history
Modified import/export to adapt with "Top" categories.
  • Loading branch information
rezaies committed Feb 5, 2018
1 parent 8df402e commit f86f8c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion question/export_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ protected function definition() {
// 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');

Expand Down
14 changes: 13 additions & 1 deletion question/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ protected function create_category_path($catpath) {
$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);
Expand All @@ -509,9 +515,15 @@ protected function create_category_path($catpath) {

// 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
Expand Down

0 comments on commit f86f8c8

Please sign in to comment.