MDL-64702 badges: Add missing data to backup

Backup and restore of badges is missing data:
* alignments
* endorsement

Also added to "clone" (duplicate a badge).
This commit is contained in:
Damyon Wiese 2019-01-25 15:47:49 +08:00
parent 0dc93da62a
commit b3337bd5a4
3 changed files with 166 additions and 2 deletions

View File

@ -806,12 +806,25 @@ class backup_badges_structure_step extends backup_structure_step {
$badge = new backup_nested_element('badge', array('id'), array('name', 'description',
'timecreated', 'timemodified', 'usercreated', 'usermodified', 'issuername',
'issuerurl', 'issuercontact', 'expiredate', 'expireperiod', 'type', 'courseid',
'message', 'messagesubject', 'attachment', 'notification', 'status', 'nextcron'));
'message', 'messagesubject', 'attachment', 'notification', 'status', 'nextcron',
'version', 'language', 'imageauthorname', 'imageauthoremail', 'imageauthorurl',
'imagecaption'));
$criteria = new backup_nested_element('criteria');
$criterion = new backup_nested_element('criterion', array('id'), array('badgeid',
'criteriatype', 'method', 'description', 'descriptionformat'));
$endorsement = new backup_nested_element('endorsement', array('id'), array('badgeid',
'issuername', 'issuerurl', 'issueremail', 'claimid', 'claimcomment', 'dateissued'));
$alignments = new backup_nested_element('alignments');
$alignment = new backup_nested_element('alignment', array('id'), array('badgeid',
'targetname', 'targeturl', 'targetdescription', 'targetframework', 'targetcode'));
$relatedbadges = new backup_nested_element('relatedbadges');
$relatedbadge = new backup_nested_element('relatedbadge', array('id'), array('badgeid',
'relatedbadgeid'));
$parameters = new backup_nested_element('parameters');
$parameter = new backup_nested_element('parameter', array('id'), array('critid',
'name', 'value', 'criteriatype'));
@ -827,6 +840,11 @@ class backup_badges_structure_step extends backup_structure_step {
$criteria->add_child($criterion);
$criterion->add_child($parameters);
$parameters->add_child($parameter);
$badge->add_child($endorsement);
$badge->add_child($alignments);
$alignments->add_child($alignment);
$badge->add_child($relatedbadges);
$relatedbadges->add_child($relatedbadge);
$badge->add_child($manual_awards);
$manual_awards->add_child($manual_award);
@ -834,6 +852,10 @@ class backup_badges_structure_step extends backup_structure_step {
$badge->set_source_table('badge', array('courseid' => backup::VAR_COURSEID));
$criterion->set_source_table('badge_criteria', array('badgeid' => backup::VAR_PARENTID));
$endorsement->set_source_table('badge_endorsement', array('badgeid' => backup::VAR_PARENTID));
$alignment->set_source_table('badge_alignment', array('badgeid' => backup::VAR_PARENTID));
$relatedbadge->set_source_table('badge_related', array('badgeid' => backup::VAR_PARENTID));
$parametersql = 'SELECT cp.*, c.criteriatype
FROM {badge_criteria_param} cp JOIN {badge_criteria} c
@ -850,6 +872,10 @@ class backup_badges_structure_step extends backup_structure_step {
$badge->annotate_ids('user', 'usermodified');
$criterion->annotate_ids('badge', 'badgeid');
$parameter->annotate_ids('criterion', 'critid');
$endorsement->annotate_ids('badge', 'badgeid');
$alignment->annotate_ids('badge', 'badgeid');
$relatedbadge->annotate_ids('badge', 'badgeid');
$relatedbadge->annotate_ids('badge', 'relatedbadgeid');
$badge->annotate_files('badges', 'badgeimage', 'id');
$manual_award->annotate_ids('badge', 'badgeid');
$manual_award->annotate_ids('user', 'recipientid');

View File

@ -2517,6 +2517,9 @@ class restore_badges_structure_step extends restore_structure_step {
$paths[] = new restore_path_element('badge', '/badges/badge');
$paths[] = new restore_path_element('criterion', '/badges/badge/criteria/criterion');
$paths[] = new restore_path_element('parameter', '/badges/badge/criteria/criterion/parameters/parameter');
$paths[] = new restore_path_element('endorsement', '/badges/badge/endorsement');
$paths[] = new restore_path_element('alignment', '/badges/badge/alignments/alignment');
$paths[] = new restore_path_element('relatedbadge', '/badges/badge/relatedbadges/relatedbadge');
$paths[] = new restore_path_element('manual_award', '/badges/badge/manual_awards/manual_award');
return $paths;
@ -2561,13 +2564,87 @@ class restore_badges_structure_step extends restore_structure_step {
'attachment' => $data->attachment,
'notification' => $data->notification,
'status' => BADGE_STATUS_INACTIVE,
'nextcron' => $data->nextcron
'nextcron' => $data->nextcron,
'version' => $data->version,
'language' => $data->language,
'imageauthorname' => $data->imageauthorname,
'imageauthoremail' => $data->imageauthoremail,
'imageauthorurl' => $data->imageauthorurl,
'imagecaption' => $data->imagecaption
);
$newid = $DB->insert_record('badge', $params);
$this->set_mapping('badge', $data->id, $newid, $restorefiles);
}
/**
* Create an endorsement for a badge.
*
* @param mixed $data
* @return void
*/
public function process_endorsement($data) {
global $DB;
$data = (object)$data;
$params = [
'badgeid' => $this->get_new_parentid('badge'),
'issuername' => $data->issuername,
'issuerurl' => $data->issuerurl,
'issueremail' => $data->issueremail,
'claimid' => $data->claimid,
'claimcomment' => $data->claimcomment,
'dateissued' => $this->apply_date_offset($data->dateissued)
];
$newid = $DB->insert_record('badge_endorsement', $params);
$this->set_mapping('endorsement', $data->id, $newid);
}
/**
* Link to related badges for a badge. This relies on post processing in after_execute().
*
* @param mixed $data
* @return void
*/
public function process_relatedbadge($data) {
global $DB;
$data = (object)$data;
$relatedbadgeid = $data->relatedbadgeid;
if ($relatedbadgeid) {
// Only backup and restore related badges if they are contained in the backup file.
$params = array(
'badgeid' => $this->get_new_parentid('badge'),
'relatedbadgeid' => $relatedbadgeid
);
$newid = $DB->insert_record('badge_related', $params);
}
}
/**
* Link to an alignment for a badge.
*
* @param mixed $data
* @return void
*/
public function process_alignment($data) {
global $DB;
$data = (object)$data;
$params = array(
'badgeid' => $this->get_new_parentid('badge'),
'targetname' => $data->targetname,
'targeturl' => $data->targeturl,
'targetdescription' => $data->targetdescription,
'targetframework' => $data->targetframework,
'targetcode' => $data->targetcode
);
$newid = $DB->insert_record('badge_alignment', $params);
$this->set_mapping('alignment', $data->id, $newid);
}
public function process_criterion($data) {
global $DB;
@ -2580,6 +2657,7 @@ class restore_badges_structure_step extends restore_structure_step {
'description' => isset($data->description) ? $data->description : '',
'descriptionformat' => isset($data->descriptionformat) ? $data->descriptionformat : 0,
);
$newid = $DB->insert_record('badge_criteria', $params);
$this->set_mapping('criterion', $data->id, $newid);
}
@ -2613,6 +2691,14 @@ class restore_badges_structure_step extends restore_structure_step {
} else {
return;
}
} else if ($data->criteriatype == BADGE_CRITERIA_TYPE_COMPETENCY) {
$competencyid = $this->get_mappingid('competency', $data->value);
if (!empty($competencyid)) {
$params['name'] = 'competency_' . $competencyid;
$params['value'] = $competencyid;
} else {
return;
}
}
if (!$DB->record_exists('badge_criteria_param', $params)) {
@ -2645,8 +2731,38 @@ class restore_badges_structure_step extends restore_structure_step {
}
protected function after_execute() {
global $DB;
// Add related files.
$this->add_related_files('badges', 'badgeimage', 'badge');
$badgeid = $this->get_new_parentid('badge');
// Remap any related badges.
// We do this in the DB directly because this is backup/restore it is not valid to call into
// the component API.
$params = array('badgeid' => $badgeid);
$query = "SELECT DISTINCT br.id, br.badgeid, br.relatedbadgeid
FROM {badge_related} br
WHERE (br.badgeid = :badgeid)";
$relatedbadges = $DB->get_records_sql($query, $params);
$newrelatedids = [];
foreach ($relatedbadges as $relatedbadge) {
$relatedid = $this->get_mappingid('badge', $relatedbadge->relatedbadgeid);
$params['relatedbadgeid'] = $relatedbadge->relatedbadgeid;
$DB->delete_records_select('badge_related', '(badgeid = :badgeid AND relatedbadgeid = :relatedbadgeid)', $params);
if ($relatedid) {
$newrelatedids[] = $relatedid;
}
}
if (!empty($newrelatedids)) {
$relatedbadges = [];
foreach ($newrelatedids as $relatedid) {
$relatedbadge = new stdClass();
$relatedbadge->badgeid = $badgeid;
$relatedbadge->relatedbadgeid = $relatedid;
$relatedbadges[] = $relatedbadge;
}
$DB->insert_records('badge_related', $relatedbadges);
}
}
}

View File

@ -324,6 +324,28 @@ class badge {
$crit->make_clone($new);
}
// Copy endorsement.
$endorsement = $this->get_endorsement();
if ($endorsement) {
unset($endorsement->id);
$endorsement->badgeid = $new;
$newbadge->save_endorsement($endorsement);
}
// Copy alignments.
$alignments = $this->get_alignments();
foreach ($alignments as $alignment) {
unset($alignment->id);
$alignment->badgeid = $new;
$newbadge->save_alignment($alignment);
}
// Copy related badges.
$related = $this->get_related_badges(true);
if (!empty($related)) {
$newbadge->add_related_badges(array_keys($related));
}
// Trigger event, badge duplicated.
$eventparams = array('objectid' => $new, 'context' => $PAGE->context);
$event = \core\event\badge_duplicated::create($eventparams);