Index: Dev/trunk/classes/SurveyAnswerDatabaseInterface.php
===================================================================
--- Dev/trunk/classes/SurveyAnswerDatabaseInterface.php	(revision 31)
+++ Dev/trunk/classes/SurveyAnswerDatabaseInterface.php	(revision 31)
@@ -0,0 +1,31 @@
+<?php
+
+// Survey class as intermediate for storing data from the site to the RDF database 
+require 'rdfConstants.php';
+
+// Include RAP Library to write RDF files
+define("RDFAPI_INCLUDE_DIR", "rdfapi/");
+include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
+
+
+
+class SurveyAnswerDatabaseInterface
+{		
+	protected $surveyAnswerRDFWriter;
+	protected $surveyAnswerRDFReader;
+ 
+
+    public function __construct($surveyID=null)
+    {
+		if($surveyID == null)
+			$surveyUID = md5( uniqid(rand(), true) );
+		else
+			$surveyUID = $surveyID;
+					
+		$this->surveyAnswerRDFWriter = new SurveyAnswerRDFWriter($surveyUID);
+		$this->surveyAnswerRDFReader = new SurveyAnswerRDFReader($surveyUID);
+    }
+	
+	
+
+?>
Index: Dev/trunk/classes/SurveyAnswerRDFReader.php
===================================================================
--- Dev/trunk/classes/SurveyAnswerRDFReader.php	(revision 31)
+++ Dev/trunk/classes/SurveyAnswerRDFReader.php	(revision 31)
@@ -0,0 +1,49 @@
+<?php
+
+class SurveyAnswerRDFReader extends SurveyRDFReader
+{	
+    public function __construct($surveyUID)
+    {
+        parent::__construct($surveyUID);
+	}
+	
+	public function loadSurvey()
+	{
+		parent::loadSurvey();
+		$answerPath = 'surveys/answers_'.$this->surveyUID;
+		$answers = array();
+		
+		if($handle = opendir('$answerPath'))
+		{
+			while (false !== ($file = readdir($handle))) {
+				if(strstr($file, 'answer_'))
+					$answers[] = substr($file,0,strlen($file)-4);
+			}
+		}
+		
+		foreach($answers as $answer)
+		{
+			$this->model->load($answerPath.'/'.$answer);
+		}
+	}
+	
+	public function getAnswer($questionID)
+	{
+	$querystring = '
+			PREFIX 	predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
+			PREFIX	resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
+			SELECT 	?answered 
+			WHERE 	
+			{
+				_question	predicates:resource_type	resources:question ;
+							predicates:uid				"'.$questionID.'"	;
+							predicates:answered 		?answered 	
+			}';
+			
+		$result = $this->model->sparqlQuery($querystring);
+				
+		return $result;
+	}
+}
+
+?>
Index: Dev/trunk/classes/SurveyAnswerRDFWriter.php
===================================================================
--- Dev/trunk/classes/SurveyAnswerRDFWriter.php	(revision 31)
+++ Dev/trunk/classes/SurveyAnswerRDFWriter.php	(revision 31)
@@ -0,0 +1,51 @@
+<?php
+
+class SurveyAnswerRDFWriter extends SurveyRDFWriter
+{
+	protected $userUID;
+
+	public function __construct($surveyUID,$userUID)
+    {
+        parent::__construct($surveyUID);
+		
+		$this->userUID = $userUID;
+    }
+	
+	public function saveSurvey()
+	{	
+		$answerPath = 'surveys/answers_'.$this->surveyUID;
+		if (!is_dir($answerPath))
+			mkdir($answerPath);		
+		$this->model->saveAs($answerPath.'/answer_'.$this->userUID.'.rdf','rdf');
+	}
+	
+	public function setUserData($name)
+	{				
+		$resourceUser = new Resource(USER);
+		
+		$LiteralUserName = new Literal($name);
+		$predicateName = new Resource(NAME);
+		$this->model->add(new Statement($resourceUser,$predicateUniqueID,$literalSurveyID));		
+		
+		$literalUserID = new Literal($this->userUID);
+		$predicateUniqueID = new Resource(UID);
+		$this->model->add(new Statement($resourceUser,$predicateUniqueID,$literalUserID));
+		
+		$predicateRespondent = new Resource(REPONDENT);
+		$this->model->add(new Statement($this->resourceSurvey,$predicateRespondent,$resourceUser));
+	}
+	
+	public function setAnswers($answers)
+	{
+		foreach($answers as $questionID => $answerValue)
+		{					
+			$resourceQuestion = new Resource(QUESTION.'/'.$questionID);
+						
+			$predicateAnswered = new Resource(ANSWERED);	
+			$answer = new Literal($answerValue);
+			$this->model->add(new Statement($resourceQuestion,$predicateAnswered,$answer));
+		}
+	}
+}
+
+?>
Index: Dev/trunk/classes/SurveyDatabaseInterface.php
===================================================================
--- Dev/trunk/classes/SurveyDatabaseInterface.php	(revision 30)
+++ Dev/trunk/classes/SurveyDatabaseInterface.php	(revision 31)
@@ -25,9 +25,4 @@
 		$this->surveyRDFReader = new SurveyRDFReader($surveyUID);
     }
