/* ======================================================================
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.server.events;

import org.bodington.database.*;
import org.bodington.sqldatabase.*;
import org.bodington.server.*;
import org.bodington.server.realm.User;
import org.bodington.server.realm.Permission;
import org.bodington.server.resources.Resource;
import org.bodington.server.BuildingServerException;

import java.math.BigDecimal;
import java.io.PrintWriter;



public class AssessmentEvent extends org.bodington.server.events.Event
	{
	PrimaryKey assessment_event_id;
	Integer result_id;
	BigDecimal mark;
	
	public static final int EVENT_MIN					     =1;
	public static final int EVENT_SUBMISSION                 =1;
	public static final int EVENT_RECORD_ASSESSMENT          =2;
	public static final int EVENT_VIEW_ASSESSMENT            =3;
	public static final int EVENT_RECORD_ADJUSTED_ASSESSMENT =4;
	public static final int EVENT_RELEASE_ASSESSMENT         =5;
	public static final int EVENT_ADD_ASSESSMENT_ITEM        =6;
	public static final int EVENT_EDIT_ASSESSMENT_ITEM       =7;
	public static final int EVENT_DELETE_ASSESSMENT_ITEM     =8;
	public static final int EVENT_MAX					     =8;


	static final String[] event_message_a = 
		{
		"",
		" submitted answers to questions",
		" saved marks and/or feedback",
		" viewed the mark and/or feedback",
		" ratified the mark and/or feedback",
		" released a mark and/or feedback",
		" added a question or assessment item",
		" edited a question or assessment item",
		" deleted a question or assessment item"
		};
	
	static final String[] event_message_b = 
		{
		"",
		".",
		" against someones work.",
		" for his/her work.",
		" against someones work.",
		" to someones.",
		".",
		".",
		"."
		};

	static final String[] event_message_c = 
		{
		"",
		" against the name of ",
		" against the work of ",
		" for the work of ",
		" against the work of ",
		" to ",
		" for ",
		" for ",
		" for "
		};



	
	public AssessmentEvent()
		{
		super();
		result_id=null;
		mark=null;
		}
	
	public AssessmentEvent( int event_code, 
							PrimaryKey resource_id, 
							PrimaryKey active_user_id, 
							PrimaryKey passive_user_id, 
							Integer result_id, 
							BigDecimal mark )
		{
		super();
		
		this.event_code = event_code;
		if ( event_code >= EVENT_MIN && event_code <= EVENT_MAX ) 
			importance = Event.IMPORTANCE_MANAGEMENT_MIN;
			
		if ( event_code == EVENT_SUBMISSION || event_code == EVENT_RELEASE_ASSESSMENT )
			importance = Event.IMPORTANCE_USER_MIN;
			
		
		this.resource_id=resource_id;
		this.active_user_id=active_user_id;
		this.passive_user_id=passive_user_id;
		
		this.result_id = result_id;
		this.mark = mark;
		}
	
    /**
     * Restrict access on submissions and assessment events to the manager
     * or to the active user on submissions and the passive user on assessments.
     */
	public boolean checkPermission()
		{
		if ( event_code != EVENT_SUBMISSION && event_code != EVENT_RELEASE_ASSESSMENT )
			return true;

		try
			{
			Resource resource = getResource();
			if ( resource.checkPermission( Permission.MANAGE ) )
				return true;
			}
		catch ( Exception ex )
			{
			//drop through and check as if the user hasn't got management rights.
			}
		
		PrimaryKey event_user_id;
		if ( event_code == EVENT_SUBMISSION  )
			event_user_id = getActiveUserId();
		else
			event_user_id = getPassiveUserId();

		User user = (User)BuildingContext.getContext().getUser();
		
		if ( user==null || event_user_id==null )
			return false;
		
		return user.getUserId().equals( event_user_id );
		}
	
	
    public PrimaryKey getPrimaryKey()
	    {
        return getAssessmentEventId();
    	}

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

	public PrimaryKey getAssessmentEventId()
		{
		return assessment_event_id;
		}
		
    public void setAssessmentEventId(PrimaryKey key)
    	{
    	assessment_event_id = key;
    	setEventId( key );
    	setUnsaved();
    	}


	public void setResultId( Integer i )
		{
		result_id = i;
    	setUnsaved();
		}
		
	public Integer getResultId()
		{
		return result_id;
		}
		

	public void setMark( BigDecimal m )
		{
		mark = m;
    	setUnsaved();
		}
		
	public BigDecimal getMark()
		{
		return mark;
		}
		

		

	public String messageTitle()
		{
		if ( event_code>=EVENT_MIN && event_code<=EVENT_MIN )
			return "Someone " + event_message_a[event_code] + ".";

		return "Some generated an assessment event.";
		}

		
	public void printProperties( PrintWriter writer, boolean html )
		{
		super.printProperties( writer, html );
		
		if ( html )
			writer.println( "<PRE>" );
		if ( event_code>=EVENT_ADD_ASSESSMENT_ITEM && event_code<=EVENT_DELETE_ASSESSMENT_ITEM )
			{writer.print( "Question Id   : " );   writer.println( result_id==null?"no result id":result_id.toString() );}
		else
			{writer.print( "Result Id     : " );   writer.println( result_id==null?"no result id":result_id.toString() );}
		writer.print( "Mark          : " );   writer.println( mark==null?"no mark":mark.toString() );
		if ( html )
			writer.println( "</PRE>" );
		}

	public void printMessage( PrintWriter writer, boolean html )
		{
		User active = null;
		User passive = null;
		try
			{
			active = getActiveUser();
			passive = getPassiveUser();
			}
		catch ( BuildingServerException bsex )
			{
			// do nothing
			}

		if ( active==null )
			writer.print( "Someone" );
		else
			writer.print( active.getName() );
			
		if ( event_code>=EVENT_MIN && event_code<=EVENT_MAX )
			writer.print( event_message_a[event_code] );
		else
			{
			writer.print( " generated an assessment event." );
			return;
			}

		if ( passive==null )
			writer.print( event_message_b[event_code] );
		else
			{
			writer.print( event_message_c[event_code] );
			writer.print( passive.getName() );
			writer.print( "." );
			}

		if ( event_code>=EVENT_ADD_ASSESSMENT_ITEM && event_code<=EVENT_DELETE_ASSESSMENT_ITEM )
			{
			if ( result_id!=null )
				{
				writer.print( " (QuestionId=" );
				writer.print( result_id.toString() );
				writer.print( ".)" );
				}
			}
		 
		if ( mark!=null )
			{
			writer.print( " (Mark=" );
			writer.print( mark.toString() );
			writer.print( ")" );
			}
		}

	}
	
