Fixed backup and restore after refactor

This commit is contained in:
Mark Nelson 2016-04-05 15:52:43 +08:00
parent ebbcfd1d4a
commit 058618ce1b
3 changed files with 60 additions and 26 deletions

View file

@ -38,7 +38,23 @@ class backup_customcert_activity_structure_step extends backup_activity_structur
// The instance. // The instance.
$customcert = new backup_nested_element('customcert', array('id'), array( $customcert = new backup_nested_element('customcert', array('id'), array(
'name', 'intro', 'introformat', 'requiredtime', 'protection', 'templateid', 'name', 'intro', 'introformat', 'requiredtime', 'protection',
'timecreated', 'timemodified'));
// The template.
$template = new backup_nested_element('template', array('id'), array(
'name', 'contextid', 'timecreated', 'timemodified'));
// The pages.
$pages = new backup_nested_element('pages');
$page = new backup_nested_element('page', array('id'), array(
'templateid', 'width', 'height', 'leftmargin', 'rightmargin',
'sequence', 'timecreated', 'timemodified'));
// The elements.
$element = new backup_nested_element('element', array('id'), array(
'pageid', 'name', 'element', 'data', 'font', 'size',
'colour', 'posx', 'posy', 'width', 'refpoint', 'sequence',
'timecreated', 'timemodified')); 'timecreated', 'timemodified'));
// The issues. // The issues.
@ -46,30 +62,22 @@ class backup_customcert_activity_structure_step extends backup_activity_structur
$issue = new backup_nested_element('issue', array('id'), array( $issue = new backup_nested_element('issue', array('id'), array(
'customcertid', 'userid', 'timecreated', 'code')); 'customcertid', 'userid', 'timecreated', 'code'));
// The pages.
$pages = new backup_nested_element('pages');
$page = new backup_nested_element('page', array('id'), array(
'customcertid', 'width', 'height', 'leftmargin', 'rightmargin',
'sequence', 'timecreated', 'timemodified'));
// The elements.
$element = new backup_nested_element('element', array('id'), array(
'pageid', 'name', 'element', 'data', 'font', 'size',
'colour', 'width', 'refpoint', 'posx', 'posy', 'sequence',
'timecreated', 'timemodified'));
// Build the tree. // Build the tree.
$customcert->add_child($issues); $customcert->add_child($issues);
$issues->add_child($issue); $issues->add_child($issue);
$customcert->add_child($pages); $customcert->add_child($template);
$template->add_child($pages);
$pages->add_child($page); $pages->add_child($page);
$page->add_child($element); $page->add_child($element);
// Define sources. // Define sources.
$customcert->set_source_table('customcert', array('id' => backup::VAR_ACTIVITYID)); $customcert->set_source_table('customcert', array('id' => backup::VAR_ACTIVITYID));
// Define template source.
$template->set_source_table('customcert_templates', array('contextid' => backup::VAR_CONTEXTID));
// Define page source. // Define page source.
$page->set_source_table('customcert_pages', array('customcertid' => backup::VAR_ACTIVITYID)); $page->set_source_table('customcert_pages', array('templateid' => backup::VAR_PARENTID));
// Define element source, each element belongs to a page. // Define element source, each element belongs to a page.
$element->set_source_table('customcert_elements', array('pageid' => backup::VAR_PARENTID)); $element->set_source_table('customcert_elements', array('pageid' => backup::VAR_PARENTID));

View file

