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

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

/**
 * Class that encapsulates a questionnaire question. The ordering of questions
 * within a questionnaire is determined by the value of its <code>ordinal</code>
 * property (see {@link #getOrdinal()}). Therefore the position of a question
 * in the list can be altered by changing the value of this property. A
 * <i>standard</i> question is one which consists of
 * {@link #STANDARD_STATEMENT_COUNT} responses (see
 * {@link #isStandardQuestion()}). A question may allow for a free-text
 * response (see {@link #isCanComment()}). Currently upto 1 of 10 responses
 * (labelled A - J) are possible. If the text for one of these is blank (just
 * consists of whitespace) it is regarded as invalid and thus is not returned as
 * one of the valid indices by the method {@link #getValidStatementIndices()}.
 * @see Questionnaire
 * @author Jon Maber
 * @author Alexis O'Connor
 */
public class QuestionnaireQuestion extends org.bodington.sqldatabase.SqlPersistentObject
	{
	private static final byte Q_FLAG_CAN_COMMENT	= 1;
	private static final byte Q_FLAG_STANDARD		= 2;
    public static final int MAX_STATEMENT_COUNT = 10;
    public static final int STANDARD_STATEMENT_COUNT = 5;
	
	private PrimaryKey primary_key;
    private PrimaryKey questionnaire_id;
	private PrimaryKey introduction_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 PrimaryKey statement_f_big_string_id;
	private PrimaryKey statement_g_big_string_id;
	private PrimaryKey statement_h_big_string_id;
	private PrimaryKey statement_i_big_string_id;
	private PrimaryKey statement_j_big_string_id;
	private int ordinal = 0;
	private byte flags = 0;
	private int statements= -1;
    
    public QuestionnaireQuestion()
        {
        // NOTE: the persistence layer relies on the introduction & statements
        // as being non-null strings, hence the initialization here.
        try
            {
            setIntroduction("");
            clearStatements();
            }
        catch ( BuildingServerException e )
            {
            return;
            }
        }
	
   	public static QuestionnaireQuestion findQuestionnaireQuestion( PrimaryKey key )
		throws BuildingServerException
		{
		return (QuestionnaireQuestion)findPersistentObject( key, QuestionnaireQuestion.class.getName() );
		}
    
	public static Enumeration findQuestionnaireQuestions( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, QuestionnaireQuestion.class.getName() );
	    }
    
	public static Enumeration findQuestionnaireQuestions( String where, String order )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, order, QuestionnaireQuestion.class.getName() );
	    }

    public PrimaryKey getPrimaryKey()
    	{
        return primary_key;
    	}

    public void setPrimaryKey(PrimaryKey key)
    	{
        primary_key = key;
        setUnsaved();
    	}

	public PrimaryKey getQuestionnaireQuestionId()
		{
		return getPrimaryKey();
		}
		
	public void setQuestionnaireQuestionId( PrimaryKey id )
		{
		setPrimaryKey(id);
		}

    /**
     * Kept for backwards compatibility. This class is <em>not</em> an
     * instance of {@link org.bodington.server.resources.Resource} therefore it
     * should not have this method. Unfortunately, it's use is embedded in the
     * persistence layer so it is maintained for the time being. In practice,
     * the value of this property is synonymous with
     * {@link #getQuestionnaireId()}, which is why you should call that method
     * directly instead.
     * @return the ID.
     * @deprecated Use {@link #getQuestionnaireId()} instead.
     */
	public PrimaryKey getResourceId()
		{
		return getQuestionnaireId();
		}
		
    /**
     * Kept for backwards compatibility. This class is <em>not</em> an
     * instance of {@link org.bodington.server.resources.Resource} therefore it
     * should not have this method. Unfortunately, it's use is embedded in the
     * persistence layer so it is maintained for the time being.
     * @param id the ID to set.
     * @deprecated Use {@link #setQuestionnaireId(PrimaryKey)} instead.
     * @see #getResourceId()
     */
	public void setResourceId( PrimaryKey id )
		{
        setQuestionnaireId(id);
		}
    
    /**
     * Get the ID of the containing questionnaire. This is the questionnaire
     * of which this question is a part of.
     * <p>
     * NOTE: for legacy reasons, the corresponding name for this field in the
     * persistence layer is currently <code>resource_id</code>.
     * @return the ID of the containing questionnaire.
     */
    public PrimaryKey getQuestionnaireId()
        {
        return questionnaire_id;
        }
    
    /**
     * Set the ID of the containing questionnaire.
     * @param questionnaireID the ID of the containing questionnaire.
     * @see #getQuestionnaireId()
     */
    public void setQuestionnaireId(PrimaryKey questionnaireID)
        {
        questionnaire_id = questionnaireID;
        setUnsaved();
        }

	public PrimaryKey getIntroductionBigStringId()
		{
		return introduction_big_string_id;
		}

	public void setIntroductionBigStringId( PrimaryKey id )
		{
		introduction_big_string_id = id;
		setUnsaved();
		}
		
	/**
     * Get the introductory text of the question. This is the text that is used
     * to gain a response from the user, most typically an actual question.
	 * @return the introductory text of the question.
	 */
	public String getIntroduction()
		throws BuildingServerException
		{
		if ( introduction_big_string_id == null )
			return null;
		BigString bstr = BigString.findBigString( introduction_big_string_id );
		if ( bstr == null )
			return null;
		return bstr.getString();
		}
		
	/**
     * Set the introductory text.
	 * @param s the introductory text.
     * @see #getIntroduction()
	 */
	public void setIntroduction( String s )
		throws BuildingServerException
		{
		BigString bstr=null;
		if ( introduction_big_string_id != null )
			bstr = BigString.findBigString( introduction_big_string_id );
		if ( bstr == null )
			{
			bstr = new BigString();
			bstr.setString( s );
			bstr.save();
			introduction_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 getStatementFBigStringId()
		{
		return statement_f_big_string_id;
		}

	public void setStatementFBigStringId( PrimaryKey id )
		{
		statement_f_big_string_id = id;
		setUnsaved();
		}
		
	public PrimaryKey getStatementGBigStringId()
		{
		return statement_g_big_string_id;
		}

	public void setStatementGBigStringId( PrimaryKey id )
		{
		statement_g_big_string_id = id;
		setUnsaved();
		}
		
	public PrimaryKey getStatementHBigStringId()
		{
		return statement_h_big_string_id;
		}

	public void setStatementHBigStringId( PrimaryKey id )
		{
		statement_h_big_string_id = id;
		setUnsaved();
		}
		
	public PrimaryKey getStatementIBigStringId()
		{
		return statement_i_big_string_id;
		}

	public void setStatementIBigStringId( PrimaryKey id )
		{
		statement_i_big_string_id = id;
		setUnsaved();
		}
		
	public PrimaryKey getStatementJBigStringId()
		{
		return statement_j_big_string_id;
		}

	public void setStatementJBigStringId( PrimaryKey id )
		{
		statement_j_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();
			case 5:
				return getStatementFBigStringId();
			case 6:
				return getStatementGBigStringId();
			case 7:
				return getStatementHBigStringId();
			case 8:
				return getStatementIBigStringId();
			case 9:
				return getStatementJBigStringId();
			}
			
		return null;
		}
		
	/**
     * Get the standard statement text for a given index. It could be imagined
     * that in future this method would be i18n aware.
	 * @param index the specified index.
	 * @return the standard statement text for a given index. 
     * @see #isStandardQuestion()
	 */
	public String getStandardStatement(int index)
        {
        // TODO: this is not exactly i18n is it!?!
        switch ( index )
            {
            case 0:
                return "I strongly agree.";
            case 1:
                return "I agree.";
            case 2:
                return "I partly agree and partly disagree.";
            case 3:
                return "I disagree.";
            case 4:
                return "I strongly disagree.";
            }
        return null;
        }
		
	/**
     * Get the statement text for the specified index. As this class maintains
     * 10 strings indexed [A - J] it is possible that all or none of these may
     * be classed as <i>invalid</i>. An invalid statement is one that just 
     * consists or whitespace. In order to acquire the valid statement indices,
     * call the method {@link #getValidStatementIndices()} first.
	 * @param n the specified index.
	 * @return the statement text for the specified index.
     * @see #getValidStatementIndices()
	 */
	public String getStatement( int n )
		throws BuildingServerException
		{
		if ( n<0 || n>MAX_STATEMENT_COUNT - 1 )
			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;
			case 5:
				big_string_id = getStatementFBigStringId();
				break;
			case 6:
				big_string_id = getStatementGBigStringId();
				break;
			case 7:
				big_string_id = getStatementHBigStringId();
				break;
			case 8:
				big_string_id = getStatementIBigStringId();
				break;
			case 9:
				big_string_id = getStatementJBigStringId();
				break;
			}
			
		if ( big_string_id == null )
			return null;
			
		BigString bs = BigString.findBigString( big_string_id );
        
        // NOTE: this is required for backwards compatibility:
        if ( isStandardQuestion() && bs.toString().length() == 0 )
            return getStandardStatement( n );
		
		return bs.toString();
		}
    
    /**
     * Clear all the statements. This sets the text of all the statements to be
     * blank strings. This renders them <i>invalid</i>.
     * @see #getValidStatementIndices()
     */
    public void clearStatements() throws BuildingServerException
        {
        // TODO: this may well be a sub-optimal implementation.
        for ( int i = 0; i < MAX_STATEMENT_COUNT; i++ )
            setStatement( i, "" );
        }
		
	public void setStatement( int n, String s )
		throws BuildingServerException
		{
		if ( n<0 || n>MAX_STATEMENT_COUNT - 1 )
			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;
			case 5:
				big_string_id = getStatementFBigStringId();
				break;
			case 6:
				big_string_id = getStatementGBigStringId();
				break;
			case 7:
				big_string_id = getStatementHBigStringId();
				break;
			case 8:
				big_string_id = getStatementIBigStringId();
				break;
			case 9:
				big_string_id = getStatementJBigStringId();
				break;
			}
			
		BigString bs;
		
		if ( big_string_id == null )
			{
			bs = new BigString();
			bs.setString( s );
			bs.save();
			big_string_id = bs.getBigStringId();
			switch ( n )
				{
				case 0:
					setStatementABigStringId( big_string_id );
					break;
				case 1:
					setStatementBBigStringId( big_string_id );
					break;
				case 2:
					setStatementCBigStringId( big_string_id );
					break;
				case 3:
					setStatementDBigStringId( big_string_id );
					break;
				case 4:
					setStatementEBigStringId( big_string_id );
					break;
				case 5:
					setStatementFBigStringId( big_string_id );
					break;
				case 6:
					setStatementGBigStringId( big_string_id );
					break;
				case 7:
					setStatementHBigStringId( big_string_id );
					break;
				case 8:
					setStatementIBigStringId( big_string_id );
					break;
				case 9:
					setStatementJBigStringId( big_string_id );
					break;
				}
			setUnsaved();
			return;
			}

		bs = BigString.findBigString( big_string_id );
		
		if ( bs==null )
			return;

		bs.setString( s );
		bs.save();
		}
	
	/**
     * Get the ordinal of this question. The aggregating parent container for a
     * questionnaire question, is an instance of {@link Questionnaire}.
     * Questions within a questionnaire are ordered on the basis of their
     * ordinal. This allows the ordering of a question to be altered by changing
     * its ordinal.
     * @return the ordinal of this question.
     * @see Questionnaire
     */
	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();
		}

	/**
     * Indicates whether this question allows for a free text response. Whether
     * or not a question allows for a free text response is mutually exclusive
     * of whether or not there are any statements.
     * @return <code>true</code> if this question allows for a free text
     *         response, otherwise <code>false</code>.
     */
	public boolean isCanComment()
		{
		return (flags & Q_FLAG_CAN_COMMENT) != 0;
		}
    
	/**
     * Set whether or not this question allows for a free text comment.
	 * @param b the value for this property to take.
     * @see #isCanComment()
	 */
	public void setCanComment( boolean b )
		{
		if ( isCanComment() == b )
			return;
		setFlags( (byte)(getFlags() ^ Q_FLAG_CAN_COMMENT) );
		}

	/**
     * Indicates whether or not this question is a standard one. A standard
     * question has {@link #STANDARD_STATEMENT_COUNT} number of statements. In
     * turn, the statement at each index corresponds with the statement returned
     * by {@link #getStandardStatement(int)}.
     * @return <code>true</code> if this question is a standard one, otherwise
     *         <code>false</code>.
     * @see #STANDARD_STATEMENT_COUNT
     */
	public boolean isStandardQuestion()
		{
		return (flags & Q_FLAG_STANDARD) != 0;
		}
    
	public void setStandardQuestion( boolean b )
		throws BuildingServerException
		{
		if ( isStandardQuestion() == b )
			return;


		setFlags( (byte)(getFlags() ^ Q_FLAG_STANDARD) );

		
		if ( isStandardQuestion() )
            {
            clearStatements();
			for ( int i=0; i<STANDARD_STATEMENT_COUNT; i++ )
				setStatement( i, getStandardStatement(i) );
            }
		}

	/**
     * Get the number of valid statements. An <i>invalid</i> statement is one
     * that only contains whitespace. Therefore, a valid statement is regarded
     * as one that actually contains content.
     * @return the number of valid statements.
     * @throws BuildingServerException
     * @see #getValidStatementIndices()
     */
	public int getStatementCount()
		throws BuildingServerException
		{
		if ( !isUnsaved() && statements>=0 )
			return statements;
			
		statements=getValidStatementIndices().length;
		return statements;
		}
    
    /**
     * Get the indices of the valid statements. A valid statement is regarded
     * as one that actually contains content. This method returns the indices
     * of the statements in order.
     * @return the indices of the valid statements.
     * @throws BuildingServerException
     * @see #getStatementCount()
     */
    public int[] getValidStatementIndices() throws BuildingServerException
        {
        int[] indices = new int[0];
        if ( isStandardQuestion() )
            {
            indices = new int[STANDARD_STATEMENT_COUNT];
            for ( int i = 0; i < indices.length; i++ )
                indices[i] = i;
            }
        else
            {
            Vector v = new Vector();
            for ( int i = 0; i < MAX_STATEMENT_COUNT; i++ )
                {
                String s = getStatement( i );
                if ( s != null && s.trim().length() > 0 )
                    v.add( new Integer( i ) );
                }
            indices = new int[v.size()];
            for ( int i = 0; i < indices.length; i++ )
                indices[i] = ((Integer)v.elementAt( i )).intValue();
            }

        return indices;
        }
    
    /**
     * Indicates whether or not this question has a multiple-choice element.
     * @return <code>true</code> if this question has a multiple-choice
     *         element, otherwise <code>false</code>.
     * @see #getStatementCount()
     */
    public boolean isMultipleChoice() throws BuildingServerException
        { 
        return getStatementCount() > 0; 
        }

	public void setUnsaved()
		{
		statements=-1;
		super.setUnsaved();
		}
		
	public void exportXml( PrintWriter xml_out )
	    throws IOException, BuildingServerException
	    {
	    exportXml( xml_out, true );
	    }
	    
	public void exportXml( PrintWriter xml_out, boolean allow_composite )
	    throws IOException, BuildingServerException
	    {
	    if ( isCanComment() )
	        {
	        if ( getStatementCount() > 0 )
	            {
	            if ( allow_composite )
	                exportXmlComposite( xml_out, "pk_" + getPrimaryKey() + "_composite"  );
	            else
	                {
	                exportXmlChoice( xml_out, "pk_" + getPrimaryKey() + "_choice", true  );
	                exportXmlComment( xml_out, "pk_" + getPrimaryKey() + "_comment", true  );
	                }
	            }
	        else
	            {
                exportXmlComment( xml_out, "pk_" + getPrimaryKey() + "_comment", false );
                }
	        }
	    else
	        {
	        if ( getStatementCount() > 0 )
                exportXmlChoice( xml_out, "pk_" + getPrimaryKey() + "_choice", false );
            else
                exportXmlExplanation( xml_out, "pk_" + getPrimaryKey() + "_explanation"  );
	        }
	    }

	private void exportXmlExplanation( PrintWriter xmlout, String ident )
	    throws IOException, BuildingServerException
	    {
        xmlout.print( "\t<item ident=\"" );
        xmlout.print( ident );
        xmlout.print( "\">" );
        xmlout.println();

        xmlout.println( "\t\t<itemmetadata>" );
        xmlout.println( "\t\t\t<qmd_computerscored>No</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>Explanation</qmd_itemtype>" );
        xmlout.println( "\t\t\t<qmd_maximumscore>0</qmd_maximumscore>" );
        xmlout.println( "\t\t\t<qmd_renderingtype>None</qmd_renderingtype>" );
        xmlout.println( "\t\t\t<qmd_responsetype>None</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( getIntroduction() );
                xmlout.print( "]]></mattext>" );
                xmlout.println();
            xmlout.println( "\t\t\t</material>" );

            xmlout.println( "<response_extension/>" );
        xmlout.println( "\t\t</presentation>" );
        xmlout.print( "\t</item>" );
        xmlout.println();
        }
	    
	private void exportXmlComment( PrintWriter xmlout, String ident, boolean broken_composite )
	    throws IOException, BuildingServerException
	    {
        xmlout.print( "\t<item ident=\"" );
        xmlout.print( ident );
        xmlout.print( "\">" );
        xmlout.println();

        xmlout.println( "\t\t<itemmetadata>" );
        xmlout.println( "\t\t\t<qmd_computerscored>No</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>String</qmd_itemtype>" );
        xmlout.println( "\t\t\t<qmd_maximumscore>0</qmd_maximumscore>" );
        xmlout.println( "\t\t\t<qmd_renderingtype>String</qmd_renderingtype>" );
        xmlout.println( "\t\t\t<qmd_responsetype>Single</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[" );
                if ( broken_composite )
                    {
                    xmlout.println();
                    xmlout.println( "<P>This is the comment box for the previous question.</P>" );
                    }
                else
                    xmlout.print( getIntroduction() );
                xmlout.print( "]]></mattext>" );
                xmlout.println();
            xmlout.println( "\t\t\t</material>" );

            xmlout.println( "<response_str ident=\"1\" rcardinality=\"Single\">" );
            xmlout.println( "<render_fib fibtype=\"String\" prompt=\"Box\" rows=\"5\" columns=\"30\" />" );
            xmlout.println( "</response_str>" );

        xmlout.println( "\t\t</presentation>" );
        xmlout.print( "\t</item>" );
        xmlout.println();
        }
	    
	private void exportXmlChoice( PrintWriter xmlout, String ident, boolean broken_composite )
	    throws IOException, BuildingServerException
	    {
	    int i, j, n;

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

        xmlout.println( "\t\t<itemmetadata>" );
        xmlout.println( "\t\t\t<qmd_computerscored>No</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>0</qmd_maximumscore>" );
        xmlout.println( "\t\t\t<qmd_renderingtype>Choice</qmd_renderingtype>" );
        xmlout.println( "\t\t\t<qmd_responsetype>Single</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( getIntroduction() );
                //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.println( "\t\t\t<response_lid ident=\"1\" rcardinality=\"Single\">" );

                xmlout.println( "\t\t\t\t<render_choice shuffle=\"No\">" );
                        
        n=getStatementCount();
        for ( j=0; j<n; 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.print( "\t</item>" );
        xmlout.println();
        }
	    	    
	private void exportXmlComposite( PrintWriter xmlout, String ident )
	    throws IOException, BuildingServerException
	    {
	    int i, j, n;
	        
        xmlout.print( "\t<item ident=\"" );
        xmlout.print( ident );
        xmlout.print( "\">" );
        xmlout.println();

        xmlout.println( "\t\t<itemmetadata>" );
        xmlout.println( "\t\t\t<qmd_computerscored>No</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>Composite</qmd_itemtype>" );
        xmlout.println( "\t\t\t<qmd_maximumscore>0</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( "\t\t<presentation>" );
            xmlout.println( "\t\t\t<material>" );
                xmlout.print( "\t\t\t\t<mattext texttype=\"text/html\"><![CDATA[" );
                xmlout.print( getIntroduction() );
                xmlout.print( "]]></mattext>" );
                xmlout.println();
            xmlout.println( "\t\t\t</material>" );
            xmlout.println( "\t\t\t<response_lid ident=\"1\" rcardinality=\"Single\">" );

                xmlout.println( "\t\t\t\t<render_choice shuffle=\"No\">" );
                        
        n=getStatementCount();
        for ( j=0; j<n; 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( "<response_str ident=\"2\" rcardinality=\"Single\">" );
            xmlout.println( "<render_fib fibtype=\"String\" prompt=\"Box\" rows=\"5\" columns=\"30\" />" );
            xmlout.println( "</response_str>" );

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