source: Dev/trunk/classes/SessionConnector.php @ 170

Last change on this file since 170 was 170, checked in by jkraaijeveld, 13 years ago
  • Session objects now have a 'toSPSS($location)' method, which creates a file at the specified location which can be imported into SPSS.
File size: 7.4 KB
Line 
1<?php
2// Survey database interface class as intermediate for storing data from the site to the RDF database
3require_once 'rdfConstants.php';
4// Include RAP Library to write RDF files
5include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
6
7/**
8 * Description of SessionConnector
9 *
10 * @author jkraaijeveld
11 */
12class SessionConnector implements IConnector
13{
14        protected $model;
15        protected $fileName = 'data/sessions/sessions.rdf';
16        protected $db;
17
18        /**
19         * Constructor for the SessionConnector
20         */
21        public function __construct()
22        {
23                //Ensure the required folder for this connector exists
24                if(!is_dir('data/sessions'))
25                        mkdir('data/sessions');
26        }
27
28        /**
29         * function load()
30         * Loads the file into the standard MemModel
31         */
32        public function load()
33        {
34                $this->model = ModelFactory::getDefaultModel();
35                if(file_exists($this->fileName))
36                        $this->model->load($this->fileName);
37        }
38
39        /**
40         * fucntion save()
41         * Saves the MemModel into the given file.
42         */
43        public function save()
44        {
45                $this->model->saveAs($this->fileName,'rdf');
46        }
47
48        /**
49         * function get()
50         * @param type $arguments : An array containing one or more of the following elements:
51         * 'uid', 'title', 'datetime', 'applications', 'surveys', 'answersets'
52         */
53        public function get($arguments)
54        {
55                $this->load();
56
57                $keys = array_keys($arguments);
58                $uid = ""; $title = ""; $creator = ""; $datetime = ""; $applications = ""; $surveys = ""; $answersets = "";
59                if(in_array("uid", $keys))
60                        $uid = 'predicates:uid \''.$arguments["uid"].'\' ';
61                if(in_array("title", $keys))
62                        $title = 'predicates:title \''.$arguments["title"].'\' ';
63                if(in_array("creator", $keys))
64                        $creator = 'predicates:creator \''.$arguments["creator"].'\' ';
65                if(in_array("datetime", $keys))
66                        $datetime = 'predicates:datetime \''.$arguments["datetime"].'\' ';
67                if(in_array("applications", $keys))
68                {
69                        foreach($arguments["applications"] as $application)
70                        {
71                                $applications = $applications . 'predicates:has_application \'' . $application . '\' ';
72                        }
73                }
74                if(in_array("surveys", $keys))
75                {
76                        foreach($arguments["surveys"] as $survey)
77                        {
78                                $surveys = $surveys . 'predicates:has_survey \'' . $survey . '\' ';
79                        }
80                }
81                if(in_array("answersets", $keys))
82                {
83                        foreach($arguments["answersets"] as $answerset)
84                        {
85                                $answersets = $answersets . 'predicates:has_answerset \'' . $answerset . '\' ';
86                        }
87                }
88       
89                //Build the query string
90                $querystring = '
91                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
92                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
93                        SELECT DISTINCT ?uid, ?title, ?creator, ?datetime
94                        WHERE
95                        {
96                                _session predicates:resource_type resources:session ;
97                                predicates:uid ?uid ;
98                                predicates:title ?title ;
99                                predicates:creator ?creator ;
100                                predicates:datetime ?datetime ;
101                                ' . $uid . $title . $creator . $datetime . $applications . $surveys . $answersets . '
102                        }';
103                //Query the model
104                $results = $this->model->sparqlQuery($querystring);
105                $sessions = array();
106                if(!empty($results))
107                {
108                        $this->db = new DatabaseInterface();
109                        foreach($results as $result)
110                        {
111                                //Create a session object out of every result, get all required fields as well.
112                                $pipeline = $this->getPipeline($result['?uid']->label);
113                                $answersets = $this->getAnswerSets($result['?uid']->label);
114                                $creator = $this->db->get("User", array("uid" => $result['?creator']->label));
115                                $datetime = new DateTime();
116                                $datetime->setTimestamp(intval($result['?datetime']->label));
117                                $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator[0], $datetime, $pipeline, $answersets);
118                        }
119                }
120                return $sessions;
121        }
122        /**
123         * function getPipeline()
124         * param type $uid : The Session uid for which the pipeline should be retrieved.
125         */
126        private function getPipeline($uid)
127        {
128                $result = $this->model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null);
129                $iterator = $result->getStatementIterator();
130                $pipeline = array();
131                while($iterator->hasNext())
132                {
133                        $element = $iterator->next();
134                        if(strpos($element->getLabelPredicate(), "has_application") != false)
135                        {
136                                $applications = $this->db->get("application", array("uid" => $element->getLabelObject()));
137                                $pipeline[] = $applications[0];
138                        }
139                        else
140                        {
141                                $surveys = $this->db->get("survey", array("uid" => $element->getLabelObject()));
142                                $pipeline[] = $surveys[0];
143                        }
144                }
145                return $pipeline;
146        }
147
148        /**
149         * function getAnswerSets()
150         * @param type $uid : The Session uid for which the answerSets should be retrieved.
151         */
152        private function getAnswerSets($uid)
153        {
154                $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
155                $iterator = $result->getStatementIterator();
156                $answersets = array();
157                while($iterator->hasNext())
158                {
159                        $element = $iterator->next();
160                        $resultsets = $this->db->get("answerset", array("uid" => $element->getLabelObject()));
161                        $answersets[] = $resultsets[0];
162                }
163                return $answersets;
164        }
165
166        /**
167         * function set()
168         * @param type $rToolObjects : The ResearchToolObject to be saved.
169         */
170        public function set($rToolObject)
171        {
172                $this->load();
173                $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
174                //Remove the old value stored with the given id
175                $this->model->subtract($this->model->find($resourceSession, null, null));
176
177                //Set the new values
178                $resourceSessionType = new Resource(SESSION);
179                $predicateType = new Resource(RTYPE);
180                $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
181
182                $sessionId = new Literal($rToolObject->uid);
183                $predicateId = new Resource(UID);
184                $this->model->add(new Statement($resourceSession, $predicateId, $sessionId));
185
186                $sessionTitle = new Literal($rToolObject->title);
187                $predicateTitle = new Resource(TITLE);
188                $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
189
190                $sessionCreator = new Literal($rToolObject->creator->uid);
191                $predicateCreator = new Resource(CREATOR);
192                $this->model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator));
193
194                $sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp());
195        //      $sessionTimestamp = new Literal($rToolObject->datetime);    // Edit of above function, allows for creation of session, but still results in errors...
196        $predicateTimestamp = new Resource(DATETIME);
197                $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
198
199                //Create a sequence to store the different applications and surveys.
200                if(isset($rToolObject->pipeline))
201                {
202                        foreach($rToolObject->pipeline as $element)
203                        {
204                                $sessionObject = new Literal($element->uid);
205                                $predicateObject = null;
206                                if(get_class($element) == "Application")
207                                        $predicateObject = new Resource(HAS_APPLICATION);
208                                else if(get_class($element) == "Survey")
209                                        $predicateObject = new Resource(HAS_SURVEY);
210                                if(isset($predicateObject))
211                                        $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
212                        }
213                }
214
215                if(isset($rToolObject->answersets))
216                {
217                        foreach($rToolObject->answersets as $answerset)
218                        {
219                                $sessionAnswerSet = new Literal($answerset->uid);
220                                $predicateAnswerSet = new Resource(HAS_ANSWERSET);
221                                $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
222                        }
223                }
224
225                $this->save();
226        }
227}
228?>
229
Note: See TracBrowser for help on using the repository browser.