From eba566355c657b34e04a76ea480eb38a65e2fcb6 Mon Sep 17 00:00:00 2001 From: Matt Davidson Date: Wed, 4 Oct 2023 15:40:45 -0400 Subject: [PATCH] Bug fixes (#37, #48, #52) (#53) * Update processor.php change to GENERAL_MODE to make more predictable temp file storage Fixed issue where exact duplicate backups would have multiple db entries and show up multiple times on Archives page. Optimized code * fix for #52 * add a session keep alive iframe for long running processes Possible fix for #48 --- classes/processor.php | 40 +++++++++++++++------------------------- settings.php | 2 +- step4.php | 8 ++++++++ 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/classes/processor.php b/classes/processor.php index ff6e029..19d26bc 100644 --- a/classes/processor.php +++ b/classes/processor.php @@ -623,8 +623,6 @@ protected function archivecourse($obj, $delete = true) { } $admin = get_admin(); - - $coursetobackup = $obj["course"]->id; // Set this to one existing choice cmid in your dev site. $userdoingthebackup = $admin->id; // Set this to the id of your admin account. try { @@ -636,7 +634,7 @@ protected function archivecourse($obj, $delete = true) { "/\\"); // Prepare backup filename. - $suffix = '-ID-'.$obj["course"]->id; + $suffix = '-ID-' . $obj["course"]->id; if (!empty($obj["course"]->idnumber)) { $suffix .= '-IDNUM-' . $obj["course"]->idnumber; } @@ -662,33 +660,20 @@ protected function archivecourse($obj, $delete = true) { } // Perform Backup. - $bc = new backup_controller(backup::TYPE_1COURSE, $coursetobackup, backup::FORMAT_MOODLE, - backup::INTERACTIVE_NO, backup::MODE_AUTOMATED, $userdoingthebackup); + $bc = new backup_controller(backup::TYPE_1COURSE, $obj["course"]->id, backup::FORMAT_MOODLE, + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userdoingthebackup); $bc->execute_plan(); // Execute backup. $results = $bc->get_results(); // Get the file information needed. - $file = $results['backup_destination']; - - if (!empty($file)) { - $file->copy_content_to($path . '/' . $archivefile); - } else { - $config = get_config('backup'); - $dir = $config->backup_auto_destination; - if (!empty($dir)) { // The backup file will have already been moved, so I have to find it. - $file = $this->find_course_file($obj["course"]->id, $dir); - if (!empty($file)) { - rename($dir . '/' . $file, $path . '/' . $archivefile); - } else { - throw new Exception(get_string('errorbackup', 'tool_coursearchiver')); - } - } else { - throw new Exception(get_string('errorbackup', 'tool_coursearchiver')); - } - } - $bc->destroy(); unset($bc); + if (!empty($results['backup_destination'])) { // Course backup file area. + $results['backup_destination']->copy_content_to($path . '/' . $archivefile); + } else { // Specified backup file area. + throw new Exception(get_string('errorbackup', 'tool_coursearchiver')); + } + if (file_exists($path . '/' . $archivefile)) { // Make sure file got moved. $owners = $this->get_course_users_with_role($obj["course"]->id, get_config('tool_coursearchiver', 'ownerroleid')); @@ -697,12 +682,17 @@ protected function archivecourse($obj, $delete = true) { foreach ($owners["owners"] as $owner) { $ownerslist .= $owner->id . '|'; } + // Save course info to the database. $record = new stdClass(); $record->filename = $folder . '/' . $archivefile; $record->owners = $ownerslist; $record->timetodelete = 0; - $DB->insert_record('tool_coursearchiver_archived', $record, false); + + // Backup alone could overwrite a previous backup. Don't make duplicate records. + if (!$DB->get_record('tool_coursearchiver_archived', ['filename' => $record->filename])) { + $DB->insert_record('tool_coursearchiver_archived', $record, false); + } // Remove Course. if ($delete) { diff --git a/settings.php b/settings.php index e6a304f..1f87eb8 100644 --- a/settings.php +++ b/settings.php @@ -51,7 +51,7 @@ } $name = new lang_string('ownerroleid', 'tool_coursearchiver'); $description = new lang_string('ownerroleid_help', 'tool_coursearchiver'); - $role = $DB->get_record('role', array('shortname' => 'editingteacher')); + $role = $DB->get_record('role', array('archetype' => 'editingteacher')); $default = array($role->id); $settings->add(new admin_setting_configmultiselect('tool_coursearchiver/ownerroleid', $name, diff --git a/step4.php b/step4.php index 3f36a1b..95fcd37 100644 --- a/step4.php +++ b/step4.php @@ -28,6 +28,7 @@ require_once($CFG->libdir . '/adminlib.php'); header('X-Accel-Buffering: no'); +header('Content-Encoding: identity'); require_login(); admin_externalpage_setup('toolcoursearchiver'); @@ -120,11 +121,18 @@ $returnurl = new moodle_url('/admin/tool/coursearchiver/step2.php'); redirect($returnurl); } + $processor = new tool_coursearchiver_processor(array("mode" => $mode, "data" => $courses)); if (!empty($folder)) { $processor->folder = $folder; } + + // Automatic refreshing iframe to keep sessions alive during long script execution. + echo ''; + + // Execute process. $processor->execute(tool_coursearchiver_tracker::OUTPUT_HTML, null); + echo $OUTPUT->footer(); break; default: