/* ======================================================================
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.rmi.RemoteException;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
import java.sql.Timestamp;

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

import java.sql.*;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
import java.rmi.*;
import java.math.BigDecimal;
import java.math.BigInteger;

import org.bodington.server.*;
import org.bodington.assessment.*;
import org.bodington.server.realm.User;
import org.bodington.server.events.*;
import org.bodington.database.*;
import org.bodington.text.BigString;

public class TextQSessionImpl
	extends org.bodington.server.SingleResourceSession 
	implements TextQSession 
	{

	int max_ordinal=0;
	
	public TextQSessionImpl()
		throws RemoteException
		{
		super();
		}

	public TextQPaper getTextQPaper() 
		throws RemoteException, BuildingServerException
		{
		TextQPaper paper = TextQPaper.findTextQPaper( getResource().getResourceId() );
		return paper;
		}
	
	public void setMaximumMark( int m )
		throws RemoteException, BuildingServerException
		{
		TextQPaper paper = TextQPaper.findTextQPaper( getResource().getResourceId() );
		if ( paper == null )
			throw new BuildingServerException( "Can't find paper." );
		paper.setMaximumMark( m );
		paper.save();
		}
	
	public void setGeneralAssessment( int available, BigDecimal weight, String notes )
		throws RemoteException, BuildingServerException
		{
		TextQPaper paper = TextQPaper.findTextQPaper( getResource().getResourceId() );
		if ( paper == null )
			throw new BuildingServerException( "Can't find paper." );
		paper.setGeneralAvailable( available );
		paper.setGeneralWeight( weight );
		paper.setGeneralNotes( notes );
		paper.save();
		}

	
	public void setDates( Timestamp open, Timestamp deadline, Timestamp penalty )
		throws RemoteException, BuildingServerException
		{
		TextQPaper paper = TextQPaper.findTextQPaper( getResource().getResourceId() );
		if ( paper == null )
			throw new BuildingServerException( "Can't find paper." );
		paper.setDateOpen( open );
		paper.setDateDeadline( deadline );
		paper.setDatePenalty( penalty );
		paper.save();
		}
	
	public Vector getTextQuestionIds()
		throws RemoteException, BuildingServerException
		{
		Vector list = new Vector();
		
		TextQuestion question;
		Enumeration enumeration = TextQuestion.findTextQuestions( "resource_id = " + getResource().getResourceId().toString(), "ordinal" );
		while ( enumeration.hasMoreElements() )
			{
			question = (TextQuestion)enumeration.nextElement();
			list.addElement( question.getTextQuestionId() ); 
			if ( question.getOrdinal() > max_ordinal )
				max_ordinal = question.getOrdinal();
			}
		
		return list;
		}

	
	public Hashtable getTextQuestions()
		throws RemoteException, BuildingServerException
		{
		Hashtable table = new Hashtable();
		
		TextQuestion question;
		Enumeration enumeration = TextQuestion.findTextQuestions( "resource_id = " + getResource().getResourceId().toString() );
		while ( enumeration.hasMoreElements() )
			{
			question = (TextQuestion)enumeration.nextElement();
			table.put( question.getTextQuestionId(), question );
			if ( question.getOrdinal() > max_ordinal )
				max_ordinal = question.getOrdinal();
			}
		
		return table;
		}
	
	public Vector getTextQuestionsInOrder()
		throws RemoteException, BuildingServerException
		{
		Vector list = new Vector();
		
		TextQuestion question;
		Enumeration enumeration = TextQuestion.findTextQuestions( "resource_id = " + getResource().getResourceId().toString(), "ordinal" );
		while ( enumeration.hasMoreElements() )
			{
			question = (TextQuestion)enumeration.nextElement();
			list.addElement( question ); 
			if ( question.getOrdinal() > max_ordinal )
				max_ordinal = question.getOrdinal();
			}
		
		return list;
		}
	
	public Hashtable getStrings()
		throws RemoteException, BuildingServerException
		{
		return null;
		}

	public TextQuestion createQuestion()
		throws RemoteException, BuildingServerException
		{
		TextQuestion question = new TextQuestion();
		
		question.setResourceId( getResource().getResourceId() );
		question.setQuestion( "The question goes here." );
		question.setNotes( "Notes for marker here." );
		question.setExplanation( "Explanation for model answer." );
		question.setOrdinal( max_ordinal+100 );
		question.setAvailable( 1 );
		question.setWeight( new BigDecimal( "1.0" ) );
		question.save();
		
		return question;
		}
	
	public void removeQuestion( PrimaryKey id )
		throws RemoteException, BuildingServerException
		{
		TextQuestion question =  TextQuestion.findTextQuestion( id );
		if ( question == null )
			throw new BuildingServerException( "Question not found." );

		if ( !question.getResourceId().equals( getResource().getResourceId() ) )
			throw new BuildingServerException( "Question doesn't belong in this paper." );

		question.delete();
		}
	
	public void changeQuestion( PrimaryKey id, TextQuestion model )
		throws RemoteException, BuildingServerException
		{
		TextQuestion question =  TextQuestion.findTextQuestion( id );
		if ( question == null )
			throw new BuildingServerException( "Question not found." );

		if ( !question.getResourceId().equals( getResource().getResourceId() ) )
			throw new BuildingServerException( "Question doesn't belong in this MCQ paper." );

		question.setOrdinal( model.getOrdinal() );
		question.setAvailable( model.getAvailable() );
		question.setWeight( model.getWeight() );
		question.save();

		if ( question.getOrdinal() > max_ordinal )
			max_ordinal = question.getOrdinal();
		}
	
	public void changeQuestionText( PrimaryKey qid, PrimaryKey tid, String new_text )
		throws RemoteException, BuildingServerException
		{
		TextQuestion question =  TextQuestion.findTextQuestion( qid );
		if ( question == null )
			throw new BuildingServerException( "Question not found." );
			
		if ( !question.getResourceId().equals( getResource().getResourceId() ) )
			throw new BuildingServerException( "Question doesn't belong in this paper." );

		boolean found = false;
		found = tid.equals( question.getQuestionBigStringId() );
		found = found || tid.equals( question.getExplanationBigStringId() );
		found = found || tid.equals( question.getNotesBigStringId() );

		if ( !found )
			throw new BuildingServerException( "Can't identify qhich part of question to edit." );

		BigString big = BigString.findBigString( tid );
		big.setString( new_text );
		big.save();
		}

	
	public boolean canSubmit() throws RemoteException, BuildingServerException
		{
		return canSubmit( null, new StringBuffer() );
		}
		
	private boolean canSubmit( PrimaryKey uid, StringBuffer message ) throws RemoteException, BuildingServerException
		{
		TextQPaper paper = getTextQPaper();
		TextQResult entry;
		if ( uid==null )
			entry = getTextQResult();
		else
			entry = getTextQResult( uid );
		
		if ( entry == null )
			{
			entry = new TextQResult();
			entry.setStatus( TextQResult.STATUS_UPLOAD_BY_DATE );
			}
			
		java.util.Date now					=new java.util.Date();
		java.util.Date now_minus_24_hours	=new java.util.Date( now.getTime()-(24L*60L*60L*1000L) );
		
		switch ( entry.getStatus() )
			{
			case TextQResult.STATUS_UPLOAD_BY_DATE:
				if ( paper.getDateOpen() !=null && paper.getDateOpen().after( now ) )
					{
					message.append( " the pigeon hole is not yet available for use " );
					return false;
					}
				
				if ( paper.getDateDeadline() == null )
					return true;
				if ( paper.getDateDeadline().after( now ) )
					return true;
				if ( paper.getDatePenalty() == null )
					{
					message.append( " the deadline for uploading to this pigeon hole has passed and uploading files after the deadline is not allowed " );
					return false;
					}
				if ( paper.getDatePenalty().before( now ) )
					{
					message.append( " the penalty period deadline for uploading to this pigeon hole has passed " );
					return false;
					}
				// at this point we know that "now" lies between deadline and penalty
				// allowing them to upload depends on whether and when they previously
				// uploaded a file.

				// if never uploaded it is OK to do so between deadline and penalty date
				if ( entry.getWhenSaved() == null )
					return true;

				// if user uploaded before deadline they can't change it
				if ( entry.getWhenSaved().before( paper.getDateDeadline() ) )
					{
					message.append( " a file was uploaded before the deadline and one can't change a file now the deadline has passed " );
					return false;
					}

				// if user uploaded during the penalty period but more than
				// 24 hours ago they can't change it.
				if ( entry.getWhenSaved().before( now_minus_24_hours ) )
					{
					message.append( " a file was uploaded during the penalty period but more than 24 hours ago " );
					return false;
					}
						
				// if we reached here then the user must have uploaded within the
				// last 24 hours and they can replace their work now and get another
				// 24 hours.
				return true;


			case TextQResult.STATUS_UPLOAD_CHANGEABLE:
				return true;

			case TextQResult.STATUS_UPLOAD_LOCKED:
			case TextQResult.STATUS_UPLOAD_PROCESSED:
			case TextQResult.STATUS_UPLOAD_MARKED:
			case TextQResult.STATUS_UPLOAD_RETURNED:
				message.append( " the manager of this pigeon hole has locked your file " );
				return false;			

			default:
				message.append( " a technical problem has been detected " );
				return false;			
			}
		}
		
	public boolean canSubmit( PrimaryKey user_id ) throws RemoteException, BuildingServerException
		{
		return canSubmit( user_id, new StringBuffer() );
		}
	
	public boolean canSeeMark() throws RemoteException, BuildingServerException
		{
		return canSeeMark( null );
		}
		
	public boolean canSeeMark( PrimaryKey uid ) throws RemoteException, BuildingServerException
		{
		TextQPaper paper = getTextQPaper();
		TextQResult entry;
		if ( uid==null )
			entry = getTextQResult();
		else
			entry = getTextQResult( uid );
		
		if ( entry == null )
			return false;
			
		return entry.getStatus() == TextQResult.STATUS_UPLOAD_RETURNED;
		}
		
	
	public String denySubmitMessage() throws RemoteException, BuildingServerException
		{
		StringBuffer message = new StringBuffer();
		canSubmit( null, message );
		return message.toString();
		}
		
	public String denySubmitMessage( PrimaryKey user_id ) throws RemoteException, BuildingServerException
		{
		StringBuffer message = new StringBuffer();
		canSubmit( user_id, message );
		return message.toString();
		}
	
	public TextQResult record( PrimaryKey[] text_question_ids, String[] responses )
		throws RemoteException, BuildingServerException
		{
		// forces uppload to sub directory named after user id
		// only allows one file per user.  (Fixes file name?)
		User user = (User)BuildingContext.getContext().getUser();
		PrimaryKey uid = user.getUserId();
		TextQResult entry;
		TextQResponse response;
		Hashtable response_table;

		if ( !canSubmit() )
			throw new BuildingServerException( "You are not allowed to submite answers at this time." );
			
		entry = getTextQResult();
		if ( entry == null )
			{
			entry = new TextQResult();
			entry.setResourceId( getResource().getResourceId() );
			entry.setUserId( uid );
			entry.setFeedback( "" );
			entry.setComments( "" );
			}

		entry.setWhenSaved( new Timestamp( System.currentTimeMillis() ) );
		entry.save();
		
		response_table = this.getTextQResponses( entry.getTextQResultId() );
		
		for ( int i=0; i< text_question_ids.length; i++ )
			{
			response = (TextQResponse)response_table.get( text_question_ids[i] );
			if ( response == null )
				{
				response= new TextQResponse();
				response.setTextQResultId( entry.getTextQResultId() );
				response.setTextQuestionId( text_question_ids[i] );
				response.setFeedback( "" );
				}
			response.setResponse( responses[i] );
			response.save();
			}
		
		return entry;
		}
		
	public TextQResult recordStatus( PrimaryKey entry_id, int status )
		throws RemoteException, BuildingServerException
		{
		TextQResult entry = TextQResult.findTextQResult( entry_id );
		if ( entry==null )
			throw new BuildingServerException( "Can't find entry." );
			
		entry.setStatus( status );
		entry.save();
		
		return entry;
		}
		
		
	public TextQResult recordMarks( PrimaryKey entry_id, PrimaryKey[] text_question_ids, int[] marks, String[] feedback, String general_feedback, String comments )
		throws RemoteException, BuildingServerException
		{
		int i;
		TextQPaper tq = getTextQPaper();
		Hashtable responses = getTextQResponses( entry_id );
		TextQResult entry = TextQResult.findTextQResult( entry_id );
		TextQResponse response;
		Hashtable questions = getTextQuestions();
		TextQuestion question;
		
		double total_mark=0.0;
		double total_weight=0.0;
		boolean incomplete=false;
		
		for ( i=0; i<text_question_ids.length; i++ )
			{
			question = (TextQuestion)questions.get( text_question_ids[i] );
			response = (TextQResponse)responses.get( text_question_ids[i] );
			if ( response == null )
				{
				response = new TextQResponse();
				response.setTextQResultId( entry_id );
				response.setTextQuestionId( text_question_ids[i] );
				}
			if ( marks[i]<0 )
				{
				response.setMark( null );
				incomplete=true;
				}
			else
				{
				response.setMark( new Integer( marks[i] ) );
				total_mark += question.getWeight().doubleValue() * (double)marks[i] / (double)question.getAvailable();
				total_weight += question.getWeight().doubleValue();
				}
			
			if ( feedback[i] == null )
				response.setFeedback("");
			else
				response.setFeedback( feedback[i].trim() );
				
			response.save();
			}
		
		if ( tq.getGeneralAvailable()>0 )
			{
			if ( marks[i]<0 )
				{
				entry.setGeneralMark( null );
				incomplete=true;
				}
			else
				{
				entry.setGeneralMark( new Integer( marks[i] ) );
				total_mark += tq.getGeneralWeight().doubleValue() * (double)marks[i] / (double)tq.getGeneralAvailable();
				total_weight += tq.getGeneralWeight().doubleValue();
				}
			}
		
		if ( incomplete )
			entry.setMark( null );
		else
			entry.setMark( new Integer( (int)Math.round((double)tq.getMaximumMark() * total_mark/total_weight )) );
		
		entry.setStatus( TextQResult.STATUS_UPLOAD_PROCESSED );
		entry.setWhenMarked( new Timestamp( System.currentTimeMillis() ) );
		User user = (User)BuildingContext.getContext().getUser();
		entry.setMarkerUserId( user.getUserId() );
		entry.setFeedback( general_feedback );
		entry.setComments( comments );
		entry.save();
		
		return entry;
		}

	
	
	public TextQResult recordAdjustedMark( PrimaryKey entry_id, int adjusted_mark, String general_feedback, String comments )
		throws RemoteException, BuildingServerException
		{
		TextQPaper tq = getTextQPaper();
		TextQResult entry = TextQResult.findTextQResult( entry_id );
		
		entry.setAdjustedMark( new Integer( adjusted_mark ) );
		entry.setFeedback( general_feedback );
		entry.setComments( comments );
		entry.save();
		
		return entry;
		}
	
	public TextQResult getTextQResult()
		throws RemoteException, BuildingServerException
		{
		User user = (User)BuildingContext.getContext().getUser();
		return getTextQResult( user.getUserId() );
		}
		
	public TextQResult getTextQResult( PrimaryKey uid )
		throws RemoteException, BuildingServerException
		{
		TextQResult entry = 
			TextQResult.findTextQResult( 
				"resource_id = " + getResource().getResourceId() + " AND user_id = " + uid );
		
		return entry;
		}
		
	public Hashtable getTextQResults() throws RemoteException, BuildingServerException
		{
		Hashtable table = new Hashtable();
		TextQResult entry;
		
		Enumeration enumeration= 
			TextQResult.findTextQResults( "resource_id = " + getResource().getResourceId() );
		
		while ( enumeration.hasMoreElements() )
			{
			entry = (TextQResult)enumeration.nextElement();
			table.put( entry.getUserId(), entry );
			}

		return table;
		}
		
		
	public Vector getTextQUsers() throws RemoteException, BuildingServerException
		{
		Vector users= new Vector();
		
		Enumeration enumeration = 
			User.findUsers(
				"user_id IN (SELECT user_id FROM textq_results WHERE resource_id = " +
				getResource().getResourceId() +
				")",
				"surname, initials" );
		
		while ( enumeration.hasMoreElements() )
			{
			users.addElement( enumeration.nextElement() );
			}
		
		return users;
		}


	public Hashtable getTextQResponses( PrimaryKey entryid )
		throws RemoteException, BuildingServerException
		{
		Hashtable table = new Hashtable();
		TextQResponse response;
		
		Enumeration enumeration = TextQResponse.findTextQResponses( "textq_result_id = " + entryid );
		while ( enumeration.hasMoreElements() )
			{
			response = (TextQResponse)enumeration.nextElement();
			table.put( response.getTextQuestionId(), response );
			}
		
		return table;
		}
	
	// returns enumeration of results found and if analysis is non null it is filled in with
	// statistical analysis.
	public Enumeration analyse( String where_clause, TextQAnalysis analysis )
		throws RemoteException, BuildingServerException
		{
		return null;
		}
	
	
	}
	
