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

Last change on this file since 153 was 152, checked in by fpvanagthoven, 13 years ago
  • getInfo.php returnet informatie over het aangevraagde object. Dit kan via ajax routines op verscheidene infopanels weergegeven worden. (Bijvoorbeeld in de huidige versie van selectSession.php). Deze script wordt later nog uitgebreid om verschillende soorten objecten en sets informatie weer te geven. (Momenteel alleen sessions!)
  • selectSession werkt grotendeels, op deleteSession en een non-fatal error bij createSession na.
  • logout.php gebruikt nu ook destroy_session();
  • sequencerScripts.js uitgebreid om een simpel aan te roepen AJAX routine mogelijk te maken. Dit biedt de mogelijkheid om pagina's aan te passen zonder een refresh.
File size: 6.9 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         * Get the Sessions corresponding to the arguments
50         * @ param type $arguments
51         * Arguments can be one or more of the following:
52         * uid, title, datetime, applications, surveys, answersets
53         * Where applications, surveys and answersets are arrays containing ids
54         */
55        public function get($arguments)
56        {
57                $this->load();
58
59                $keys = array_keys($arguments);
60                $uid = ""; $title = ""; $datetime = ""; $applications = ""; $surveys = ""; $answersets = "";
61                if(in_array("uid", $keys))
62                        $uid = 'predicates:uid \''.$arguments["uid"].'\' ';
63                if(in_array("title", $keys))
64                        $title = 'predicates:title \''.$arguments["title"].'\' ';
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, ?datetime
94                        WHERE
95                        {
96                                _session predicates:resource_type resources:session ;
97                                predicates:uid ?uid ;
98                                predicates:title ?title ;
99                                ' . $uid . $title . $datetime . $applications . $surveys . $answersets . '
100                        }';
101                //Query the model
102                $results = $this->model->sparqlQuery($querystring);
103
104                $sessions = array();
105                if(!empty($results))
106                {
107                        $this->db = new DatabaseInterface();
108                        foreach($results as $result)
109                        {
110                                //Create a session object out of every result, get all required fields as well.
111                                $pipeline = $this->getPipeline($result['?uid']->label);
112                                $answersets = $this->getAnswerSets($result['?uid']->label);
113                                $datetime = new DateTime();
114                                $datetime->setTimestamp(intval($result['?datetime']));
115                                $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $datetime, $pipeline, $answersets);
116                        }
117                }
118                return $sessions;
119        }
120        /**
121         * function getPipeline
122         * Get the application and survey objects of the Session in order
123         */
124        private function getPipeline($uid)
125        {
126                $result = $this->model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null);
127                $iterator = $result->getStatementIterator();
128                $pipeline = array();
129                while($iterator->hasNext())
130                {
131                        $element = $iterator->next();
132                        if(strpos($element->getLabelPredicate(), "has_application") != false)
133                        {
134                                $applications = $this->db->get("application", array("uid" => $element->getLabelObject()));
135                                $pipeline[] = $applications[0];
136                        }
137                        else
138                        {
139                                $surveys = $this->db->get("survey", array("uid" => $element->getLabelObject()));
140                                $pipeline[] = $surveys[0];
141                        }
142                }
143                return $pipeline;
144        }
145
146        /**
147         * function getAnswerSets
148         * Get the answersets belonging to the given session
149         */
150        private function getAnswerSets($uid)
151        {
152                $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
153                $iterator = $result->getStatementIterator();
154                $answersets = array();
155                while($iterator->hasNext())
156                {
157                        $element = $iterator->next();
158                        $resultsets = $this->db->get("answerset", array("uid" => $element->getLabelObject()));
159                        $answersets[] = $resultsets[0];
160                }
161                return $answersets;
162        }
163
164        /**
165         * Save the given Session object in the file.
166         * @param type $rToolObjects
167         */
168        public function set($rToolObject)
169        {
170                $this->load();
171                $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
172                //Remove the old value stored with the given id
173                $this->model->subtract($this->model->find($resourceSession, null, null));
174
175                //Set the new values
176                $resourceSessionType = new Resource(SESSION);
177                $predicateType = new Resource(RTYPE);
178                $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
179
180                $sessionId = new Literal($rToolObject->uid);
181                $predicateId = new Resource(UID);
182                $this->model->add(new Statement($resourceSession, $predicateId, $sessionId));
183
184                $sessionTitle = new Literal($rToolObject->title);
185                $predicateTitle = new Resource(TITLE);
186                $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
187
188                //$sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp());
189                $sessionTimestamp = new Literal($rToolObject->datetime);    // Edit of above function, allows for creation of session, but still results in errors...
190                $predicateTimestamp = new Resource(DATETIME);
191                $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
192
193                //Create a sequence to store the different applications and surveys.
194                if(isset($rToolObject->pipeline))
195                {
196                        foreach($rToolObject->pipeline as $element)
197                        {
198                                $sessionObject = new Literal($element->uid);
199                                $predicateObject = null;
200                                if(get_class($element) == "Application")
201                                        $predicateObject = new Resource(HAS_APPLICATION);
202                                else if(get_class($element) == "Survey")
203                                        $predicateObject = new Resource(HAS_SURVEY);
204                                if(isset($predicateObject))
205                                        $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
206                        }
207                }
208
209                if(isset($rToolObject->answersets))
210                {
211                        foreach($rToolObject->answersets as $answerset)
212                        {
213                                $sessionAnswerSet = new Literal($answerset->uid);
214                                $predicateAnswerSet = new Resource(HAS_ANSWERSET);
215                                $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
216                        }
217                }
218
219                $this->save();
220        }
221}
222?>
223
Note: See TracBrowser for help on using the repository browser.