From 864842aa832da81a7b61e464b4622c026c3091de Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Tue, 6 Jul 2010 22:28:38 +0000 Subject: [PATCH] MDL-21432 backup - all sort of restore tasks --- backup/moodle2/backup_settingslib.php | 2 + .../moodle2/restore_activity_task.class.php | 184 ++++++++++++++++++ backup/moodle2/restore_course_task.class.php | 86 ++++++++ backup/moodle2/restore_final_task.class.php | 50 +++++ backup/moodle2/restore_root_task.class.php | 169 ++++++++++++++++ backup/moodle2/restore_section_task.class.php | 98 ++++++++++ backup/moodle2/restore_settingslib.php | 141 ++++++++++++++ 7 files changed, 730 insertions(+) create mode 100644 backup/moodle2/restore_activity_task.class.php create mode 100644 backup/moodle2/restore_course_task.class.php create mode 100644 backup/moodle2/restore_final_task.class.php create mode 100644 backup/moodle2/restore_root_task.class.php create mode 100644 backup/moodle2/restore_section_task.class.php create mode 100644 backup/moodle2/restore_settingslib.php diff --git a/backup/moodle2/backup_settingslib.php b/backup/moodle2/backup_settingslib.php index be3c1ed763b..d73839d8c41 100644 --- a/backup/moodle2/backup_settingslib.php +++ b/backup/moodle2/backup_settingslib.php @@ -22,6 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +// TODO: Reduce these to the minimum because ui/dependencies are 100% separated + // Root backup settings /** diff --git a/backup/moodle2/restore_activity_task.class.php b/backup/moodle2/restore_activity_task.class.php new file mode 100644 index 00000000000..7c9c0a82d6f --- /dev/null +++ b/backup/moodle2/restore_activity_task.class.php @@ -0,0 +1,184 @@ +. + +/** + * @package moodlecore + * @subpackage backup-moodle2 + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * abstract activity task that provides all the properties and common tasks to be performed + * when one activity is being restored + * + * TODO: Finish phpdocs + */ +abstract class restore_activity_task extends restore_task { + + protected $info; // info related to activity gathered from backup file + + /** + * Constructor - instantiates one object of this class + */ + public function __construct($name, $info, $plan = null) { + + $this->info = $info; + parent::__construct($name, $plan); + } + + /** + * Activity tasks have their own directory to read files + */ + public function get_taskbasepath() { + return $this->get_basepath() . '/activities/' . $info->modulename . '_' . $info->moduleid; + } + + /** + * Create all the steps that will be part of this task + */ + public function build() { + + // If we have decided not to restore activities, prevent anything to be built + if (!$this->get_setting_value('activities')) { + $this->built = true; + return; + } + + // TODO: Link all the section steps here + + // At the end, mark it as built + $this->built = true; + } + + /** + * Exceptionally override the execute method, so, based in the activity_included setting, we are able + * to skip the execution of one task completely + */ + public function execute() { + + // Find activity_included_setting + if (!$this->get_setting_value('included')) { + $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name); + + } else { // Setting tells us it's ok to execute + parent::execute(); + } + } + + + /** + * Specialisation that, first of all, looks for the setting within + * the task with the the prefix added and later, delegates to parent + * without adding anything + */ + public function get_setting($name) { + $namewithprefix = $this->info->modulename . '_' . $this->info->moduleid . '_' . $name; + $result = null; + foreach ($this->settings as $key => $setting) { + if ($setting->get_name() == $namewithprefix) { + if ($result != null) { + throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix); + } else { + $result = $setting; + } + } + } + if ($result) { + return $result; + } else { + // Fallback to parent + return parent::get_setting($name); + } + } + +// Protected API starts here + + /** + * Define the common setting that any restore activity will have + */ + protected function define_settings() { + + // All the settings related to this activity will include this prefix + $settingprefix = $this->info->modulename . '_' . $this->info->moduleid . '_'; + + // All these are common settings to be shared by all activities + + // Define activity_include (to decide if the whole task must be really executed) + // Dependent of: + // - activities root setting + // - section_included setting (if exists) + $settingname = $settingprefix . 'included'; + $activity_included = new restore_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->info->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: + // - users root setting + // - section_userinfo setting (if exists) + // - activity_included setting + $settingname = $settingprefix . 'userinfo'; + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($info->settings[$settingname]) && $info->settings[$settingname]) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $activity_userinfo = new restore_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue); + $activity_userinfo->set_ui(new backup_setting_ui_select($activity_userinfo, get_string('includeuserinfo','backup'), $selectvalues)); + $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->info->sectionid . '_userinfo'; + if ($this->plan->setting_exists($settingname)) { + $section_userinfo = $this->plan->get_setting($settingname); + $section_userinfo->add_dependency($activity_userinfo); + } + // Look for "activity_included" setting + $activity_included->add_dependency($activity_userinfo); + + // End of common activity settings, let's add the particular ones + $this->define_my_settings(); + } + + /** + * Define (add) particular settings that each activity can have + */ + abstract protected function define_my_settings(); + + /** + * Define (add) particular steps that each activity can have + */ + abstract protected function define_my_steps(); + + /** + * Code the transformations to perform in the course in + * order to get encoded transformed back to working links + */ + abstract static public function decode_content_links($content); + +} diff --git a/backup/moodle2/restore_course_task.class.php b/backup/moodle2/restore_course_task.class.php new file mode 100644 index 00000000000..8c24e167225 --- /dev/null +++ b/backup/moodle2/restore_course_task.class.php @@ -0,0 +1,86 @@ +. + +/** + * @package moodlecore + * @subpackage backup-moodle2 + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * course task that provides all the properties and common steps to be performed + * when one course is being restored + * + * TODO: Finish phpdocs + */ +class restore_course_task extends restore_task { + + protected $info; // info related to course gathered from backup file + + /** + * Constructor - instantiates one object of this class + */ + public function __construct($name, $info, $plan = null) { + $this->info = $info; + parent::__construct($name, $plan); + } + + /** + * Course tasks have their own directory to read files + */ + public function get_taskbasepath() { + + return $this->get_basepath() . '/course'; + } + + /** + * Create all the steps that will be part of this task + */ + public function build() { + + // TODO: Link all the course steps here + + // At the end, mark it as built + $this->built = true; + } + + /** + * Code the transformations to perform in the course in + * order to get encoded transformed back to working links + */ + static public function decode_content_links($content) { + + // TODO: Decode COURSEVIEWBYID + + return $content; + } + +// Protected API starts here + + /** + * Define the common setting that any restore section will have + */ + protected function define_settings() { + + // Define overwrite_conf to decide if course configuration will be restored over existing one + $overwrite = new restore_course_overwrite_conf_setting('overwrite_conf', base_setting::IS_BOOLEAN, false); + $overwrite->set_ui(new backup_setting_ui_select($overwrite, $overwrite->get_name(), array(1=>get_string('yes'), 0=>get_string('no')))); + $this->add_setting($overwrite); + + } +} diff --git a/backup/moodle2/restore_final_task.class.php b/backup/moodle2/restore_final_task.class.php new file mode 100644 index 00000000000..69f9d8bbc3a --- /dev/null +++ b/backup/moodle2/restore_final_task.class.php @@ -0,0 +1,50 @@ +. + +/** + * @package moodlecore + * @subpackage backup-moodle2 + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Final task that provides all the final steps necessary in order to finish one + * restore like gradebook, interlinks... apart from some final cleaning + * + * TODO: Finish phpdocs + */ +class restore_final_task extends restore_task { + + /** + * Create all the steps that will be part of this task + */ + public function build() { + + // TODO: Link all the final steps here + $this->built = true; + } + +// Protected API starts here + + /** + * Define the common setting that any restore type will have + */ + protected function define_settings() { + // This task has not settings (could have them, like destination or so in the future, let's see) + } +} diff --git a/backup/moodle2/restore_root_task.class.php b/backup/moodle2/restore_root_task.class.php new file mode 100644 index 00000000000..4c3aaed4e53 --- /dev/null +++ b/backup/moodle2/restore_root_task.class.php @@ -0,0 +1,169 @@ +. + +/** + * @package moodlecore + * @subpackage backup-moodle2 + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Start task that provides all the settings common to all restores and other initial steps + * + * TODO: Finish phpdocs + */ +class restore_root_task extends restore_task { + + /** + * Create all the steps that will be part of this task + */ + public function build() { + + // TODO: Link all the preloading/precreation steps here + + // At the end, mark it as built + $this->built = true; + } + +// Protected API starts here + + /** + * Define the common setting that any restore type will have + */ + protected function define_settings() { + + // Load all the root settings found in backup file from controller + $rootsettings = $this->get_info()->root_settings; + + // Define users setting (keeping it on hand to define dependencies) + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($rootsettings['users']) && $rootsettings['users']) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $users = new restore_users_setting('users', base_setting::IS_BOOLEAN, $defaultvalue); + $users->set_ui(new backup_setting_ui_select($users, $users->get_name(), $selectvalues)); + $this->add_setting($users); + + // Define role_assignments (dependent of users) + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($rootsettings['role_assignments']) && $rootsettings['role_assignments']) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $roleassignments = new restore_role_assignments_setting('role_assignments', base_setting::IS_BOOLEAN, $defaultvalue); + $roleassignments->set_ui(new backup_setting_ui_select($roleassignments, $roleassignments->get_name(), $selectvalues)); + $this->add_setting($roleassignments); + $users->add_dependency($roleassignments); + + // Define user_files (dependent of users) + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($rootsettings['user_files']) && $rootsettings['user_files']) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $userfiles = new restore_user_files_setting('user_files', base_setting::IS_BOOLEAN, $defaultvalue); + $userfiles->set_ui(new backup_setting_ui_select($userfiles, $userfiles->get_name(), $selectvalues)); + $this->add_setting($userfiles); + $users->add_dependency($userfiles); + + // Define activitites + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($rootsettings['activities']) && $rootsettings['activities']) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $activities = new restore_activities_setting('activities', base_setting::IS_BOOLEAN, $defaultvalue); + $activities->set_ui(new backup_setting_ui_select($activities, $activities->get_name(), $selectvalues)); + $this->add_setting($activities); + + // Define blocks + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($rootsettings['blocks']) && $rootsettings['blocks']) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $blocks = new restore_generic_setting('blocks', base_setting::IS_BOOLEAN, $defaultvalue); + $blocks->set_ui(new backup_setting_ui_select($blocks, $blocks->get_name(), $selectvalues)); + $this->add_setting($blocks); + + // Define filters + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($rootsettings['filters']) && $rootsettings['filters']) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $filters = new restore_generic_setting('filters', base_setting::IS_BOOLEAN, $defaultvalue); + $filters->set_ui(new backup_setting_ui_select($filters, $filters->get_name(), $selectvalues)); + $this->add_setting($filters); + + // Define comments (dependent of users) + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($rootsettings['comments']) && $rootsettings['comments']) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $comments = new restore_comments_setting('comments', base_setting::IS_BOOLEAN, $defaultvalue); + $comments->set_ui(new backup_setting_ui_select($comments, $comments->get_name(), $selectvalues)); + $this->add_setting($comments); + $users->add_dependency($comments); + + // Define completion (dependent of users) + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($rootsettings['userscompletion']) && $rootsettings['userscompletion']) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $completion = new restore_userscompletion_setting('userscompletion', base_setting::IS_BOOLEAN, $defaultvalue); + $completion->set_ui(new backup_setting_ui_select($completion, $completion->get_name(), $selectvalues)); + $this->add_setting($completion); + $users->add_dependency($completion); + + // Define logs (dependent of users) + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($rootsettings['logs']) && $rootsettings['logs']) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $logs = new restore_logs_setting('logs', base_setting::IS_BOOLEAN, $defaultvalue); + $logs->set_ui(new backup_setting_ui_select($logs, $logs->get_name(), $selectvalues)); + $this->add_setting($logs); + $users->add_dependency($logs); + + // Define grade_histories (dependent of users) + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($rootsettings['grade_histories']) && $rootsettings['grade_histories']) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $gradehistories = new restore_grade_histories_setting('grade_histories', base_setting::IS_BOOLEAN, $defaultvalue); + $gradehistories->set_ui(new backup_setting_ui_select($gradehistories, $gradehistories->get_name(), $selectvalues)); + $this->add_setting($gradehistories); + $users->add_dependency($gradehistories); + } +} diff --git a/backup/moodle2/restore_section_task.class.php b/backup/moodle2/restore_section_task.class.php new file mode 100644 index 00000000000..c8d9672ccf9 --- /dev/null +++ b/backup/moodle2/restore_section_task.class.php @@ -0,0 +1,98 @@ +. + +/** + * @package moodlecore + * @subpackage backup-moodle2 + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * section task that provides all the properties and common steps to be performed + * when one section is being restored + * + * TODO: Finish phpdocs + */ +class restore_section_task extends restore_task { + + protected $info; // info related to section gathered from backup file + + /** + * Constructor - instantiates one object of this class + */ + public function __construct($name, $info, $plan = null) { + $this->info = $info; + parent::__construct($name, $plan); + } + + /** + * Section tasks have their own directory to read files + */ + public function get_taskbasepath() { + + return $this->get_basepath() . '/sections/section_' . $info->sectionid; + } + + /** + * Create all the steps that will be part of this task + */ + public function build() { + + // TODO: Link all the section steps here + + // At the end, mark it as built + $this->built = true; + } + +// Protected API starts here + + /** + * Define the common setting that any restore section will have + */ + protected function define_settings() { + + // All the settings related to this activity will include this prefix + $settingprefix = 'section_' . $this->info->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 restore_section_included_setting($settingname, base_setting::IS_BOOLEAN, true); + $this->add_setting($section_included); + + // Define section_userinfo. Dependent of: + // - users root setting + // - section_included setting + $settingname = $settingprefix . 'userinfo'; + $selectvalues = array(0=>get_string('no')); // Safer options + $defaultvalue = false; // Safer default + if (isset($info->settings[$settingname]) && $info->settings[$settingname]) { // Only enabled when available + $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); + $defaultvalue = true; + } + $section_userinfo = new restore_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue); + $section_userinfo->set_ui(new backup_setting_ui_select($section_userinfo, get_string('includeuserinfo','backup'), $selectvalues)); + $this->add_setting($section_userinfo); + // Look for "users" root setting + $users = $this->plan->get_setting('users'); + $users->add_dependency($section_userinfo); + // Look for "section_included" section setting + $section_included->add_dependency($section_userinfo); + } +} diff --git a/backup/moodle2/restore_settingslib.php b/backup/moodle2/restore_settingslib.php new file mode 100644 index 00000000000..df8c369c5ad --- /dev/null +++ b/backup/moodle2/restore_settingslib.php @@ -0,0 +1,141 @@ +. + +/** + * @package moodlecore + * @subpackage backup-moodle2 + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +// TODO: Reduce these to the minimum because ui/dependencies are 100% separated + +// Root restore settings + +/** + * root generic setting to store different things without dependencies + */ +class restore_generic_setting extends root_backup_setting {} + +/** + * root setting to control if restore will create user information + * A lot of other settings are dependent of this (module's user info, + * grades user info, messages, blogs... + */ +class restore_users_setting extends restore_generic_setting {} + +/** + * root setting to control if restore will create role assignments + * or no (any level), depends of @restore_users_setting + */ +class restore_role_assignments_setting extends root_backup_setting {} + +/** + * root setting to control if restore will create + * user files or no (images, local storage), depends of @restore_users_setting + * exactly in the same way than @restore_role_assignments_setting so we extend from it + */ +class restore_user_files_setting extends restore_role_assignments_setting {} + +/** + * root setting to control if restore will create activities + * A lot of other settings (_included at activity levels) + * are dependent of this setting + */ +class restore_activities_setting extends restore_generic_setting {} + +/** + * root setting to control if restore will create + * comments or no, depends of @restore_users_setting + * exactly in the same way than @restore_role_assignments_setting so we extend from it + */ +class restore_comments_setting extends restore_role_assignments_setting {} + +/** + * root setting to control if restore will create + * completion info or no, depends of @restore_users_setting + * exactly in the same way than @restore_role_assignments_setting so we extend from it + */ +class restore_userscompletion_setting extends restore_role_assignments_setting {} + +/** + * root setting to control if restore will create + * logs or no, depends of @restore_users_setting + * exactly in the same way than @restore_role_assignments_setting so we extend from it + */ +class restore_logs_setting extends restore_role_assignments_setting {} + +/** + * root setting to control if restore will create + * grade_histories or no, depends of @restore_users_setting + * exactly in the same way than @restore_role_assignments_setting so we extend from it + */ +class restore_grade_histories_setting extends restore_role_assignments_setting {} + + +// Course restore settings + +/** + * generic course setting to pass various settings between tasks and steps + */ +class restore_course_generic_setting extends course_backup_setting {} + +/** + * Setting to define is we are going to overwrite course configuration + */ +class restore_course_overwrite_conf_setting extends restore_course_generic_setting {} + + +// Section restore settings + +/** + * generic section setting to pass various settings between tasks and steps + */ +class restore_section_generic_setting extends section_backup_setting {} + +/** + * Setting to define if one section is included or no. Activities _included + * settings depend of them if available + */ +class restore_section_included_setting extends restore_section_generic_setting {} + +/** + * section backup setting to control if section will include + * user information or no, depends of @restore_users_setting + */ +class restore_section_userinfo_setting extends restore_section_generic_setting {} + + +// Activity backup settings + +/** + * generic activity setting to pass various settings between tasks and steps + */ +class restore_activity_generic_setting extends activity_backup_setting {} + +/** + * activity backup setting to control if activity will + * be included or no, depends of @restore_activities_setting and + * optionally parent section included setting + */ +class restore_activity_included_setting extends restore_activity_generic_setting {} + +/** + * activity backup setting to control if activity will include + * user information or no, depends of @restore_users_setting + */ +class restore_activity_userinfo_setting extends restore_activity_generic_setting {} -- 2.43.0