diff --git a/import_cli.php b/import_cli.php index 990558e..f183c70 100644 --- a/import_cli.php +++ b/import_cli.php @@ -48,8 +48,7 @@ if (empty($options['timestamp'])) { cli_error('Missing mandatory argument timestamp.', 2); } -function generateRandomString($length = 10) -{ +function generateRandomString($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; @@ -59,6 +58,35 @@ function generateRandomString($length = 10) 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']; $categoryid = $options["categoryid"]; @@ -79,9 +107,9 @@ try { if (!$course) { while (!$DB->get_record('course', array('id' => ((int) $courseid) - 1), '*')) { $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) { - cli_error('Did you delete courses by any chance? Cannot create course ' . $courseid . '.'); + cli_error('Did you delete courses by any chance? Cannot create course ' . $courseid . '.'); } } } @@ -105,9 +133,10 @@ if (!is_readable($infile)) { $backupdir = "restore_" . uniqid(); -if (isset($CFG->backuptempdir)) { +if (isset($CFG->backuptempdir)){ $path = $CFG->backuptempdir . DIRECTORY_SEPARATOR . $backupdir; -} else { +} +else{ $path = $CFG->tempdir . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . $backupdir; } @@ -141,21 +170,19 @@ if (file_exists($questionfile)) { $questions->save($questionfile . ".bak"); - $questionsection = $questions->documentElement->getElementsByTagName("questions")->item(0); - - if ($questionsection) { - foreach ($questionsection->getElementsByTagName("question") as $question) { + foreach ($questions->getElementsByTagName("question") as $question) { $questiontext = $question->getElementsByTagName("questiontext")->item(0); $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) { $answertext = $answer->getElementsByTagName("answertext")->item(0); $newanswertext = strip_tags(html_entity_decode($answertext->textContent)); - $answertext->nodeValue = htmlentities($newanswertext, ENT_XML1); + $answertext->removeChild($answertext->firstChild); + $answertext->appendChild(new DOMText($newanswertext)); } } - } $questions->save($questionfile); } @@ -179,28 +206,16 @@ if (!$course && $DB->get_record('course', array('category' => $category->id, 'sh if ($course) { cli_writeln("Overwriting current content of existing course -> Course ID: $courseid"); - $rc = new restore_controller( - $backupdir, - $courseid, - backup::INTERACTIVE_NO, - backup::MODE_GENERAL, - 2, - 0 - ); + $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO, + backup::MODE_GENERAL, 2, 0); $rc->get_plan()->get_setting('overwrite_conf')->set_value(true); - + } else { cli_writeln("Creating new course to restore backup"); $courseid = restore_dbops::create_new_course($fullname, $shortname, $category->id); - $rc = new restore_controller( - $backupdir, - $courseid, - backup::INTERACTIVE_NO, - backup::MODE_GENERAL, - 2, - backup::TARGET_NEW_COURSE - ); + $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO, + backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE); } if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) { @@ -209,7 +224,7 @@ if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) { $plan = $rc->get_plan(); -if (!$rc->execute_precheck()) { +if (!$rc->execute_precheck()){ $check = $rc->get_precheck_results(); cli_error("Restore pre-check failed!"); } @@ -222,8 +237,6 @@ restore_dbops::delete_course_content($courseid, $deletingoptions); $rc->execute_plan(); $rc->destroy(); -#unlink($infile); - $course = get_course($courseid); $course->category = $categoryid; @@ -232,5 +245,7 @@ $course->shortname = $shortname; $DB->update_record('course', $course); +removePath($path); + cli_writeln("New course ID for '$shortname': $courseid in category {$category->id}"); cli_writeln("OK");