Index: Dev/branches/jos-branch/server/classes/models/Answer.php
===================================================================
--- Dev/branches/jos-branch/server/classes/models/Answer.php	(revision 266)
+++ Dev/branches/jos-branch/server/classes/models/Answer.php	(revision 268)
@@ -40,5 +40,5 @@
 		if(is_string($this->question))
 		{
-			$result = Question::get(array("code" => $this->question));
+			$result = Question::get(array("uid" => $this->question));
 			if(!isset($result[0]))
 				return false;
@@ -73,5 +73,5 @@
 		//Query the model
 		$results = $model->sparqlQuery($querystring);	
-		//An answer can have multiple values, get all these values for the answer instances found.
+	//An answer can have multiple values, get all these values for the answer instances found.
 		$answers = array();
 		if(!empty($results))
Index: Dev/branches/jos-branch/server/classes/models/AnswerSet.php
===================================================================
--- Dev/branches/jos-branch/server/classes/models/AnswerSet.php	(revision 266)
+++ Dev/branches/jos-branch/server/classes/models/AnswerSet.php	(revision 268)
@@ -80,6 +80,5 @@
 	public function save()
 	{
-		//If evaluation fails, some references are incorrect.
-		//We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
+		//If evaluation fails, some references are incorrect. We shouldn't save in this case.
 		//TODO: Decide how to fix invalid references graciously.
 		if(!$this->evaluate()) 
Index: Dev/branches/jos-branch/server/classes/models/Question.php
===================================================================
--- Dev/branches/jos-branch/server/classes/models/Question.php	(revision 266)
+++ Dev/branches/jos-branch/server/classes/models/Question.php	(revision 268)
@@ -68,5 +68,5 @@
         $model->add(new Statement($resourceQuestion,$predicateUid,$questionUid));
         
-        $questionQCode = new Literal($this->uid);
+        $questionQCode = new Literal($this->code);
         $predicateQCode = new Resource(QCODE);
         $model->add(new Statement($resourceQuestion,$predicateQCode,$questionQCode));
@@ -124,10 +124,9 @@
                 predicates:question_category ?category ;
 				'. ResearchToolObject::createArguments($arguments) . '
-	}';
+			}';
 
 		
         //Create the querystring 
         $results = $model->sparqlQuery($querystring);
-
         
         $questions = array();
@@ -144,5 +143,5 @@
     
     /**
-     * Gets the answers belonging to the given uid.
+     * Gets the preset answers belonging to the given uid.
      * @param type $uid : The uid of the Question in question (haha, pun).
      */
