<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"
    >
    <xsl:output
    method="html"
    encoding="UTF-8"
    media-type="string"
    indent="yes"
    />
    
    <!-- 
        quick and dirty MATRIX-CONTENT-CHECKER template, from serensoft.com in 2008 - enjoy!
        - create the template based on this XSL
        - the sole expected ingredient is a MATRIX
        
        will show how much text has been entered for each cell:
        - description
        - instructions
        - rationale
        - examples
        
        makes it a LOT easier to know which cells still need some lovin'.
        
        note that we make no particular claims about the cleverness of the code (except the
        expression 'brute force and awkwardness' comes to mind). :)
    -->
    
    <xsl:variable name="lockedcolor"><xsl:value-of select="//lockedColor"/></xsl:variable>
    <xsl:variable name="pendingcolor"><xsl:value-of select="//pendingColor"/></xsl:variable>
    <xsl:variable name="readycolor"><xsl:value-of select="//readyColor"/></xsl:variable>
    <xsl:variable name="completedcolor"><xsl:value-of select="//completedColor"/></xsl:variable>

    <xsl:template match="/ospiPresentation">
        <xsl:apply-templates select="*/artifact/scaffolding"/>
    </xsl:template>

    <xsl:template match="scaffolding">
        <xsl:variable name="matrix-name"><xsl:value-of select="title"/></xsl:variable>
        <xsl:variable name="worksite-name"><xsl:value-of select="worksiteName"/></xsl:variable>
        <xsl:variable name="matrix-worksite">
            <xsl:text>Matrix "</xsl:text>
            <xsl:value-of select="$matrix-name"/>
            <xsl:text>" from worksite "</xsl:text>
            <xsl:value-of select="$worksite-name"/>
            <xsl:text>"</xsl:text>
        </xsl:variable>

        <html>
            <head>
                <title><xsl:value-of select="$matrix-worksite"/></title>
                <style type="text/css">
                    <xsl:text>
