Index: Dev/trunk/classes/DatabaseInterface.php
===================================================================
--- Dev/trunk/classes/DatabaseInterface.php	(revision 131)
+++ Dev/trunk/classes/DatabaseInterface.php	(revision 139)
@@ -20,4 +20,5 @@
     private $questionConnector;
     private $userConnector;
+    private $surveyConnector;
     
     /**
@@ -29,4 +30,5 @@
         $this->questionConnector = new QuestionConnector();
         $this->userConnector = new UserConnector();
+        $this->surveyConnector = new SurveyConnector();
     }
     
@@ -50,4 +52,6 @@
                 return $this->userConnector->get($arguments);
                 break;
+            case "survey":
+                return $this->surveyConnector->get($arguments);
         }
     }
@@ -70,4 +74,6 @@
                 $this->userConnector->set($rToolObject);
                 break;
+            case "Survey":
+                $this->surveyConnector->set($rToolObject);
         }
     }
Index: Dev/trunk/classes/Survey.php
===================================================================
--- Dev/trunk/classes/Survey.php	(revision 139)
+++ Dev/trunk/classes/Survey.php	(revision 139)
@@ -0,0 +1,26 @@
+<?php
+
+/**
+ * Description of Survey
+ *
+ * @author jkraaijeveld
+ */
+class Survey extends ResearchToolObject{
+    public $title;
+    public $description;
+    public $creator;
+    public $questions;
+    
+    public function __construct($uid = null, $title = null, $description = null, $questions = null) {
+        if(!isset($uid))
+        {
+            $uid = md5(uniqid(rand(), true));
+        }
+        $this->uid = $uid;
+        $this->title = $title;
+        $this->description = $description;
+        $this->questions = $questions;
+    }
+}
+
+?>
Index: Dev/trunk/classes/SurveyConnector.php
===================================================================
--- Dev/trunk/classes/SurveyConnector.php	(revision 139)
+++ Dev/trunk/classes/SurveyConnector.php	(revision 139)
@@ -0,0 +1,180 @@
+<?php
+
+/**
+ * Description of SurveyConnector
+ *
+ * @author jkraaijeveld
+ */
+class SurveyConnector implements IConnector{
+    protected $model;
+    protected $fileName = 'data/surveys/surveys.rdf';
+    protected $db;
+
+    /**
+     * Constructor for ApplicationConnector.
+     */
+    public function __construct()
+    {
+        //Ensure the required folder for this connector exists
+        if (!is_dir('data/surveys/'))
+            mkdir('data/surveys/');	
+    }
+    
+    /**
+     * function load()
+     * Loads the file into the standard MemModel.
+     */
+    public function load() {
+        //Get the Memory Model from the ModelFactory
+        $this->model = ModelFactory::getDefaultModel();
+        
+        //Ensure the required file exists before loading
+        if(file_exists($this->fileName))
+            $this->model->load($this->fileName);
+    }
+    
+    /**
+     * function save()
+     * Saves the MemModel into the given file.
+     */
+    public function save() {
+        $this->model->saveAs($this->fileName,'rdf'); 
+    }
+    
+    /**
+     * 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 function get($arguments)
+    {
+        $this->load();
+        
+        $keys = array_keys($arguments);
+        //Set default values for arguments
+        $uid = "?uid"; $title = "?title"; $description = "?description"; $questions = "";
+        if(in_array("uid", $keys))
+            $uid = '\''.$arguments["uid"].'\'';
+        if(in_array("title", $keys))
+            $title = '\''.$arguments["title"].'\'';
+        if(in_array("description", $keys))
+            $description = "\"".$arguments["description"]."\"";
+        if(in_array("questions", $keys))
+        {
+            //Append the questions string to filter for questions properly
+            foreach ($arguments["questions"] as $questionUid)
+            {
+                $questions = $questions . 'predicates:has_question \'' . $questionUid . '\' ';
+            }
+        }
+        
+        //Build the query string
+        $querystring = '
+            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
+            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
+            SELECT DISTINCT ?uid, ?title, ?description
+            WHERE
+            {
+                _survey     predicates:resource_type    resources:survey ;
+                predicates:uid ?uid ;
+                predicates:title ?title ;
+                predicates:description ?description ;
+                predicates:uid ' . $uid . ' 
+                predicates:title ' . $title . '
+                predicates:description ' . $description . '
+                ' . $questions . '
+            }';
+        
+        //Query the model
+        $results = $this->model->sparqlQuery($querystring);
+        
+        //Create the corresponding Survey objects
+        $surveys = array();
+        if(!empty($results))
+        {
+            $this->db = new DatabaseInterface();
+            foreach($results as $result)
+            {
+                $questions = $this->getQuestions($result['?uid']->label);
+                $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $questions);
+            }
+        }
+        return $surveys;
+    }
+    
+    /**
+     * Gets the questions corresponding to the survey
+     * @param type $uid 
+     */
+    private function getQuestions($uid)
+    {
+        //Create the querystring. Note that the UID is the only argument in this case.
+        $querystring = '
+            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
+            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
+            SELECT DISTINCT ?has_question
+            WHERE
+            {
+                 _survey	predicates:resource_type	resources:survey ;
+                predicates:has_question ?has_question ; 
+                predicates:uid \'' . $uid . '\'
+            }';
+
+        $results = $this->model->sparqlQuery($querystring);
+        $questions = array();
+        //For every question id, get the corresponding question
+        if(!empty($results))
+        {
+            foreach($results as $questionId)
+            {
+                $resultQ = $this->db->get("question", array("uid" => $questionId['?has_question']->label));
+                $questions[] = $resultQ[0];
+            }
+        }
+        return $questions;
+    }
+    
+    /**
+     * Saves the given Survey object.
+     * @param type $rToolObject 
+     */
+    public function set($rToolObject)
+    {
+        $this->load();
+        
+        $resourceSurvey = new Resource(SURVEY.'/'.$rToolObject->uid);
+        //Remove the old value stored with the given id
+        $this->model->subtract($this->model->find($resourceSurvey, null, null));
+        
+        $resourceSurveyType = new Resource(SURVEY);
+        $predicateRType = new Resource(RTYPE);
+        $this->model->add(new Statement($resourceSurvey,$predicateRType,$resourceSurveyType));
+
+        $predicateUniqueID = new Resource(UID);
+        $literalSurveyID = new Literal($rToolObject->uid);
+        $this->model->add(new Statement($resourceSurvey,$predicateUniqueID,$literalSurveyID));
+
+        $predicateTitle = new Resource(TITLE);		
+        $surveyTitle = new Literal($rToolObject->title);
+        $this->model->add(new Statement($resourceSurvey,$predicateTitle,$surveyTitle));		
+
+        $predicateDescription = new Resource(DESCRIPTION);
+        $surveyDescription = new Literal($rToolObject->description);
+        $this->model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription));
+        
+        if(isset($rToolObject->questions))
+        {
+            foreach($rToolObject->questions as $question)
+            {
+                $questionUid = new Literal($question->uid);
+                $predicateQuid = new Resource(HAS_QUESTION);
+                $this->model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid));
+            }
+        }
+        $this->save();
+    }
+}
+
+?>
