1 | <?php
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Description of AnswerSetConnector
|
---|
5 | *
|
---|
6 | * @author jkraaijeveld
|
---|
7 | */
|
---|
8 | class AnswerSetConnector implements IConnector{
|
---|
9 | protected $model;
|
---|
10 | protected $fileName = 'data/answers/answersets.rdf';
|
---|
11 | protected $db;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Constructor for the AnswerSetConnector
|
---|
15 | */
|
---|
16 | public function __construct()
|
---|
17 | {
|
---|
18 | //Ensure the required folder for this connector exists
|
---|
19 | if(!is_dir('data/answers'))
|
---|
20 | mkdir('data/answers');
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * function load()
|
---|
25 | * Loads the file into the standard MemModel
|
---|
26 | */
|
---|
27 | public function load()
|
---|
28 | {
|
---|
29 | //Get the Memory Model from the ModelFactory
|
---|
30 | $this->model = ModelFactory::getDefaultModel();
|
---|
31 |
|
---|
32 | //Ensure the required file exists before loading
|
---|
33 | if(file_exists($this->fileName))
|
---|
34 | $this->model->load($this->fileName);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * function save()
|
---|
39 | * Saves the MemModel into the given file.
|
---|
40 | */
|
---|
41 | public function save() {
|
---|
42 | $this->model->saveAs($this->fileName,'rdf');
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Get the AnswerSets corresponding to the arguments
|
---|
47 | * Arguments can be one or more of the following:
|
---|
48 | * uid, survey, respondent, answers
|
---|
49 | * Where answers is an array of answer ids
|
---|
50 | */
|
---|
51 | public function get($arguments)
|
---|
52 | {
|
---|
53 | $this->load();
|
---|
54 |
|
---|
55 | $keys = array_keys($arguments);
|
---|
56 | $uid = ""; $survey = ""; $respondent = ""; $answers = "";
|
---|
57 | if(in_array("uid", $keys))
|
---|
58 | $uid = 'predicates:uid \''.$arguments["uid"].'\' ';
|
---|
59 | if(in_array("survey", $keys))
|
---|
60 | $survey = 'predicates:for_survey \''.$arguments["survey"].'\' ';
|
---|
61 | if(in_array("respondent", $keys))
|
---|
62 | $respondent = 'predicates:by_respondent \''.$arguments["respondent"].'\' ';
|
---|
63 | if(in_array("answers", $keys))
|
---|
64 | {
|
---|
65 | foreach ($arguments["answers"] as $answer)
|
---|
66 | {
|
---|
67 | $answers = $answers . 'predicates:given_answer \'' . $answer . '\' ';
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | //Build the query string
|
---|
72 | $querystring = '
|
---|
73 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
|
---|
74 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
|
---|
75 | SELECT DISTINCT ?uid, ?for_survey, ?by_respondent
|
---|
76 | WHERE
|
---|
77 | {
|
---|
78 | _answerset predicates:resource_type resources:answerset ;
|
---|
79 | predicates:uid ?uid ;
|
---|
80 | predicates:for_survey ?for_survey ;
|
---|
81 | predicates:by_respondent ?by_respondent ;
|
---|
82 | ' . $uid . $survey . $respondent . $answers . '
|
---|
83 | }';
|
---|
84 |
|
---|
85 | //Query the model
|
---|
86 | $results = $this->model->sparqlQuery($querystring);
|
---|
87 | $answerSets = array();
|
---|
88 | if(!empty($results))
|
---|
89 | {
|
---|
90 | $this->db = new DatabaseInterface();
|
---|
91 | foreach($results as $result)
|
---|
92 | {
|
---|
93 | $survey = $this->db->get("survey", array("uid" => $result['?for_survey']->label));
|
---|
94 | $respondent = $this->db->get("respondent", array("uid" => $result['?by_respondent']->label));
|
---|
95 | $answers = $this->getAnswers($result['?uid']->label);
|
---|
96 | $answerSets[] = new AnswerSet($result['?uid']->label, $survey[0], $respondent[0], $answers);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | return $answerSets;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * function getAnswers()
|
---|
104 | * Gets the answers corresponding to the given answerset uid
|
---|
105 | */
|
---|
106 | private function getAnswers($uid)
|
---|
107 | {
|
---|
108 | $querystring = '
|
---|
109 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE .'>
|
---|
110 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE .'>
|
---|
111 | SELECT DISTINCT ?given_answer
|
---|
112 | WHERE
|
---|
113 | {
|
---|
114 | _answerset predicates:resource_type resources:answerset ;
|
---|
115 | predicates:given_answer ?given_answer ;
|
---|
116 | predicates:uid \'' . $uid . '\'
|
---|
117 | }';
|
---|
118 | $answers = $this->model->sparqlQuery($querystring);
|
---|
119 | $returnArray = array();
|
---|
120 | if(!empty($answers))
|
---|
121 | {
|
---|
122 | foreach($answers as $answer)
|
---|
123 | {
|
---|
124 | $answerR = $this->db->get("answer", array("uid" => $answer["?given_answer"]->label));
|
---|
125 | $returnArray[] = $answerR[0];
|
---|
126 | }
|
---|
127 | }
|
---|
128 | return $returnArray;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Save the given AnswerSet in the file.
|
---|
133 | * @param type $rToolObject
|
---|
134 | */
|
---|
135 | public function set($rToolObject)
|
---|
136 | {
|
---|
137 | $this->load();
|
---|
138 | $resourceAnswerSet = new Resource(ANSWERSET . '/' . $rToolObject->uid);
|
---|
139 | //Remove the old value stored with the given id
|
---|
140 | $this->model->subtract($this->model->find($resourceAnswerSet, null, null));
|
---|
141 |
|
---|
142 | //Set the new values
|
---|
143 | $resourceAnswerSetType = new Resource(ANSWERSET);
|
---|
144 | $predicateRType = new Resource(RTYPE);
|
---|
145 | $this->model->add(new Statement($resourceAnswerSet, $predicateRType, $resourceAnswerSetType));
|
---|
146 |
|
---|
147 | $answerSetId = new Literal($rToolObject->uid);
|
---|
148 | $predicateId = new Resource(UID);
|
---|
149 | $this->model->add(new Statement($resourceAnswerSet, $predicateId, $answerSetId));
|
---|
150 |
|
---|
151 | $surveyId = new Literal($rToolObject->survey->uid);
|
---|
152 | $predicateSurvey = new Resource(FOR_SURVEY);
|
---|
153 | $this->model->add(new Statement($resourceAnswerSet, $predicateSurvey, $surveyId));
|
---|
154 |
|
---|
155 | $respondentId = new Literal($rToolObject->respondent->uid);
|
---|
156 | $predicateRespondent = new Resource(BY_RESPONDENT);
|
---|
157 | $this->model->add(new Statement($resourceAnswerSet, $predicateRespondent, $respondentId));
|
---|
158 |
|
---|
159 | if(isset($rToolObject->answers))
|
---|
160 | {
|
---|
161 | foreach($rToolObject->answers as $answer)
|
---|
162 | {
|
---|
163 | $answerId = new Literal($answer->uid);
|
---|
164 | $predicateAnswer = new Resource(GIVEN_ANSWER);
|
---|
165 | $this->model->add(new Statement($resourceAnswerSet, $predicateAnswer, $answerId));
|
---|
166 | }
|
---|
167 | }
|
---|
168 | $this->save();
|
---|
169 | }
|
---|
170 | }
|
---|