Index: /Dev/branches/jos-branch/classes/models/Answer.php
===================================================================
--- /Dev/branches/jos-branch/classes/models/Answer.php	(revision 215)
+++ /Dev/branches/jos-branch/classes/models/Answer.php	(revision 215)
@@ -0,0 +1,152 @@
+<?php
+// Survey database interface class as intermediate for storing data from the site to the RDF database 
+require_once 'rdfConstants.php';
+// Include RAP Library to write RDF files
+include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
+
+/**
+ * Description of Answer
+ *
+ * @author jkraaijeveld
+ */
+class Answer extends ResearchToolObject {
+	public static $filename = 'data/answers/answers.rdf';
+
+    public $question;
+	public $values;
+    
+    /**
+     * Constructor for an Answer object
+     * @param type $uid : The uid of the Answer object.
+     * @param type $question : The Question object this Answer answers.
+     * @param type $values : An array of strings, containing the answers.
+     */
+    public function __construct($uid = null, $question = null, $values = null) {
+      if(!isset($uid))
+      {
+            $uid = md5(uniqid(rand(), true));
+      }
+      $this->uid = $uid;
+      $this->question = $question;
+      $this->values = $values;
+	}
+	
+	/**
+	 * function evaluate()
+	 * Evaluates the references and fills in the objects for them instead.
+	 */
+	public function evaluate()
+	{
+		if(is_string($this->question))
+		{
+			$result = Question::get(array("code" => $this->question));
+			if(!isset($result[0]))
+				return false;
+			$this->question = $result[0];
+		}
+		return true;
+	}
+
+
+	/**
+	 * function get()
+	 * @param type $arguments: An array having one or more of the following elements:
+	 * 'uid', 'question', 'values'.
+	 */
+    public static function get($arguments)
+	{
+		$model = ResearchToolObject::load(Answer::$filename);
+		//Create the querystring
+		$querystring = '
+			PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
+			PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
+			SELECT DISTINCT ?uid, ?question_code
+			WHERE
+			{
+				_answer predicates:resource_type resources:answer;
+
+				predicates:uid ?uid ;
+				predicates:question_code ?question_code ;
+				' . ResearchToolObject::createArguments($arguments) . '
+			}';
+
+		//Query the model
+		$results = $model->sparqlQuery($querystring);	
+		//An answer can have multiple values, get all these values for the answer instances found.
+		$answers = array();
+		if(!empty($results))
+		{
+			foreach($results as $result)
+			{
+				$answers[] = new Answer($result['?uid']->label, $result['?question_code']->label, Answer::getValues($model, $result['?uid']->label));
+			}
+		}
+		return $answers;
+	}
+
+	/**
+	 * function getValues()
+	 * @param type $uid : The uid of the Answer for which the values should be gotten.
+	 */
+	private static function getValues($model, $uid)
+	{
+		$result = $model->findRegex("[(".$uid.")]", "[(answered)]", null);
+		$iterator = $result->getStatementIterator();
+		$values = array();
+		while($iterator->hasNext())
+		{
+			$element = $iterator->next();
+			$values[] = $element->getLabelObject();
+		}
+		return $values;
+	} 
+
+    /**
+	 * 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. Instead - let the user know. This function returns false if the evaluation has failed.
+		//TODO: Decide how to fix invalid references graciously.
+		if(!$this->evaluate()) 
+			return false;
+
+		//Ensure the required folder exists.
+		if(!is_dir('data/answers'))
+			mkdir('data/answers');
+		$model = ResearchToolObject::load(Answer::$filename);
+
+        $resourceAnswer = new Resource(ANSWER.'/'.$this->uid);        
+        //Remove the old value stored with the given id
+        $model->subtract($model->find($resourceAnswer, null, null));
+        
+        //Save all the new values
+        $resourceAnswerType = new Resource(ANSWER);
+        $predicateRType = new Resource(RTYPE);
+        $model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType));
+        
+		$answerId = new Literal($this->uid);
+		$predicateId = new Resource(UID);
+		$model->add(new Statement($resourceAnswer, $predicateId, $answerId));
+
+		$questionId = new Literal($this->question->uid);
+		$predicateQId = new Resource(QCODE);
+		$model->add(new Statement($resourceAnswer, $predicateQId, $questionId));
+
+		if(isset($this->values))
+		{
+			foreach($this->values as $value)
+			{
+				$answerValue = new Literal($value);
+				$predicateAnswer = new Resource(ANSWERED);
+				$model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue));
+			}
+		}
+		$model->saveAs(Answer::$filename, 'rdf');
+		return true;
+	}
+}
+
+?>
Index: /Dev/branches/jos-branch/classes/models/AnswerSet.php
===================================================================
--- /Dev/branches/jos-branch/classes/models/AnswerSet.php	(revision 215)
+++ /Dev/branches/jos-branch/classes/models/AnswerSet.php	(revision 215)
@@ -0,0 +1,180 @@
+<?php
+// Survey database interface class as intermediate for storing data from the site to the RDF database 
+require_once 'rdfConstants.php';
+// Include RAP Library to write RDF files
+include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
+
+/**
+ * AnswerSet is a collection of answers corresponding to a Session
+ *
+ */
+class AnswerSet extends ResearchToolObject{
+	public static $filename = 'data/answers/answersets.rdf';
+
+	public $survey;
+	public $respondent;
+	public $answers;
+
+	/**
+	 * Constructor for an AnswerSet object
+	 * @param type $uid
+	 * @param type $survey
+	 * @param type $respondent
+	 * @param type $answers
+	 */
+	public function __construct($uid=null, $survey=null, $respondent=null, $answers=null)
+	{
+		if(!isset($uid))
+		{
+			$uid = md5(uniqid(rand(), true));
+		}
+		$this->uid = $uid;
+		$this->survey = $survey;
+		$this->respondent = $respondent;
+		$this->answers = $answers;
+	}
+
+	/**
+	 * function evaluate()
+	 * evaluates the references of survey, respondent and answers.
+	 */
+	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];
+		}
+		if(is_string($this->respondent))
+		{
+			$result = Respondent::get(array("uid" => $this->respondent));
+			if(!isset($result[0]))
+				return false;
+			$this->respondent = $result[0];
+		}
+		if(!empty($this->answers) && is_string($this->answers[0]))
+		{
+			$newanswers = array();
+			foreach($this->answers as $answer)
+			{
+				$result = Answer::get(array("uid" => $answer));
+				if(!isset($result[0]))
+					return false;
+				$newanswers[] = $result[0];
+			}
+			$this->answers = $newanswers;
+		}
+		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. Instead - let the user know. This function returns false if the evaluation has failed.
+		//TODO: Decide how to fix invalid references graciously.
+		if(!$this->evaluate()) 
+			return false;
+		//Ensure the required folder exists.
+		if(!is_dir('data/answers/'))
+			mkdir('data/answers/');
+
+		$model = ResearchToolObject::load(AnswerSet::$filename);
+
+
+		$resourceAnswerSet = new Resource(ANSWERSET . '/' . $this->uid);
+		//Remove the old value stored with the given id
+		$model->subtract($model->find($resourceAnswerSet, null, null));
+
+		//Set the new values
+		$resourceAnswerSetType = new Resource(ANSWERSET);
+		$predicateRType = new Resource(RTYPE);
+		$model->add(new Statement($resourceAnswerSet, $predicateRType, $resourceAnswerSetType));
+
+		$answerSetId = new Literal($this->uid);
+		$predicateId = new Resource(UID);
+		$model->add(new Statement($resourceAnswerSet, $predicateId, $answerSetId));
+
+		$surveyId = new Literal($this->survey->uid);
+		$predicateSurvey = new Resource(FOR_SURVEY);
+		$model->add(new Statement($resourceAnswerSet, $predicateSurvey, $surveyId));
+
+		$respondentId = new Literal($this->respondent->uid);
+		$predicateRespondent = new Resource(BY_RESPONDENT);
+		$model->add(new Statement($resourceAnswerSet, $predicateRespondent, $respondentId));
+		
+		if(isset($this->answers))
+		{
+			foreach($this->answers as $answer)
+			{
+				$answerId = new Literal($answer->uid);
+				$predicateAnswer = new Resource(GIVEN_ANSWER);
+				$model->add(new Statement($resourceAnswerSet, $predicateAnswer, $answerId));
+			}
+		}
+		$model->saveAs(AnswerSet::$filename, 'rdf');
+		return true;
+	}
+
+	/**
+	 * function get()
+	 * @param type $arguments : An array having one ore more of the following elements:
+	 * 'uid', 'survey', 'respondent', 'answers'.
+	 */
+	public static function get($arguments)
+	{
+		$model = ResearchToolObject::load(AnswerSet::$filename);
+
+		//Build the query string
+		$querystring = '
+			PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
+			PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
+			SELECT DISTINCT ?uid, ?for_survey, ?by_respondent
+			WHERE
+			{
+				_answerset	predicates:resource_type	resources:answerset ;
+				predicates:uid ?uid ;
+				predicates:for_survey ?for_survey ;
+				predicates:by_respondent ?by_respondent ;
+				' . ResearchToolObject::createArguments($arguments) . '
+			}';
+		//Query the model
+		$results = $model->sparqlQuery($querystring);
+		$answerSets = array();
+		if(!empty($results))
+		{
+			foreach($results as $result)
+			{
+				$survey = $result['?for_survey']->label;
+				$respondent = $result['?by_respondent']->label;
+				$answers = 	AnswerSet::getAnswers($model, $result['?uid']->label);
+				$answerSets[] = new AnswerSet($result['?uid']->label, $survey, $respondent, $answers);
+			}
+		}
+		return $answerSets;
+	}
+
+	/**
+	 * function getAnswers()
+	 * @param type $uid : the uid for which the answers should be retrieved.
+	 */
+	private static function getAnswers($model, $uid)
+	{
+		$result = $model->findRegex("[(".$uid.")]", "[(given_answer)]", null);
+		$iterator = $result->getStatementIterator();
+		$answers = array();
+		while($iterator->hasNext())
+		{
+			$element = $iterator->next();
+			$answers[] = $element->getLabelObject();
+		}
+		return $answers;
+	}
+}
+
Index: /Dev/branches/jos-branch/classes/models/Question.php
===================================================================
--- /Dev/branches/jos-branch/classes/models/Question.php	(revision 215)
+++ /Dev/branches/jos-branch/classes/models/Question.php	(revision 215)
@@ -0,0 +1,158 @@
+<?php
+// Survey database interface class as intermediate for storing data from the site to the RDF database 
+require_once 'rdfConstants.php';
+// Include RAP Library to write RDF files
+include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
+
+/**
+ * Description of Question
+ *
+ * @author jkraaijeveld
+ */
+class Question extends ResearchToolObject{    
+	private static $filename = 'data/questions/questions.rdf';
+
+    public $title;
+    public $type;
+    public $description;
+    public $category;
+    public $answers; // format answers['#']
+    
+    /**
+     * Constructor for a Question. $uid equals the corresponding code.
+     * @param type $uid
+     * @param type $title
+     * @param type $type
+     * @param type $description
+     * @param type $category
+     * @param type $answers 
+     */
+    public function __construct($uid, $title = null, $type = null, $description = null, $category = null, $answers = null)
+    {
+		if(!isset($uid))
+		{
+			$uid = md5(uniqid(rand(), true));
+		}
+        $this->uid = $uid;
+        $this->title = $title;
+        $this->type = $type;
+        $this->description = $description;
+        $this->category =  $category;
+        $this->answers = $answers;
+	}
+
+	/**
+	 * function save()
+	 * Saves the current object into the database.
+	 */
+	public function save()
+	{
+        //Ensure the required folder exists
+        if (!is_dir('data/questions/'))
+            mkdir('data/questions/');	
+
+		$model = ResearchToolObject::load(Question::$filename);
+		
+		$resourceQuestion = new Resource(QUESTION.'/'.$this->uid);
+		//Remove the old value stored with the given id
+        $model->subtract($model->find($resourceQuestion, null, null));
+        
+        //Save all the new values
+        $resourceQuestionType = new Resource(QUESTION);
+        $predicateRType = new Resource(RTYPE);
+        $model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType));
+        
+        $questionQCode = new Literal($this->uid);
+        $predicateQCode = new Resource(QCODE);
+        $model->add(new Statement($resourceQuestion,$predicateQCode,$questionQCode));
+        
+        $questionTitle = new Literal($this->title);
+        $predicateTitle = new Resource(TITLE);		
+        $model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle));	
+
+        $questionDescription = new Literal($this->description);
+        $predicateDescription = new Resource(DESCRIPTION);
+        $model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription));		
+
+        $resourceQuestionType = new Literal($this->type);
+        $predicateQType = new Resource(QTYPE);
+        $model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType));
+        
+        $questionQCategory = new Literal($this->category);
+        $predicateQCategory = new Resource(QCATEGORY);
+        $model->add(new Statement($resourceQuestion,$predicateQCategory,$questionQCategory));
+
+        if(isset($this->answers))
+        {
+            foreach($this->answers as $answer)
+            {		
+                $answerValue = new Literal($answer);
+                $predicateAnswer = new Resource(HAS_ANSWER);	
+                $model->add(new Statement($resourceQuestion,$predicateAnswer,$answerValue));
+            }
+        }
+		$model->saveAs(Question::$filename, 'rdf');
+		return true;
+	}
+
+    /**
+     * Get the questions corresponding to the arguments.
+	 * @param type $arguments : An array having one ore more of the following elements:
+	 * 'code', 'title', 'type', 'description', 'category', 'definedanswers'.
+     */
+    public static function get($arguments)
+    {
+		$model = ResearchToolObject::load(Question::$filename);
+           
+        $querystring = '
+            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
+            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
+            SELECT DISTINCT ?uid, ?title, ?type, ?description, ?category
+            WHERE 	
+            {
+                _question	predicates:resource_type	resources:question ;                
+                predicates:question_code ?uid ;
+                predicates:title ?title ;
+                predicates:question_type ?type ;
+                predicates:description ?description ;
+                predicates:question_category ?category ;
+				'. ResearchToolObject::createArguments($arguments) . '
+	}';
+
+		
+        //Create the querystring 
+        $results = $model->sparqlQuery($querystring);
+
+        
+        $questions = array();
+        if(!empty($results))
+        {
+            foreach($results as $result)
+            {
+				$answers = Question::getAnswers($model, $result['?uid']->label);
+                $questions[] = new Question($result['?uid']->label, $result['?title']->label, $result['?type']->label, $result['?description']->label, $result['?category']->label, $answers);
+            }
+        }
+        return $questions;
+    }
+    
+    /**
+     * Gets the answers belonging to the given uid.
+     * @param type $uid : The uid of the Question in question (haha, pun).
+     */
+    private static function getAnswers($model, $uid)
+    {
+		$result = $model->findRegex("[(".$uid.")]", "[(has_answer)]", null);
+		$iterator = $result->getStatementIterator();
+		$answers = array();
+		while($iterator->hasNext())
+		{
+			$element = $iterator->next();
+			$answers[] = $element->getLabelObject();
+		}
+		return $answers;
+	}
+
+}
+
+?>
Index: /Dev/branches/jos-branch/classes/models/Survey.php
===================================================================
--- /Dev/branches/jos-branch/classes/models/Survey.php	(revision 215)
+++ /Dev/branches/jos-branch/classes/models/Survey.php	(revision 215)
@@ -0,0 +1,174 @@
+<?php
+
+/**
+ * Description of Survey
+ *
+ * @author jkraaijeveld
+ */
+class Survey extends ResearchToolObject{
+	private static $filename = 'data/surveys/surveys.rdf';
+
+    public $title;
+    public $description;
+    public $creator;
+    public $questions;
+    
+    public function __construct($uid = null, $title = null, $description = null, $creator = null, $questions = null) {
+        if(!isset($uid))
+        {
+            $uid = md5(uniqid(rand(), true));
+        }
+        $this->uid = $uid;
+        $this->title = $title;
+		$this->description = $description;
+		$this->creator = $creator;
+        $this->questions = $questions;
+	}
+
+	/**
+	 * function evaluate()
+	 * Evaluates the references
+	 */
+	public function evaluate()
+	{
+		if(is_string($this->creator))
+		{
+			$result = User::get(array("uid" => $this->creator));	
+			if(!isset($result[0]))
+				return false;
+			$this->creator = $result[0];
+		}
+
+		if(!empty($this->questions) && is_string($this->questions[0]))
+		{
+			$newQuestions = array();
+			foreach($this->questions as $question)
+			{
+				$result = Question::get(array("code" => $question));
+				if(!isset($result[0]))
+					return false;
+				$newQuestions[] = $result[0];			
+			}
+			$this->questions = $newQuestions;
+		}
+		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. Instead - let the user know. This function returns false if the evaluation has failed.
+		//TODO: Decide how to fix invalid references graciously.
+		if(!$this->evaluate()) 
+			return false;
+
+		if(!is_dir('data/surveys/'))
+			mkdir('data/surveys/');
+
+		$model = ResearchToolObject::load(Survey::$filename);
+
+        $resourceSurvey = new Resource(SURVEY.'/'.$this->uid);
+        //Remove the old value stored with the given id
+        $model->subtract($model->find($resourceSurvey, null, null));
+        
+        $resourceSurveyType = new Resource(SURVEY);
+        $predicateRType = new Resource(RTYPE);
+        $model->add(new Statement($resourceSurvey,$predicateRType,$resourceSurveyType));
+
+        $predicateUniqueID = new Resource(UID);
+        $literalSurveyID = new Literal($this->uid);
+        $model->add(new Statement($resourceSurvey,$predicateUniqueID,$literalSurveyID));
+
+        $predicateTitle = new Resource(TITLE);		
+        $surveyTitle = new Literal($this->title);
+        $model->add(new Statement($resourceSurvey,$predicateTitle,$surveyTitle));		
+
+        $predicateDescription = new Resource(DESCRIPTION);
+        $surveyDescription = new Literal($this->description);
+        $model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription));
+        
+		$predicateCreator = new Resource(CREATOR);
+		$surveyCreator = new Literal($this->creator->uid);
+		$model->add(new Statement($resourceSurvey, $predicateCreator, $surveyCreator));
+
+        if(isset($this->questions))
+        {
+            foreach($this->questions as $question)
+            {
+                $questionUid = new Literal($question->uid);
+                $predicateQuid = new Resource(HAS_QUESTION);
+                $model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid));
+            }
+		}
+		$model->saveAs(Survey::$filename, 'rdf');
+		return true;
+	}
+
+    /**
+     * function get($arguments)
+     * Gets the array of Survey objects belonging to arguments supplied.
+     * @param type $arguments : An array containing zero or more of the following keys:
+     *                          'uid', 'title', 'description', 'questions'
+     * Note: questions has to be an array of question IDs (codes) for this filter to work.
+     */
+    public static function get($arguments)
+    {
+		$model = ResearchToolObject::load(Survey::$filename);
+        
+        //Build the query string
+        $querystring = '
+            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
+            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
+            SELECT DISTINCT ?uid, ?title, ?description, ?creator
+            WHERE
+            {
+                _survey     predicates:resource_type    resources:survey ;
+                predicates:uid ?uid ;
+                predicates:title ?title ;
+				predicates:description ?description ;
+				predicates:creator ?creator ;
+				' . ResearchToolObject::createArguments($arguments) . '
+            }';
+        
+        //Query the model
+        $results = $model->sparqlQuery($querystring);
+        
+        //Create the corresponding Survey objects
+        $surveys = array();
+        if(!empty($results))
+        {
+            foreach($results as $result)
+            {
+				$questions = Survey::getQuestions($model, $result['?uid']->label);
+				$creator = $result['?creator']->label;
+                $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $creator, $questions);
+            }
+        }
+        return $surveys;
+    }
+    
+    /**
+     * Gets the questions corresponding to the survey
+     * @param type $uid : The uid for which the questions should be gotten.
+     */
+    private static function getQuestions($model, $uid)
+    {
+		$result = $model->findRegex("[(".$uid.")]", "[(has_question)]", null);
+		$iterator = $result->getStatementIterator();
+		$questions = array();
+		while($iterator->hasNext())
+		{
+			$element = $iterator->next();
+			$questions[] = $element->getLabelObject();
+		}
+		return $questions;
+    }
+
+
+}
+
+?>
