Permalink
Browse files

MDL-39169: Now the question's tags are added in backup/restore options

  • Loading branch information...
1 parent fc282e4 commit 55157bfc2eb566774ba7366b888dad6044a1339a Maria Torres committed with danpoltawski Apr 26, 2013
Showing with 40 additions and 1 deletion.
  1. +13 −0 backup/moodle2/backup_stepslib.php
  2. +27 −1 backup/moodle2/restore_stepslib.php
@@ -1764,6 +1764,10 @@ protected function define_structure() {
$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);
@@ -1772,6 +1776,9 @@ protected function define_structure() {
$question->add_child($qhints);
$qhints->add_child($qhint);
+ $question->add_child($tags);
+ $tags->add_child($tag);
+
// Define the sources
$qcategory->set_source_sql("
@@ -1791,6 +1798,12 @@ protected function define_structure() {
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}
@@ -2875,10 +2875,13 @@ protected function define_structure() {
$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);
- return array($category, $question, $hint);
+ return array($category, $question, $hint, $tag);
}
protected function process_question_category($data) {
@@ -3025,6 +3028,29 @@ protected function process_question_hint($data) {
$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;

0 comments on commit 55157bf

Please sign in to comment.