diff --git a/backup/moodle2/backup_activity_task.class.php b/backup/moodle2/backup_activity_task.class.php index daac34afda7..d8e1d99c4b5 100644 --- a/backup/moodle2/backup_activity_task.class.php +++ b/backup/moodle2/backup_activity_task.class.php @@ -91,6 +91,12 @@ abstract class backup_activity_task extends backup_task { */ public function build() { + // If we have decided not to backup activities, prevent anything to be built + if (!$this->get_setting_value('activities')) { + $this->built = true; + return; + } + // Add some extra settings that related processors are going to need $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid)); $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid())); @@ -213,17 +219,31 @@ abstract class backup_activity_task extends backup_task { // Define activity_include (to decide if the whole task must be really executed) $settingname = $settingprefix . 'included'; - $activity_userinfo = new backup_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true); - $this->add_setting($activity_userinfo); + $activity_included = new backup_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true); + $this->add_setting($activity_included); + // Look for "activities" root setting + $activities = $this->plan->get_setting('activities'); + $activities->add_dependency($activity_included); + // Look for "section_included" section setting (if exists) + $settingname = 'section_' . $this->sectionid . '_included'; + if ($this->plan->setting_exists($settingname)) { + $section_included = $this->plan->get_setting($settingname); + $section_included->add_dependency($activity_included); + } // Define activity_userinfo (dependent of root users setting) $settingname = $settingprefix . 'userinfo'; - $settingname = $this->modulename . '_' . $this->moduleid . '_userinfo'; $activity_userinfo = new backup_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true); $this->add_setting($activity_userinfo); // Look for "users" root setting $users = $this->plan->get_setting('users'); $users->add_dependency($activity_userinfo); + // Look for "section_userinfo" section setting (if exists) + $settingname = 'section_' . $this->sectionid . '_userinfo'; + if ($this->plan->setting_exists($settingname)) { + $section_userinfo = $this->plan->get_setting($settingname); + $section_userinfo->add_dependency($activity_userinfo); + } // End of common activity settings, let's add the particular ones $this->define_my_settings(); diff --git a/backup/moodle2/backup_root_task.class.php b/backup/moodle2/backup_root_task.class.php index 6cd5a7b1195..a73397905bf 100644 --- a/backup/moodle2/backup_root_task.class.php +++ b/backup/moodle2/backup_root_task.class.php @@ -69,6 +69,10 @@ class backup_root_task extends backup_task { $this->add_setting($userfiles); $users->add_dependency($userfiles); + // Define activitites + $activities = new backup_activities_setting('activities', base_setting::IS_BOOLEAN, true); + $this->add_setting($activities); + // Define blocks $blocks = new backup_generic_setting('blocks', base_setting::IS_BOOLEAN, true); $this->add_setting($blocks); diff --git a/backup/moodle2/backup_section_task.class.php b/backup/moodle2/backup_section_task.class.php index 6ec9e855901..2a9353d3fd7 100644 --- a/backup/moodle2/backup_section_task.class.php +++ b/backup/moodle2/backup_section_task.class.php @@ -96,7 +96,22 @@ class backup_section_task extends backup_task { */ protected function define_settings() { - // Nothing to add, sections doesn't have common settings (for now) + // All the settings related to this activity will include this prefix + $settingprefix = 'section_' . $this->sectionid . '_'; + // All these are common settings to be shared by all sections + + // Define section_included (to decide if the whole task must be really executed) + $settingname = $settingprefix . 'included'; + $section_included = new backup_section_included_setting($settingname, base_setting::IS_BOOLEAN, true); + $this->add_setting($section_included); + + // Define section_userinfo (dependent of root users setting) + $settingname = $settingprefix . 'userinfo'; + $section_userinfo = new backup_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true); + $this->add_setting($section_userinfo); + // Look for "users" root setting + $users = $this->plan->get_setting('users'); + $users->add_dependency($section_userinfo); } } diff --git a/backup/moodle2/backup_settingslib.php b/backup/moodle2/backup_settingslib.php index 20dc0cd0236..8b48c0cba97 100644 --- a/backup/moodle2/backup_settingslib.php +++ b/backup/moodle2/backup_settingslib.php @@ -47,6 +47,14 @@ class backup_filename_setting extends backup_generic_setting { class backup_users_setting extends backup_generic_setting { } +/** + * root setting to control if backup will include activities or no. + * A lot of other settings (_included at activity levels) + * are dependent of this setting + */ +class backup_activities_setting extends backup_generic_setting { +} + /** * root setting to control if backup will generate anonymized * user info or no, depends of @backup_users_setting so only is @@ -124,25 +132,34 @@ class backup_userscompletion_setting extends backup_anonymize_setting { // Nothing to do. All the logic is in backup_anonymize_setting } - -// Activity backup settings +// Section backup settings /** - * generic activity setting to pass various settings between tasks and steps + * generic section setting to pass various settings between tasks and steps */ -class backup_activity_generic_setting extends activity_backup_setting { +class backup_section_generic_setting extends section_backup_setting { public function process_change($setting, $ctype, $oldv) { // Nothing to do, no dependencies } } /** - * activity backup setting to control if activity will include + * Setting to define if one section is included or no. Activities _included + * settings depend of them if available + */ +class backup_section_included_setting extends section_backup_setting { + public function process_change($setting, $ctype, $oldv) { + // Nothing to do, no dependencies + } +} + +/** + * section backup setting to control if section will include * user information or no, depends of @backup_users_setting */ -class backup_activity_userinfo_setting extends activity_backup_setting { +class backup_section_userinfo_setting extends section_backup_setting { public function process_change($setting, $ctype, $oldv) { - // If change detected in backup_users_setting, proceed + // If change detected in backup_users_setting if ($setting instanceof backup_users_setting) { switch ($ctype) { case self::CHANGED_VALUE: // backup_users = false, this too, and locked @@ -165,3 +182,78 @@ class backup_activity_userinfo_setting extends activity_backup_setting { } } } + + +// Activity backup settings + +/** + * generic activity setting to pass various settings between tasks and steps + */ +class backup_activity_generic_setting extends activity_backup_setting { + public function process_change($setting, $ctype, $oldv) { + // Nothing to do, no dependencies + } +} + +/** + * activity backup setting to control if activity will + * be included or no, depends of @backup_activities_setting and + * optionally parent section included setting + */ +class backup_activity_included_setting extends activity_backup_setting { + public function process_change($setting, $ctype, $oldv) { + // If change detected in backup_activities_setting or backup_section_included_setting + if ($setting instanceof backup_activities_setting || + $setting instanceof backup_section_included_setting) { + switch ($ctype) { + case self::CHANGED_VALUE: // backup_users = false, this too, and locked + if (!$setting->get_value()) { + $this->set_value(false); + $this->set_status(self::LOCKED_BY_HIERARCHY); + } + break; + case self::CHANGED_VISIBILITY: // backup_users not visible, this too + if (!$setting->get_visibility()) { + $this->set_visibility(false); + } + break; + case self::CHANGED_STATUS: // backup_users unlocked, this too + if ($setting->get_status() == self::NOT_LOCKED) { + $this->set_status(self::NOT_LOCKED); + } + break; + } + } + } +} + +/** + * activity backup setting to control if activity will include + * user information or no, depends of @backup_users_setting + */ +class backup_activity_userinfo_setting extends activity_backup_setting { + public function process_change($setting, $ctype, $oldv) { + // If change detected in backup_users_setting or backup_section_userinfo_setting + if ($setting instanceof backup_users_setting || + $setting instanceof backup_section_userinfo_setting) { + switch ($ctype) { + case self::CHANGED_VALUE: // backup_users = false, this too, and locked + if (!$setting->get_value()) { + $this->set_value(false); + $this->set_status(self::LOCKED_BY_HIERARCHY); + } + break; + case self::CHANGED_VISIBILITY: // backup_users not visible, this too + if (!$setting->get_visibility()) { + $this->set_visibility(false); + } + break; + case self::CHANGED_STATUS: // backup_users unlocked, this too + if ($setting->get_status() == self::NOT_LOCKED) { + $this->set_status(self::NOT_LOCKED); + } + break; + } + } + } +} diff --git a/backup/util/plan/base_plan.class.php b/backup/util/plan/base_plan.class.php index b551dcaa24a..fa2f767ac5b 100644 --- a/backup/util/plan/base_plan.class.php +++ b/backup/util/plan/base_plan.class.php @@ -96,6 +96,19 @@ abstract class base_plan implements checksumable, executable { return $result; } + /** + * Wrapper over @get_setting() that returns if the requested setting exists or no + */ + public function setting_exists($name) { + try { + $this->get_setting($name); + return true; + } catch (base_plan_exception $e) { + // Nothing to do + } + return false; + } + /** * Function responsible for building the tasks of any plan