Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 242ef4590f7a185066e5680e4f0d0fb54e844efe 1 parent 31dda93
authored April 25, 2013 danpoltawski committed May 06, 2013
13  backup/moodle2/backup_stepslib.php
@@ -1820,6 +1820,10 @@ protected function define_structure() {
1820 1820
         $qhint = new backup_nested_element('question_hint', array('id'), array(
1821 1821
             'hint', 'hintformat', 'shownumcorrect', 'clearwrong', 'options'));
1822 1822
 
  1823
+        $tags = new backup_nested_element('tags');
  1824
+
  1825
+        $tag = new backup_nested_element('tag', array('id'), array('name', 'rawname'));
  1826
+
1823 1827
         // Build the tree
1824 1828
 
1825 1829
         $qcategories->add_child($qcategory);
@@ -1828,6 +1832,9 @@ protected function define_structure() {
1828 1832
         $question->add_child($qhints);
1829 1833
         $qhints->add_child($qhint);
1830 1834
 
  1835
+        $question->add_child($tags);
  1836
+        $tags->add_child($tag);
  1837
+
1831 1838
         // Define the sources
1832 1839
 
1833 1840
         $qcategory->set_source_sql("
@@ -1847,6 +1854,12 @@ protected function define_structure() {
1847 1854
                 ORDER BY id',
1848 1855
                 array('questionid' => backup::VAR_PARENTID));
1849 1856
 
  1857
+        $tag->set_source_sql("SELECT t.id, t.name, t.rawname
  1858
+                              FROM {tag} t
  1859
+                              JOIN {tag_instance} ti ON ti.tagid = t.id
  1860
+                              WHERE ti.itemid = ?
  1861
+                              AND ti.itemtype = 'question'", array(backup::VAR_PARENTID));
  1862
+
1850 1863
         // don't need to annotate ids nor files
1851 1864
         // (already done by {@link backup_annotate_all_question_files}
1852 1865
 
27  backup/moodle2/restore_stepslib.php
@@ -3040,13 +3040,15 @@ protected function define_structure() {
3040 3040
         $hint = new restore_path_element('question_hint',
3041 3041
                 '/question_categories/question_category/questions/question/question_hints/question_hint');
3042 3042
 
  3043
+        $tag = new restore_path_element('tag','/question_categories/question_category/questions/question/tags/tag');
  3044
+
3043 3045
         // Apply for 'qtype' plugins optional paths at question level
3044 3046
         $this->add_plugin_structure('qtype', $question);
3045 3047
 
3046 3048
         // Apply for 'local' plugins optional paths at question level
3047 3049
         $this->add_plugin_structure('local', $question);
3048 3050
 
3049  
-        return array($category, $question, $hint);
  3051
+        return array($category, $question, $hint, $tag);
3050 3052
     }
3051 3053
 
3052 3054
     protected function process_question_category($data) {
@@ -3193,6 +3195,29 @@ protected function process_question_hint($data) {
3193 3195
         $this->set_mapping('question_hint', $oldid, $newitemid);
3194 3196
     }
3195 3197
 
  3198
+    protected function process_tag($data) {
  3199
+        global $CFG, $DB;
  3200
+
  3201
+        $data = (object)$data;
  3202
+        $newquestion = $this->get_new_parentid('question');
  3203
+
  3204
+        if (!empty($CFG->usetags)) { // if enabled in server
  3205
+            // TODO: This is highly inneficient. Each time we add one tag
  3206
+            // we fetch all the existing because tag_set() deletes them
  3207
+            // so everything must be reinserted on each call
  3208
+            $tags = array();
  3209
+            $existingtags = tag_get_tags('question', $newquestion);
  3210
+            // Re-add all the existitng tags
  3211
+            foreach ($existingtags as $existingtag) {
  3212
+                $tags[] = $existingtag->rawname;
  3213
+            }
  3214
+            // Add the one being restored
  3215
+            $tags[] = $data->rawname;
  3216
+            // Send all the tags back to the question
  3217
+            tag_set('question', $newquestion, $tags);
  3218
+        }
  3219
+    }
  3220
+
3196 3221
     protected function after_execute() {
3197 3222
         global $DB;
3198 3223
 

0 notes on commit 242ef45

Please sign in to comment.
Something went wrong with that request. Please try again.