diff --git a/backup/controller/backup_controller.class.php b/backup/controller/backup_controller.class.php index 7180a3aa673..cf5fcf96622 100644 --- a/backup/controller/backup_controller.class.php +++ b/backup/controller/backup_controller.class.php @@ -271,6 +271,37 @@ class backup_controller extends base_controller { return $this->includefiles; } + /** + * Returns the default value for $this->includefiles before we consider any settings. + * + * @return bool + * @throws dml_exception + */ + protected function get_include_files_default() : bool { + // We normally include files. + $includefiles = true; + + // In an import, we don't need to include files. + if ($this->get_mode() === backup::MODE_IMPORT) { + $includefiles = false; + } + + // When a backup is intended for the same site, we don't need to include the files. + // Note, this setting is only used for duplication of an entire course. + if ($this->get_mode() === backup::MODE_SAMESITE) { + $includefiles = false; + } + + // If backup is automated and we have set auto backup config to exclude + // files then set them to be excluded here. + $backupautofiles = (bool) get_config('backup', 'backup_auto_files'); + if ($this->get_mode() === backup::MODE_AUTOMATED && !$backupautofiles) { + $includefiles = false; + } + + return $includefiles; + } + public function get_operation() { return $this->operation; } @@ -326,6 +357,12 @@ class backup_controller extends base_controller { // Basic/initial prevention against time/memory limits core_php_time_limit::raise(1 * 60 * 60); // 1 hour for 1 course initially granted raise_memory_limit(MEMORY_EXTRA); + + // If the controller has decided that we can include files, then check the setting, otherwise do not include files. + if ($this->get_include_files()) { + $this->set_include_files((bool) $this->get_plan()->get_setting('files')->get_value()); + } + // If this is not a course backup, or single activity backup (e.g. duplicate) inform the plan we are not // including all the activities for sure. This will affect any // task/step executed conditionally to stop including information @@ -386,42 +423,19 @@ class backup_controller extends base_controller { $this->log('applying plan defaults', backup::LOG_DEBUG); backup_controller_dbops::apply_config_defaults($this); $this->set_status(backup::STATUS_CONFIGURED); - $this->set_include_files(); + $this->set_include_files($this->get_include_files_default()); } /** * Set the initial value for the include_files setting. * + * @param bool $includefiles * @see backup_controller::get_include_files for further information on the purpose of this setting. - * @return int Indicates whether files should be included in backups. */ - protected function set_include_files() { - // We normally include files. - $includefiles = true; - - // In an import, we don't need to include files. - if ($this->get_mode() === backup::MODE_IMPORT) { - $includefiles = false; - } - - // When a backup is intended for the same site, we don't need to include the files. - // Note, this setting is only used for duplication of an entire course. - if ($this->get_mode() === backup::MODE_SAMESITE) { - $includefiles = false; - } - - // If backup is automated and we have set auto backup config to exclude - // files then set them to be excluded here. - $backupautofiles = (bool)get_config('backup', 'backup_auto_files'); - if ($this->get_mode() === backup::MODE_AUTOMATED && !$backupautofiles) { - $includefiles = false; - } - - $this->includefiles = (int) $includefiles; + protected function set_include_files(bool $includefiles) { $this->log("setting file inclusion to {$this->includefiles}", backup::LOG_DEBUG); - return $this->includefiles; + $this->includefiles = (int) $includefiles; } - } /* diff --git a/backup/moodle2/backup_custom_fields.php b/backup/moodle2/backup_custom_fields.php index 75bab78615d..9de5ce68067 100644 --- a/backup/moodle2/backup_custom_fields.php +++ b/backup/moodle2/backup_custom_fields.php @@ -242,11 +242,6 @@ class file_nested_element extends backup_nested_element { protected $backupid; - /** - * @var bool Are we including the files? - */ - protected $includefiles = true; - public function process($processor) { // Get current backupid from processor, we'll need later if (is_null($this->backupid)) { @@ -259,36 +254,25 @@ class file_nested_element extends backup_nested_element { // Fill values parent::fill_values($values); // Do our own tasks (copy file from moodle to backup) - if ($this->includefiles) { - try { - backup_file_manager::copy_file_moodle2backup($this->backupid, $values); - } catch (file_exception $e) { - $this->add_result(array('missing_files_in_pool' => true)); + try { + backup_file_manager::copy_file_moodle2backup($this->backupid, $values); + } catch (file_exception $e) { + $this->add_result(array('missing_files_in_pool' => true)); - // Build helpful log message with all information necessary to identify - // file location. - $context = context::instance_by_id($values->contextid, IGNORE_MISSING); - $contextname = ''; - if ($context) { - $contextname = ' \'' . $context->get_context_name() . '\''; - } - $message = 'Missing file in pool: ' . $values->filepath . $values->filename . - ' (context ' . $values->contextid . $contextname . ', component ' . - $values->component . ', filearea ' . $values->filearea . ', itemid ' . - $values->itemid . ') [' . $e->debuginfo . ']'; - $this->add_log($message, backup::LOG_WARNING); + // Build helpful log message with all information necessary to identify + // file location. + $context = context::instance_by_id($values->contextid, IGNORE_MISSING); + $contextname = ''; + if ($context) { + $contextname = ' \'' . $context->get_context_name() . '\''; } + $message = 'Missing file in pool: ' . $values->filepath . $values->filename . + ' (context ' . $values->contextid . $contextname . ', component ' . + $values->component . ', filearea ' . $values->filearea . ', itemid ' . + $values->itemid . ') [' . $e->debuginfo . ']'; + $this->add_log($message, backup::LOG_WARNING); } } - - /** - * Sets the value for includefiles. - * - * @param bool $value The value to set it to - */ - public function set_include_files(bool $value) { - $this->includefiles = $value; - } } /** diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index 9cebbe80692..11dd4ce9c78 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -1794,9 +1794,6 @@ class backup_final_files_structure_step extends backup_structure_step { 'source', 'author', 'license', 'sortorder', 'repositorytype', 'repositoryid', 'reference')); - // Set whether we want to store the actual files in the backup. - $file->set_include_files($this->get_setting_value('files')); - // Build the tree $files->add_child($file); @@ -1836,8 +1833,7 @@ class backup_main_structure_step extends backup_structure_step { $info['backup_date'] = time(); $info['backup_uniqueid']= $this->get_backupid(); $info['mnet_remoteusers']=backup_controller_dbops::backup_includes_mnet_remote_users($this->get_backupid()); - $info['include_files'] = $this->get_setting_value('files') && - backup_controller_dbops::backup_includes_files($this->get_backupid()); + $info['include_files'] = backup_controller_dbops::backup_includes_files($this->get_backupid()); $info['include_file_references_to_external_content'] = backup_controller_dbops::backup_includes_file_references($this->get_backupid()); $info['original_wwwroot']=$CFG->wwwroot;