Index: Dev/branches/jos-branch/server/classes/models/SurveyInstance.php
===================================================================
--- Dev/branches/jos-branch/server/classes/models/SurveyInstance.php	(revision 268)
+++ Dev/branches/jos-branch/server/classes/models/SurveyInstance.php	(revision 268)
@@ -0,0 +1,246 @@
+<?php
+require_once 'rdfConstants.php';
+include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
+/**
+ * Description of SurveyInstance
+ *
+ * @author jkraaijeveld
+ */
+class SurveyInstance extends ResearchToolObject{
+    public static $filename = 'data/surveys/surveyinstances.rdf';
+    
+    public $survey;
+    public $starttime;
+    public $endtime;
+    public $open;
+    public $presetanswers;
+    public $answersets;
+
+    /**
+     * Constructor for a SurveyInstance object
+     * @param type $uid: The UID of the survey instance object.
+     * @param type $starttime: The starttime of the survey round.
+     * @param type $endtime: The endtime of the survey round.
+     * @param type $open: A boolean to specify whether the survey is open.
+     * @param type $presetanswers: A list of answers which are preset for everyone doing the survey.
+     * @param type $answersets: A list of answersets.
+     */
+    public function __construct($uid = null, $survey = null, $starttime = null, $endtime = null, $open = false, $presetanswers = null, $answersets = null)
+    {
+        if(!isset($uid))
+        {
+                $uid = md5(uniqid(rand(), true));
+        }
+
+        $this->uid = $uid;
+        $this->survey = $survey;
+        $this->starttime = $starttime;
+        $this->endtime = $endtime;
+        $this->open = $open;
+        $this->presetanswers = $presetanswers;
+        $this->answersets = $answersets;
+    }
+
+    /**
+     * function evaluate()
+     * Evaluates the references in this object
+     */
+    public function evaluate()
+    {
+        if(is_string($this->survey))
+        {
+            $result = Survey::get(array("uid" => $this->survey));
+            if(!isset($result[0]))
+            {
+                return false;
+            }
+            $this->survey = $result[0];
+        }
+        
+        /* Check if all preset answers exist */
+        if(!empty($this->presetanswers) && is_string($this->presetanswers[0]))
+        {
+                $newPresets = array();
+                foreach($this->presetanswers as $answer)
+                {
+                        $result = Answer::get(array("uid" => $answer));
+                        if(!isset($result[0]))
+                                return false;
+                        $newPresets[] = $result[0];
+                }
+                $this->presetanswers = $newPresets;
+        }
+
+        /* Check if all answersets exist */
+        if(!empty($this->answersets) && is_string($this->answersets[0]))
+        {
+                $newAnswerSets = array();
+                foreach($this->answersets as $answerSet)
+                {
+                        $result = AnswerSet::get(array("uid" => $answerSet));
+                        if(!isset($result[0]))
+                        {
+                                return false;
+                        }
+                        $newAnswerSets[] = $result[0];
+                }
+                $this->answersets = $newAnswerSets;
+        }
+        return true;
+    }
+
+    /**
+     * function save()
+     * Saves the current object into the database.
+     */
+    public function save()
+    {
+        //If evaluation fails, some references are incorrect. We shouldn't save in this case.
+        //TODO: Decide how to fix invalid references graciously.
+        if(!$this->evaluate()) 
+                throw new Exception('Evaluation failed.');
+
+        //Ensure the required folder exists.
+        if(!is_dir('data/surveys/'))
+                mkdir('data/surveys/');
+
+        $model = ResearchToolObject::load(SurveyInstance::$filename);
+
+        $resourceSI = new Resource(SURVEYINSTANCE . '/' . $this->uid);
+        //Remove the old value stored with the given id
+        $model->subtract($model->find($resourceSI, null, null));
+
+        //Set the new values
+        $resourceSIType = new Resource(SURVEYINSTANCE);
+        $predicateRType = new Resource(RTYPE);
+        $model->add(new Statement($resourceSI, $predicateRType, $resourceSIType));
+
+        $SIID = new Literal($this->uid);
+        $predicateId = new Resource(UID);
+        $model->add(new Statement($resourceSI, $predicateId, $SIID));
+        
+        $SISurvey = new Literal($this->survey->uid);
+        $predicateSurvey = new Resource(OF_SURVEY);
+        $model->add(new Statement($resourceSI, $predicateSurvey, $SISurvey));
+
+        $SIStartTime = new Literal($this->starttime->getTimestamp());
+        $predicateStartTime = new Resource(STARTTIME);
+        $model->add(new Statement($resourceSI, $predicateStartTime, $SIStartTime));
+
+        $SIEndTime = new Literal($this->endtime->getTimestamp());
+        $predicateEndTime = new Resource(ENDTIME);
+        $model->add(new Statement($resourceSI, $predicateEndTime, $SIEndTime));
+
+        $SIOpen = new Literal($this->open);
+        $predicateOpen = new Resource(OPEN);
+        $model->add(new Statement($resourceSI, $predicateOpen, $SIOpen));
+
+        if(isset($this->presetanswers))
+        {
+                foreach($this->presetanswers as $answer)
+                {
+                        $answerId = new Literal($answer->uid);
+                        $predicateAnswer = new Resource(PRESET_ANSWER);
+                        $model->add(new Statement($resourceSI, $predicateAnswer, $answerId));
+                }
+        }
+
+        if(isset($this->answersets))
+        {
+                foreach($this->answersets as $answer)
+                {
+                        $answerSetId = new Literal($answer->uid);
+                        $predicateAnswerSet = new Resource(HAS_ANSWERSET);
+                        $model->add(new Statement($resourceSI, $predicateAnswerSet, $answerSetId));
+                }
+        }
+        $model->saveAs(SurveyInstance::$filename, 'rdf');
+        return true;
+    }
+   /**
+	* function get($arguments)
+	* Gets the array of SurveyInstance objects belonging tothe arguments supplied.
+	* @param type $arguments: An array containing zero or more of the following keys:
+	* 						  'uid', 'of_survey', 'starttime', 'endtime', 'open', 'preset_answers', 'has_answerset'
+	*/
+	public static function get($arguments)
+	{
+		$model = ResearchToolObject::load(SurveyInstance::$filename);
+
+		//Build the query string
+		$querystring = '
+			PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
+			PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
+			SELECT DISTINCT ?uid, ?of_survey, ?starttime, ?endtime, ?open
+			WHERE
+			{
+				_surveyinstance 	predicates:resource_type	resources:surveyinstance;
+				predicates:uid ?uid ;
+				predicates:of_survey ?of_survey ;
+				predicates:starttime ?starttime ;
+				predicates:endtime ?endtime ;
+				predicates:open ?open ;
+				' . ResearchToolObject::createArguments($arguments) . '
+			}';
+		//Query the model
+		$results = $model->sparqlQuery($querystring);
+		//Create the corresponding SurveyInstance objects
+		$sInstances = array();
+		if(!empty($results))
+		{
+			foreach($results as $result)
+			{
+				$startTime = new DateTime();
+				$startTime->setTimestamp(intval($result['?starttime']->label));
+				$endTime = new DateTime();
+				$endTime->setTimestamp(intval($result['?endtime']->label));
+				$open = false;
+				if ($result['?open'] == 'true')
+					$open = true;
+				$presetAnswers = SurveyInstance::getPresetAnswers($model, $result['?uid']->label);
+				$answerSets = SurveyInstance::getAnswerSets($model, $result['?uid']->label);
+				$sInstances[] = new SurveyInstance($result['?uid']->label, $result['?of_survey']->label, $startTime, $endTime, $open, $presetAnswers, $answerSets);
+			}
+		}
+		return $sInstances;
+	}
+
+	/**
+	 * function getPresetAnswers()
+	 * @param type $model : The RDF model to query against.
+	 * @param type $uid : The UID of the SurveyInstance for which the preset answers should be retrieved.
+	 */
+	private static function getPresetAnswers($model, $uid)
+	{
+		$result = $model->findRegex("[(".$uid.")]", "[(preset_answer)]", null);
+		$iterator = $result->getStatementIterator();
+		$answers = array();
+		while($iterator->hasNext())
+		{
+			$element = $iterator->next();
+			$answers[] = $element->getLabelObject();
+		}
+		return $answers;
+	}
+
+	/**
+	 * function getAnswerSets()
+	 * @param type $model : The RDF model to query against.
+	 * @param type $uid : The UID of the SurveyInstance for which the preset answers should be retrieved.
+	 */
+	private static function getAnswerSets($model, $uid)
+	{
+		$result = $model->findRegex("[(".$uid.")]", "[(has_answerset)]", null);
+		$iterator = $result->getStatementIterator();
+		$answersets = array();
+		while($iterator->hasNext())
+		{
+			$element = $iterator->next();
+			$answersets[] = $element->getLabelObject();
+		}
+		return $answersets;
+	}
+
+}
+
+?>