@ -95,9 +95,7 @@ class restore_customcert_activity_task extends restore_activity_task {
* was available at the time. * was available at the time.
*/ */
public function after_restore() { public function after_restore() {
global $CFG, $DB; global $DB;
require_once($CFG->dirroot . '/mod/customcert/locallib.php');
// Get the customcert elements. // Get the customcert elements.
$sql = "SELECT e.* $sql = "SELECT e.*
@ -105,7 +103,7 @@ class restore_customcert_activity_task extends restore_activity_task {
INNER JOIN {customcert_pages} p INNER JOIN {customcert_pages} p
ON e.pageid = p.id ON e.pageid = p.id
INNER JOIN {customcert} c INNER JOIN {customcert} c
ON p.customcertid = c.id ON p.templateid = c.templateid
WHERE c.id = :customcertid"; WHERE c.id = :customcertid";
if ($elements = $DB->get_records_sql($sql, array('customcertid' => $this->get_activityid()))) { if ($elements = $DB->get_records_sql($sql, array('customcertid' => $this->get_activityid()))) {
// Go through the elements for the certificate. // Go through the elements for the certificate.

View file

@ -41,11 +41,14 @@ class restore_customcert_activity_structure_step extends restore_activity_struct
// The customcert instance. // The customcert instance.
$paths[] = new restore_path_element('customcert', '/activity/customcert'); $paths[] = new restore_path_element('customcert', '/activity/customcert');
// The templates.
$paths[] = new restore_path_element('customcert_template', '/activity/customcert/template');
// The pages. // The pages.
$paths[] = new restore_path_element('customcert_page', '/activity/customcert/pages/page'); $paths[] = new restore_path_element('customcert_page', '/activity/customcert/template/pages/page');
// The elements. // The elements.
$paths[] = new restore_path_element('customcert_element', '/activity/customcert/pages/page/element'); $paths[] = new restore_path_element('customcert_element', '/activity/customcert/template/pages/page/element');
// Check if we want the issues as well. // Check if we want the issues as well.
if ($this->get_setting_value('userinfo')) { if ($this->get_setting_value('userinfo')) {
@ -59,7 +62,7 @@ class restore_customcert_activity_structure_step extends restore_activity_struct
/** /**
* Handles restoring the customcert activity. * Handles restoring the customcert activity.
* *
* @param $data the customcert data * @param stdClass $data the customcert data
*/ */
protected function process_customcert($data) { protected function process_customcert($data) {
global $DB; global $DB;
@ -79,7 +82,32 @@ class restore_customcert_activity_structure_step extends restore_activity_struct
/** /**
* Handles restoring a customcert page. * Handles restoring a customcert page.
* *
* @param $data the customcert data * @param stdClass $data the customcert data
*/
protected function process_customcert_template($data) {
global $DB;
$data = (object) $data;
$oldid = $data->id;
$data->contextid = $this->task->get_contextid();
$data->timecreated = $this->apply_date_offset($data->timecreated);
$data->timemodified = $this->apply_date_offset($data->timemodified);
$newitemid = $DB->insert_record('customcert_templates', $data);
$this->set_mapping('customcert_template', $oldid, $newitemid);
// Update the template id for the customcert.
$customcert = new stdClass();
$customcert->id = $this->get_new_parentid('customcert');
$customcert->templateid = $newitemid;
$DB->update_record('customcert', $customcert);
}
/**
* Handles restoring a customcert template.
*
* @param stdClass $data the customcert data
*/ */
protected function process_customcert_page($data) { protected function process_customcert_page($data) {
global $DB; global $DB;
@ -87,7 +115,7 @@ class restore_customcert_activity_structure_step extends restore_activity_struct
$data = (object) $data; $data = (object) $data;
$oldid = $data->id; $oldid = $data->id;
$data->customcertid = $this->get_new_parentid('customcert'); $data->templateid = $this->get_new_parentid('customcert_template');
$data->timecreated = $this->apply_date_offset($data->timecreated); $data->timecreated = $this->apply_date_offset($data->timecreated);
$data->timemodified = $this->apply_date_offset($data->timemodified); $data->timemodified = $this->apply_date_offset($data->timemodified);
@ -98,7 +126,7 @@ class restore_customcert_activity_structure_step extends restore_activity_struct
/** /**
* Handles restoring a customcert element. * Handles restoring a customcert element.
* *
* @param $data the customcert data * @param stdclass $data the customcert data
*/ */
protected function process_customcert_element($data) { protected function process_customcert_element($data) {
global $DB; global $DB;
@ -117,7 +145,7 @@ class restore_customcert_activity_structure_step extends restore_activity_struct
/** /**
* Handles restoring a customcert issue. * Handles restoring a customcert issue.
* *
* @param $data the customcert data * @param stdClass $data the customcert data
*/ */
protected function process_customcert_issue($data) { protected function process_customcert_issue($data) {
global $DB; global $DB;