commit 584eb9d32fa71d9578555674f4ae7cd3d5e33783 Author: Maria Torres Date: Tue Apr 23 09:56:24 2013 +1200 Addind question_tags to backup and restore. Tracker Number: MDL-39169 - Restoring activity using question bank does not restore question's tags diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index f2ba2af..f12944b 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -1820,6 +1820,20 @@ class backup_questions_structure_step extends backup_structure_step { $qhint = new backup_nested_element('question_hint', array('id'), array( 'hint', 'hintformat', 'shownumcorrect', 'clearwrong', 'options')); + // add question's tags - Tracker Number: MDL-39169 + $question_tags = new backup_nested_element('question_tags'); + + // Begin Tracker Number: MDL-39169 + $tags = new backup_nested_element('tags'); + + $tag = new backup_nested_element('tag', array('id'), array( + 'userid', 'name', 'rawname', 'tagtype', 'description', 'descriptionformat', + 'flag', 'timemodified')); + + $tag_instance = new backup_nested_element('tag_instance', array('id'), array( + 'tagid', 'itemtype', 'itemid', 'tiuserid', 'ordering', 'timemodified')); + // End Tracker Number: MDL-39169 + // Build the tree $qcategories->add_child($qcategory); @@ -1828,6 +1842,13 @@ class backup_questions_structure_step extends backup_structure_step { $question->add_child($qhints); $qhints->add_child($qhint); + // Begin Tracker Number: MDL-39169 + $question->add_child($question_tags); + $question_tags->add_child($tags); + $tags->add_child($tag); + $tag->add_child($tag_instance); + // End Tracker Number: MDL-39169 + // Define the sources $qcategory->set_source_sql(" @@ -1847,6 +1868,19 @@ class backup_questions_structure_step extends backup_structure_step { ORDER BY id', array('questionid' => backup::VAR_PARENTID)); + // Begin Tracker Number: MDL-39169 + $tag->set_source_sql("SELECT t.* + FROM {tag} t + JOIN {tag_instance} ti ON ti.tagid = t.id + WHERE ti.itemid = ? + AND ti.itemtype = 'question'", array(backup::VAR_PARENTID)); + + $tag_instance->set_source_sql("SELECT ti.* + FROM {tag_instance} ti + WHERE ti.tagid = ? + AND ti.itemtype = 'question'", array(backup::VAR_PARENTID)); + // End Tracker Number: MDL-39169 + // don't need to annotate ids nor files // (already done by {@link backup_annotate_all_question_files} @@ -1854,8 +1888,6 @@ class backup_questions_structure_step extends backup_structure_step { } } - - /** * This step will generate all the file annotations for the already * annotated (final) users. Need to do this here because each user diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 55042f7..2acabc7 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -3035,13 +3035,21 @@ 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'); + // Begin Tracker Number: MDL-39169 + $tag = new restore_path_element('tag', + '/question_categories/question_category/questions/question/question_tags/tags/tag'); + + $tag_instance = new restore_path_element('tag_instance', + '/question_categories/question_category/questions/question/question_tags/tags/tag/tag_instance'); + // End Tracker Number: MDL-39169 + // 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, $tag_instance); } protected function process_question_category($data) { @@ -3188,6 +3196,69 @@ class restore_create_categories_and_questions extends restore_structure_step { $this->set_mapping('question_hint', $oldid, $newitemid); } +// Begin Tracker Number: MDL-39169. Tags in questions + protected function process_tag($data) { + global $DB; + + $data = (object)$data; + $oldid = $data->id; + + // Query to validate if the tag exist. + $sql = 'SELECT id + FROM {tag} + WHERE name = ?'; + $params = array($data->name); + $tagcreated = $DB->get_field_sql($sql, $params); + + // Check that this tag is not in database + if ($tagcreated) { + $this->set_mapping('tag', $oldid, $tagcreated); + } else { + $userid = $this->get_mappingid('userid', $data->userid); + $data->userid = $userid ? $userid : $this->task->get_userid(); + $data->tagid = ''; + $newtag = $DB->insert_record('tag', $data); + $this->set_mapping('tag', $oldid, $newtag); + $this->set_mapping('tag_created', $oldid, $newtag); + } + } + +// Begin Tracker Number: MDL-39169. Tags instance in questions + protected function process_tag_instance($data) { + global $DB; + + $data = (object)$data; + $oldid = $data->id; + + // Previos Ids mapped + $oldtag = $this->get_old_parentid('tag'); + $newtag = $this->get_new_parentid('tag'); + $newquestion = $this->get_new_parentid('question'); + $tagcreated = $this->get_mappingid('tag_created', $oldtag) ? true : false; + + if ($tagcreated) { // Tag created by restore + $data->tagid = $newtag; + $data->itemid = $newquestion; + $newitemid = $DB->insert_record('tag_instance', $data); + $this->set_mapping('tag_instance', $oldid, $newitemid); + } else { // The tag is already in database + // Query to verify if the tag is associated with the question + $sql = 'SELECT id + FROM {tag_instance} + WHERE tagid = ? + AND itemid = ?'; + $params = array($newtag, $newquestion); + $taginstanceid = $DB->get_field_sql($sql, $params); + + if(!$taginstanceid){ + $data->tagid = $newtag; + $data->itemid = $newquestion; + $newitemid = $DB->insert_record('tag_instance', $data); + $this->set_mapping('tag_instance', $oldid, $newitemid); + } + } + } + protected function after_execute() { global $DB;