1 | <?php
|
---|
2 | // Survey database interface class as intermediate for storing data from the site to the RDF database
|
---|
3 | require_once 'rdfConstants.php';
|
---|
4 | // Include RAP Library to write RDF files
|
---|
5 | include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Description of SessionInstance
|
---|
9 | *
|
---|
10 | * @author jkraaijeveld
|
---|
11 | */
|
---|
12 |
|
---|
13 | class SessionInstance extends ResearchToolObject
|
---|
14 | {
|
---|
15 | public static $filename = 'data/sessions/sessioninstances.rdf';
|
---|
16 |
|
---|
17 | public $title;
|
---|
18 | public $location;
|
---|
19 | public $facilitator;
|
---|
20 | public $starttime;
|
---|
21 | public $endtime;
|
---|
22 | public $notes;
|
---|
23 | public $session;
|
---|
24 | public $surveyinstances;
|
---|
25 | public $applicationinstances;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructor for a SessionInstance object
|
---|
29 | * @param type $uid
|
---|
30 | * @param type $title
|
---|
31 | * @param type $location
|
---|
32 | * @param type $facilitator
|
---|
33 | * @param type $starttime
|
---|
34 | * @param type $endtime
|
---|
35 | * @param type $notes
|
---|
36 | * @param type $session
|
---|
37 | * @param type $surveyinstances
|
---|
38 | * @param type $applicationinstances
|
---|
39 | */
|
---|
40 | public function __construct($uid = null, $title = null, $location = null, $facilitator = null, $starttime = null, $endtime = null, $notes = null, $session = null, $surveyinstances = null, $applicationinstances = null)
|
---|
41 | {
|
---|
42 | if(!isset($uid))
|
---|
43 | {
|
---|
44 | $uid = md5(uniqid(rand(), true));
|
---|
45 | }
|
---|
46 | $this->uid = $uid;
|
---|
47 | $this->title = $title;
|
---|
48 | $this->location = $location;
|
---|
49 | $this->facilitator = $facilitator;
|
---|
50 | $this->starttime = $starttime;
|
---|
51 | $this->endtime = $endtime;
|
---|
52 | $this->notes = $notes;
|
---|
53 | $this->session = $session;
|
---|
54 | $this->surveyinstances = $surveyinstances;
|
---|
55 | $this->applicationinstances = $applicationinstances;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * function evaluate()
|
---|
60 | * Evaluates all the references of this object.
|
---|
61 | */
|
---|
62 | public function evaluate()
|
---|
63 | {
|
---|
64 | if(is_string($this->facilitator))
|
---|
65 | {
|
---|
66 | $faci = ResearchToolObject::stripUri($this->facilitator);
|
---|
67 | $result = User::get(array("uid" => $faci["uid"]));
|
---|
68 | if(!isset($result[0]))
|
---|
69 | return false;
|
---|
70 | $this->facilitator = $result[0];
|
---|
71 | }
|
---|
72 |
|
---|
73 | if(is_string($this->session))
|
---|
74 | {
|
---|
75 | $sess = ResearchToolObject::stripUri($this->session);
|
---|
76 | $result = Session::get(array("uid" => $sess["uid"]));
|
---|
77 | if(!isset($result[0]))
|
---|
78 | return false;
|
---|
79 | $this->session = $result[0];
|
---|
80 | }
|
---|
81 | if(!empty($this->surveyinstances) && is_string($this->surveyinstances[0]))
|
---|
82 | {
|
---|
83 | $newSurveyInstances = array();
|
---|
84 | foreach($this->surveyinstances as $surveyInstance)
|
---|
85 | {
|
---|
86 | $sins = ResearchToolObject::stripUri($surveyInstance);
|
---|
87 | $result = SurveyInstance::get(array("uid" => $sins["uid"]));
|
---|
88 | if(!isset($result[0]))
|
---|
89 | return false;
|
---|
90 | $newSurveyInstances[] = $result[0];
|
---|
91 | }
|
---|
92 | $this->surveyinstances = $newSurveyInstances;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if(!empty($this->applicationinstances) && is_string($this->applicationinstances[0]))
|
---|
96 | {
|
---|
97 | $newApplicationInstances = array();
|
---|
98 | foreach($this->applicationinstances as $applicationInstance)
|
---|
99 | {
|
---|
100 | $ains = ResearchToolObject::stripUri($applicationInstance);
|
---|
101 | $result = ApplicationInstance::get(array("uid" => $ains["uid"]));
|
---|
102 | if(!isset($result[0]))
|
---|
103 | return false;
|
---|
104 | $newApplicationInstances[] = $result[0];
|
---|
105 | }
|
---|
106 | $this->applicationinstances = $newApplicationInstances;
|
---|
107 | }
|
---|
108 | return true;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * function save()
|
---|
113 | * Saves the current object into the database.
|
---|
114 | */
|
---|
115 | public function save()
|
---|
116 | {
|
---|
117 | //If evaluation fails, some references are incorrect.
|
---|
118 | //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
|
---|
119 | //TODO: Decide how to fix invalid references graciously.
|
---|
120 | if(!$this->evaluate())
|
---|
121 | throw new Exception('Evaluation failed.');
|
---|
122 |
|
---|
123 | //Ensure the required folder exists.
|
---|
124 | if(!is_dir('data/sessions/'))
|
---|
125 | mkdir('data/sessions/');
|
---|
126 |
|
---|
127 | $model = ResearchToolObject::load(SessionInstance::$filename);
|
---|
128 |
|
---|
129 | $resourceSInstance = new Resource(SESSIONINSTANCE . '/' . $this->uid);
|
---|
130 | //Remove the old value stored with the given id
|
---|
131 | $model->subtract($model->find($resourceSInstance, null, null));
|
---|
132 |
|
---|
133 | //Set the new values
|
---|
134 | $resourceSInstanceType = new Resource(SESSIONINSTANCE);
|
---|
135 | $predicateType = new Resource(RTYPE);
|
---|
136 | $model->add(new Statement($resourceSInstance, $predicateType, $resourceSInstanceType));
|
---|
137 |
|
---|
138 | $sInstanceId = new Literal($this->uid);
|
---|
139 | $predicateId = new Resource(UID);
|
---|
140 | $model->add(new Statement($resourceSInstance, $predicateId, $sInstanceId));
|
---|
141 |
|
---|
142 | $sInstanceTitle = new Literal($this->title);
|
---|
143 | $predicateTitle = new Resource(TITLE);
|
---|
144 | $model->add(new Statement($resourceSInstance, $predicateTitle, $sInstanceTitle));
|
---|
145 |
|
---|
146 | $sInstanceLocation = new Literal($this->location);
|
---|
147 | $predicateLocation = new Resource(LOCATION);
|
---|
148 | $model->add(new Statement($resourceSInstance, $predicateLocation, $sInstanceLocation));
|
---|
149 |
|
---|
150 | $sInstanceFacilitator = new Resource(USER . '/' . $this->facilitator->uid);
|
---|
151 | $predicateFacilitator = new Resource(FACILITATOR);
|
---|
152 | $model->add(new Statement($resourceSInstance, $predicateFacilitator, $sInstanceFacilitator));
|
---|
153 |
|
---|
154 | $sInstanceStartTime = new Literal($this->starttime->getTimestamp());
|
---|
155 | $predicateStartTime = new Resource(STARTTIME);
|
---|
156 | $model->add(new Statement($resourceSInstance, $predicateStartTime, $sInstanceStartTime));
|
---|
157 |
|
---|
158 | $sInstanceEndTime = new Literal($this->endtime->getTimestamp());
|
---|
159 | $predicateEndTime = new Resource(ENDTIME);
|
---|
160 | $model->add(new Statement($resourceSInstance, $predicateEndTime, $sInstanceEndTime));
|
---|
161 |
|
---|
162 | if(isset($this->notes))
|
---|
163 | {
|
---|
164 | foreach($this->notes as $note)
|
---|
165 | {
|
---|
166 | $sInstanceNote = new Literal($note);
|
---|
167 | $predicateNote = new Resource(HAS_NOTE);
|
---|
168 | $model->add(new Statement($resourceSInstance, $predicateNote, $sInstanceNote));
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | $sInstanceSession = new Resource(SESSION . '/' . $this->session->uid);
|
---|
173 | $predicateSession = new Resource(OF_SESSION);
|
---|
174 | $model->add(new Statement($resourceSInstance, $predicateSession, $sInstanceSession));
|
---|
175 |
|
---|
176 | if(isset($this->surveyinstances))
|
---|
177 | {
|
---|
178 | foreach($this->surveyinstances as $surveyinstance)
|
---|
179 | {
|
---|
180 | $sInstanceSurveyInstance = new Resource(SURVEYINSTANCE . '/' . $surveyinstance->uid);
|
---|
181 | $predicateSurveyInstance = new Resource(HAS_SURVEYINSTANCE);
|
---|
182 | $model->add(new Statement($resourceSInstance, $predicateSurveyInstance, $sInstanceSurveyInstance));
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | if(isset($this->applicationinstances))
|
---|
187 | {
|
---|
188 | foreach($this->applicationinstances as $applicationinstance)
|
---|
189 | {
|
---|
190 | $sInstanceAppInstance = new Resource(APPLICATIONINSTANCE . '/' . $applicationinstance->uid);
|
---|
191 | $predicateAppInstance = new Resource(HAS_APPLICATIONINSTANCE);
|
---|
192 | $model->add(new Statement($resourceSInstance, $predicateAppInstance, $sInstanceAppInstance));
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | $model->saveAs(SessionInstance::$filename, 'rdf');
|
---|
197 | return true;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * function get()
|
---|
202 | * @param type $arguments : An array containing one or more of the following elements:
|
---|
203 | * 'uid', 'title', 'location', 'facilitator', 'starttime', 'endtime' 'notes', 'session', 'applicationinstances', 'surveyinstances'
|
---|
204 | */
|
---|
205 | public static function get($arguments)
|
---|
206 | {
|
---|
207 | $model = ResearchToolObject::load(SessionInstance::$filename);
|
---|
208 |
|
---|
209 | //Build the query string
|
---|
210 | $querystring = '
|
---|
211 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
|
---|
212 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
|
---|
213 | SELECT DISTINCT ?uid, ?title, ?location, ?facilitator, ?starttime, ?endtime, ?of_session
|
---|
214 | WHERE
|
---|
215 | {
|
---|
216 | _sessioninstance predicates:resource_type resources:sessioninstance ;
|
---|
217 | predicates:uid ?uid ;
|
---|
218 | predicates:title ?title ;
|
---|
219 | predicates:location ?location ;
|
---|
220 | predicates:facilitator ?facilitator ;
|
---|
221 | predicates:starttime ?starttime ;
|
---|
222 | predicates:endtime ?endtime ;
|
---|
223 | predicates:of_session ?of_session ;
|
---|
224 | ' . ResearchToolObject::createArguments($arguments) . '
|
---|
225 | }';
|
---|
226 | //Query the model
|
---|
227 | $results = $model->sparqlQuery($querystring);
|
---|
228 | $sessioninstances = array();
|
---|
229 | if(!empty($results))
|
---|
230 | {
|
---|
231 | foreach($results as $result)
|
---|
232 | {
|
---|
233 | //Create a SessionInstance object out of every result. Get all the required fields as well.
|
---|
234 | $fields = SessionInstance::getFields($model, $result['?uid']->label);
|
---|
235 | $resultset = isset($result['?has_resultset']) ? $result['?has_resultset']->label : null;
|
---|
236 | $starttime = new DateTime();
|
---|
237 | $starttime->setTimestamp(intval($result['?starttime']->label));
|
---|
238 | $endtime = new DateTime();
|
---|
239 | $endtime->setTimestamp(intval($result['?endtime']->label));
|
---|
240 | $sessioninstances[] = new SessionInstance(
|
---|
241 | $result['?uid']->label,
|
---|
242 | $result['?title']->label,
|
---|
243 | $result['?location']->label,
|
---|
244 | $result['?facilitator']->uri,
|
---|
245 | $starttime,
|
---|
246 | $endtime,
|
---|
247 | $fields[0],
|
---|
248 | $result['?of_session']->uri,
|
---|
249 | $fields[1],
|
---|
250 | $fields[2]
|
---|
251 | );
|
---|
252 | }
|
---|
253 | }
|
---|
254 | return $sessioninstances;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * function getFields()
|
---|
259 | * param type $uid : The SessionInstance uid for which the fields should be retrieved.
|
---|
260 | * returns : An array with [0] => all notes, [1] => all SurveyInstances and [2] => all ApplicationInstances.
|
---|
261 | */
|
---|
262 | public static function getFields($model, $uid)
|
---|
263 | {
|
---|
264 | $result = $model->findRegex("[(".$uid.")]", "[(has_note)|(has_applicationinstance)|(has_surveyinstance)]", null);
|
---|
265 | $iterator = $result->getStatementIterator();
|
---|
266 | $notes = array();
|
---|
267 | $applicationinstances = array();
|
---|
268 | $surveyinstances = array();
|
---|
269 | while($iterator->hasNext())
|
---|
270 | {
|
---|
271 | $element = $iterator->next();
|
---|
272 | if($element->getLabelPredicate() == HAS_NOTE)
|
---|
273 | $notes[] = $element->getLabelObject();
|
---|
274 | else if($element->getLabelPredicate() == HAS_APPLICATIONINSTANCE)
|
---|
275 | $applicationinstances[] = $element->getLabelObject();
|
---|
276 | else if($element->getLabelPredicate() == HAS_SURVEYINSTANCE)
|
---|
277 | $surveyinstances[] = $element->getLabelObject();
|
---|
278 | }
|
---|
279 | return array($notes, $surveyinstances, $applicationinstances);
|
---|
280 | }
|
---|
281 |
|
---|
282 | public static function create($obj) {
|
---|
283 | return new SessionInstance($obj->uid, $obj->title, $obj->location,
|
---|
284 | $obj->facilitator, $obj->starttime, $obj->endtime, $obj->notes,
|
---|
285 | $obj->session, $obj->resultset);
|
---|
286 | }
|
---|
287 |
|
---|
288 | }
|
---|
289 |
|
---|
290 | ?>
|
---|