From 21e51c868e6b1db75bc65dd1bf1f26e775b6da5d Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Wed, 21 Jul 2010 22:18:42 +0000 Subject: [PATCH] MDL-21432 backup - filters and comments --- backup/moodle2/restore_course_task.class.php | 15 ++++ backup/moodle2/restore_stepslib.php | 76 ++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/backup/moodle2/restore_course_task.class.php b/backup/moodle2/restore_course_task.class.php index ec2e74adc3f..e65c86939d7 100644 --- a/backup/moodle2/restore_course_task.class.php +++ b/backup/moodle2/restore_course_task.class.php @@ -72,6 +72,21 @@ class restore_course_task extends restore_task { // Restore course enrolments (plugins and membership) $this->add_step(new restore_enrolments_structure_step('course_enrolments', 'enrolments.xml')); + // Restore course filters (conditionally) + if ($this->get_setting_value('filters')) { + $this->add_step(new restore_filters_structure_step('course_filters', 'filters.xml')); + } + + // Restore course comments (conditionally) + if ($this->get_setting_value('comments')) { + $this->add_step(new restore_comments_structure_step('course_comments', 'comments.xml')); + } + + // Restore course logs (conditionally) + if ($this->get_setting_value('logs')) { + //$this->add_step(new restore_course_logs_structure_step('course_logs', 'logs.xml')); + } + // At the end, mark it as built $this->built = true; } diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 9b8c3068309..137861030fa 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -694,3 +694,79 @@ class restore_enrolments_structure_step extends restore_structure_step { } } } + + +/** + * This structure steps restores the filters and their configs + */ +class restore_filters_structure_step extends restore_structure_step { + + protected function define_structure() { + + $paths = array(); + + $paths[] = new restore_path_element('active', '/filters/filter_actives/filter_active'); + $paths[] = new restore_path_element('config', '/filters/filter_configs/filter_config'); + + return $paths; + } + + public function process_active($data) { + + $data = (object)$data; + + if (!filter_is_enabled($data->filter)) { // Not installed or not enabled, nothing to do + return; + } + filter_set_local_state($data->filter, $this->task->get_contextid(), $data->active); + } + + public function process_config($data) { + + $data = (object)$data; + + if (!filter_is_enabled($data->filter)) { // Not installed or not enabled, nothing to do + return; + } + filter_set_local_config($data->filter, $this->task->get_contextid(), $data->name, $data->value); + } +} + + +/** + * This structure steps restores the comments + * Note: Cannot use the comments API because defaults to USER->id. + * That should change allowing to pass $userid + */ +class restore_comments_structure_step extends restore_structure_step { + + protected function define_structure() { + + $paths = array(); + + $paths[] = new restore_path_element('comment', '/comments/comment'); + + return $paths; + } + + public function process_comment($data) { + global $DB; + + $data = (object)$data; + + // First of all, if the comment has some itemid, ask to the task what to map + $mapping = false; + $newitemid = 0; + if ($data->itemid) { + $mapping = $this->task->get_comment_mapping_itemname(); + $newitemid = $this->get_mappingid($mapping, $data->itemid); + } + // Only restore the comment if has no mapping OR we have found the matching mapping + if (!$mapping || $newitemid) { + if ($data->userid = $this->get_mappingid('user', $data->userid)) { + $data->contextid = $this->task->get_contextid(); + $DB->insert_record('comments', $data); + } + } + } +}