MDL-61410 question: change logic on xml format
authorSimey Lameze <simey@moodle.com>
Fri, 2 Mar 2018 01:01:40 +0000 (09:01 +0800)
committerSimey Lameze <simey@moodle.com>
Mon, 19 Mar 2018 03:45:55 +0000 (11:45 +0800)
question/format/xml/format.php

index 63ae9f5..5dbc7fc 100644 (file)
@@ -1467,34 +1467,30 @@ class qformat_xml extends qformat_default {
         $expout .= $this->write_hints($question);
 
         // Write the question tags.
-        // If the question has course tags then we need to filter them to only
-        // include the tags for this course.
-        get_question_options($question, true);
-        if (isset($question->coursetagobjects)) {
-            // Get the tag objects for the course being viewed.
-            $courseid = $this->course->id;
-            $coursecontext = context_course::instance($courseid);
-
-            $coursetagobjects = array_filter(
-                $question->coursetagobjects,
-                function($tagobject) use ($coursecontext) {
-                    return $coursecontext->id == $tagobject->taginstancecontextid;
+        if (core_tag_tag::is_enabled('core_question', 'question')) {
+            $tagobjects = core_tag_tag::get_items_tags('core_question', 'question', $question->id);
+
+            if (!is_null($tagobjects)) {
+                $tagobjects = $tagobjects[$question->id];
+                $sortedtagobjects = question_sort_tags($tagobjects, $this->category);
+
+                if (!empty($sortedtagobjects->coursetags)) {
+                    // Set them on the form to be rendered as existing tags.
+                    $expout .= "    <coursetags>\n";
+                    foreach ($sortedtagobjects->coursetags as $coursetag) {
+                        $expout .= "      <tag>" . $this->writetext($coursetag, 0, true) . "</tag>\n";
+                    }
+                    $expout .= "    </coursetags>\n";
                 }
-            );
 
-            // Set them on the form to be rendered as existing tags.
-            $expout .= "    <coursetags>\n";
-            foreach ($coursetagobjects as $tagobject) {
-                $expout .= "      <tag>" . $this->writetext($tagobject->get_display_name(), 0, true) . "</tag>\n";
-            }
-            $expout .= "    </coursetags>\n";
-        }
-        if (!empty($question->tags)) {
-            $expout .= "    <tags>\n";
-            foreach ($question->tags as $tag) {
-                $expout .= "      <tag>" . $this->writetext($tag, 0, true) . "</tag>\n";
+                if (!empty($sortedtagobjects->tags)) {
+                    $expout .= "    <tags>\n";
+                    foreach ($sortedtagobjects->tags as $tag) {
+                        $expout .= "      <tag>" . $this->writetext($tag, 0, true) . "</tag>\n";
+                    }
+                    $expout .= "    </tags>\n";
+                }
             }
-            $expout .= "    </tags>\n";
         }
 
         // Close the question tag.