Skip to content

Commit

Permalink
Bug fixes (#37, #48, #52) (#53)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Syxton authored Oct 4, 2023
1 parent c77f002 commit eba5663
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
40 changes: 15 additions & 25 deletions classes/processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand All @@ -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'));
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions step4.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 '<iframe style="display:none" src="' . $CFG->wwwroot . '/admin/tool/coursearchiver/keepalive.php"></iframe>';

// Execute process.
$processor->execute(tool_coursearchiver_tracker::OUTPUT_HTML, null);

echo $OUTPUT->footer();
break;
default:
Expand Down

0 comments on commit eba5663

Please sign in to comment.