I hope you never need this

I hope you never need this

by Tim Hunt -
Number of replies: 0
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The Moodle back-up system is fairly flexible, but there is some annoying hard-coded logic in there, based on the backup::MODE_XXX constants.

For example, if you are trying to automate copying sites, and if you don't want it to spend time processing files, becuase you will restore to the same Moodle site, then you have to use backup::MODE_IMPORT or MODE_SAMESITE, but if you do that, then there is no way to get it to enclude the enrolment plugins that were set up for your course.

I just worked out how to get round this, although I am not sure that what I have done is a good idea. I am sharing it in case it saves anyone esle from an afternoon as frustrating as mine has been:

Define the classes at the end of this post, then replace your call to

new backup_controller(..., backup::MODE_IMPORT, ...)

with

new backup_controller(..., backup::MODE_GENERAL, ...)

/**
* A hacked version of backup_controller:
*
* - it forcibly sets includefiles to false.
* - it uses our report_osepconversion_custom_backup_plan in place of backup_plan.
*
* @copyright 2016 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_osepconversion_custom_backup_controller extends backup_controller {
protected function set_include_files() {
$this->includefiles = 0;
return $this->includefiles;
}

protected function load_plan() {
$this->log('loading controller plan', backup::LOG_DEBUG);
$this->plan = new report_osepconversion_custom_backup_plan($this);
$this->plan->build(); // Build plan for this controller
$this->set_status(backup::STATUS_PLANNED);
}
}


/**
* A hacked version of backup_plan:
*
* - when a backup_final_task is added, it substitutes one of our hacked
* report_osepconversion_custom_backup_final_task tasks instead.
*
* @copyright 2016 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_osepconversion_custom_backup_plan extends backup_plan {
public function add_task($task) {
if ($task instanceof backup_final_task) {
$task = new report_osepconversion_custom_backup_final_task('final_task');
}
parent::add_task($task);
}
}


/**
* A hacked version of backup_final_task, that overrides a few things:
*
* - it ignores any attempts to add a backup_zip_contents or backup_store_backup_file step.
* - when the drop_and_clean_temp_stuff step is added, it forcibly sets skip_cleaning_temp_dir to true.
*
* @copyright 2016 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_osepconversion_custom_backup_final_task extends backup_final_task {
public function add_step($step) {
if ($step instanceof backup_zip_contents ||
$step instanceof backup_store_backup_file) {
return;
}
if ($step instanceof drop_and_clean_temp_stuff) {
$step->skip_cleaning_temp_dir(true);
}
parent::add_step($step);
}
}
Average of ratings: -