-		
-	public function __destruct() 
-	{
-		echo "Destroyed";
-	}
 	
 	public function setSurveyInfo($survey)
@@ -44,4 +39,8 @@
 			$qDescription = $survey['questionDescription'.$qNumber];
 			$qType = $survey['questionType'.$qNumber];
+			if (isset($survey['questionID'.$qNumber]))
+				$qID = $survey['questionID'.$qNumber];
+			else
+				$qID = md5( uniqid(rand(), true) );
 			
 			$qAnswers = array();
@@ -57,5 +56,5 @@
 			}
 			
-			$this->surveyRDFWriter->addQuestion($qTitle,$qDescription,$qType,$qAnswers);
+			$this->surveyRDFWriter->addQuestion($qTitle,$qDescription,$qType,$qID,$qAnswers);
 			
 			$qNumber++;
@@ -77,10 +76,10 @@
 		for($qNumber = 1;$qNumber<=sizeof($resultSurvey[1][0]);$qNumber++)
 		{
+			$surveyInfo['questionID'.$qNumber] = substr($resultSurvey[1][3][$qNumber-1]['?questionID'],9,strlen($resultSurvey[1][3][$qNumber-1]['?questionID'])-11);
 			$surveyInfo['questionTitle'.$qNumber] = substr($resultSurvey[1][0][$qNumber-1]['?questionTitle'],9,strlen($resultSurvey[1][0][$qNumber-1]['?questionTitle'])-11);
 			$surveyInfo['questionDescription'.$qNumber] = substr($resultSurvey[1][1][$qNumber-1]['?questionDescription'],9,strlen($resultSurvey[1][1][$qNumber-1]['?questionDescription'])-11);
 			$surveyInfo['questionType'.$qNumber] = substr($resultSurvey[1][2][$qNumber-1]['?questionType'],9,strlen($resultSurvey[1][2][$qNumber-1]['?questionType'])-11);
-			$questionID = substr($resultSurvey[1][3][$qNumber-1]['?questionID'],9,strlen($resultSurvey[1][3][$qNumber-1]['?questionID'])-11);
-			
-			$resultAnswers = $this->surveyRDFReader->readSurveyAnswers($questionID);
+						
+			$resultAnswers = $this->surveyRDFReader->readSurveyAnswers($surveyInfo['questionID'.$qNumber]);
 						
 			if ($resultAnswers != null)
@@ -92,8 +91,29 @@
 			}
 		}
-				
+		
 		return $surveyInfo;
 	}
 
+	public function getExistingSurveys()
+	{
+		$surveyIDs = array();
+		$surveys = array();
+	
+		if($handle = opendir('surveys/'))
+		{
+			while (false !== ($file = readdir($handle))) {
+				if(strstr($file, 'survey_'))
+					$surveyIDs[] = substr($file,0,strlen($file)-4);
+			}
+		}
+		
+		foreach($surveyIDs as $surveyID)
+		{
+			$surveyTitle = $this->surveyRDFReader->getSurveyTitleByID($surveyID);
+			$surveys[$surveyID] = substr($surveyTitle[0]['?title'],9,strlen($surveyTitle[0]['?title'])-11);
+		}
+		
+		return $surveys;
+	}
 }
 ?>
Index: Dev/trunk/classes/SurveyRDFReader.php
===================================================================
--- Dev/trunk/classes/SurveyRDFReader.php	(revision 30)
+++ Dev/trunk/classes/SurveyRDFReader.php	(revision 31)
@@ -148,4 +148,28 @@
 		return $result;
 	}
+	
+	public function getSurveyTitleByID($surveyID)
+	{
+		echo $surveyID.'<br/>';
+		
+        // Create empty MemModel
+		$factory = new ModelFactory();
+		$tempmodel= $factory->getDefaultModel();
+		$tempmodel->load('surveys/'.$surveyID.'.rdf');
+		
+		$querystring = '
+			PREFIX 	predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
+			PREFIX	resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
+			SELECT 	?title 
+			WHERE 	
+			{
+				_survey		predicates:resource_type	resources:survey ;
+							predicates:title 			?title 	
+			}';
+			
+		$result = $tempmodel->sparqlQuery($querystring);
+				
+		return $result;
+	}
 }
 
Index: Dev/trunk/classes/SurveyRDFWriter.php
===================================================================
--- Dev/trunk/classes/SurveyRDFWriter.php	(revision 30)
+++ Dev/trunk/classes/SurveyRDFWriter.php	(revision 31)
@@ -44,7 +44,7 @@
 	}
 	
-	public function addQuestion($qTitle,$qDescription,$qType,$qAnswers)
+	public function addQuestion($qTitle,$qDescription,$qType,$qID,$qAnswers)
 	{
-		$questionID = md5( uniqid(rand(), true) );
+		$questionID = $qID;
 		$resourceQuestion = new Resource(QUESTION.'/'.$questionID);
 				
