<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html"/>

<xsl:template match="/course">
    <html>
	<head>
	<title>
	    <xsl:value-of select="@designator"/> 
	    Standard Course Description
	</title>
	</head>
	<body> 
	    <h2>
		<xsl:value-of select="@designator"/> 
		Standard Course Description
		<br/><br/>
		<xsl:value-of select="title"/>
	    </h2>
	    <hr/>

	    <xsl:if test="alias">
		<h4>Alias</h4>
		<xsl:for-each select="alias">
		    <xsl:value-of select="."/><br/>
		</xsl:for-each>
	    </xsl:if>

	    <h4>Owner</h4>
	    <table border="0">
		<xsl:for-each select="owner">
		    <xsl:call-template name="person-row"/>
		</xsl:for-each>
	    </table>

	    <xsl:if test="instructor">
		<h4>Recent Instructors</h4>
		<table border="1">
		    <tr><th>Year</th><th>Term</th><th>Section</th><th>Instructor</th></tr>
		    <xsl:apply-templates select="instructor">
			<xsl:sort select="@year" order="descending"/>
			<xsl:sort select="@term" order="ascending"/>
			<xsl:sort select="@section" order="ascending"/>
		    </xsl:apply-templates>
		</table>
	    </xsl:if>


	    <xsl:if test="subscriber">
		<h4>Subscribers</h4>
		<table border="0">
		    <xsl:for-each select="subscriber">
			<xsl:call-template name="person-row"/>
		    </xsl:for-each>
		</table>
		<a>
		    <xsl:attribute name="href">
			mailto:<xsl:for-each select="//e-mail"><xsl:value-of select="."/>;</xsl:for-each>
		    </xsl:attribute>
		    e-mail to all of the above
		</a>
	    </xsl:if>

	    <xsl:if test="change">
	    <h5>Pending Change Proposals</h5>
		<ul>
		    <xsl:apply-templates select="change"/>
		</ul>
	    </xsl:if>

	    <h4>Course Description</h4>
	    <table border="0" width="600">
		<tr><td>
		    <p><xsl:copy-of select="calendar-description"/></p>
		    <p><xsl:copy-of select="additional-description"/></p>
		</td></tr>
	    </table>


	    <xsl:if test="background">
		<h4>Background</h4>
		<table border="0" width="600">
		    <tr><td>
			<p><xsl:copy-of select="background"/></p>
		    </td></tr>
		</table>
	    </xsl:if>

	    <xsl:if test="prereq">
		<h4>Prerequisistes</h4>
		<ul>
		    <xsl:apply-templates select="prereq"/>
		</ul>
	    </xsl:if>

	    <xsl:if test="coreq">
		<h4>Corequisistes</h4>
		<ul>
		    <xsl:apply-templates select="coreq"/>
		</ul>
	    </xsl:if>

	    <xsl:if test="exclusion">
		<h4>Exclusions</h4>
		<ul>
		    <xsl:apply-templates select="exclusion"/>
		</ul>
	    </xsl:if>

	    <xsl:if test="followon">
		<h4>Follow-On Courses</h4>
		<ul>
		    <xsl:apply-templates select="followon"/>
		</ul>
	    </xsl:if>

	    <xsl:if test="goal">
		<h4>Learning Objectives</h4>
		<xsl:call-template name="handle-goals"/>
	    </xsl:if>

	    <xsl:if test="topic">
		<h4>Topics (not necessarily in this order)</h4>
		<xsl:call-template name="handle-topics"/>
	    </xsl:if>

	    <xsl:if test="text">
		<h4>Suggested Texts</h4>
		<ol>
		    <xsl:apply-templates select="text"/>
		</ol>
	    </xsl:if>

	    <xsl:if test="tech">
		<h4>Technology</h4>
		<xsl:call-template name="handle-tech"/>
	    </xsl:if>

	    <xsl:if test="eval">
		<h4>Sample Evaluation</h4>
		<xsl:call-template name="handle-evals"/>
	    </xsl:if>
	</body>
    </html>
</xsl:template>

<xsl:template name="handle-topics">
    <xsl:if test="topic">
	<ul>
	    <xsl:for-each select="topic">
		<li>
		    <xsl:call-template name="write-optional"/>
		    <xsl:if test="@hours">(<xsl:value-of select="@hours"/> hr.)</xsl:if>
		    <xsl:call-template name="handle-topics"/>
		</li>
	    </xsl:for-each>
	</ul>
    </xsl:if>
