
/* ======================================================================
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.math.BigInteger;
import java.math.BigDecimal;
import java.util.Enumeration;
import java.io.*;

import org.bodington.sqldatabase.*;
import org.bodington.database.*;
import org.bodington.server.*;
import org.bodington.text.BigString;

public class McqQuestion extends org.bodington.sqldatabase.SqlPersistentObject
	{
	private static final byte FLAGS_TYPE_MASK = 7;
	private static final byte FLAGS_TYPE_REV_MASK = (byte)0xf8;
        
	public static final byte TYPE_MULTIPLE_TRUE = 0;
	public static final byte TYPE_ONE_TRUE = 1;
	public static final byte TYPE_MULTIPLE_TF = 2;
	public static final byte TYPE_MULTIPLE_TFD = 3;
	public static final byte TYPE_STRICT_MULTIPLE_TF = 4;
	
	private PrimaryKey mcq_question_id;
	private PrimaryKey resource_id;
	private PrimaryKey question_big_string_id;
	private PrimaryKey explanation_big_string_id;
	private PrimaryKey statement_a_big_string_id;
	private PrimaryKey statement_b_big_string_id;
	private PrimaryKey statement_c_big_string_id;
	private PrimaryKey statement_d_big_string_id;
	private PrimaryKey statement_e_big_string_id;
	private int ordinal;
	private byte flags;
	private byte answers;
	
	public static McqQuestion findMcqQuestion( PrimaryKey key )
	    throws BuildingServerException
	    {
	    return (McqQuestion)findPersistentObject( key, "org.bodington.server.assessment.McqQuestion" );
	    }
	
	public static McqQuestion findMcqQuestion( String where )
	    throws BuildingServerException
	    {
	    return (McqQuestion)findPersistentObject( where, "org.bodington.assessment.McqQuestion" );
	    }
	
	public static Enumeration findMcqQuestions( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, "org.bodington.assessment.McqQuestion" );
	    }
	
	public static Enumeration findMcqQuestions( String where, String order )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, order, "org.bodington.assessment.McqQuestion" );
	    }
	

	
    public PrimaryKey getPrimaryKey()
    	{
        return getMcqQuestionId();
    	}

    public void setPrimaryKey(PrimaryKey key)
    	{
    	setMcqQuestionId( key );
    	}

	
	public PrimaryKey getMcqQuestionId()
		{
		return mcq_question_id;
		}
		
	public void setMcqQuestionId( PrimaryKey id )
		{
		mcq_question_id = id;
		}
		
		
	public PrimaryKey getResourceId()
		{
		return resource_id;
		}

	public void setResourceId( PrimaryKey id )
		{
		resource_id = id;
		}

		
		
	public PrimaryKey getQuestionBigStringId()
		{
		return question_big_string_id;
		}

	public void setQuestionBigStringId( PrimaryKey id )
		{
		question_big_string_id = id;
		setUnsaved();
		}
		
	public String getQuestion()
		throws BuildingServerException
		{
		if ( question_big_string_id == null )
			return null;
		BigString bstr = BigString.findBigString( question_big_string_id );
		if ( bstr == null )
			return null;
		return bstr.getString();
		}
		
	public void setQuestion( String s )
		throws BuildingServerException
		{
		BigString bstr=null;
		if ( question_big_string_id != null )
			bstr = BigString.findBigString( question_big_string_id );
		if ( bstr == null )
			{
			bstr = new BigString();
			bstr.setString( s );
			bstr.save();
			question_big_string_id = bstr.getBigStringId();
			setUnsaved();
			return;
			}

		bstr.setString( s );
		bstr.save();
		}
	
		
	public PrimaryKey getExplanationBigStringId()
		{
		return explanation_big_string_id;
		}

	public void setExplanationBigStringId( PrimaryKey id )
		{
		explanation_big_string_id = id;
		setUnsaved();
		}
		
	public String getExplanation()
		throws BuildingServerException
		{
		if ( explanation_big_string_id == null )
			return null;
		BigString bstr = BigString.findBigString( explanation_big_string_id );
		if ( bstr == null )
			return null;
		return bstr.getString();
		}
		
	public void setExplanation( String s )
		throws BuildingServerException
		{
		BigString bstr=null;
		if ( question_big_string_id != null )
			bstr = BigString.findBigString( explanation_big_string_id );
		if ( bstr == null )
			{
			bstr = new BigString();
			bstr.setString( s );
			bstr.save();
			explanation_big_string_id = bstr.getBigStringId();
			setUnsaved();
			return;
			}

		bstr.setString( s );
		bstr.save();
		}
	


	public PrimaryKey getStatementABigStringId()
		{
		return statement_a_big_string_id;
		}

	public void setStatementABigStringId( PrimaryKey id )
		{
		statement_a_big_string_id = id;
		setUnsaved();
		}
		
		
	public PrimaryKey getStatementBBigStringId()
		{
		return statement_b_big_string_id;
		}

	public void setStatementBBigStringId( PrimaryKey id )
		{
		statement_b_big_string_id = id;
		setUnsaved();
		}
		
		
	public PrimaryKey getStatementCBigStringId()
		{
		return statement_c_big_string_id;
		}

	public void setStatementCBigStringId( PrimaryKey id )
		{
		statement_c_big_string_id = id;
		setUnsaved();
		}
		
		
	public PrimaryKey getStatementDBigStringId()
		{
		return statement_d_big_string_id;
		}

	public void setStatementDBigStringId( PrimaryKey id )
		{
		statement_d_big_string_id = id;
		setUnsaved();
		}
		
		
	public PrimaryKey getStatementEBigStringId()
		{
		return statement_e_big_string_id;
		}

	public void setStatementEBigStringId( PrimaryKey id )
		{
		statement_e_big_string_id = id;
		setUnsaved();
		}
		

	public PrimaryKey getStatementBigStringId( int n )
		throws BuildingServerException
		{
		switch ( n )
			{
			case 0:
				return getStatementABigStringId();
			case 1:
				return getStatementBBigStringId();
			case 2:
				return getStatementCBigStringId();
			case 3:
				return getStatementDBigStringId();
			case 4:
				return getStatementEBigStringId();
			}
			
		return null;
		}
		
	public String getStatement( int n )
		throws BuildingServerException
		{
		if ( n<0 || n>4 )
			return null;
			
		PrimaryKey big_string_id = null;
		switch ( n )
			{
			case 0:
				big_string_id = getStatementABigStringId();
				break;
			case 1:
				big_string_id = getStatementBBigStringId();
				break;
			case 2:
				big_string_id = getStatementCBigStringId();
				break;
			case 3:
				big_string_id = getStatementDBigStringId();
				break;
			case 4:
				big_string_id = getStatementEBigStringId();
				break;
			}
			
		if ( big_string_id == null )
			return null;
			
		BigString bs = BigString.findBigString( big_string_id );
		if ( bs == null )
			return null;
		
		return bs.toString();
		}
		
	public void setStatement( int n, String s )
		throws BuildingServerException
		{
		if ( n<0 || n>4 )
			return;
		
		PrimaryKey big_string_id = null;
		switch ( n )
			{
			case 0:
				big_string_id = getStatementABigStringId();
				break;
			case 1:
				big_string_id = getStatementBBigStringId();
				break;
			case 2:
				big_string_id = getStatementCBigStringId();
				break;
			case 3:
				big_string_id = getStatementDBigStringId();
				break;
			case 4:
				big_string_id = getStatementEBigStringId();
				break;
			}
			
		BigString bs=null;
		
		if ( big_string_id !=null )
			bs = BigString.findBigString( big_string_id );
		
		if ( bs == null )
			{
			bs = new BigString();
			bs.setString( s );
			bs.save();
			switch ( n )
				{
				case 0:
					setStatementABigStringId( bs.getBigStringId() );
					break;
				case 1:
					setStatementBBigStringId( bs.getBigStringId() );
					break;
				case 2:
					setStatementCBigStringId( bs.getBigStringId() );
					break;
				case 3:
					setStatementDBigStringId( bs.getBigStringId() );
					break;
				case 4:
					setStatementEBigStringId( bs.getBigStringId() );
					break;
				}
			setUnsaved();
			return;
			}
		

		bs.setString( s );
		bs.save();
		}
	

		
	public int getOrdinal()
		{
		return ordinal;
		}
		
	public void setOrdinal( int o )
		{
		if ( o==ordinal )
			return;
		ordinal = o;
		setUnsaved();
		}
		
		
	public byte getFlags()
		{
		return flags;
		}
		
	public void setFlags( byte b )
		{
		if ( b==flags )
			return;
		flags = b;
		setUnsaved();
		}

	public byte getQuestionType()
		{
		return (byte)(getFlags() & FLAGS_TYPE_MASK);
		}
        
	public void setQuestionType( byte t )
		{
		if ( getQuestionType() == t )
			return;
		setFlags( (byte)((getFlags() & FLAGS_TYPE_REV_MASK) | t) );
		}


	public byte getAnswers()
		{
		return answers;
		}
		
	public void setAnswers( byte b )
		{
		if ( answers == b )
			return;
		answers = b;
		setUnsaved();
		}
		
	public boolean getAnswer( int n )
		{
		if ( n<0 || n>4 ) return false;
		return (answers & (1 << n)) != 0;
		}
		
	public void setAnswer( int n, boolean answer )
		{
		if ( n<0 || n>4 ) return;
		if ( answer == getAnswer( n ) )
			return;
			
		setAnswers( (byte)(getAnswers() ^ (1 << n)) );
		}



    public int getMaximumMark()
        {
        int m=0;

        if ( getQuestionType() == TYPE_ONE_TRUE || getQuestionType() == TYPE_MULTIPLE_TRUE )
        {
            for ( int i=0; i<5; i++ )
                if ( getAnswer( i ) )
                    m++;
        }
        else
            m=5;
        
        return m;
        }

        public void exportXmlTf( PrintWriter xmlout, boolean dontknow, boolean strict )
            throws IOException, BuildingServerException
        {
            int j;
            
            String ident = "pk" + getPrimaryKey() +"tfd";
            xmlout.print( "\t<item ident=\"" );
            xmlout.print( ident );
            xmlout.print( "\">" );
            xmlout.println();
            
            xmlout.println( "\t\t<itemmetadata>" );
            xmlout.println( "\t\t\t<qmd_computerscored>Yes</qmd_computerscored>" );
            xmlout.println( "\t\t\t<qmd_feedbackpermitted>No</qmd_feedbackpermitted>" );
            xmlout.println( "\t\t\t<qmd_hintspermitted>No</qmd_hintspermitted>" );
            xmlout.println( "\t\t\t<qmd_itemtype>Logical Identifier</qmd_itemtype>" );
            xmlout.println( "\t\t\t<qmd_maximumscore>5</qmd_maximumscore>" );
            xmlout.println( "\t\t\t<qmd_renderingtype>Choice</qmd_renderingtype>" );
            xmlout.println( "\t\t\t<qmd_responsetype>Multiple</qmd_responsetype>" );
            xmlout.println( "\t\t\t<qmd_scoringpermitted>No</qmd_scoringpermitted>" );
            xmlout.println( "\t\t\t<qmd_toolvendor>Bodington System</qmd_toolvendor>" );
            xmlout.println( "\t\t</itemmetadata>" );
            xmlout.println();
            
            xmlout.println("\t\t<presentation label='"+ident+"'>");
            xmlout.println("\t\t\t<response_grp ident='"+ident+"' rcardinality='Multiple' rtiming='No'>");
            xmlout.println();
            
            xmlout.println("\t\t\t\t<material>");
            xmlout.print("\t\t\t\t\t<mattext texttype='text/html'>");
            xmlout.print("<![CDATA[");
            xmlout.print(getQuestion());
            xmlout.println("]]></mattext>");
            xmlout.println("\t\t\t\t</material>");
            xmlout.println();
            
            xmlout.println("\t\t\t\t<render_choice shuffle='No'>");
            xmlout.println();
            
            
            
            for (j=0; j<5; j++)
            {
                xmlout.print("\t\t\t\t\t<response_label ident='");
                xmlout.print((char)('A'+j));
                if ( dontknow )
                    xmlout.println("' match_max='1' match_group='T,F,D,U'>");
                else
                    xmlout.println("' match_max='1' match_group='T,F,U'>");
                xmlout.println("\t\t\t\t\t\t<material>");
                xmlout.print( "\t\t\t\t\t\t\t<mattext texttype='text/html'><![CDATA[" );
                xmlout.print( getStatement( j ) );
                xmlout.print( "]]></mattext>" );
                xmlout.println("\t\t\t\t\t\t</material>");
                xmlout.println("\t\t\t\t\t</response_label>");
                xmlout.println();
            }
            
            xmlout.println("\t\t\t\t\t<response_label ident='T'>");
            xmlout.println("\t\t\t\t\t\t<material>");
            xmlout.println("\t\t\t\t\t\t\t<mattext  texttype='text/html'><![CDATA[T]]></mattext>");
            xmlout.println("\t\t\t\t\t\t</material>");
            xmlout.println("\t\t\t\t\t</response_label>");
            xmlout.println();
            
            xmlout.println("\t\t\t\t\t<response_label ident='F'>");
            xmlout.println("\t\t\t\t\t\t<material>");
            xmlout.println("\t\t\t\t\t\t\t<mattext  texttype='text/html'><![CDATA[F]]></mattext>");
            xmlout.println("\t\t\t\t\t\t</material>");
            xmlout.println("\t\t\t\t\t</response_label>");
            xmlout.println();
            
            if ( dontknow )
            {
                xmlout.println("\t\t\t\t\t<response_label ident='D'>");
                xmlout.println("\t\t\t\t\t\t<material>");
                xmlout.println("\t\t\t\t\t\t\t<mattext  texttype='text/html'><![CDATA[D]]></mattext>");
                xmlout.println("\t\t\t\t\t\t</material>");
                xmlout.println("\t\t\t\t\t</response_label>");
                xmlout.println();
            }
            
            xmlout.println("\t\t\t\t\t<response_label ident='U'>");
            xmlout.println("\t\t\t\t\t\t<material>");
            xmlout.println("\t\t\t\t\t\t\t<mattext  texttype='text/html'><![CDATA[U]]></mattext>");
            xmlout.println("\t\t\t\t\t\t</material>");
            xmlout.println("\t\t\t\t\t</response_label>");
            xmlout.println();
            
            xmlout.println("\t\t\t\t</render_choice>");
            xmlout.println("\t\t\t</response_grp>");
            xmlout.println("\t\t</presentation>");
            xmlout.println();
            
            xmlout.println("\t\t<resprocessing>");
            
            xmlout.println("\t\t\t<outcomes>");
            xmlout.println("\t\t\t\t<decvar varname='SCORE' vartype='Integer' defaultval='0' minvalue='0' maxvalue='5'/>");
            xmlout.println("\t\t\t</outcomes>");
            
            /* can't see the point of this respcondition because the next
             * ones can be programmed to always provide the feedback */
            /*
            xmlout.println("\t\t\t<respcondition title='Right' continue='Yes'>");
            xmlout.println("\t\t\t\t<conditionvar>");
            xmlout.println("\t\t\t\t\t<and>");
            for (j=0; j<5; j++)
            {
                xmlout.print("\t\t\t\t\t\t<varsubset respident='"+ident+"'>");
                xmlout.print((char)('A'+j)+",");
                if (getAnswer(j)) xmlout.print("T"); else xmlout.print("F");
                xmlout.println("</varsubset>");
            }
            xmlout.println("\t\t\t\t\t</and>");
            xmlout.println("\t\t\t\t</conditionvar>");
            xmlout.println("\t\t\t\t<displayfeedback feedbacktype='Response' linkrefid='"+ident+".Response'/>");
            xmlout.println("\t\t\t</respcondition>");
             **/
        
            if ( strict )
            {
                // add 5 marks if all statements correctly selected
                xmlout.println("\t\t\t<respcondition title='Right' continue='Yes'>");
                xmlout.println("\t\t\t\t<conditionvar>");
                xmlout.println("\t\t\t\t\t<and>");
                for (j=0; j<5; j++)
                {
                    xmlout.print("\t\t\t\t\t\t<varsubset respident='"+ident+"'>");
                    xmlout.print((char)('A'+j)+",");
                    if (getAnswer(j)) xmlout.print("T"); else xmlout.print("F");
                    xmlout.println("</varsubset>");
                }
                xmlout.println("\t\t\t\t\t</and>");
                xmlout.println("\t\t\t\t</conditionvar>");
                xmlout.println("\t\t\t\t<setvar action='Add' varname='SCORE'>5</setvar>");
                xmlout.println("\t\t\t</respcondition>");
            }
            else
            {
                // add one mark for each correct statement
                for (j=0; j<5; j++)
                {
                    xmlout.println("\t\t\t<respcondition title='"+(char)('A'+j)+"R' continue='Yes'>");
                    xmlout.println("\t\t\t\t<conditionvar>");
                    xmlout.print("\t\t\t\t\t\t<varsubset respident='"+ident+"'>");
                    xmlout.print((char)('A'+j)+",");
                    if (getAnswer(j)) xmlout.print("T"); else xmlout.print("F");
                    xmlout.println("</varsubset>");
                    xmlout.println("\t\t\t\t</conditionvar>");
                    xmlout.println("\t\t\t\t<setvar action='Add' varname='SCORE'>1</setvar>");
                    xmlout.println("\t\t\t</respcondition>");
                    xmlout.println();
                }
                // negative marking needs another set of conditions....
                // but can't go totally -ve because of minvalue attribute on SCORE
                if ( dontknow )
                {
                    for (j=0; j<5; j++)
                    {
                        xmlout.println("\t\t\t<respcondition title='"+(char)('A'+j)+"R' continue='Yes'>");
                        xmlout.println("\t\t\t\t<conditionvar>");
                        xmlout.print("\t\t\t\t\t\t<varsubset respident='"+ident+"'>");
                        xmlout.print((char)('A'+j)+",");
                        if (getAnswer(j)) xmlout.print("F"); else xmlout.print("T");
                        xmlout.println("</varsubset>");
                        xmlout.println("\t\t\t\t</conditionvar>");
                        xmlout.println("\t\t\t\t<setvar action='Add' varname='SCORE'>-1</setvar>");
                        xmlout.println("\t\t\t</respcondition>");
                        xmlout.println();
                    }
                }
            }

            xmlout.println("\t\t\t<respcondition title='Feedback' continue='Yes'>");
            xmlout.println("\t\t\t\t<conditionvar>");
            xmlout.println("\t\t\t\t\t<other/>");
            xmlout.println("\t\t\t\t</conditionvar>");
            xmlout.println("\t\t\t\t<displayfeedback feedbacktype='Response' linkrefid='"+ident+".Response'/>");
            xmlout.println("\t\t\t</respcondition>");
            xmlout.println();
            
            
            xmlout.println("\t\t</resprocessing>");
            xmlout.println();
            
            xmlout.println("\t\t<itemfeedback ident='"+ident+".Response' view='Candidate'>");
            xmlout.println("\t\t\t<material>");
            xmlout.println("\t\t\t\t<mattext texttype='text/html'>");
            xmlout.print("\t\t\t\t\t<![CDATA[");
            xmlout.print(getExplanation());
            xmlout.println("]]>");
            xmlout.println("\t\t\t\t</mattext>");
            xmlout.println("\t\t\t</material>");
            xmlout.println("\t\t</itemfeedback>");
            xmlout.println();
            
            xmlout.println("\t</item>");
        }
        
        
	public void exportXml( PrintWriter xmlout )
	    throws IOException, BuildingServerException
	    {
	    int j;
            byte type = getQuestionType();
            
            if ( type == TYPE_MULTIPLE_TF )
                { exportXmlTf( xmlout, false, false ); return; }
            
            if ( type == TYPE_STRICT_MULTIPLE_TF )
                { exportXmlTf( xmlout, true, true ); return; }
            
            if ( type == TYPE_MULTIPLE_TFD )
                { exportXmlTf( xmlout, true, false ); return; }
            
        String ident = "pk" + getPrimaryKey() + ((type==TYPE_ONE_TRUE)?"mcq":"mrq");
        String card = (type==TYPE_ONE_TRUE)?"Single":"Multiple";
        String maxr = (type==TYPE_ONE_TRUE)?"1":"5";

        xmlout.print( "\t<item ident=\"" );
        xmlout.print( ident );
        xmlout.print( "\">" );
        xmlout.println();

        xmlout.println( "\t\t<itemmetadata>" );
        xmlout.println( "\t\t\t<qmd_computerscored>Yes</qmd_computerscored>" );
        xmlout.println( "\t\t\t<qmd_feedbackpermitted>No</qmd_feedbackpermitted>" );
        xmlout.println( "\t\t\t<qmd_hintspermitted>No</qmd_hintspermitted>" );
        xmlout.println( "\t\t\t<qmd_itemtype>Logical Identifier</qmd_itemtype>" );
        xmlout.print( "\t\t\t<qmd_maximumscore>" );
        xmlout.print( getMaximumMark() );
        xmlout.println( "</qmd_maximumscore>" );
        
        xmlout.println( "\t\t\t<qmd_renderingtype>Choice</qmd_renderingtype>" );
        xmlout.print( "\t\t\t<qmd_responsetype>" );
        xmlout.print( card );
        xmlout.println( "</qmd_responsetype>" );
        
        xmlout.println( "\t\t\t<qmd_scoringpermitted>No</qmd_scoringpermitted>" );
        xmlout.println( "\t\t\t<qmd_toolvendor>Bodington System</qmd_toolvendor>" );
        xmlout.println( "\t\t</itemmetadata>" );
                
        xmlout.println( "\t\t<presentation>" );
            xmlout.println( "\t\t\t<material>" );
                xmlout.print( "\t\t\t\t<mattext texttype=\"text/html\"><![CDATA[" );
                xmlout.print( getQuestion() );
                //if ( broken_composite )
                //    {
                //    xmlout.println();
                //    xmlout.println( "<P>(The next question contains the comment box for this question.)</P>" );
                //    }
                xmlout.print( "]]></mattext>" );
                xmlout.println();
            xmlout.println( "\t\t\t</material>" );
            xmlout.print( "\t\t\t<response_lid ident=\"1\" rcardinality=\"" );
            xmlout.print( card );
            //xmlout.print( "\" minnumber=\"1\" maxnumber=\"" );
            //xmlout.print( maxr );
            xmlout.println( "\">" );

                xmlout.println( "\t\t\t\t<render_choice shuffle=\"No\">" );
                        
        for ( j=0; j<5; j++ )
            {
            xmlout.print( "\t\t\t\t\t<response_label ident=\"" );
            xmlout.print( (char)('A'+j) );
            xmlout.print( "\">" );
            xmlout.println();
                        
            xmlout.println( "\t\t\t\t\t\t<material>" );

                xmlout.print( "\t\t\t\t\t\t\t<mattext texttype=\"text/html\"><![CDATA[" );
                xmlout.print( getStatement( j ) );
                xmlout.print( "]]></mattext>" );
                xmlout.println();

            xmlout.println( "\t\t\t\t\t\t</material>" );
            xmlout.println( "\t\t\t\t\t</response_label>" );
            }

                xmlout.println( "\t\t\t\t</render_choice>" );
            xmlout.println( "\t\t\t</response_lid>" );
        xmlout.println( "\t\t</presentation>" );

        xmlout.println( "\t\t<resprocessing>" );
            xmlout.println( "\t\t\t<outcomes>" );
                xmlout.println( "\t\t\t\t<decvar varname=\"SCORE\" vartype=\"Integer\" minvalue=\"0\" maxvalue=\"5\" defaultval=\"0\"/>" );
            xmlout.println( "\t\t\t</outcomes>" );
            
            for ( j=0; j<5; j++ )
                {
                // need a response condition for each statement that is true
                // and for all 5 statements if there could be negative marking
                if ( getAnswer( j ) || type!=TYPE_ONE_TRUE )
                    {
                    xmlout.print( "\t\t\t<respcondition title=\"" );
                    xmlout.print( (char)('A'+j) );
                    xmlout.println( "\" continue=\"Yes\">" );
                        xmlout.println( "\t\t\t\t<conditionvar>" );
                            xmlout.print( "\t\t\t\t\t<varequal respident=\"1\">" );
                            xmlout.print( (char)('A'+j) );
                            xmlout.println( "</varequal>" );
                        xmlout.println( "\t\t\t\t</conditionvar>" );

                    if ( getAnswer( j ) )
                        xmlout.println( "\t\t\t\t<setvar action=\"Add\" varname=\"SCORE\">1</setvar>" );
                    else
                        xmlout.println( "\t\t\t\t<setvar action=\"Add\" varname=\"SCORE\">-1</setvar>" );
                    xmlout.println( "\t\t\t</respcondition>" );
                    }
                }

                    xmlout.print( "\t\t\t<respcondition title=\"Other\">" );
                        xmlout.println( "\t\t\t\t<conditionvar>" );
                            xmlout.print( "\t\t\t\t\t<other/>" );
                        xmlout.println( "\t\t\t\t</conditionvar>" );
                        xmlout.println( "\t\t\t\t<displayfeedback linkrefid=\"explanation\" feedbacktype=\"Solution\"/>" );
                    xmlout.println( "\t\t\t</respcondition>" );

                
        xmlout.println( "\t\t</resprocessing>" );
		xmlout.println( "\t\t<itemfeedback ident=\"Explanation\" view=\"Candidate\">" );
		    xmlout.print( "\t\t\t<material><mattext texttype=\"text/html\"><![CDATA[" );
		    xmlout.print( getExplanation() );
            xmlout.print( "]]></mattext></material>" );
            xmlout.println();
		xmlout.println( "\t\t</itemfeedback>" );

        
        
        xmlout.print( "\t</item>" );
        xmlout.println();
	    }

	    
	    
	}