Skip to content

Commit

Permalink
MDL-68314 core_contentbank: Course content bank backup and restore
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed May 13, 2020
1 parent 206e179 commit 3449e22
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 5 deletions.
20 changes: 20 additions & 0 deletions admin/settings/courses.php
Expand Up @@ -235,6 +235,11 @@
new lang_string('generalgroups', 'backup'), new lang_string('configgeneralgroups', 'backup'),
array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_competencies', new lang_string('generalcompetencies','backup'), new lang_string('configgeneralcompetencies','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_contentbankcontent',
new lang_string('generalcontentbankcontent', 'backup'),
new lang_string('configgeneralcontentbankcontent', 'backup'),
['value' => 1, 'locked' => 0])
);

$ADMIN->add('backups', $temp);

Expand All @@ -256,6 +261,12 @@
new lang_string('generalgroups', 'backup'), new lang_string('configgeneralgroups', 'backup'),
array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_import_competencies', new lang_string('generalcompetencies','backup'), new lang_string('configgeneralcompetencies','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock(
'backup/backup_import_contentbankcontent',
new lang_string('generalcontentbankcontent', 'backup'),
new lang_string('configgeneralcontentbankcontent', 'backup'),
['value' => 1, 'locked' => 0])
);

$ADMIN->add('backups', $temp);

Expand Down Expand Up @@ -375,6 +386,12 @@
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_groups', new lang_string('generalgroups', 'backup'),
new lang_string('configgeneralgroups', 'backup'), 1));
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_competencies', new lang_string('generalcompetencies','backup'), new lang_string('configgeneralcompetencies','backup'), 1));
$temp->add(new admin_setting_configcheckbox(
'backup/backup_auto_contentbankcontent',
new lang_string('generalcontentbankcontent', 'backup'),
new lang_string('configgeneralcontentbankcontent', 'backup'),
1)
);

//$temp->add(new admin_setting_configcheckbox('backup/backup_auto_messages', new lang_string('messages', 'message'), new lang_string('backupmessageshelp','message'), 0));
//$temp->add(new admin_setting_configcheckbox('backup/backup_auto_blogs', new lang_string('blogs', 'blog'), new lang_string('backupblogshelp','blog'), 0));
Expand Down Expand Up @@ -435,6 +452,9 @@
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_competencies',
new lang_string('generalcompetencies', 'backup'),
new lang_string('configrestorecompetencies', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_contentbankcontent',
new lang_string('generalcontentbankcontent', 'backup'),
new lang_string('configrestorecontentbankcontent', 'backup'), array('value' => 1, 'locked' => 0)));

// Restore defaults when merging into another course.
$temp->add(new admin_setting_heading('mergerestoredefaults', new lang_string('mergerestoredefaults', 'backup'), ''));
Expand Down
5 changes: 5 additions & 0 deletions backup/moodle2/backup_course_task.class.php
Expand Up @@ -139,6 +139,11 @@ public function build() {
// Migrate the already exported inforef entries to final ones
$this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));

// Generate the content bank file (conditionally).
if ($this->get_setting_value('contentbankcontent')) {
$this->add_step(new backup_contentbankcontent_structure_step('course_contentbank', 'contentbank.xml'));
}

// At the end, mark it as built
$this->built = true;
}
Expand Down
5 changes: 5 additions & 0 deletions backup/moodle2/backup_root_task.class.php
Expand Up @@ -179,5 +179,10 @@ protected function define_settings() {
$customfields = new backup_customfield_setting('customfield', base_setting::IS_BOOLEAN, true);
$customfields->set_ui(new backup_setting_ui_checkbox($customfields, get_string('rootsettingcustomfield', 'backup')));
$this->add_setting($customfields);

// Define content bank content inclusion setting.
$contentbank = new backup_contentbankcontent_setting('contentbankcontent', base_setting::IS_BOOLEAN, true);
$contentbank->set_ui(new backup_setting_ui_checkbox($contentbank, get_string('rootsettingcontentbankcontent', 'backup')));
$this->add_setting($contentbank);
}
}
6 changes: 6 additions & 0 deletions backup/moodle2/backup_settingslib.php
Expand Up @@ -199,3 +199,9 @@ class backup_activity_included_setting extends activity_backup_setting {}
* user information or no, depends of @backup_users_setting
*/
class backup_activity_userinfo_setting extends activity_backup_setting {}

/**
* Root setting to control if backup will include content bank content or no
*/
class backup_contentbankcontent_setting extends backup_generic_setting {
}
31 changes: 31 additions & 0 deletions backup/moodle2/backup_stepslib.php
Expand Up @@ -2740,3 +2740,34 @@ protected function define_structure() {

}
}

