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

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

Bugfix in toSPSS(): entry elements were printed in an incorrect order. They now properly match the question stated in the header.

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                //Query the model
51                $results = $this->model->sparqlQuery($querystring);
52                $sessions = array();
53                if(!empty($results))
54                {
55                        $this->db = new DatabaseInterface();
56                        foreach($results as $result)
57                        {
58                                //Create a session object out of every result, get all required fields as well.
59                                $pipeline = $this->getPipeline($result['?uid']->label);
60                                $answersets = $this->getAnswerSets($result['?uid']->label);
61                                $creator = $this->db->get("User", array("uid" => $result['?creator']->label));
62                                $datetime = new DateTime();
63                                $datetime->setTimestamp(intval($result['?datetime']->label));
64                                $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator[0], $datetime, $pipeline, $answersets);
65                        }
66                }
67                return $sessions;
68        }
69        /**
70         * function getPipeline()
71         * param type $uid : The Session uid for which the pipeline should be retrieved.
72         */
73        private function getPipeline($uid)
74        {
75                $result = $this->model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null);
76                $iterator = $result->getStatementIterator();
77                $pipeline = array();
78                while($iterator->hasNext())
79                {
80                        $element = $iterator->next();
81                        if(strpos($element->getLabelPredicate(), "has_application") != false)
82                        {
83                                $applications = $this->db->get("application", array("uid" => $element->getLabelObject()));
84                                $pipeline[] = $applications[0];
85                        }
86                        else
87                        {
88                                $surveys = $this->db->get("survey", array("uid" => $element->getLabelObject()));
89                                $pipeline[] = $surveys[0];
90                        }
91                }
92                return $pipeline;
93        }
94
95        /**
96         * function getAnswerSets()
97         * @param type $uid : The Session uid for which the answerSets should be retrieved.
98         */
99        private function getAnswerSets($uid)
100        {
101                $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
102                $iterator = $result->getStatementIterator();
103                $answersets = array();
104                while($iterator->hasNext())
105                {
106                        $element = $iterator->next();
107                        $resultsets = $this->db->get("answerset", array("uid" => $element->getLabelObject()));
108                        $answersets[] = $resultsets[0];
109                }
110                return $answersets;
111        }
112
113        /**
114         * function set()
115         * @param type $rToolObjects : The ResearchToolObject to be saved.
116         */
117        public function set($rToolObject)
118        {
119                $this->load();
120                $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
121                //Remove the old value stored with the given id
122                $this->model->subtract($this->model->find($resourceSession, null, null));
123
124                //Set the new values
125                $resourceSessionType = new Resource(SESSION);
126                $predicateType = new Resource(RTYPE);
127                $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
128
129                $sessionId = new Literal($rToolObject->uid);
130                $predicateId = new Resource(UID);
131                $this->model->add(new Statement($resourceSession, $predicateId, $sessionId));
132
133                $sessionTitle = new Literal($rToolObject->title);
134                $predicateTitle = new Resource(TITLE);
135                $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
136
137                $sessionCreator = new Literal($rToolObject->creator->uid);
138                $predicateCreator = new Resource(CREATOR);
139                $this->model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator));
140
141                $sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp());
142        //      $sessionTimestamp = new Literal($rToolObject->datetime);    // Edit of above function, allows for creation of session, but still results in errors...
143        $predicateTimestamp = new Resource(DATETIME);
144                $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
145
146                //Create a sequence to store the different applications and surveys.
147                if(isset($rToolObject->pipeline))
148                {
149                        foreach($rToolObject->pipeline as $element)
150                        {
151                                $sessionObject = new Literal($element->uid);
152                                $predicateObject = null;
153                                if(get_class($element) == "Application")
154                                        $predicateObject = new Resource(HAS_APPLICATION);
155                                else if(get_class($element) == "Survey")
156                                        $predicateObject = new Resource(HAS_SURVEY);
157                                if(isset($predicateObject))
158                                        $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
159                        }
160                }
161
162                if(isset($rToolObject->answersets))
163                {
164                        foreach($rToolObject->answersets as $answerset)
165                        {
166                                $sessionAnswerSet = new Literal($answerset->uid);
167                                $predicateAnswerSet = new Resource(HAS_ANSWERSET);
168                                $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
169                        }
170                }
171
172                $this->save();
173        }
174}
175?>
176
Note: See TracBrowser for help on using the repository browser.