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