</xsl:template>

<xsl:template name="handle-goals">
    <xsl:if test="goal">
	<ul>
	    <xsl:for-each select="goal">
		<li>
		    <xsl:call-template name="write-optional"/>
		    <xsl:call-template name="handle-goals"/>
		</li>
	    </xsl:for-each>
	</ul>
    </xsl:if>
</xsl:template>

<xsl:template match="prereq | coreq | exclusion | followon">
    <li>
	<xsl:value-of select="text()"/>
	<xsl:choose>
	    <xsl:when test="reason">
		<ul>
		    <xsl:for-each select="reason">
			<li>
			    <xsl:value-of select="."/>
			</li>
		    </xsl:for-each>
		</ul>
	    </xsl:when>
	    <xsl:otherwise>
		<ul>
		    <li><b>NO REASON KNOWN</b></li>
		</ul>
	    </xsl:otherwise>
	</xsl:choose>
    </li>
</xsl:template>

<xsl:template match="text">
    <li>
    <pre>
	<xsl:choose><xsl:when test="@type">
	    <b><xsl:value-of select="translate(@type,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/></b><br/>
	</xsl:when><xsl:otherwise>
	    <b>REQUIRED</b>
	</xsl:otherwise></xsl:choose>
	<br/>
	<xsl:value-of select="title"/>
	<xsl:if test="edition"> (<xsl:value-of select="edition"/> edition)</xsl:if>
	<br/>
	<xsl:value-of select="author"/>
	<xsl:if test="publisher">, </xsl:if>
	<xsl:value-of select="publisher"/>
	<xsl:if test="year">, </xsl:if>
	<xsl:value-of select="year"/>
	<xsl:if test="ISBN"><br/>ISBN <xsl:value-of select="ISBN"/></xsl:if>
    </pre>
    </li>
</xsl:template>

<xsl:template name="handle-tech">
    <ul>
	<xsl:for-each select="tech">
	    <li>
		<xsl:call-template name="write-optional"/>
	    </li>
	</xsl:for-each>
    </ul>
</xsl:template>

<xsl:template name="handle-evals">
    <table border="1" width="600">
	<thead><tr><th>type</th><th>weight</th><th>description</th></tr></thead>
	<tbody>
	    <xsl:for-each select="eval">
		<tr>
		    <td><xsl:value-of select="@type"/></td>
		    <td><xsl:value-of select="@weight"/></td>
		    <td><xsl:value-of select="."/></td>
		</tr>
	    </xsl:for-each>
	</tbody>
    </table>
</xsl:template>

<xsl:template name="write-optional">
    <xsl:choose><xsl:when test="@type='optional'">
	(OPTIONAL) <i><xsl:value-of select="text()"/></i>
    </xsl:when><xsl:otherwise>
	<xsl:value-of select="text()"/>
    </xsl:otherwise></xsl:choose>
</xsl:template>

<xsl:template name="person-row">
    <tr>
        <td><xsl:value-of select="text()"/></td>
	<td>
	    <a>
		<xsl:attribute name="href">
		    mailto:<xsl:value-of select="e-mail"/>
		</xsl:attribute>
		<xsl:value-of select="e-mail"/>
	    </a>
	</td>
    </tr>
</xsl:template>

<xsl:template match="change">
    <li>
	(<xsl:value-of select="date"/> - <xsl:value-of select="who"/>) <xsl:value-of select="text()"/>
	<br/><a href="form.html">Make an objection to this change proposal</a>
	<xsl:if test="objection">
	    <br/><b>Objections so far:</b>
	    <ul>
		<xsl:for-each select="objection">
		    <li>
			(<xsl:value-of select="date"/> - <xsl:value-of select="who"/>) <xsl:value-of select="text()"/>
		    </li>
		</xsl:for-each>
	    </ul>
	</xsl:if>
    </li>
</xsl:template>

<xsl:template match="instructor">
    <tr>
	<td><xsl:value-of select="@year"/></td>
	<td><xsl:value-of select="@term"/></td>
	<td><xsl:value-of select="@section"/></td>
	<td><xsl:value-of select="@name"/></td>
    </tr>
</xsl:template>

</xsl:stylesheet>

