From 5af9aa6341e24071648bd2db09831a33272392ab Mon Sep 17 00:00:00 2001 From: Daniel Neis Araujo Date: Fri, 11 Jan 2019 10:55:03 +0100 Subject: [PATCH] MDL-57898 core_course: backup/restore support for course custom fields This commit is part of work on Custom fields API, to minimize commit history in moodle core the work of a team of developers was split into several commits with different authors but the authorship of individual lines of code may be different from the commit author. --- backup/moodle2/backup_root_task.class.php | 5 +++++ backup/moodle2/backup_settingslib.php | 9 +++++++++ backup/moodle2/backup_stepslib.php | 12 ++++++++++++ backup/moodle2/restore_root_task.class.php | 4 ++++ backup/moodle2/restore_settingslib.php | 9 +++++++++ backup/moodle2/restore_stepslib.php | 13 ++++++++++++- lang/en/backup.php | 1 + 7 files changed, 52 insertions(+), 1 deletion(-) diff --git a/backup/moodle2/backup_root_task.class.php b/backup/moodle2/backup_root_task.class.php index 6366c8fbefa..1bc7ce4ded0 100644 --- a/backup/moodle2/backup_root_task.class.php +++ b/backup/moodle2/backup_root_task.class.php @@ -168,5 +168,10 @@ class backup_root_task extends backup_task { $competencies = new backup_competencies_setting(); $competencies->set_ui(new backup_setting_ui_checkbox($competencies, get_string('rootsettingcompetencies', 'backup'))); $this->add_setting($competencies); + + // Define custom fields inclusion setting if custom fields are used. + $customfields = new backup_customfield_setting('customfield', base_setting::IS_BOOLEAN, true); + $customfields->set_ui(new backup_setting_ui_checkbox($customfields, get_string('rootsettingcustomfields', 'backup'))); + $this->add_setting($customfields); } } diff --git a/backup/moodle2/backup_settingslib.php b/backup/moodle2/backup_settingslib.php index d2e1e5f09a5..91256731058 100644 --- a/backup/moodle2/backup_settingslib.php +++ b/backup/moodle2/backup_settingslib.php @@ -74,6 +74,15 @@ class backup_users_setting extends backup_generic_setting {} class backup_groups_setting extends backup_generic_setting { } +/** + * root setting to control if backup will include custom field information + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @copyright 2018 Daniel Neis Araujo + */ +class backup_customfield_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) diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index ef99fa67920..19e314f75fe 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -391,6 +391,11 @@ class backup_course_structure_step extends backup_structure_step { $tag = new backup_nested_element('tag', array('id'), array( 'name', 'rawname')); + $customfields = new backup_nested_element('customfields'); + $customfield = new backup_nested_element('customfield', array('id'), array( + 'shortname', 'type', 'value', 'valueformat' + )); + // attach format plugin structure to $course element, only one allowed $this->add_plugin_structure('format', $course, false); @@ -425,6 +430,9 @@ class backup_course_structure_step extends backup_structure_step { $course->add_child($tags); $tags->add_child($tag); + $course->add_child($customfields); + $customfields->add_child($customfield); + // Set the sources $courserec = $DB->get_record('course', array('id' => $this->task->get_courseid())); @@ -457,6 +465,10 @@ class backup_course_structure_step extends backup_structure_step { backup_helper::is_sqlparam('course'), backup::VAR_PARENTID)); + $handler = core_course\customfield\course_handler::create(); + $fieldsforbackup = $handler->get_instance_data_for_backup($this->task->get_courseid()); + $customfield->set_source_array($fieldsforbackup); + // Some annotations $course->annotate_ids('grouping', 'defaultgroupingid'); diff --git a/backup/moodle2/restore_root_task.class.php b/backup/moodle2/restore_root_task.class.php index eb033182c04..6bb4927ff9a 100644 --- a/backup/moodle2/restore_root_task.class.php +++ b/backup/moodle2/restore_root_task.class.php @@ -286,5 +286,9 @@ class restore_root_task extends restore_task { $competencies = new restore_competencies_setting($hascompetencies); $competencies->set_ui(new backup_setting_ui_checkbox($competencies, get_string('rootsettingcompetencies', 'backup'))); $this->add_setting($competencies); + + $customfields = new restore_customfield_setting('customfields', base_setting::IS_BOOLEAN, $defaultvalue); + $customfields->set_ui(new backup_setting_ui_checkbox($customfields, get_string('rootsettingcustomfields', 'backup'))); + $this->add_setting($customfields); } } diff --git a/backup/moodle2/restore_settingslib.php b/backup/moodle2/restore_settingslib.php index 4d559e048c9..daa3fecbbcc 100644 --- a/backup/moodle2/restore_settingslib.php +++ b/backup/moodle2/restore_settingslib.php @@ -52,6 +52,15 @@ class restore_users_setting extends restore_generic_setting {} class restore_groups_setting extends restore_generic_setting { } +/** + * root setting to control if restore will include custom field information + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @copyright 2018 Daniel Neis Araujo + */ +class restore_customfield_setting extends restore_generic_setting { +} + /** * root setting to control if restore will create role assignments * or no (any level), depends of @restore_users_setting diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 859198ea0e0..b3d5322f70d 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -1785,6 +1785,7 @@ class restore_course_structure_step extends restore_structure_step { $course = new restore_path_element('course', '/course'); $category = new restore_path_element('category', '/course/category'); $tag = new restore_path_element('tag', '/course/tags/tag'); + $customfield = new restore_path_element('customfield', '/course/customfields/customfield'); $allowed_module = new restore_path_element('allowed_module', '/course/allowed_modules/module'); // Apply for 'format' plugins optional paths at course level @@ -1808,7 +1809,7 @@ class restore_course_structure_step extends restore_structure_step { // Apply for admin tool plugins optional paths at course level. $this->add_plugin_structure('tool', $course); - return array($course, $category, $tag, $allowed_module); + return array($course, $category, $tag, $customfield, $allowed_module); } /** @@ -1932,6 +1933,16 @@ class restore_course_structure_step extends restore_structure_step { context_course::instance($this->get_courseid()), $data->rawname); } + /** + * Process custom fields + * + * @param array $data + */ + public function process_customfield($data) { + $handler = core_course\customfield\course_handler::create(); + $handler->restore_instance_data_from_backup($this->task, $data); + } + public function process_allowed_module($data) { $data = (object)$data; diff --git a/lang/en/backup.php b/lang/en/backup.php index 422296d7106..d0070a87351 100644 --- a/lang/en/backup.php +++ b/lang/en/backup.php @@ -275,6 +275,7 @@ $string['restoringcourse'] = 'Course restoration in progress'; $string['restoringcourseshortname'] = 'restoring'; $string['restorerolemappings'] = 'Restore role mappings'; $string['rootenrolmanual'] = 'Restore as manual enrolments'; +$string['rootsettingcustomfields'] = 'Include custom fields'; $string['rootsettingenrolments'] = 'Include enrolment methods'; $string['rootsettingenrolments_always'] = 'Yes, always'; $string['rootsettingenrolments_never'] = 'No, restore users as manual enrolments';