/**
* Structure step in charge of constructing the contentbank.xml file for all the contents found in a given context
*/
class backup_contentbankcontent_structure_step extends backup_structure_step {

/**
* Define structure for content bank step
*/
protected function define_structure() {

// Define each element separated.
$contents = new backup_nested_element('contents');
$content = new backup_nested_element('content', ['id'], [
'name', 'contenttype', 'instanceid', 'configdata', 'usercreated', 'usermodified', 'timecreated', 'timemodified']);

// Build the tree.
$contents->add_child($content);

// Define sources.
$content->set_source_table('contentbank_content', ['contextid' => backup::VAR_CONTEXTID]);

// Define annotations.
$content->annotate_ids('user', 'usercreated');
$content->annotate_ids('user', 'usermodified');
$content->annotate_files('contentbank', 'public', 'id');

// Return the root element (contents).
return $contents;
}
}
5 changes: 5 additions & 0 deletions backup/moodle2/restore_course_task.class.php
Expand Up @@ -126,6 +126,11 @@ public function build() {
// Activity completion defaults.
$this->add_step(new restore_completion_defaults_structure_step('course_completion_defaults', 'completiondefaults.xml'));

// Content bank content (conditionally).
if ($this->get_setting_value('contentbankcontent')) {
$this->add_step(new restore_contentbankcontent_structure_step('course_contentbank', 'contentbank.xml'));
}

// At the end, mark it as built
$this->built = true;
}
Expand Down
12 changes: 12 additions & 0 deletions backup/moodle2/restore_root_task.class.php
Expand Up @@ -290,5 +290,17 @@ protected function define_settings() {
$customfields = new restore_customfield_setting('customfields', base_setting::IS_BOOLEAN, $defaultvalue);
$customfields->set_ui(new backup_setting_ui_checkbox($customfields, get_string('rootsettingcustomfield', 'backup')));
$this->add_setting($customfields);

// Define Content bank content.
$defaultvalue = false;
$changeable = false;
if (isset($rootsettings['contentbankcontent']) && $rootsettings['contentbankcontent']) { // Only enabled when available.
$defaultvalue = true;
$changeable = true;
}
$contents = new restore_contentbankcontent_setting('contentbankcontent', base_setting::IS_BOOLEAN, $defaultvalue);
$contents->set_ui(new backup_setting_ui_checkbox($contents, get_string('rootsettingcontentbankcontent', 'backup')));
$contents->get_ui()->set_changeable($changeable);
$this->add_setting($contents);
}
}
6 changes: 6 additions & 0 deletions backup/moodle2/restore_settingslib.php
Expand Up @@ -236,3 +236,9 @@ class restore_activity_included_setting extends restore_activity_generic_setting
* user information or no, depends of @restore_users_setting
*/
class restore_activity_userinfo_setting extends restore_activity_generic_setting {}

/**
* root setting to control if restore will create content bank content or no
*/
class restore_contentbankcontent_setting extends restore_generic_setting {
}
54 changes: 54 additions & 0 deletions backup/moodle2/restore_stepslib.php
Expand Up @@ -3984,6 +3984,60 @@ protected function process_grade_grade($data) {
}
}

/**
* This structure steps restores the content bank content
*/
class restore_contentbankcontent_structure_step extends restore_structure_step {

/**
* Define structure for content bank step
*/
protected function define_structure() {

$paths = [];
$paths[] = new restore_path_element('contentbankcontent', '/contents/content');

return $paths;
}

/**
* Define data processed for content bank
*
* @param mixed $data
*/
public function process_contentbankcontent($data) {
global $DB;

$data = (object)$data;
$oldid = $data->id;

$params = [
'name' => $data->name,
'contextid' => $this->task->get_contextid(),
'contenttype' => $data->contenttype,
'instanceid' => $data->instanceid,
'timecreated' => $data->timecreated,
];
$exists = $DB->record_exists('contentbank_content', $params);
if (!$exists) {
$params['configdata'] = $data->configdata;
$params['usercreated'] = $this->get_mappingid('user', $data->usercreated);
$params['usermodified'] = $this->get_mappingid('user', $data->usermodified);
$params['timemodified'] = time();
$newitemid = $DB->insert_record('contentbank_content', $params);
$this->set_mapping('contentbank_content', $oldid, $newitemid, true);
}
}

/**
* Define data processed after execute for content bank
*/
protected function after_execute() {
// Add related files.
$this->add_related_files('contentbank', 'public', 'contentbank_content');
}
}

