source: Dev/branches/Cartis/classes/SessionInstanceConnector.php @ 297

Last change on this file since 297 was 207, checked in by cartis, 13 years ago

Got latest classes from trunk & update 05-12-2012

File size: 6.2 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 set()
114         * @param type $rToolObject: The ResearchToolObject to be saved.
115         */
116        public function set($rToolObject)
117        {
118                $this->load();
119                //If evaluation fails, some references are incorrect.
120                //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
121                //TODO: Decide how to fix invalid references graciously.
122                if(!$rToolObject->evaluate())
123                        return false;
124                $resourceSInstance = new Resource(SESSIONINSTANCE . '/' . $rToolObject->uid);
125                //Remove the old value stored with the given id
126                $this->model->subtract($this->model->find($resourceSInstance, null, null));
127
128                //Set the new values
129                $resourceSInstanceType = new Resource(SESSIONINSTANCE);
130                $predicateType = new Resource(RTYPE);
131                $this->model->add(new Statement($resourceSInstance, $predicateType, $resourceSInstanceType));
132
133                $sInstanceId = new Literal($rToolObject->uid);
134                $predicateId = new Resource(UID);
135                $this->model->add(new Statement($resourceSInstance, $predicateId, $sInstanceId));
136
137                $sInstanceTitle = new Literal($rToolObject->title);
138                $predicateTitle = new Resource(TITLE);
139                $this->model->add(new Statement($resourceSInstance, $predicateTitle, $sInstanceTitle));
140
141                $sInstanceLocation = new Literal($rToolObject->location);
142                $predicateLocation = new Resource(LOCATION);
143                $this->model->add(new Statement($resourceSInstance, $predicateLocation, $sInstanceLocation));
144
145                $sInstanceFacilitator = new Literal($rToolObject->facilitator->uid);
146                $predicateFacilitator = new Resource(FACILITATOR);
147                $this->model->add(new Statement($resourceSInstance, $predicateFacilitator, $sInstanceFacilitator));
148
149                $sInstanceStartTime = new Literal($rToolObject->starttime->getTimestamp());
150                $predicateStartTime = new Resource(STARTTIME);
151                $this->model->add(new Statement($resourceSInstance, $predicateStartTime, $sInstanceStartTime));
152
153                $sInstanceEndTime = new Literal($rToolObject->endtime->getTimestamp());
154                $predicateEndTime = new Resource(ENDTIME);
155                $this->model->add(new Statement($resourceSInstance, $predicateEndTime, $sInstanceEndTime));
156
157                if(isset($rToolObject->notes))
158                {
159                        foreach($rToolObject->notes as $note)
160                        {
161                                $sInstanceNote = new Literal($note);
162                                $predicateNote = new Resource(HAS_NOTE);
163                                $this->model->add(new Statement($resourceSInstance, $predicateNote, $sInstanceNote));
164                        }
165                }
166
167                $sInstanceSession = new Literal($rToolObject->session->uid);
168                $predicateSession = new Resource(OF_SESSION);
169                $this->model->add(new Statement($resourceSInstance, $predicateSession, $sInstanceSession));
170
171                if(isset($rToolObject->resultset))
172                {
173                        //$sInstanceResultSet = new Literal($rToolObject->resultset->uid);
174                        $sInstanceResultSet = new Literal($rToolObject->resultset);
175                        $predicateResultSet = new Resource(HAS_RESULTSET);
176                        $this->model->add(new Statement($resourceSInstance, $predicateResultSet, $sInstanceResultSet));
177                }
178                $this->save();
179                return true;
180        }
181}
182?>
Note: See TracBrowser for help on using the repository browser.