diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index f2ba2af09a0..dcb7c2bd715 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -1820,6 +1820,10 @@ class backup_questions_structure_step extends backup_structure_step { $qhint = new backup_nested_element('question_hint', array('id'), array( 'hint', 'hintformat', 'shownumcorrect', 'clearwrong', 'options')); + $tags = new backup_nested_element('tags'); + + $tag = new backup_nested_element('tag', array('id'), array('name', 'rawname')); + // Build the tree $qcategories->add_child($qcategory); @@ -1828,6 +1832,9 @@ class backup_questions_structure_step extends backup_structure_step { $question->add_child($qhints); $qhints->add_child($qhint); + $question->add_child($tags); + $tags->add_child($tag); + // Define the sources $qcategory->set_source_sql(" @@ -1847,6 +1854,12 @@ class backup_questions_structure_step extends backup_structure_step { ORDER BY id', array('questionid' => backup::VAR_PARENTID)); + $tag->set_source_sql("SELECT t.id, t.name, t.rawname + FROM {tag} t + JOIN {tag_instance} ti ON ti.tagid = t.id + WHERE ti.itemid = ? + AND ti.itemtype = 'question'", array(backup::VAR_PARENTID)); + // don't need to annotate ids nor files // (already done by {@link backup_annotate_all_question_files} diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index a5eceec8476..6886bb5d89e 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -3040,13 +3040,15 @@ class restore_create_categories_and_questions extends restore_structure_step { $hint = new restore_path_element('question_hint', '/question_categories/question_category/questions/question/question_hints/question_hint'); + $tag = new restore_path_element('tag','/question_categories/question_category/questions/question/tags/tag'); + // Apply for 'qtype' plugins optional paths at question level $this->add_plugin_structure('qtype', $question); // Apply for 'local' plugins optional paths at question level $this->add_plugin_structure('local', $question); - return array($category, $question, $hint); + return array($category, $question, $hint, $tag); } protected function process_question_category($data) { @@ -3193,6 +3195,29 @@ class restore_create_categories_and_questions extends restore_structure_step { $this->set_mapping('question_hint', $oldid, $newitemid); } + protected function process_tag($data) { + global $CFG, $DB; + + $data = (object)$data; + $newquestion = $this->get_new_parentid('question'); + + if (!empty($CFG->usetags)) { // if enabled in server + // TODO: This is highly inneficient. Each time we add one tag + // we fetch all the existing because tag_set() deletes them + // so everything must be reinserted on each call + $tags = array(); + $existingtags = tag_get_tags('question', $newquestion); + // Re-add all the existitng tags + foreach ($existingtags as $existingtag) { + $tags[] = $existingtag->rawname; + } + // Add the one being restored + $tags[] = $data->rawname; + // Send all the tags back to the question + tag_set('question', $newquestion, $tags); + } + } + protected function after_execute() { global $DB;