Skip to content

Commit

Permalink
allow course-restore to category=0 or a course within category=0.
Browse files Browse the repository at this point in the history
The root category (id=0) is implicit
- it doesn't have a row in {course_categories} table
- {course_categories}.parent can be zero
- and {courses}.category can be zero
  • Loading branch information
fireartist authored and tmuras committed May 29, 2024
1 parent 997dcb8 commit d51f0c0
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions Moosh/Command/Moodle39/Course/CourseRestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ public function execute() {

// Check if category is OK.
if (!$options['existing']) {
$category = $DB->get_record('course_categories', array('id' => $this->arguments[1]), '*', MUST_EXIST);
$categoryid = $this->arguments[1];
} else {
$course = $DB->get_record('course', array('id' => $this->arguments[1]), '*', MUST_EXIST);
$category = $DB->get_record('course_categories', array('id' => $course->category), '*', MUST_EXIST);
$categoryid = $course->category;
}

if ($categoryid != "0") {
$DB->get_record('course_categories', array('id' => $categoryid), '*', MUST_EXIST);
}

if (!$options['directory']) {
Expand Down Expand Up @@ -137,7 +141,7 @@ public function execute() {
}

// Get unique shortname if creating new course.
if (!$options['existing'] && $DB->get_record('course', array('category' => $category->id, 'shortname' => $shortname))) {
if (!$options['existing'] && $DB->get_record('course', array('category' => $categoryid, 'shortname' => $shortname))) {
$matches = NULL;
preg_match('/(.*)_(\d+)$/', $shortname, $matches);
if ($matches) {
Expand All @@ -148,7 +152,7 @@ public function execute() {
$number = 1;
}
$shortname = $base . '_' . $number;
while ($DB->get_record('course', array('category' => $category->id, 'shortname' => $shortname))) {
while ($DB->get_record('course', array('category' => $categoryid, 'shortname' => $shortname))) {
$number++;
$shortname = $base . '_' . $number;
}
Expand All @@ -167,12 +171,12 @@ public function execute() {
}
} else {
echo "Creating new course to restore backup\n";
$courseid = restore_dbops::create_new_course($fullname, $shortname, $category->id);
$courseid = restore_dbops::create_new_course($fullname, $shortname, $categoryid);
$rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
backup::MODE_GENERAL, $USER->id, backup::TARGET_NEW_COURSE);
}

echo "Restoring (new course id,shortname,destination category): $courseid,$shortname," . $category->id . "\n";
echo "Restoring (new course id,shortname,destination category): $courseid,$shortname," . $categoryid . "\n";

if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
$rc->convert();
Expand All @@ -198,6 +202,6 @@ public function execute() {
$rc->execute_plan();
$rc->destroy();

echo "New course ID for '$shortname': $courseid in category {$category->id}\n";
echo "New course ID for '$shortname': $courseid in category {$categoryid}\n";
}
}

0 comments on commit d51f0c0

Please sign in to comment.