MDL-61410 question: merge course and question tags
authorSimey Lameze <simey@moodle.com>
Thu, 8 Mar 2018 04:18:17 +0000 (12:18 +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 c042337..6e00169 100644 (file)
@@ -51,7 +51,6 @@ class qformat_default {
     public $stoponerror = true;
     public $translator = null;
     public $canaccessbackupdata = true;
-
     protected $importcontext = null;
 
     // functions to indicate import/export functionality
@@ -425,14 +424,31 @@ class qformat_default {
 
             $result = question_bank::get_qtype($question->qtype)->save_question_options($question);
 
-            if (!empty($question->tags)) {
-                core_tag_tag::set_item_tags('core_question', 'question', $question->id,
-                    $question->context, $question->tags);
-            }
+            if (core_tag_tag::is_enabled('core_question', 'question')) {
+                // Is the current context we're importing in a course context?
+                $importingcontext = $this->importcontext;
+                $importingcoursecontext = $importingcontext->get_course_context(false);
+                $isimportingcontextcourseoractivity = !empty($importingcoursecontext);
+
+                if (!empty($question->coursetags)) {
+                    if ($isimportingcontextcourseoractivity) {
+                        $mergedtags = array_merge($question->coursetags, $question->tags);
+
+                        core_tag_tag::set_item_tags('core_question', 'question', $question->id,
+                            $question->context, $mergedtags);
+                    } else {
+                        core_tag_tag::set_item_tags('core_question', 'question', $question->id,
+                            context_course::instance($this->course->id), $question->coursetags);
 
-            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($question->tags)) {
+                            core_tag_tag::set_item_tags('core_question', 'question', $question->id,
+                                $importingcontext, $question->tags);
+                        }
+                    }
+                } else {
+                    core_tag_tag::set_item_tags('core_question', 'question', $question->id,
+                        $question->context, $question->tags);
+                }
             }
 
             if (!empty($result->error)) {
index dabec48..d3b9c82 100644 (file)
@@ -1478,7 +1478,7 @@ class qformat_xml extends qformat_default {
         if (core_tag_tag::is_enabled('core_question', 'question')) {
             $tagobjects = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
 
-            if (!is_null($tagobjects)) {
+            if (!empty($tagobjects)) {
                 $context = context::instance_by_id($contextid);
                 $sortedtagobjects = question_sort_tags($tagobjects, $context, [$this->course]);