/* ======================================================================
   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.io.OutputStream;
import java.util.List;
import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;

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

/**
 * Interface for objects that represent a users session with a questionnaire.
 * @see org.bodington.assessment.Questionnaire
 * @author Jon Maber
 * @author Alexis O'Connor
 */
public interface QuestionnaireSession extends BuildingSession 
	{
    /**
     * Get the questionnaire that is associated with this session. 
     * @return the questionnaire that is associated with this session.
     * @throws BuildingServerException if there is some problem.
     */
    public Questionnaire getQuestionnaire() throws BuildingServerException;
    
	/**
     * Get the list of questions. This method returns a list of the
     * {@link PrimaryKey}s of the questions. These are ordered on the basis of
     * the value of the {@link QuestionnaireQuestion#getOrdinal()} property.
     * @return the list of the questions.
     * @throws BuildingServerException if there is some problem.
     * @see QuestionnaireQuestion
     * @see QuestionnaireQuestion#getQuestionnaireQuestionId()
     */
	public List getQuestionnaireQuestionIds() throws BuildingServerException;
	
	/**
     * Get a map of the questions. This method returns a map, where each pair
     * pertains to a single question. The key is the value of
     * {@link QuestionnaireQuestion#getQuestionnaireQuestionId()}, the value is
     * the question itself. The ordering is unspecified.
     * @return a map of the questions.
     * @throws BuildingServerException if there is some problem.
     * @see QuestionnaireQuestion
     * @see QuestionnaireQuestion#getQuestionnaireQuestionId()
     */
	public Map getQuestionnaireQuestions() throws BuildingServerException;
	
	/**
     * Get a list of the questions in order. This list contains instances of
     * {@link QuestionnaireQuestion}. These are ordered on the basis of the
     * value of the {@link QuestionnaireQuestion#getOrdinal()} property.
     * @return a list of the questions in order.
     * @throws BuildingServerException
     */
	public List getQuestionnaireQuestionsInOrder() throws BuildingServerException;
    
    /**
     * Create a new question.
     * @return the newly created question.
     * @see #createQuestion(int)
     */
    public QuestionnaireQuestion createQuestion() throws BuildingServerException;

    /**
     * Create a new question. The <code>ordinal</code> parameter allows you to
     * specify at which point in the list the new question should go, relative
     * to the existing ones.
     * @param ordinal the ordinal for the new question.
     * @return the newly created question.
     * @see QuestionnaireQuestion#getOrdinal()
     * @see Questionnaire#getMaxOrdinal()
     */
    public QuestionnaireQuestion createQuestion(int ordinal) throws BuildingServerException;
	
	/**
     * Remove the specified question. The specified parameter corresponds to the
     * value of {@link QuestionnaireQuestion#getQuestionnaireQuestionId()}.
     * @param questionID the ID of the question to be removed.
     * @throws BuildingServerException
     */
	public void removeQuestion( PrimaryKey questionID ) throws BuildingServerException;

	/**
     * Change the specified question. The <code>questionID</code> parameter
     * corresponds to the value of
     * {@link QuestionnaireQuestion#getQuestionnaireQuestionId()}.
     * @param questionID the ID of the question to be changed.
     * @param question an object containing the new values for the specified
     *        question.
     * @throws BuildingServerException
     */
	public void changeQuestion( PrimaryKey questionID, QuestionnaireQuestion question ) throws BuildingServerException;
	
	/**
     * Change the specified question. The <code>questionID</code> parameter
     * corresponds to the value of
     * {@link QuestionnaireQuestion#getQuestionnaireQuestionId()}. The text
     * that can be changed includes the introduction and the text of the
     * statements / responses.
     * @param questionID the ID of the question whose text is to be changed.
     * @param textID the ID of the text to be changed.
     * @param newText the value of the new text.
     * @throws BuildingServerException
     */
	public void changeQuestionText( PrimaryKey questionID, PrimaryKey textID, String newText ) throws BuildingServerException;
	
	/**
     * Record a users response to a questionnaire. Across the parameters
     * <code>questionIDs</code>, <code>selections</code> and
     * <code>comments</code> a given index respectively captures (for a user)
     * a specified question, the selection(s) they made and any free text
     * response they gave.
     * @param newResult indicates whether this should be stored in a new
     *        {@link QuestionnaireResult} instance.
     * @param questionIDs an array of the IDs of the
     *        {@link QuestionnaireQuestion} instances.
     * @param selections a list of the items that were selected. The element at
     *        each index is an instance of <code>byte[]</code>.
     * @param comments an array of free text comments.
     * @return ID of the corresponding {@link QuestionnaireResult} instance.
     * @see QuestionnaireQuestion
     * @see QuestionnaireQuestion#isMultipleResponse()
     * @see QuestionnaireResponse#getSelection()
     */
	public PrimaryKey record( boolean newResult, PrimaryKey[] questionIDs, List selections, String[] comments ) throws BuildingServerException;
	
	/**
     * Get the questionnaire result associated with the current user.
	 * @return the questionnaire result associated with the current user.
     * @see #getQuestionnaireResult(PrimaryKey)
	 */
	public QuestionnaireResult getQuestionnaireResult() throws BuildingServerException;

    /**
     * Get the questionnaire result associated with the specified user. This is
     * specifically an associated result object saved to persistence storage. If
     * the user has not yet recorded a set of answers, then this method returns
     * <code>null</code>. For questionnaires that support multiple results
     * and where the user has recorded more than one set of answers, this method
     * returns the most recently recorded result.
     * @param userID the ID of the specified user.
     * @return the questionnaire result associated with the specified user.
     * @see Questionnaire#isMultipleResults()
     * @see org.bodington.server.realm.User
     */
	public QuestionnaireResult getQuestionnaireResult( PrimaryKey userID ) throws BuildingServerException;

    /**
     * Get a list of the questionnaire results associated with the current user.
     * @return a list of the questionnaire results associated with the current
     *         user.
     * @see QuestionnaireResult
     */
    public List getQuestionnaireResults()
    throws BuildingServerException;
    
    /**
     * Get all the results associated with the questionnaire. This is all the
     * results associated with the implicit questionnaire in the form of a map.
     * The actual form of the map is as follows:
     * <p>
     * <code>
     *   {@link java.util.Map} &lt;{@link User}, 
     *   {@link java.util.List} &lt;{@link QuestionnaireResult}&gt;&gt; 
     * </code>
     * </p>
     * @return all the results associated with the questionnaire.
     * @see #getQuestionnaire()
     * @see QuestionnaireResult
     */
    public Map getAllQuestionnaireResults()
    throws BuildingServerException;

	/**
     * Analyse the results associated with the implicit questionnaire. This
     * method will create a list of questionnaire results based on the value of
     * the <code>whereSQL</code> parameter. If this parameter is
     * <code>null</code>, then the search criteria is that of the
     * <code>resource_id</code> field of the implicit questionnaire. If the
     * <code>analysis</code> parameter is non-null, then this will be
     * populated with questionnaire results and responses corresponding to the
     * returned list.
     * @param whereSQL a string representing the <code>WHERE</code> clause of
     *        some SQL representing the criteria upon which to select the
     *        results.
     * @param analysis an object that encapsulates the analysis of a
     *        questionnaire response.
     * @return a list of questionnaire results associated with the implicit
     *         questionnaire.
     * @see org.bodington.database.PersistentObject#findPersistentObjects(String,
     *      String)
     * @see QuestionnaireResult
     * @see QuestionnaireResponse
     * @see QuestionnaireAnalysis#addQuestionnaireResult(QuestionnaireResult)
     * @see QuestionnaireAnalysis#addQuestionnaireResponse(QuestionnaireQuestion,
     *      QuestionnaireResult, QuestionnaireResponse)
     */
	public List analyse( String whereSQL, QuestionnaireAnalysis analysis ) throws BuildingServerException;
	
	/**
     * Export the implicit questionnaire as an XML file. This file is in IMS QTI
     * format. The name that this method gives the file should have the 
     * extension <code>.xml</code> and take the <code>name</code> of the
     * questionnaire.
     * @param includeResults specify whether or not to include questionnaire
     *        results.
     * @param includeResponses specify whether or not to include questionnaire
     *        responses.
	 * @return the name of the XML file that this method exported to.
     * @see QuestionnaireResult
     * @see QuestionnaireResponse
     */
	public void exportXML( OutputStream stream ) throws BuildingServerException;

    /**
     * Import the specified XML file. The file is expected to be in IMS QTI
     * format.
	 * @param fileName the file path to the XML file to be imported.
     * @see org.bodington.assessment.ims.QuestionnaireImsQtiHandler
	 */
	public void importXML( String fileName ) throws BuildingServerException;
    
    /**
     * Indicates whether or not it is acceptable to display the non-respondents.
     * This method will use a <I>yet-to-be-determined</I> algorithm to
     * determine whether or not the non-respondents can be displayed, without
     * eroding the principle of anonymity. This will be based on factors such as
     * total number of possible respondents, number of actual respondents, etc.
     * @return <code>true</code> if it is possible to show the
     *         non-respondents, otherwise <code>false</code>.
     */
    public boolean isCanShowNonRespondents() throws BuildingServerException;
    
    /**
     * Get a list of non-respondents. Non-respondents are users who have the
     * requisite access to submit a response to the questionnaire, but have not
     * done so. The elements in this list are instances of the class
     * {@link User}.<br />
     * (Users in special groups that have been granted access are not included in the listing.)
     * @return a list of people who have not responded to the questionnaire.
     * @see #isCanShowNonRespondents()
     * @see #getSpecialGroupsWithPermission()
     */
    public List getNonRespondents() throws BuildingServerException;
    
    /**
     * Get a list of special groups who are able to submit a response
     * to the questionnaire. Users in these groups are not enumerated by {@link #getNonRespondents()}
     * @return a list of special groups of users that can submit a response to the questionnaire. 
     * @throws BuildingServerException
     * @see #getNonRespondents()
     */    
    public List getSpecialGroupsWithPermission() throws BuildingServerException;
    
    /**
     * Get a list of all the users associated with the questionnaire. This is a
     * list of all the users associated with the implicit questionnaire who have
     * recorded a response. The elements in this list are instances of the class
     * {@link User}.
     * @return a list of all the users associated with the questionnaire.
     */
    public List getQuestionnaireUsers() throws BuildingServerException;
    
    /**
     * Indicates whether or not the questionnaire is available. This indicates
     * whether or not it is still possible to submit a result. For example, if a
     * deadline has been set, whether or not this has now passed.
     * @return <code>true</code> if the questionnaire is available, otherwise
     *         <code>false</code>.
     * @see Questionnaire#getDeadline()
     */
    public boolean isAvailable() throws BuildingServerException;

    /**
     * Indicates whether or not the questionnaire is reviewable. For example, if
     * a deadline has been set, the results are not reviewable until this 
     * deadline has passed.
     * @return <code>true</code> if the questionnaire is reviewable, otherwise
     *         <code>false</code>.
     * @see #isAvailable()        
     * @see Questionnaire#getDeadline()
     */
    public boolean isReviewable() throws BuildingServerException;
    
    /**
     * Indicates whether or not the deadline can be modified.
     * @return <code>true</code> if the deadline can be modified, otherwise
     *         <code>false</code>.
     * @throws BuildingServerException
     */
    public boolean getCanModifyDeadline() throws BuildingServerException;
}
	
