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 | * AnswerSet is a collection of answers corresponding to a Session
|
---|
9 | *
|
---|
10 | */
|
---|
11 | class AnswerSet extends ResearchToolObject{
|
---|
12 | public static $filename = 'data/results/answersets.rdf';
|
---|
13 |
|
---|
14 | public $survey;
|
---|
15 | public $respondent;
|
---|
16 | public $datetime;
|
---|
17 | public $answers;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Constructor for an AnswerSet object
|
---|
21 | * @param type $uid
|
---|
22 | * @param type $survey
|
---|
23 | * @param type $respondent
|
---|
24 | * @param type $datetime
|
---|
25 | * @param type $answers
|
---|
26 | */
|
---|
27 | public function __construct($uid=null, $survey=null, $respondent=null, $datetime=null, $answers=null)
|
---|
28 | {
|
---|
29 | if(!isset($uid))
|
---|
30 | {
|
---|
31 | $uid = md5(uniqid(rand(), true));
|
---|
32 | }
|
---|
33 | $this->uid = $uid;
|
---|
34 | $this->survey = $survey;
|
---|
35 | $this->respondent = $respondent;
|
---|
36 | $this->datetime = $datetime;
|
---|
37 | $this->answers = $answers;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * function evaluate()
|
---|
42 | * evaluates the references of survey, respondent and answers.
|
---|
43 | */
|
---|
44 | public function evaluate()
|
---|
45 | {
|
---|
46 | if(is_string($this->survey))
|
---|
47 | {
|
---|
48 | $surv = ResearchToolObject::stripUri($this->survey);
|
---|
49 | $result = Survey::get(array("uid" => $surv["uid"]));
|
---|
50 | if(!isset($result[0]))
|
---|
51 | return false;
|
---|
52 | $this->survey = $result[0];
|
---|
53 | }
|
---|
54 | if(is_string($this->respondent))
|
---|
55 | {
|
---|
56 | $resp = ResearchToolObject::stripUri($this->respondent);
|
---|
57 | $result = Respondent::get(array("uid" => $resp["uid"]));
|
---|
58 | if(!isset($result[0]))
|
---|
59 | return false;
|
---|
60 | $this->respondent = $result[0];
|
---|
61 | }
|
---|
62 | if(!empty($this->answers) && is_string($this->answers[0]))
|
---|
63 | {
|
---|
64 | $newanswers = array();
|
---|
65 | foreach($this->answers as $answer)
|
---|
66 | {
|
---|
67 | $answ = ResearchToolObject::stripUri($answer);
|
---|
68 | $result = Answer::get(array("uid" => $answ["uid"]));
|
---|
69 | if(!isset($result[0]))
|
---|
70 | return false;
|
---|
71 | $newanswers[] = $result[0];
|
---|
72 | }
|
---|
73 | $this->answers = $newanswers;
|
---|
74 | }
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * function save()
|
---|
81 | * Saves the current object into the database.
|
---|
82 | */
|
---|
83 | public function save()
|
---|
84 | {
|
---|
85 | //If evaluation fails, some references are incorrect. We shouldn't save in this case.
|
---|
86 | //TODO: Decide how to fix invalid references graciously.
|
---|
87 | if(!$this->evaluate())
|
---|
88 | throw new Exception('Evaluation failed.');
|
---|
89 |
|
---|
90 | //Ensure the required folder exists.
|
---|
91 | if(!is_dir('data/results/'))
|
---|
92 | mkdir('data/results/');
|
---|
93 |
|
---|
94 | $model = ResearchToolObject::load(AnswerSet::$filename);
|
---|
95 |
|
---|
96 | $resourceAnswerSet = new Resource(ANSWERSET . '/' . $this->uid);
|
---|
97 | //Remove the old value stored with the given id
|
---|
98 | $model->subtract($model->find($resourceAnswerSet, null, null));
|
---|
99 |
|
---|
100 | //Set the new values
|
---|
101 | $resourceAnswerSetType = new Resource(ANSWERSET);
|
---|
102 | $predicateRType = new Resource(RTYPE);
|
---|
103 | $model->add(new Statement($resourceAnswerSet, $predicateRType, $resourceAnswerSetType));
|
---|
104 |
|
---|
105 | $answerSetId = new Literal($this->uid);
|
---|
106 | $predicateId = new Resource(UID);
|
---|
107 | $model->add(new Statement($resourceAnswerSet, $predicateId, $answerSetId));
|
---|
108 |
|
---|
109 | $surveyId = new Resource(SURVEY . '/' . $this->survey->uid);
|
---|
110 | $predicateSurvey = new Resource(FOR_SURVEY);
|
---|
111 | $model->add(new Statement($resourceAnswerSet, $predicateSurvey, $surveyId));
|
---|
112 |
|
---|
113 | $respondentId = new Resource(RESPONDENT . '/' . $this->respondent->uid);
|
---|
114 | $predicateRespondent = new Resource(BY_RESPONDENT);
|
---|
115 | $model->add(new Statement($resourceAnswerSet, $predicateRespondent, $respondentId));
|
---|
116 |
|
---|
117 | $dateTime = new Literal($this->datetime->getTimestamp());
|
---|
118 | $predicateDateTime = new Resource(DATETIME);
|
---|
119 | $model->add(new Statement($resourceAnswerSet, $predicateDateTime, $dateTime));
|
---|
120 |
|
---|
121 | if(isset($this->answers))
|
---|
122 | {
|
---|
123 | foreach($this->answers as $answer)
|
---|
124 | {
|
---|
125 | $answerId = new Resource(ANSWER . '/' . $answer->uid);
|
---|
126 | $predicateAnswer = new Resource(GIVEN_ANSWER);
|
---|
127 | $model->add(new Statement($resourceAnswerSet, $predicateAnswer, $answerId));
|
---|
128 | }
|
---|
129 | }
|
---|
130 | $model->saveAs(AnswerSet::$filename, 'rdf');
|
---|
131 | return true;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * function get()
|
---|
136 | * @param type $arguments : An array having one ore more of the following elements:
|
---|
137 | * 'uid', 'survey', 'respondent', 'datetime', 'answers'.
|
---|
138 | */
|
---|
139 | public static function get($arguments)
|
---|
140 | {
|
---|
141 | $model = ResearchToolObject::load(AnswerSet::$filename);
|
---|
142 |
|
---|
143 | //Build the query string
|
---|
144 | $querystring = '
|
---|
145 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
|
---|
146 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
|
---|
147 | SELECT DISTINCT ?uid, ?for_survey, ?datetime ?by_respondent
|
---|
148 | WHERE
|
---|
149 | {
|
---|
150 | _answerset predicates:resource_type resources:answerset ;
|
---|
151 | predicates:uid ?uid ;
|
---|
152 | predicates:for_survey ?for_survey ;
|
---|
153 | predicates:by_respondent ?by_respondent ;
|
---|
154 | predicates:datetime ?datetime ;
|
---|
155 | ' . ResearchToolObject::createArguments($arguments) . '
|
---|
156 | }';
|
---|
157 | //Query the model
|
---|
158 | $results = $model->sparqlQuery($querystring);
|
---|
159 | $answerSets = array();
|
---|
160 | if(!empty($results))
|
---|
161 | {
|
---|
162 | foreach($results as $result)
|
---|
163 | {
|
---|
164 | $survey = $result['?for_survey']->uri;
|
---|
165 | $respondent = $result['?by_respondent']->uri;
|
---|
166 | $datetime = new DateTime();
|
---|
167 | $datetime->setTimestamp(intval($result['?datetime']->label));
|
---|
168 | $answers = AnswerSet::getAnswers($model, $result['?uid']->label);
|
---|
169 | $answerSets[] = new AnswerSet($result['?uid']->label, $survey, $respondent, $datetime, $answers);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | return $answerSets;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * function getAnswers()
|
---|
177 | * @param type $uid : the uid for which the answers should be retrieved.
|
---|
178 | */
|
---|
179 | private static function getAnswers($model, $uid)
|
---|
180 | {
|
---|
181 | $result = $model->findRegex("[(".$uid.")]", "[(given_answer)]", null);
|
---|
182 | $iterator = $result->getStatementIterator();
|
---|
183 | $answers = array();
|
---|
184 | while($iterator->hasNext())
|
---|
185 | {
|
---|
186 | $element = $iterator->next();
|
---|
187 | $answers[] = $element->getLabelObject();
|
---|
188 | }
|
---|
189 | return $answers;
|
---|
190 | }
|
---|
191 |
|
---|
192 | public static function create($obj) {
|
---|
193 | return new AnswerSet($obj->uid, $obj->survey, $obj->respondent, $obj->datetime, $obj->answers);
|
---|
194 | }
|
---|
195 |
|
---|
196 | }
|
---|
197 |
|
---|