/* ======================================================================
The Bodington System Software License, Version 1.0
  
Copyright (c) 2001 The University of Leeds.  All rights reserved.
  
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1.  Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2.  Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3.  The end-user documentation included with the redistribution, if any,
must include the following acknowledgement:  "This product includes
software developed by the University of Leeds
(http://www.bodington.org/)."  Alternately, this acknowledgement may
appear in the software itself, if and wherever such third-party
acknowledgements normally appear.

4.  The names "Bodington", "Nathan Bodington", "Bodington System",
"Bodington Open Source Project", and "The University of Leeds" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
d.gardner@leeds.ac.uk.

5.  The name "Bodington" may not appear in the name of products derived
from this software without prior written permission of the University of
Leeds.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  TITLE,  THE IMPLIED WARRANTIES 
OF QUALITY  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO 
EVENT SHALL THE UNIVERSITY OF LEEDS OR ITS CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
=========================================================

This software was originally created by the University of Leeds and may contain voluntary 
contributions from others.  For more information on the Bodington Open Source Project, please 
see http://bodington.org/

====================================================================== */

package org.bodington.assessment;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.bodington.server.BuildingContext;
import org.bodington.server.BuildingServerException;
import org.bodington.server.events.Event;
import org.bodington.server.events.OpinionEvent;
import org.bodington.server.realm.User;
import org.bodington.servlet.Request;

/**
 * Class for rendering questionnaires in the <em>run</em> state. This class
 * is used to render the questions themselves, the functionality to enable the
 * recording of responses to those questions and, where appropriate, the
 * current user's existing responses.
 * @author Alexis O'Connor
 */
class RunQuestionnaireRenderer extends AbstractQuestionnaireRenderer
    {
    public void paperToHTML( Request request, PrintWriter out,
        QuestionnaireSession session ) throws IOException,
        BuildingServerException
        {
        List questions_in_order;
        Questionnaire questionnaire = null;
        QuestionnaireQuestion question;
        QuestionnaireResult result = null;
        QuestionnaireResponse response = null;
        Map responses = null;

        questionnaire = session.getQuestionnaire();
        questions_in_order = session.getQuestionnaireQuestionsInOrder();

        result = session.getQuestionnaireResult();
        // NOTE: we may null the result so that it is not rendered.
        result = questionnaire.isHideAnswersOnSubmit() ? null : result;
        responses = result != null ? result.getQuestionnaireResponses() : null;
        
        // What's this questionnaire all about?
        out.println( questionnaire.getIntroduction() );

        /*
         * Conditional messages depending on certain states:
         */ 
        List messages = getMessages( questionnaire, result );
        if ( messages.size() > 0 )
            {
            out.println("<p><em>NOTE: you should read the following points "
                + "about this questionnaire.</em>");
            out.print("<ul>");
            Iterator iter = messages.iterator();
            while (iter.hasNext())
                out.println( "<li>" + iter.next() + "</li>");
            out.print("</ul>");
            }

        out.println( "<FORM METHOD=POST target=buildmain " +
                "ACTION=bs_template_qrecord.html>" );
        for ( int i = 0; i < questions_in_order.size(); i++ )
            {
            question = (QuestionnaireQuestion)questions_in_order.get( i );
            response = (responses != null) 
                ? (QuestionnaireResponse)responses.get( 
                    question.getQuestionnaireQuestionId() ) : null;

            String identifier = "Q" + question.getQuestionnaireQuestionId();
            out.println( "<HR><TABLE id=" + identifier
                + "><TR><TD COLSPAN=3><H4>Question " + (i + 1)
                + "</H4></TD></TR>" );

            questionToHTML( question, response, out );

            out.println( "</TABLE>" );
            }

        if ( questions_in_order.size() == 0 )
            {
            out.println( "<P>There are currently no questions in this "
                + "questionnaire.</P>" );
            }

        out.println("<p>");
        
        if ( questions_in_order.size() > 0 )
            {
            // `Record' Button:
            out.println( "<div style=\"float: left; margin-left: 80px\">" );
            submitButton( out );
            out.println( "</div>" );
            User user = (User)BuildingContext.getContext().getUser();
            Event event = new OpinionEvent( OpinionEvent.EVENT_START, request
                .getResource().getResourceId(), user.getUserId(), null );
            event.save();
            }
        // Close the (main) form:
        out.println( "</form>" );

        
        // `Cancel' Button:
        out.println("<div>" );
        out.println( "<FORM METHOD=POST target=_top "
            + "ACTION=bs_template_qrecord.html>" );
        cancelButton( out );
        out.println( "</form>" );
        out.println("</div>");
        
        // Close 'Buttons' paragraph:
        out.println( "</p>" );
        }

    public void questionToHTML( QuestionnaireQuestion question,
        QuestionnaireResponse response, PrintWriter out ) throws IOException,
        BuildingServerException
        {
        out.println( "<TR><TD COLSPAN=3>" );
        out.println( "<INPUT TYPE=HIDDEN VALUE=TRUE NAME=Q"
            + question.getQuestionnaireQuestionId() + ">" );
        out.println( question.getIntroduction() );
        out.println( "</TD></TR>" );

        int[] indices = question.getValidStatementIndices();
        for ( int i = 0; i < indices.length; i++ )
            {
            char letter = (char)('A' + i);
            out.println( "<TR><TD VALIGN=TOP WIDTH=20><B>" );
            out.println( letter );
            out.println( "</B></TD><TD VALIGN=TOP WIDTH=20>" );
            out.print( "<INPUT TYPE=RADIO " );
            out.print( "NAME=QR" );
            out.print( question.getQuestionnaireQuestionId() );
            out.print( " VALUE=" + letter );
            if ( response != null && response.getItem() == i )
                out.print( " CHECKED>" );
            else
                out.print( ">" );
            out.println( "</TD><TD VALIGN=TOP WIDTH=1000>" );
            out.println( question.getStatement( indices[i] ) );
            out.println( "</TD></TR>" );
            }

        if ( indices.length > 0 )
            {
            out.println( "<TR><TD VALIGN=TOP WIDTH=20></TD><TD VALIGN=TOP "
                + "WIDTH=20>" );
            out.print( "<INPUT TYPE=RADIO " );
            out.println( "NAME=QR" + question.getQuestionnaireQuestionId()
                + " VALUE=X>" );
            out.println( "</TD><TD VALIGN=TOP WIDTH=1000>Cancel selection.</TD>"
                    + "</TR>" );
            }

        if ( question.isCanComment() )
            {
            out.print( "<TR>" );
            out.print( "<TD COLSPAN=3><SPAN CLASS=bs-textarea><TEXTAREA ROWS=6 "
                    + "COLS=40 NAME=QC" );
            out.print( question.getQuestionnaireQuestionId() );
            out.print( ">" );
            if ( response != null && response.getComment() != null )
                out.print( response.getComment() );
            out.println( "</TEXTAREA></SPAN></TD></TR>" );
            }
        }
    }