body { font-family: "Trebuchet MS", sans-serif; }
box { margin-left: 2em; border 1px dotted gray; }
                    </xsl:text>
                </style>
                <script type="text/javascript" language="JavaScript" src="/library/js/jquery.js"/>
                <script type="text/javascript" language="JavaScript" src="http://will.trillich.com/jquery/jquery.js"/>
            </head>
            <body>

                <h1>Debugging <xsl:value-of select="$matrix-worksite"/></h1>

                <h2>Matrix "<xsl:value-of select="$matrix-name"/>"</h2>

                <p>This template from <a href="http://www.serensoft.com/">Serensoft</a>can help a 
                    matrix-designer keep track of which cells are missing descriptive text (cell DESCRIPTION, 
                    INSTRUCTIONS, RATIONALE or EXAMPLES). Simply PREVIEW your matrix and then create 
                    a template that includes a matrix...</p>
                <p>Note that you can 'hover' your mouse pointer over most items in the table below for 
                    a bit more information.</p>

                <table>
                    <xsl:for-each select="criteria/criteria">
                        <xsl:variable name="rowid">
                            <xsl:value-of select="id"/>
                        </xsl:variable>

                        <xsl:call-template name="table-headers"/>

                        <!-- ROW HEADERS -->
                        <tr>
                            <td colspan="2" style="font-weight: bold;">
                                <xsl:attribute name="bgcolor"><xsl:value-of select="color"/></xsl:attribute>
                                <xsl:attribute name="title">
                                    <xsl:text>The name of this matrix ROW is "</xsl:text>
                                    <xsl:value-of select="description"/>
                                    <xsl:text>"</xsl:text>
                                </xsl:attribute>
                                <xsl:text>ROW: "</xsl:text>
                                <xsl:value-of select="description"/>
                                <xsl:text>"</xsl:text>
                            </td>
                            <td colspan="6">
                                <xsl:attribute name="style">
                                    <xsl:text>border-top: solid 2px </xsl:text>
                                    <xsl:value-of select="color"/>
                                </xsl:attribute>
                            </td>
                        </tr>

                        <xsl:for-each select="../../levels/level">
                            <xsl:variable name="colid">
                                <xsl:value-of select="id"/>
                            </xsl:variable>
                            <tr>
                                
                                <xsl:call-template name="expose-cell">
                                    <xsl:with-param name="row">
                                        <xsl:value-of select="$rowid"/>
                                    </xsl:with-param>
                                    <xsl:with-param name="col">
                                        <xsl:value-of select="$colid"/>
                                    </xsl:with-param>
                                </xsl:call-template>
                                <xsl:text>
                                </xsl:text>

                            </tr>
                        </xsl:for-each>
                        
                        <xsl:text>

                        </xsl:text>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="content-zero">
        <xsl:param name="n"/>
        <xsl:param name="item"/>
        <xsl:param name="cell"/>
        
        <td align="right">
            <xsl:attribute name="title">
                <xsl:value-of select="$item"/>
                <xsl:text> of cell "</xsl:text>
                <xsl:value-of select="$cell"/>
                <xsl:text>" </xsl:text>
                <xsl:choose>
                    <xsl:when test="$n = 0">is empty!</xsl:when>
                    <xsl:otherwise>
                        <xsl:text>contains </xsl:text>
                        <xsl:value-of select="$n"/>
                        <xsl:text> characters</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <xsl:choose>
                <xsl:when test="$n = 0">
                    <span style="color:#cccc99;"><xsl:value-of select="$n"/></span>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$n"/>
                </xsl:otherwise>
            </xsl:choose>
        </td>

    </xsl:template>
    
    <xsl:template name="expose-cell">
        <xsl:param name="row"/>
        <xsl:param name="col"/>
        <xsl:for-each select="//scaffoldingCell[rootCriterion/id=$row and level/id=$col]/..">
            <xsl:variable name="cell-name">
                <xsl:value-of select="scaffoldingCell/title"/>
            </xsl:variable>
            <xsl:variable name="row-name">
                <xsl:value-of select="scaffoldingCell/rootCriterion/description"/>
            </xsl:variable>
            <xsl:variable name="col-name">
                <xsl:value-of select="scaffoldingCell/level/description"/>
            </xsl:variable>

            <xsl:variable name="descr-size">
                <xsl:value-of select="string-length(wizardPage/pageDefinition/description)"/>
            </xsl:variable>

            <xsl:variable name="instructions-size">
                <xsl:value-of
                    select="string-length(wizardPage/pageDefinition/guidance/items/item[@index=0]/text)"
                />
            </xsl:variable>

            <xsl:variable name="examples-size">
                <xsl:value-of
                    select="string-length(wizardPage/pageDefinition/guidance/items/item[@index=1]/text)"
                />
            </xsl:variable>
            
            <xsl:variable name="rationale-size">
                <xsl:value-of
                    select="string-length(wizardPage/pageDefinition/guidance/items/item[@index=2]/text)"
                />
            </xsl:variable>
            

            <!-- spacer -->
            <td width="10"/>
            
            <td style="font-weight: bold;">
                <xsl:attribute name="bgcolor"><xsl:value-of select="color"/></xsl:attribute>
                <xsl:attribute name="title">
                    <xsl:text>The name of this matrix COLUMN is "</xsl:text>
                    <xsl:value-of select="$col-name"/>
                    <xsl:text>"</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="class">
                    <xsl:text>row</xsl:text>
                    <xsl:value-of select="$row"/>
                    <xsl:text> </xsl:text>
                    <xsl:text>col</xsl:text>
                    <xsl:value-of select="$col"/>
                </xsl:attribute>
                <xsl:text>COL: "</xsl:text>
                <xsl:value-of select="$col-name"/>
                <xsl:text>"</xsl:text>
            </td>

            <td>
                <xsl:text>"</xsl:text>
                <xsl:value-of select="$cell-name"/>
                <xsl:text>"</xsl:text>
            </td>

            <td  title="Initial Cell Status">
                <xsl:attribute name="bgcolor">
                    <xsl:call-template name="colorize-cell">
                        <xsl:with-param name="status"><xsl:value-of select="scaffoldingCell/initialStatus"/></xsl:with-param>
                    </xsl:call-template>
                </xsl:attribute>
                <xsl:attribute name="title">
                    <xsl:text>Initial status for cell "</xsl:text>
                    <xsl:value-of select="$cell-name"/>
                    <xsl:text>" is </xsl:text>
                    <xsl:value-of select="scaffoldingCell/initialStatus"/>
                </xsl:attribute>
                <xsl:value-of select="scaffoldingCell/initialStatus"/>
            </td>
        
            <xsl:call-template name="content-zero">
                <xsl:with-param name="cell"><xsl:value-of select="$cell-name"/></xsl:with-param>
                <xsl:with-param name="n"><xsl:value-of select="$descr-size"/></xsl:with-param>
                <xsl:with-param name="item">Description</xsl:with-param>
            </xsl:call-template>
            
            <xsl:call-template name="content-zero">
                <xsl:with-param name="cell"><xsl:value-of select="$cell-name"/></xsl:with-param>
                <xsl:with-param name="n"><xsl:value-of select="$instructions-size"/></xsl:with-param>
                <xsl:with-param name="item">Instructions</xsl:with-param>
            </xsl:call-template>
            
            <xsl:call-template name="content-zero">
                <xsl:with-param name="cell"><xsl:value-of select="$cell-name"/></xsl:with-param>
                <xsl:with-param name="n"><xsl:value-of select="$rationale-size"/></xsl:with-param>
                <xsl:with-param name="item">Rationale</xsl:with-param>
            </xsl:call-template>
            
            <xsl:call-template name="content-zero">
                <xsl:with-param name="cell"><xsl:value-of select="$cell-name"/></xsl:with-param>
                <xsl:with-param name="n"><xsl:value-of select="$examples-size"/></xsl:with-param>
                <xsl:with-param name="item">Examples</xsl:with-param>
            </xsl:call-template>
            
        </xsl:for-each>
    </xsl:template>


    <xsl:template name="table-headers">
        <tr>
            <th colspan="2">Row/Col</th>
            <th title="Cell Name">Cell Name</th>
            <th title="Initial Cell Status">Status</th>
            <th title="Cell Description">Descr</th>
            <th title="Cell Instructions">Instrux</th>
            <th title="Cell Rationale">Ration</th>
            <th title="Cell Examples">Examp</th>
<!--
            <th title=" Designated Cell Forms: Reflection, Feedback, Evaluation">Cell Forms:</th>
            <th title="Other forms added to cell">Other Forms:</th>
            <th title="Evaluators, by user name or by worksite role">Evaluators</th>
-->
        </tr>
    </xsl:template>
        
    <xsl:template name="colorize-cell">
        <xsl:param name="status"/>
        <xsl:choose>
            <xsl:when test="$status = 'LOCKED'"><xsl:value-of select="$lockedcolor"/></xsl:when>
            <xsl:when test="$status = 'READY'"><xsl:value-of select="$readycolor"/></xsl:when>
            <xsl:when test="$status = 'COMPLETED'"><xsl:value-of select="$completedcolor"/></xsl:when>
            <xsl:otherwise><xsl:value-of select="$pendingcolor"/></xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>
</xsl:stylesheet>