/**
* This structure steps restores one instance + positions of one block
* Note: Positions corresponding to one existing context are restored
Expand Down
9 changes: 6 additions & 3 deletions backup/util/dbops/backup_controller_dbops.class.php
Expand Up @@ -564,7 +564,8 @@ public static function apply_config_defaults(backup_controller $controller) {
'backup_general_histories' => 'grade_histories',
'backup_general_questionbank' => 'questionbank',
'backup_general_groups' => 'groups',
'backup_general_competencies' => 'competencies'
'backup_general_competencies' => 'competencies',
'backup_general_contentbankcontent' => 'contentbankcontent',
);
self::apply_admin_config_defaults($controller, $settings, true);
break;
Expand All @@ -577,7 +578,8 @@ public static function apply_config_defaults(backup_controller $controller) {
'backup_import_calendarevents' => 'calendarevents',
'backup_import_questionbank' => 'questionbank',
'backup_import_groups' => 'groups',
'backup_import_competencies' => 'competencies'
'backup_import_competencies' => 'competencies',
'backup_import_contentbankcontent' => 'contentbankcontent',
);
self::apply_admin_config_defaults($controller, $settings, true);
if ((!$controller->get_interactive()) &&
Expand Down Expand Up @@ -608,7 +610,8 @@ public static function apply_config_defaults(backup_controller $controller) {
'backup_auto_histories' => 'grade_histories',
'backup_auto_questionbank' => 'questionbank',
'backup_auto_groups' => 'groups',
'backup_auto_competencies' => 'competencies'
'backup_auto_competencies' => 'competencies',
'backup_auto_contentbankcontent' => 'contentbankcontent'
);
self::apply_admin_config_defaults($controller, $settings, false);
break;
Expand Down
3 changes: 2 additions & 1 deletion backup/util/dbops/restore_controller_dbops.class.php
Expand Up @@ -157,7 +157,8 @@ public static function apply_config_defaults(restore_controller $controller) {
'restore_general_histories' => 'grade_histories',
'restore_general_questionbank' => 'questionbank',
'restore_general_groups' => 'groups',
'restore_general_competencies' => 'competencies'
'restore_general_competencies' => 'competencies',
'restore_general_contentbankcontent' => 'contentbankcontent'
);
self::apply_admin_config_defaults($controller, $settings, true);

Expand Down
4 changes: 4 additions & 0 deletions lang/en/backup.php
Expand Up @@ -127,6 +127,7 @@
$string['configgeneralcalendarevents'] = 'Sets the default for including calendar events in a backup.';
$string['configgeneralcomments'] = 'Sets the default for including comments in a backup.';
$string['configgeneralcompetencies'] = 'Sets the default for including competencies in a backup.';
$string['configgeneralcontentbankcontent'] = 'Sets the default for including content bank content in a backup.';
$string['configgeneralfiles'] = 'Sets the default for including files in a backup. Please note: Disabling this setting will result in a backup which only includes references to files. This is not a problem if the backup is restored on the same site and the files have not been deleted according to the setting \'Clean up trash pool files\' (filescleanupperiod).';
$string['configgeneralfilters'] = 'Sets the default for including filters in a backup.';
$string['configgeneralhistories'] = 'Sets the default for including user history within a backup.';
Expand All @@ -143,6 +144,7 @@
$string['configrestorecalendarevents'] = 'Sets the default for restoring calendar events.';
$string['configrestorecomments'] = 'Sets the default for restoring comments.';
$string['configrestorecompetencies'] = 'Sets the default for restoring competencies.';
$string['configrestorecontentbankcontent'] = 'Sets the default for restoring content bank content.';
$string['configrestoreenrolments'] = 'Sets the default for restoring enrolment methods.';
$string['configrestorefilters'] = 'Sets the default for restoring filters.';
$string['configrestorehistories'] = 'Sets the default for restoring user history if it was included in the backup.';
Expand Down Expand Up @@ -202,6 +204,7 @@
$string['generalcalendarevents'] = 'Include calendar events';
$string['generalcomments'] = 'Include comments';
$string['generalcompetencies'] = 'Include competencies';
$string['generalcontentbankcontent'] = 'Include content bank content';
$string['generalenrolments'] = 'Include enrolment methods';
$string['generalfiles'] = 'Include files';
$string['generalfilters'] = 'Include filters';
Expand Down Expand Up @@ -333,6 +336,7 @@
$string['rootsettingfiles'] = 'Include files';
$string['rootsettingcomments'] = 'Include comments';
$string['rootsettingcalendarevents'] = 'Include calendar events';
$string['rootsettingcontentbankcontent'] = 'Include content bank content';
$string['rootsettinguserscompletion'] = 'Include user completion details';
$string['rootsettingquestionbank'] = 'Include question bank';
$string['rootsettinglogs'] = 'Include course logs';
Expand Down
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2020051200.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2020051200.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '3.9dev+ (Build: 20200512)'; // Human-friendly version name
Expand Down

0 comments on commit 3449e22

Please sign in to comment.