MDL-61410 question: support course tags on import
authorSimey Lameze <simey@moodle.com>
Fri, 2 Mar 2018 04:29:26 +0000 (12:29 +0800)
committerSimey Lameze <simey@moodle.com>
Mon, 19 Mar 2018 03:45:55 +0000 (11:45 +0800)
question/format.php
question/format/xml/format.php

index 78b5528..c042337 100644 (file)
@@ -425,8 +425,14 @@ class qformat_default {
 
             $result = question_bank::get_qtype($question->qtype)->save_question_options($question);
 
-            if (isset($question->tags)) {
-                core_tag_tag::set_item_tags('core_question', 'question', $question->id, $question->context, $question->tags);
+            if (!empty($question->tags)) {
+                core_tag_tag::set_item_tags('core_question', 'question', $question->id,
+                    $question->context, $question->tags);
+            }
+
+            if (!empty($question->coursetags)) {
+                core_tag_tag::set_item_tags('core_question', 'question', $question->id,
+                    context_course::instance($this->course->id), $question->coursetags);
             }
 
             if (!empty($result->error)) {
index 5dbc7fc..0058862 100644 (file)
@@ -383,12 +383,20 @@ class qformat_xml extends qformat_default {
     public function import_question_tags($qo, $questionxml) {
         global $CFG;
 
-        if (core_tag_tag::is_enabled('core_question', 'question')
-                && array_key_exists('tags', $questionxml['#'])
-                && !empty($questionxml['#']['tags'][0]['#']['tag'])) {
-            $qo->tags = array();
-            foreach ($questionxml['#']['tags'][0]['#']['tag'] as $tagdata) {
-                $qo->tags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true);
+        if (core_tag_tag::is_enabled('core_question', 'question')) {
+
+            $qo->tags = [];
+            if (!empty($questionxml['#']['tags'][0]['#']['tag'])) {
+                foreach ($questionxml['#']['tags'][0]['#']['tag'] as $tagdata) {
+                    $qo->tags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true);
+                }
+            }
+
+            $qo->coursetags = [];
+            if (!empty($questionxml['#']['coursetags'][0]['#']['tag'])) {
+                foreach ($questionxml['#']['coursetags'][0]['#']['tag'] as $tagdata) {
+                    $qo->coursetags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true);
+                }
             }
         }
     }
@@ -1472,7 +1480,8 @@ class qformat_xml extends qformat_default {
 
             if (!is_null($tagobjects)) {
                 $tagobjects = $tagobjects[$question->id];
-                $sortedtagobjects = question_sort_tags($tagobjects, $this->category);
+                $categorycontext = context::instance_by_id($this->category->contextid);
+                $sortedtagobjects = question_sort_tags($tagobjects, $categorycontext);
 
                 if (!empty($sortedtagobjects->coursetags)) {
                     // Set them on the form to be rendered as existing tags.