1 | <?php
|
---|
2 | require_once 'rdfConstants.php';
|
---|
3 | // Include RAP Library to write RDF files
|
---|
4 | include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
|
---|
5 | /**
|
---|
6 | * Description of Session
|
---|
7 | *
|
---|
8 | * @author jkraaijeveld
|
---|
9 | */
|
---|
10 |
|
---|
11 | class Session extends ResearchToolObject
|
---|
12 | {
|
---|
13 | private static $filename = 'data/sessions/sessions.rdf';
|
---|
14 |
|
---|
15 | public $title;
|
---|
16 | public $creator;
|
---|
17 | public $creationdate;
|
---|
18 | public $pipeline;
|
---|
19 |
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Constructor for a Session object
|
---|
23 | * @param type $uid
|
---|
24 | * @param type $title
|
---|
25 | * @param type $creator
|
---|
26 | * @param type $datetime
|
---|
27 | * @param type $pipeline
|
---|
28 | */
|
---|
29 | public function __construct($uid = null, $title = null, $creator = null, $datetime = null, $pipeline = null)
|
---|
30 | {
|
---|
31 | if(!isset($uid))
|
---|
32 | {
|
---|
33 | $uid = md5(uniqid(rand(), true));
|
---|
34 | }
|
---|
35 | $this->uid = $uid;
|
---|
36 | $this->title = $title;
|
---|
37 | $this->creator = $creator;
|
---|
38 | $this->creationdate = $datetime;
|
---|
39 | $this->pipeline = $pipeline;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * function evaluate(()
|
---|
44 | * Evaluates all the references of this object.
|
---|
45 | */
|
---|
46 | public function evaluate()
|
---|
47 | {
|
---|
48 | if(is_string($this->creator))
|
---|
49 | {
|
---|
50 | $result = User::get(array("uid" => $this->creator));
|
---|
51 | if(!isset($result[0]))
|
---|
52 | return false;
|
---|
53 | $this->creator = $result[0];
|
---|
54 | }
|
---|
55 | if(!empty($this->pipeline) && is_string($this->pipeline[0]))
|
---|
56 | {
|
---|
57 | $newPipeline = array();
|
---|
58 | foreach($this->pipeline as $element)
|
---|
59 | {
|
---|
60 | //Check if the element's UID can be found in surveys, if not:
|
---|
61 | //Check applications. If it isn't in either: invalid reference.
|
---|
62 | $result = Survey::get(array("uid" => $element));
|
---|
63 | if(!isset($result[0]))
|
---|
64 | $result = Application::get(array("uid" => $element));
|
---|
65 | if(!isset($result[0]))
|
---|
66 | return false;
|
---|
67 | $newPipeline[] = $result[0];
|
---|
68 | }
|
---|
69 | $this->pipeline = $newPipeline;
|
---|
70 | }
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * function save()
|
---|
76 | * Saves the current object into the database.
|
---|
77 | */
|
---|
78 | public function save()
|
---|
79 | {
|
---|
80 | //If evaluation fails, some references are incorrect.
|
---|
81 | //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
|
---|
82 | //TODO: Decide how to fix invalid references graciously.
|
---|
83 | if(!$this->evaluate())
|
---|
84 | throw new Exception('Evaluation failed.');
|
---|
85 |
|
---|
86 | //Ensure the required folder exists.
|
---|
87 | if(!is_dir('data/sessions/'))
|
---|
88 | mkdir('data/sessions/');
|
---|
89 |
|
---|
90 | $model = ResearchToolObject::load(Session::$filename);
|
---|
91 |
|
---|
92 | $resourceSession = new Resource(SESSION . '/' . $this->uid);
|
---|
93 | //Remove the old value stored with the given id
|
---|
94 | $model->subtract($model->find($resourceSession, null, null));
|
---|
95 |
|
---|
96 | //Set the new values
|
---|
97 | $resourceSessionType = new Resource(SESSION);
|
---|
98 | $predicateType = new Resource(RTYPE);
|
---|
99 | $model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
|
---|
100 |
|
---|
101 | $sessionId = new Literal($this->uid);
|
---|
102 | $predicateId = new Resource(UID);
|
---|
103 | $model->add(new Statement($resourceSession, $predicateId, $sessionId));
|
---|
104 |
|
---|
105 | $sessionTitle = new Literal($this->title);
|
---|
106 | $predicateTitle = new Resource(TITLE);
|
---|
107 | $model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
|
---|
108 |
|
---|
109 | $sessionCreator = new Literal($this->creator->uid);
|
---|
110 | $predicateCreator = new Resource(CREATOR);
|
---|
111 | $model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator));
|
---|
112 |
|
---|
113 | $sessionTimestamp = new Literal($this->creationdate->getTimestamp());
|
---|
114 | // $sessionTimestamp = new Literal($this->datetime); // Edit of above function, allows for creation of session, but still results in errors...
|
---|
115 | $predicateTimestamp = new Resource(CREATIONDATE);
|
---|
116 | $model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
|
---|
117 |
|
---|
118 | //Create a sequence to store the different applications and surveys.
|
---|
119 | if(isset($this->pipeline))
|
---|
120 | {
|
---|
121 | foreach($this->pipeline as $element)
|
---|
122 | {
|
---|
123 | $sessionObject = new Literal($element->uid);
|
---|
124 | $predicateObject = null;
|
---|
125 | if(get_class($element) == "Application")
|
---|
126 | $predicateObject = new Resource(HAS_APPLICATION);
|
---|
127 | else if(get_class($element) == "Survey")
|
---|
128 | $predicateObject = new Resource(HAS_SURVEY);
|
---|
129 | if(isset($predicateObject))
|
---|
130 | $model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | if(isset($this->answersets))
|
---|
135 | {
|
---|
136 | foreach($this->answersets as $answerset)
|
---|
137 | {
|
---|
138 | $sessionAnswerSet = new Literal($answerset->uid);
|
---|
139 | $predicateAnswerSet = new Resource(HAS_ANSWERSET);
|
---|
140 | $model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | $model->saveAs(Session::$filename, 'rdf');
|
---|
145 | return true;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * function get()
|
---|
151 | * @param type $arguments : An array containing one or more of the following elements:
|
---|
152 | * 'uid', 'title', 'creationdate', 'applications', 'surveys'
|
---|
153 | */
|
---|
154 | public static function get($arguments)
|
---|
155 | {
|
---|
156 | $model = ResearchToolObject::load(Session::$filename);
|
---|
157 |
|
---|
158 | //Build the query string
|
---|
159 | $querystring = '
|
---|
160 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
|
---|
161 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
|
---|
162 | SELECT DISTINCT ?uid, ?title, ?creator, ?creationdate
|
---|
163 | WHERE
|
---|
164 | {
|
---|
165 | _session predicates:resource_type resources:session ;
|
---|
166 | predicates:uid ?uid ;
|
---|
167 | predicates:title ?title ;
|
---|
168 | predicates:creator ?creator ;
|
---|
169 | predicates:creationdate ?creationdate ;
|
---|
170 | ' . ResearchToolObject::createArguments($arguments) . '
|
---|
171 | }';
|
---|
172 | //Query the model
|
---|
173 | $results = $model->sparqlQuery($querystring);
|
---|
174 | $sessions = array();
|
---|
175 | if(!empty($results))
|
---|
176 | {
|
---|
177 | foreach($results as $result)
|
---|
178 | {
|
---|
179 | //Create a session object out of every result, get all required fields as well.
|
---|
180 | $pipeline = Session::getPipeline($model, $result['?uid']->label);
|
---|
181 | $creator = $result['?creator']->label;
|
---|
182 | $creationdate = new DateTime();
|
---|
183 | $creationdate->setTimestamp(intval($result['?creationdate']->label));
|
---|
184 | $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator, $creationdate, $pipeline);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | return $sessions;
|
---|
188 | }
|
---|
189 | /**
|
---|
190 | * function getPipeline()
|
---|
191 | * param type $uid : The Session uid for which the pipeline should be retrieved.
|
---|
192 | */
|
---|
193 | private static function getPipeline($model, $uid)
|
---|
194 | {
|
---|
195 | $result = $model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null);
|
---|
196 | $iterator = $result->getStatementIterator();
|
---|
197 | $pipeline = array();
|
---|
198 | while($iterator->hasNext())
|
---|
199 | {
|
---|
200 | $element = $iterator->next();
|
---|
201 | $pipeline[] = $element->getLabelObject();
|
---|
202 | }
|
---|
203 | return $pipeline;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * function toSPSS
|
---|
208 | * @param type $location
|
---|
209 | * Writes an SPSS parseable file at the given location, named '$datetime_$title.txt'
|
---|
210 | * One big note about toSPSS: If this session contains multiple instances of THE SAME Survey,
|
---|
211 | * the world burns, people die, etcetera. (or, in a more fortunate case, the latest values
|
---|
212 | * are written to the file whilst the others get lost.
|
---|
213 | */
|
---|
214 | //DO NOT USE - BROKEN AS OF 23/12/2011
|
---|
215 | public function toSPSS($location)
|
---|
216 | {
|
---|
217 | $this->evaluate();
|
---|
218 | if(!is_dir($location))
|
---|
219 | {
|
---|
220 | mkdir($location);
|
---|
221 | }
|
---|
222 | //Create the filename, open the file at the given location
|
---|
223 | $fileName = $this->datetime->format("d-m-Y") . "_" . $this->title . ".txt";
|
---|
224 |
|
---|
225 | //Make a list of all 'column headers', e.g. "respondent" and all the question codes.
|
---|
226 | $headers = array();
|
---|
227 | $headers[] = "respondent";
|
---|
228 | foreach ($this->pipeline as $element)
|
---|
229 | {
|
---|
230 | if(get_class($element) == "Survey")
|
---|
231 | {
|
---|
232 | $element->evaluate();
|
---|
233 | foreach($element->questions as $question)
|
---|
234 | {
|
---|
235 | //TODO: This might need fixing after lazy initialization refactoring.
|
---|
236 | $headers[] = $element->title . '_' . $question->uid;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 | //Return if there are no surveys.
|
---|
241 | if(count($headers) < 2)
|
---|
242 | return;
|
---|
243 |
|
---|
244 | //Get the answers for all respondents
|
---|
245 | $answers = $this->getAnswers($headers);
|
---|
246 |
|
---|
247 | //Create the strings and write to file.
|
---|
248 | $headerString = "";
|
---|
249 | foreach($headers as $headerElement)
|
---|
250 | {
|
---|
251 | $headerString = $headerString . $headerElement . "\t";
|
---|
252 | }
|
---|
253 | $fileHandler = fopen($location . '/' . $fileName, 'w') or die("File in use");
|
---|
254 | fwrite($fileHandler, $headerString . "\n");
|
---|
255 | foreach($answers as $entry)
|
---|
256 | {
|
---|
257 | $entryString = "";
|
---|
258 | //PHP doesn't automatically sort the indices so I have to do it myself
|
---|
259 | //Entry elements can now be printed in order to match the headers
|
---|
260 | for ($i = 0; $i < count($headers); $i++)
|
---|
261 | {
|
---|
262 | if(in_array($i, array_keys($entry)))
|
---|
263 | $entryString = $entryString . $entry[$i] . "\t";
|
---|
264 | else
|
---|
265 | $entryString = $entryString . "\t";
|
---|
266 | }
|
---|
267 | fwrite($fileHandler, $entryString . "\n");
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | /*
|
---|
272 | * function getAnswers
|
---|
273 | * @param type $headers
|
---|
274 | * Based on the questions specified in the headers array, matches the answers of every
|
---|
275 | * respondent to the correct questions and returns an array containing a list of answers per
|
---|
276 | * respondent
|
---|
277 | */
|
---|
278 | private function getAnswers($headers)
|
---|
279 | {
|
---|
280 | //Add the answers per survey per respondent. Requires some extra looping to assure proper ordering.
|
---|
281 | //Keeps a list of respondents which have already been used. If an answerset exists for a respondent which
|
---|
282 | //already exists within the answers-array, use the index of the respondent. Else: make a new row.
|
---|
283 | $respondentsUsed = array();
|
---|
284 | $answers = array();
|
---|
285 | foreach($this->answersets as $answerset)
|
---|
286 | {
|
---|
287 | $entryIndex;
|
---|
288 | if(in_array($answerset->respondent->name, $respondentsUsed))
|
---|
289 | {
|
---|
290 | $entryIndex = array_search($answerset->respondent->name, $respondentsUsed);
|
---|
291 | $entry = $answers[$entryIndex];
|
---|
292 | }
|
---|
293 | else
|
---|
294 | {
|
---|
295 | $entry = array();
|
---|
296 | $entry[0] = $answerset->respondent->name;
|
---|
297 | $entryIndex = count($respondentsUsed);
|
---|
298 | $respondentsUsed[] = $answerset->respondent->name;
|
---|
299 | }
|
---|
300 | foreach ($answerset->answers as $answer)
|
---|
301 | {
|
---|
302 | $index = array_search($answerset->survey->title . '_' . $answer->question->uid, $headers);
|
---|
303 | //Index has to be a good number, else the answer doesn't belong in this session
|
---|
304 | //Causes might be a missing question or having a wrong survey
|
---|
305 | if($index != 0)
|
---|
306 | {
|
---|
307 | $value = "";
|
---|
308 | foreach ($answer->values as $val)
|
---|
309 | {
|
---|
310 | $value = $value . $val . ",";
|
---|
311 | }
|
---|
312 | //We all hate trailing commas
|
---|
313 | if (strlen($value) > 0) { $value = substr($value, 0, strlen($value)-1); }
|
---|
314 | $entry[$index] = $value;
|
---|
315 | }
|
---|
316 | }
|
---|
317 | $answers[$entryIndex] = $entry;
|
---|
318 | }
|
---|
319 | return $answers;
|
---|
320 | }
|
---|
321 |
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | ?>
|
---|