source: Dev/branches/jos-branch/classes_old/SessionInstanceConnector.php @ 217

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

Added SessionInstance? and a semi-functional resultSet.

File size: 6.6 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 SessionInstanceConnector
9 *
10 * @author jkraaijeveld
11 */
12class SessionInstanceConnector extends Connector 
13{
14        protected $db;
15
16        /**
17         * Constructor for the SessionInstanceConnector.
18         */
19
20        public function __construct()
21        {
22                $this->fileName = 'data/sessions/sessioninstances.rdf';
23                //Ensure the required folder for this connector exists.
24                if(!is_dir('data/sessions'))
25                        mkdir('data/sessions');
26        }
27
28        /**
29         * function get()
30         * @param type $arguments : An array containing one or more of the following elements:
31         * 'uid', 'title', 'location', 'facilitator', 'starttime', 'endtime' 'notes', 'session', 'resultset'
32         */
33        public function get($arguments)
34        {
35                $this->load();
36
37                //Build the query string
38                $querystring = '
39                        PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
40                        PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
41                        SELECT DISTINCT ?uid, ?title, ?location, ?facilitator, ?starttime, ?endtime, ?of_session, ?has_resultset
42                        WHERE
43                        {
44                                _sessioninstance predicates:resource_type resources:sessioninstance ;
45                                predicates:uid ?uid ;
46                                predicates:title ?title ;
47                                predicates:location ?location ;
48                                predicates:facilitator ?facilitator ;
49                                predicates:starttime ?starttime ;
50                                predicates:endtime ?endtime ;
51                                predicates:of_session ?of_session ;
52                                ' . $this->createArguments($arguments) . '
53        }';
54                //Query the model
55                $results = $this->model->sparqlQuery($querystring);
56                $sessioninstances = array();
57                if(!empty($results))
58                {       
59                        $this->db = new DatabaseInterface();
60                        foreach($results as $result)
61                        {
62                                //Create a SessionInstance object out of every result. Get all the required fields as well.
63                                $notes = $this->getNotes($result['?uid']->label);
64                                $resultset = $this->getResultSet($result['?uid']->label);
65                                $starttime = new DateTime();
66                                $starttime->setTimestamp(intval($result['?starttime']->label));
67                                $endtime = new DateTime();
68                                $endtime->setTimestamp(intval($result['?endtime']->label));
69                                $sessioninstances[] = new SessionInstance( 
70                                        $result['?uid']->label,
71                                        $result['?title']->label,
72                                        $result['?location']->label,
73                                        $result['?facilitator']->label,
74                                        $starttime,
75                                        $endtime,
76                                        $notes,
77                                        $result['?of_session'],
78                                        $resultset
79                                );
80                        }
81                }
82                return $sessioninstances;
83        }
84       
85        /**
86         * function getNotes()
87         * param type $uid : The SessionInstance uid for which the notes should be retrieved.
88         */
89        public function getNotes($uid)
90        {
91                $result = $this->model->findRegex("[(".$uid.")]", "[(has_note)]", null);
92                $iterator = $result->getStatementIterator();
93                $notes = array();
94                while($iterator->hasNext())
95                {
96                        $element = $iterator->next();
97                        $notes[] = $element->getLabelObject();
98                }
99                return $notes;
100        }
101
102        /**
103         * function getResultSet($uid)
104         * param type $uid : The SessionInstance uid for which the ResultSet should be gotten.
105         */
106        public function getResultSet($udi)
107        {
108                //TODO: Implement when ResultSet is in.
109                return null;
110        }
111       
112        /**
113         * function getAnswerSets()
114         * @param type $uid : The Session uid for which the answerSets should be retrieved.
115         */
116        private static function getAnswerSets($uid)
117        {
118                $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
119                $iterator = $result->getStatementIterator();
120                $answersets = array();
121                while($iterator->hasNext())
122                {
123                        $element = $iterator->next();
124                        $answersets[] = $element->getLabelObject();
125                }
126                return $answersets;
127        }
128       
129
130        /**
131         * function set()
132         * @param type $rToolObject: The ResearchToolObject to be saved.
133         */
134        public function set($rToolObject)
135        {
136                $this->load();
137                //If evaluation fails, some references are incorrect.
138                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
139                //TODO: Decide how to fix invalid references graciously.
140                if(!$rToolObject->evaluate())
141                        return false;
142                $resourceSInstance = new Resource(SESSIONINSTANCE . '/' . $rToolObject->uid);
143                //Remove the old value stored with the given id
144                $this->model->subtract($this->model->find($resourceSInstance, null, null));
145
146                //Set the new values
147                $resourceSInstanceType = new Resource(SESSIONINSTANCE);
148                $predicateType = new Resource(RTYPE);
149                $this->model->add(new Statement($resourceSInstance, $predicateType, $resourceSInstanceType));
150
151                $sInstanceId = new Literal($rToolObject->uid);
152                $predicateId = new Resource(UID);
153                $this->model->add(new Statement($resourceSInstance, $predicateId, $sInstanceId));
154
155                $sInstanceTitle = new Literal($rToolObject->title);
156                $predicateTitle = new Resource(TITLE);
157                $this->model->add(new Statement($resourceSInstance, $predicateTitle, $sInstanceTitle));
158
159                $sInstanceLocation = new Literal($rToolObject->location);
160                $predicateLocation = new Resource(LOCATION);
161                $this->model->add(new Statement($resourceSInstance, $predicateLocation, $sInstanceLocation));
162
163                $sInstanceFacilitator = new Literal($rToolObject->facilitator->uid);
164                $predicateFacilitator = new Resource(FACILITATOR);
165                $this->model->add(new Statement($resourceSInstance, $predicateFacilitator, $sInstanceFacilitator));
166
167                $sInstanceStartTime = new Literal($rToolObject->starttime->getTimestamp());
168                $predicateStartTime = new Resource(STARTTIME);
169                $this->model->add(new Statement($resourceSInstance, $predicateStartTime, $sInstanceStartTime));
170
171                $sInstanceEndTime = new Literal($rToolObject->endtime->getTimestamp());
172                $predicateEndTime = new Resource(ENDTIME);
173                $this->model->add(new Statement($resourceSInstance, $predicateEndTime, $sInstanceEndTime));
174
175                if(isset($rToolObject->notes))
176                {
177                        foreach($rToolObject->notes as $note)
178                        {
179                                $sInstanceNote = new Literal($note);
180                                $predicateNote = new Resource(HAS_NOTE);
181                                $this->model->add(new Statement($resourceSInstance, $predicateNote, $sInstanceNote));
182                        }
183                }
184
185                $sInstanceSession = new Literal($rToolObject->session->uid);
186                $predicateSession = new Resource(OF_SESSION);
187                $this->model->add(new Statement($resourceSInstance, $predicateSession, $sInstanceSession));
188
189                if(isset($rToolObject->resultset))
190                {
191                        //$sInstanceResultSet = new Literal($rToolObject->resultset->uid);
192                        $sInstanceResultSet = new Literal($rToolObject->resultset);
193                        $predicateResultSet = new Resource(HAS_RESULTSET);
194                        $this->model->add(new Statement($resourceSInstance, $predicateResultSet, $sInstanceResultSet));
195                }
196                $this->save();
197                return true;
198        }
199}
200?>
Note: See TracBrowser for help on using the repository browser.