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

Last change on this file since 171 was 171, checked in by jkraaijeveld, 13 years ago

Iteration of refactoring in the Connector code.

  • Abstracted load() and save() to a new superclass Connector. Every connector extends Connector.
  • Query arguments are now correctly created by Connector's createArguments() function, removing redundant if/if/if statements in every get() function.
  • Every sidefunction which previously used a seperate query to get data now uses the RAP findRegex method, should increase performance.
  • Question arguments have been changed slightly: 'uid' is now 'code' and 'answers' is now 'definedanswers' to avoid confusion.
File size: 5.8 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 extends Connector 
13{
14        protected $db;
15
16        /**
17         * Constructor for the SessionConnector
18         */
19        public function __construct()
20        {
21                $this->fileName = 'data/sessions/sessions.rdf';
22                //Ensure the required folder for this connector exists
23                if(!is_dir('data/sessions'))
24                        mkdir('data/sessions');
25        }
26
27        /**
28         * function get()
29         * @param type $arguments : An array containing one or more of the following elements:
30         * 'uid', 'title', 'datetime', 'applications', 'surveys', 'answersets'
31         */
32        public function get($arguments)
33        {
34                $this->load();
35       
36                //Build the query string
37                $querystring = '
38                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
39                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
40                        SELECT DISTINCT ?uid, ?title, ?creator, ?datetime
41                        WHERE
42                        {
43                                _session predicates:resource_type resources:session ;
44                                predicates:uid ?uid ;
45                                predicates:title ?title ;
46                                predicates:creator ?creator ;
47                                predicates:datetime ?datetime ;
48                                ' . $this->createArguments($arguments) . '
49                        }';
50                echo $querystring;
51                //Query the model
52                $results = $this->model->sparqlQuery($querystring);
53                $sessions = array();
54                if(!empty($results))
55                {
56                        $this->db = new DatabaseInterface();
57                        foreach($results as $result)
58                        {
59                                //Create a session object out of every result, get all required fields as well.
60                                $pipeline = $this->getPipeline($result['?uid']->label);
61                                $answersets = $this->getAnswerSets($result['?uid']->label);
62                                $creator = $this->db->get("User", array("uid" => $result['?creator']->label));
63                                $datetime = new DateTime();
64                                $datetime->setTimestamp(intval($result['?datetime']->label));
65                                $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator[0], $datetime, $pipeline, $answersets);
66                        }
67                }
68                return $sessions;
69        }
70        /**
71         * function getPipeline()
72         * param type $uid : The Session uid for which the pipeline should be retrieved.
73         */
74        private function getPipeline($uid)
75        {
76                $result = $this->model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null);
77                $iterator = $result->getStatementIterator();
78                $pipeline = array();
79                while($iterator->hasNext())
80                {
81                        $element = $iterator->next();
82                        if(strpos($element->getLabelPredicate(), "has_application") != false)
83                        {
84                                $applications = $this->db->get("application", array("uid" => $element->getLabelObject()));
85                                $pipeline[] = $applications[0];
86                        }
87                        else
88                        {
89                                $surveys = $this->db->get("survey", array("uid" => $element->getLabelObject()));
90                                $pipeline[] = $surveys[0];
91                        }
92                }
93                return $pipeline;
94        }
95
96        /**
97         * function getAnswerSets()
98         * @param type $uid : The Session uid for which the answerSets should be retrieved.
99         */
100        private function getAnswerSets($uid)
101        {
102                $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
103                $iterator = $result->getStatementIterator();
104                $answersets = array();
105                while($iterator->hasNext())
106                {
107                        $element = $iterator->next();
108                        $resultsets = $this->db->get("answerset", array("uid" => $element->getLabelObject()));
109                        $answersets[] = $resultsets[0];
110                }
111                return $answersets;
112        }
113
114        /**
115         * function set()
116         * @param type $rToolObjects : The ResearchToolObject to be saved.
117         */
118        public function set($rToolObject)
119        {
120                $this->load();
121                $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
122                //Remove the old value stored with the given id
123                $this->model->subtract($this->model->find($resourceSession, null, null));
124
125                //Set the new values
126                $resourceSessionType = new Resource(SESSION);
127                $predicateType = new Resource(RTYPE);
128                $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
129
130                $sessionId = new Literal($rToolObject->uid);
131                $predicateId = new Resource(UID);
132                $this->model->add(new Statement($resourceSession, $predicateId, $sessionId));
133
134                $sessionTitle = new Literal($rToolObject->title);
135                $predicateTitle = new Resource(TITLE);
136                $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
137
138                $sessionCreator = new Literal($rToolObject->creator->uid);
139                $predicateCreator = new Resource(CREATOR);
140                $this->model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator));
141
142                $sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp());
143        //      $sessionTimestamp = new Literal($rToolObject->datetime);    // Edit of above function, allows for creation of session, but still results in errors...
144        $predicateTimestamp = new Resource(DATETIME);
145                $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
146
147                //Create a sequence to store the different applications and surveys.
148                if(isset($rToolObject->pipeline))
149                {
150                        foreach($rToolObject->pipeline as $element)
151                        {
152                                $sessionObject = new Literal($element->uid);
153                                $predicateObject = null;
154                                if(get_class($element) == "Application")
155                                        $predicateObject = new Resource(HAS_APPLICATION);
156                                else if(get_class($element) == "Survey")
157                                        $predicateObject = new Resource(HAS_SURVEY);
158                                if(isset($predicateObject))
159                                        $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
160                        }
161                }
162
163                if(isset($rToolObject->answersets))
164                {
165                        foreach($rToolObject->answersets as $answerset)
166                        {
167                                $sessionAnswerSet = new Literal($answerset->uid);
168                                $predicateAnswerSet = new Resource(HAS_ANSWERSET);
169                                $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
170                        }
171                }
172
173                $this->save();
174        }
175}
176?>
177
Note: See TracBrowser for help on using the repository browser.