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

import org.bodington.database.PrimaryKey;
import org.bodington.server.BuildingServerException;
import org.bodington.server.SingleResourceSession;

/**
 * Class that encapsulates the analysis of the responses to a questionnaire.
 * @see QuestionnaireResult
 * @see QuestionnaireResponse
 * @author Jon Maber
 * @author Alexis O'Connor
 */
public class QuestionnaireAnalysis
	{
	private Hashtable item_counts;                 
    private Hashtable comments;
	
	
	/**
	 * Create a new instance.
	 */
	public QuestionnaireAnalysis()
		{
		item_counts = new Hashtable();
		comments = new Hashtable();
		}
		
		
    /* TODO: do something about this empty method.
     * Current thinking would be to implement it properly (it is called). 
     * 1) Modify the analyse() method in QuestionnaireSession to be:
     * QuestionnaireAnalysis analysis(String whereSQL, QuestionnaireAnalysis)
     * the analysis object accepted and returned would be the same object.
     * 2) Add a method such as List getQuestionnaireResults() to this class(?).
     * = Alternatively =
     * Just remove this method!
     */
    /**
     * Add the questionnaire result. This object will collate data from the
     * arguments in order to provide analytical information via other methods.
     * @param result the questionnaire result.
     * @see #getComments(PrimaryKey, int)
     * @see #getItemCounts(PrimaryKey, int)
     */
	public void addQuestionnaireResult( QuestionnaireResult result )
		{
		}
	
	/**
     * Add the questionnaire question response. This object will
     * collate data from the arguments in order to provide analytical
     * information via other methods.
	 * @param question the questionnaire question (unused).
	 * @param result the questionnaire result (unused).
	 * @param response the questionnaire question response.
     * @see #getComments(PrimaryKey, int)
     * @see #getItemCounts(PrimaryKey, int)
	 */
	public void addQuestionnaireResponse( QuestionnaireQuestion question, QuestionnaireResult result, QuestionnaireResponse response )
		throws BuildingServerException
		{
		String comment;
		int[] items = (int[])item_counts.get( response.getQuestionnaireQuestionId() );
		if ( items == null )
			{
			items = new int[10];
			item_counts.put( response.getQuestionnaireQuestionId(), items );
			}
			
		Vector[] c = (Vector[])comments.get( response.getQuestionnaireQuestionId() );
		if ( c == null )
			{
			c = new Vector[11];
			for ( int j=0; j<11; j++ )
				c[j] = new Vector();
			comments.put( response.getQuestionnaireQuestionId(), c );
			}

		if ( response.getItem()>=0 && response.getItem()<10 )
			items[response.getItem()]++;
			
		if ( response.getComment() != null )
			{
			comment = response.getComment().trim();
			if ( comment.length()>0 )
				{
				if ( response.getItem()>=0 && response.getItem()<10 )
					c[response.getItem()].addElement( comment );
				else
					c[10].addElement( comment );
				}
			}
		}

	// more methods to retrieve the results of the analysis...


	/**
     * Get the number of times a multiple-choice item was selected for the
     * specified question. The <code>statement</code> parameter refers to
     * which of the multiple-choice items you wish to obtain the total for.
     * @param question_id the ID of the question of interest.
     * @param statement the index of the statement of interest.
     * @return the number of times a multiple-choice item was selected for the
     *         specified question.
     * @see QuestionnaireQuestion#getQuestionnaireQuestionId()
     */
	public int getItemCounts( PrimaryKey question_id, int statement )
		{
		int[] items = (int[])item_counts.get( question_id );
		if ( items == null )
			return 0;
		return items[statement];
		}

    /**
     * Get the list of comments corresponding to a question and multiple-choice
     * selection. The <code>statement</code> parameter refers to which of the
     * multiple-choice items you are interested in. The maximum number of
     * possible multiple-choice choices is <code>10</code> (indexed from
     * <code>0</code> to <code>9</code>). If there was no corresponding
     * multiple-choice selection, the comment is available at index
     * <code>10</code>.
     * @param question_id the ID of the question of interest.
     * @param statement the index of the statement of interest.
     * @return the list of comments corresponding to a question and
     *         multiple-choice selection.
     * @see QuestionnaireQuestion#getQuestionnaireQuestionId()
     */
	public Vector getComments( PrimaryKey question_id, int statement )
		{
		Vector[] c = (Vector[])comments.get( question_id );
		if ( c == null )
			return new Vector();
		return c[statement];
		}
	}
