Current status

This commit is contained in:
Kumi 2022-09-12 16:00:09 +00:00
parent f717449bef
commit 906d5d65f2
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -48,8 +48,7 @@ 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 = '';
@ -59,6 +58,35 @@ function generateRandomString($length = 10)
return $randomString; return $randomString;
} }
function removePath($path) {
if(is_dir($path)) {
$handler = opendir($path);
if (!$handler) {
trigger_error('File Error: Failed to open the directory ' . $path, E_USER_ERROR);
return;
}
// list the files in the directory
while ($file = readdir($handler)) {
if ($file != '.' && $file != '..')
removePath($path.DIRECTORY_SEPARATOR.$file);
}
// tidy up: close the handler
closedir($handler);
if (!rmdir($path)) {
trigger_error('File Error: Failed to remove folder ' . $path, E_USER_ERROR);
}
}
else {
// delete it
if (!unlink($path)) {
trigger_error('File Error: Failed to remove file ' . $path, E_USER_ERROR);
}
}
}
$courseid = $options['courseid']; $courseid = $options['courseid'];
$categoryid = $options["categoryid"]; $categoryid = $options["categoryid"];
@ -107,7 +135,8 @@ $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;
} }
@ -141,19 +170,17 @@ if (file_exists($questionfile)) {
$questions->save($questionfile . ".bak"); $questions->save($questionfile . ".bak");
$questionsection = $questions->documentElement->getElementsByTagName("questions")->item(0); foreach ($questions->getElementsByTagName("question") as $question) {
if ($questionsection) {
foreach ($questionsection->getElementsByTagName("question") as $question) {
$questiontext = $question->getElementsByTagName("questiontext")->item(0); $questiontext = $question->getElementsByTagName("questiontext")->item(0);
$newquestiontext = strip_tags(html_entity_decode($questiontext->textContent)); $newquestiontext = strip_tags(html_entity_decode($questiontext->textContent));
$questiontext->nodeValue = htmlentities($newquestiontext, ENT_XML1); $questiontext->removeChild($questiontext->firstChild);
$questiontext->appendChild(new DOMText($newquestiontext));
foreach ($question->getElementsByTagName("answer") as $answer) { foreach ($question->getElementsByTagName("answer") as $answer) {
$answertext = $answer->getElementsByTagName("answertext")->item(0); $answertext = $answer->getElementsByTagName("answertext")->item(0);
$newanswertext = strip_tags(html_entity_decode($answertext->textContent)); $newanswertext = strip_tags(html_entity_decode($answertext->textContent));
$answertext->nodeValue = htmlentities($newanswertext, ENT_XML1); $answertext->removeChild($answertext->firstChild);
} $answertext->appendChild(new DOMText($newanswertext));
} }
} }
@ -179,28 +206,16 @@ 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( $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
$backupdir, backup::MODE_GENERAL, 2, 0);
$courseid,
backup::INTERACTIVE_NO,
backup::MODE_GENERAL,
2,
0
);
$rc->get_plan()->get_setting('overwrite_conf')->set_value(true); $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( $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
$backupdir, backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE);
$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) {
@ -222,8 +237,6 @@ restore_dbops::delete_course_content($courseid, $deletingoptions);
$rc->execute_plan(); $rc->execute_plan();
$rc->destroy(); $rc->destroy();
#unlink($infile);
$course = get_course($courseid); $course = get_course($courseid);
$course->category = $categoryid; $course->category = $categoryid;
@ -232,5 +245,7 @@ $course->shortname = $shortname;
$DB->update_record('course', $course); $DB->update_record('course', $course);
removePath($path);
cli_writeln("New course ID for '$shortname': $courseid in category {$category->id}"); cli_writeln("New course ID for '$shortname': $courseid in category {$category->id}");
cli_writeln("OK"); cli_writeln("OK");