diff --git a/question/format/xml/format.php b/question/format/xml/format.php
index efd209c6579..63ae9f56777 100644
--- a/question/format/xml/format.php
+++ b/question/format/xml/format.php
@@ -1467,10 +1467,31 @@ class qformat_xml extends qformat_default {
$expout .= $this->write_hints($question);
// Write the question tags.
- $tags = core_tag_tag::get_item_tags_array('core_question', 'question', $question->id);
- if (!empty($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;
+ }
+ );
+
+ // Set them on the form to be rendered as existing tags.
+ $expout .= " \n";
+ foreach ($coursetagobjects as $tagobject) {
+ $expout .= " " . $this->writetext($tagobject->get_display_name(), 0, true) . "\n";
+ }
+ $expout .= " \n";
+ }
+ if (!empty($question->tags)) {
$expout .= " \n";
- foreach ($tags as $tag) {
+ foreach ($question->tags as $tag) {
$expout .= " " . $this->writetext($tag, 0, true) . "\n";
}
$expout .= " \n";