<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 

  <xsl:template match="/quiz">
    <html>
      <head>
        <title>Moodle Quiz Review</title>
        <style>
          body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f4; color: #333; }
          h1 { color: #0056b3; text-align: center; margin-bottom: 30px; }
          .question-container {
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
          }
          .question-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
          }
          .question-name {
            font-weight: bold;
            font-size: 1.1em;
            color: #555;
          }
          .question-type {
            font-style: italic;
            color: #777;
            font-size: 0.9em;
          }
          .question-text {
            margin-bottom: 15px;
            line-height: 1.6;
            font-size: 1em;
          }
          .options-list, .matching-table, .answer-feedback {
            margin-top: 10px;
            padding-left: 20px;
          }
          .options-list li {
            margin-bottom: 8px;
            line-height: 1.4;
          }
          .correct-answer {
            color: #28a745;
            font-weight: bold;
          }
          .feedback {
            font-size: 0.9em;
            color: #666;
            margin-top: 5px;
            padding-left: 5px;
            border-left: 3px solid #ccc;
          }
          .matching-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 15px;
          }
          .matching-table th, .matching-table td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: left;
          }
          .matching-table th {
            background-color: #f2f2f2;
            font-weight: bold;
          }
          .general-feedback {
            margin-top: 20px;
            padding: 15px;
            background-color: #e9f7ef;
            border-left: 5px solid #28a745;
            color: #218838;
            border-radius: 4px;
          }
          .shortanswer-answer {
            font-style: italic;
            color: #007bff;
          }
        </style>
      </head>
      <body>
        <h1>Moodle Quiz Review</h1>
        <xsl:apply-templates select="question"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="question">
    <div class="question-container">
      <div class="question-header">
        <span class="question-name">
          <!-- Display question name as plain text. If it contains HTML, Moodle export might be the source of the issue. -->
          <xsl:value-of select="name/text"/>
        </span>
        <span class="question-type">
          Type: <xsl:value-of select="@type"/>
        </span>
      </div>
      <div class="question-text">
        <!-- Use xsl:value-of on the parent 'questiontext' node with disable-output-escaping.
             This often helps in correctly rendering HTML content that might be escaped within the text child. -->
        <xsl:value-of select="questiontext" disable-output-escaping="yes"/>
      </div>

      <xsl:choose>
        <!-- Multichoice Question -->
        <xsl:when test="@type = 'multichoice'">
          <ul class="options-list">
            <xsl:for-each select="answer">
              <li>
                <xsl:choose>
                  <xsl:when test="normalize-space(fraction) = '100'">
                    <span class="correct-answer">
                      <!-- Apply disable-output-escaping to the 'answer' node's text content -->
                      <xsl:value-of select="text" disable-output-escaping="yes"/>
                    </span>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="text" disable-output-escaping="yes"/>
                  </xsl:otherwise>
                </xsl:choose>
                <xsl:if test="feedback/text != ''">
                  <div class="feedback">
                    Feedback: <xsl:value-of select="feedback" disable-output-escaping="yes"/>
                  </div>
                </xsl:if>
              </li>
            </xsl:for-each>
          </ul>
        </xsl:when>

        <!-- True/False Question -->
        <xsl:when test="@type = 'truefalse'">
          <p class="options-list">
            <xsl:for-each select="answer">
              <xsl:if test="normalize-space(fraction) = '100'">
                Correct Answer: <span class="correct-answer"><xsl:value-of select="text" disable-output-escaping="yes"/></span>
              </xsl:if>
            </xsl:for-each>
          </p>
          <xsl:if test="feedback/text != ''">
            <div class="feedback">
              Feedback: <xsl:value-of select="feedback" disable-output-escaping="yes"/>
            </div>
          </xsl:if>
        </xsl:when>

        <!-- Short Answer Question -->
        <xsl:when test="@type = 'shortanswer'">
          <p>Expected Answers:</p>
          <ul class="options-list">
            <xsl:for-each select="answer">
              <li>
                <span class="shortanswer-answer">
                  <xsl:value-of select="text" disable-output-escaping="yes"/>
                </span>
                <xsl:if test="feedback/text != ''">
                  <div class="feedback">
                    Feedback: <xsl:value-of select="feedback" disable-output-escaping="yes"/>
                  </div>
                </xsl:if>
              </li>
            </xsl:for-each>
          </ul>
        </xsl:when>

        <!-- Essay Question -->
        <xsl:when test="@type = 'essay'">
          <p>This is an essay question. No predefined answers are displayed here.</p>
        </xsl:when>

        <!-- Matching Question -->
        <xsl:when test="@type = 'matching'">
          <table class="matching-table">
            <thead>
              <tr>
                <th>Subquestion</th>
                <th>Correct Match</th>
              </tr>
            </thead>
            <tbody>
              <xsl:for-each select="subquestion">
                <tr>
                  <td><xsl:value-of select="text" disable-output-escaping="yes"/></td>
                  <td><xsl:value-of select="answer/text" disable-output-escaping="yes"/></td>
                </tr>
              </xsl:for-each>
            </tbody>
          </table>
        </xsl:when>

        <!-- Description Question (just displays text) -->
        <xsl:when test="@type = 'description'">
          <p>This is a description item. No answers are associated with it.</p>
        </xsl:when>

        <!-- Fallback for unsupported types -->
        <xsl:otherwise>
          <p><em>Unsupported question type: <xsl:value-of select="@type"/>. Displaying raw question text.</em></p>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:if test="generalfeedback/text != ''">
        <div class="general-feedback">
          <strong>General Feedback:</strong> <xsl:value-of select="generalfeedback" disable-output-escaping="yes"/>
        </div>
      </xsl:if>
    </div>
  </xsl:template>

</xsl:stylesheet>
