/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

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

package org.bodington.assessment;

import java.math.BigDecimal;
import java.util.*;
import java.io.*;


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

public class McqAnalysis
	{
	public static final int SUMMARY						=10;
	public static final int SUMMARY_AND_QUESTION		=20;
	public static final int SUMMARY_QUESTION_AND_ITEM	=30;
	
	
	int depth;
	boolean html_output=false;
	
	
	int n;
	double sum, sumsq, min=100.0, max=0.0;
	Hashtable question_analyses;


	public McqAnalysis( int depth, boolean html )
		{
		this.depth = depth;
		html_output = html;
		question_analyses = new Hashtable();
		}
	
	public int getDepth()
		{
		return depth;
		}
			
	public void addMcqResult( McqResult result )
		{
		double m = result.mark().doubleValue();
		n++;
		sum += m;
		sumsq += m*m;
		min = min<m?min:m;
		max = max>m?max:m;
		}
	
	
	public void addMcqResponse( McqQuestion question, McqResult result, McqResponse response )
		throws BuildingServerException
		{
		McqQuestionAnalysis q_analysis = (McqQuestionAnalysis)question_analyses.get( question.getMcqQuestionId() );
		if ( q_analysis == null )
			{
			q_analysis = new McqQuestionAnalysis( question.getMcqQuestionId(), depth>=SUMMARY_QUESTION_AND_ITEM, html_output );
			question_analyses.put( question.getMcqQuestionId(), q_analysis );
			}

		response.mark( question );
		q_analysis.addMcqResponse( result, response );
		}

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


	public void dumpAnalysis( PrintWriter out )
		{
		if ( depth >= SUMMARY )
			{
			double mean = sum/(double)n;
			double s= Math.sqrt( (sumsq - (sum*sum/n) )/(n-1) );
			
			String sep = html_output?"</B></TD><TD><B>":"\t";

			if ( html_output )
				out.print( "<TABLE BORDER=1><TR><TD><B>" );
				
			out.print( "Count" );
			out.print( sep );
			out.print( "Minimum" );
			out.print( sep );
			out.print( "Maximum" );
			out.print( sep );
			out.print( "Mean" );
			out.print( sep );
			out.print( "Standard Deviation" );
			
			if ( html_output )
				out.print( "</B></TD></TR>\n<TR><TD>" );
			else
				out.println();
				
			sep = html_output?"</TD><TD>":"\t";

			out.println( n );
			out.print( sep );
			out.println( min );
			out.print( sep );
			out.println( max );
			out.print( sep );
			out.println( mean );
			out.print( sep );
			out.println( s );
			
			if ( html_output )
				out.print( "</TD></TR></TABLE>" );

			}

		out.flush();
		}
	
	public McqQuestionAnalysis getMcqQuestionAnalysis( PrimaryKey mcq_question_id )
		{
		return (McqQuestionAnalysis)question_analyses.get( mcq_question_id );
		}
	}
