Allow importer to overwrite course config, I guess?

This commit is contained in:
Kumi 2022-08-22 15:12:21 +00:00
parent c9742bf794
commit 21094f4a51
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -48,7 +48,8 @@ if (empty($options['timestamp'])) {
cli_error('Missing mandatory argument timestamp.', 2); cli_error('Missing mandatory argument timestamp.', 2);
} }
function generateRandomString($length = 10) { function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters); $charactersLength = strlen($characters);
$randomString = ''; $randomString = '';
@ -76,7 +77,7 @@ try {
} }
if (!$course) { if (!$course) {
while (!get_course(((int) $courseid) - 1)) { while (!$DB->get_record('course', array('id' => ((int) $courseid) - 1), '*')) {
$name = generateRandomString(); $name = generateRandomString();
$newcourse = restore_dbops::create_new_course("Placeholder " . $name, $name, $category->id); $newcourse = restore_dbops::create_new_course("Placeholder " . $name, $name, $category->id);
if ((int) $newcourse > (int) $courseid) { if ((int) $newcourse > (int) $courseid) {
@ -104,10 +105,9 @@ if (!is_readable($infile)) {
$backupdir = "restore_" . uniqid(); $backupdir = "restore_" . uniqid();
if (isset($CFG->backuptempdir)){ if (isset($CFG->backuptempdir)) {
$path = $CFG->backuptempdir . DIRECTORY_SEPARATOR . $backupdir; $path = $CFG->backuptempdir . DIRECTORY_SEPARATOR . $backupdir;
} } else {
else{
$path = $CFG->tempdir . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . $backupdir; $path = $CFG->tempdir . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . $backupdir;
} }
@ -152,13 +152,28 @@ if (!$course && $DB->get_record('course', array('category' => $category->id, 'sh
if ($course) { if ($course) {
cli_writeln("Overwriting current content of existing course -> Course ID: $courseid"); cli_writeln("Overwriting current content of existing course -> Course ID: $courseid");
$rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO, $rc = new restore_controller(
backup::MODE_GENERAL, 2, 0); $backupdir,
$courseid,
backup::INTERACTIVE_NO,
backup::MODE_GENERAL,
2,
0
);
$rc->get_plan()->get_setting('overwrite_conf')->set_value(true);
} else { } else {
cli_writeln("Creating new course to restore backup"); cli_writeln("Creating new course to restore backup");
$courseid = restore_dbops::create_new_course($fullname, $shortname, $category->id); $courseid = restore_dbops::create_new_course($fullname, $shortname, $category->id);
$rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO, $rc = new restore_controller(
backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE); $backupdir,
$courseid,
backup::INTERACTIVE_NO,
backup::MODE_GENERAL,
2,
backup::TARGET_NEW_COURSE
);
} }
if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) { if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
@ -167,7 +182,7 @@ if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
$plan = $rc->get_plan(); $plan = $rc->get_plan();
if (!$rc->execute_precheck()){ if (!$rc->execute_precheck()) {
$check = $rc->get_precheck_results(); $check = $rc->get_precheck_results();
cli_error("Restore pre-check failed!"); cli_error("Restore pre-check failed!");
} }