/* ======================================================================
   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.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_SUBMIT_MARKS               =9;
    public static final int EVENT_MAX                        =9;


	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",
        " submitted marks"
		};
	
	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( ")" );
			}
		}

	}
	
