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

  <!-- ================================================================= -->
  <!-- simple-quiz-node-published.xsl (v002) -                           -->
  <!-- opprettet 03.04.06 - Even Halvorsen (even.halvorsen@usit.uio.no)  -->  
  <!--                                                                   -->
  <!-- To be used in published course ....                               -->
  <!-- ================================================================= -->

  <xsl:import href="../../../system/api-v001.xsl" />

  <xsl:import href="../../../common/components/structuredtext/v002/structuredtext.xsl" />
  <xsl:import href="../../components/node-list-ref/v001/node-list-ref.xsl" />

  <xsl:include href="../../../felles/ressurser/breadcrumb-v001.xsl" />  
  <xsl:include href="../../../felles/ressurser/output-method-html-v001.xsl" />


  <xsl:strip-space elements="*"/>

  <!-- xsl:variable name="current-link" is set in published.xsl -->

  <xsl:template match="simple-quiz-node">
    <xsl:apply-templates select="title | author | description | number-of-questions | quiz-mode | ordering" />

    <xsl:if test="processing-instruction('error')">
      <p><font color="red">
          Du svarte ikke på disse spørsmålene:
          <!--xsl:value-of select="processing-instruction('error')" /-->
      </font></p>
    </xsl:if>

    <xsl:choose>
      <xsl:when test="processing-instruction('mode')='ask'">
        <form method="post" action="{$current-link}"> <!-- ex. format: http://www.uio.no/course?page=1.2 -->

          <xsl:apply-templates select="question-choice[processing-instruction('show')='true'] 
                                       | question-open[processing-instruction('show')='true']" mode="ask-question" />

          <hr />
          <input value="Svar" name="answered" type="submit" />
        </form>
      </xsl:when>
      <xsl:when test="processing-instruction('mode')='feedback'">
        <xsl:apply-templates select="question-choice[processing-instruction('show')='true'] 
                                     | question-open[processing-instruction('show')='true']" mode="give-feedback" />
        <hr />
      </xsl:when>
      <xsl:when test="processing-instruction('mode')='feedback-and-next'">
        <form method="post" action="{$current-link}">
          <xsl:apply-templates select="question-choice[processing-instruction('show')='true'] 
                                     | question-open[processing-instruction('show')='true']" mode="give-feedback" />
          <hr />
          <input value="Neste spørsmål" name="next" type="submit" />
        </form>
      </xsl:when>
      <xsl:otherwise>
        <!-- if no mode ? -->
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="title">
    <h1><xsl:value-of select="." /></h1>
  </xsl:template>

  <xsl:template match="author">
    <p>av&#160;<xsl:value-of select="." /></p>
  </xsl:template>

  <xsl:template match="description">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="number-of-questions | quiz-mode | ordering" />

  <!-- ================================================================= -->
  <!-- Questiontypes                                                     -->
  <!-- ================================================================= -->

  <xsl:template match="question-choice" mode="ask-question">
    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </xsl:variable>

    <hr />
    <h3><xsl:value-of select="title" /></h3>
    <xsl:apply-templates select="content" /> 
    <xsl:apply-templates select="instruction" />

    <xsl:apply-templates select="alternatives/answer" mode="ask-question">
        <xsl:with-param name="parentpath" select="$path" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="question-choice" mode="give-feedback">
    <hr />
    <h3><xsl:value-of select="title" /></h3>
    <xsl:apply-templates select="content" /> 
    <!-- xsl:apply-templates select="instruction" / -->

    <!-- apply feedback from the chosen aswer -->
    <xsl:apply-templates select="alternatives/answer[processing-instruction('chosen')='true']" mode="give-feedback"/> 

    <xsl:apply-templates select="followup" /> 

    <!-- show the right answer as well ? --> 
  </xsl:template>


  <xsl:template match="question-open" mode="ask-question">
    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </xsl:variable>
    <hr />
    <h3><xsl:value-of select="title" /></h3>
    <xsl:apply-templates select="content" /> 
    <xsl:apply-templates select="instruction" />
    <textarea id="{$path}" name="{$path}" rows="5" cols="50" />
  </xsl:template>

  <xsl:template match="question-open" mode="give-feedback">
    <hr />
    <h3><xsl:value-of select="title" /></h3>
    <xsl:apply-templates select="content" /> 
    <!-- xsl:apply-templates select="instruction" / -->

    <!-- the given answer is ... -->
    
    <p>Du svarte: <xsl:value-of select="processing-instruction('submitted-answer')" /></p>

    <xsl:apply-templates select="followup" />
    <xsl:apply-templates select="feedback" />
  </xsl:template>

 
  <!-- ================================================================= -->
  <!-- Subelements of question                                           -->
  <!-- ================================================================= -->

  <xsl:template match="type" /> <!-- value is used in answer -->

  <xsl:template match="content">
    <xsl:apply-templates select="description" />
    <!-- Don't support this yet -->
    <!-- xsl:apply-templates select="node-list-ref" / -->
  </xsl:template>

  <!-- xsl:template match="description" -->

  <xsl:template match="node-list-ref" />

  <xsl:template match="instruction">
    <p><xsl:value-of select="." /></p>
  </xsl:template>

  <xsl:template match="feedback | followup">
    <xsl:apply-templates /> <!-- structuredtext -->
  </xsl:template>

  <!-- ================================================================= -->
  <!-- Answer - subelements of alternatives                              -->
  <!-- ================================================================= -->

  <xsl:template match="answer" />

  <xsl:template match="answer" mode="ask-question">
    <xsl:param name="parentpath" />

    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="../../type='multi-select'"> 
        <input name="{$path}" id="{$path}" type="checkbox" />
      </xsl:when>
      <xsl:otherwise> 
        <!-- default = "single-select" -->
        <input id="{$path}" type="radio" name="{$parentpath}" value="{$path}" />
      </xsl:otherwise>
    </xsl:choose>
    <label for="{$path}">&#160;<xsl:value-of select="response" /></label><br />
  </xsl:template>

  <xsl:template match="answer" mode="give-feedback">
    <p>Du svarte:</p>
    <xsl:choose>
      <xsl:when test="../../type='multi-select'"> 
        <input name="tmp" type="checkbox" checked="true"/>
      </xsl:when>
      <xsl:otherwise> 
        <!-- default = "single-select" -->
        <input type="radio" checked="true" />
      </xsl:otherwise>
    </xsl:choose>&#160;<xsl:value-of select="response" />

    <xsl:apply-templates select="feedback" />
  </xsl:template>

  <xsl:template match="response" />

  <xsl:template match="validity" />

  <!-- ================================================================= -->
  <!-- Some help methods                                                 -->
  <!-- ================================================================= -->

  <xsl:template name="xpath">
    <xsl:for-each select="ancestor::*">
      <xsl:sort select="position()" order="ascending" data-type="number"/>
      <!-- Adding a "." if the element has a parent -->
      <xsl:if test="ancestor::*"><xsl:text>.</xsl:text></xsl:if>
      <xsl:value-of select="count(preceding-sibling::*) + 1"/>
    </xsl:for-each>
    <xsl:choose>
      <!-- Checking whether current() is an attribute or not -->
      <xsl:when test="count(. | ../@*) = count(../@*)">:<xsl:value-of select="name()"/></xsl:when>
      <xsl:otherwise>
        <xsl:text>.</xsl:text><xsl:value-of select="count(preceding-sibling::*) + 1"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
 
</xsl:stylesheet>
