Skip to content

Commit

Permalink
MDL-57898 core_course: backup/restore support for course custom fields
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danielneis authored and marinaglancy committed Jan 18, 2019
1 parent 028ed12 commit 5af9aa6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions backup/moodle2/backup_root_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,10 @@ protected function define_settings() {
$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);
}
}
9 changes: 9 additions & 0 deletions backup/moodle2/backup_settingslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ protected function define_structure() {
$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);

Expand Down Expand Up @@ -425,6 +430,9 @@ protected function define_structure() {
$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()));
Expand Down Expand Up @@ -457,6 +465,10 @@ protected function define_structure() {
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');
Expand Down
4 changes: 4 additions & 0 deletions backup/moodle2/restore_root_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,9 @@ protected function define_settings() {
$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);
}
}
9 changes: 9 additions & 0 deletions backup/moodle2/restore_settingslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,7 @@ protected function define_structure() {
$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
Expand All @@ -1808,7 +1809,7 @@ protected function define_structure() {
// 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);
}

/**
Expand Down Expand Up @@ -1932,6 +1933,16 @@ public function process_tag($data) {
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;

Expand Down
1 change: 1 addition & 0 deletions lang/en/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
$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';
Expand Down

0 comments on commit 5af9aa6

Please sign in to comment.