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 AnswerConnector |
---|
9 | * |
---|
10 | * @author jkraaijeveld |
---|
11 | */ |
---|
12 | class AnswerConnector implements IConnector{ |
---|
13 | protected $model; |
---|
14 | protected $fileName = 'data/answers/answers.rdf'; |
---|
15 | protected $db; |
---|
16 | |
---|
17 | /** |
---|
18 | * Constructor for AnswerConnector |
---|
19 | */ |
---|
20 | public function __construct() { |
---|
21 | //Ensure the required folder for this connector exists |
---|
22 | if (!is_dir('data/answers/')) |
---|
23 | mkdir('data/answers/'); |
---|
24 | } |
---|
25 | |
---|
26 | /** |
---|
27 | * function load() |
---|
28 | * Loads the file into the standard MemModel. |
---|
29 | */ |
---|
30 | public function load() { |
---|
31 | //Get the Memory Model from the ModelFactory |
---|
32 | $this->model = ModelFactory::getDefaultModel(); |
---|
33 | |
---|
34 | //Ensure the required file exists before loading |
---|
35 | if(file_exists($this->fileName)) |
---|
36 | $this->model->load($this->fileName); |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | * function save() |
---|
41 | * Saves the MemModel into the given file. |
---|
42 | */ |
---|
43 | public function save() { |
---|
44 | $this->model->saveAs($this->fileName,'rdf'); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * function get() |
---|
49 | * @param type $arguments: An array having one or more of the following elements: |
---|
50 | * 'uid', 'question', 'values'. |
---|
51 | */ |
---|
52 | public function get($arguments) |
---|
53 | { |
---|
54 | $this->load(); |
---|
55 | $keys = array_keys($arguments); |
---|
56 | $uid = ""; $question = ""; $value = ""; |
---|
57 | if(in_array("uid", $keys)) |
---|
58 | $uid = 'predicates:uid \''.$arguments["uid"].'\''; |
---|
59 | if(in_array("question", $keys)) |
---|
60 | $question = 'predicates:question_code \''.$arguments["question"].'\''; |
---|
61 | if(in_array("values", $keys)) |
---|
62 | { |
---|
63 | foreach ($arguments["values"] as $val) |
---|
64 | { |
---|
65 | $value = $value . 'predicates:answered \'' . $val . '\' '; |
---|
66 | } |
---|
67 | } |
---|
68 | //Create the querystring |
---|
69 | $querystring = ' |
---|
70 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
71 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
72 | SELECT DISTINCT ?uid, ?question_code |
---|
73 | WHERE |
---|
74 | { |
---|
75 | _answer predicates:resource_type resources:answer; |
---|
76 | |
---|
77 | predicates:uid ?uid ; |
---|
78 | predicates:question_code ?question_code ; |
---|
79 | ' . $uid . $question . $value . ' |
---|
80 | }'; |
---|
81 | |
---|
82 | //Query the model |
---|
83 | $results = $this->model->sparqlQuery($querystring); |
---|
84 | //An answer can have multiple values, get all these values for the answer instances found. |
---|
85 | $answers = array(); |
---|
86 | if(!empty($results)) |
---|
87 | { |
---|
88 | $this->db = new DatabaseInterface(); |
---|
89 | foreach($results as $result) |
---|
90 | { |
---|
91 | $values = $this->getValues($result['?uid']->label); |
---|
92 | $questionResult = $this->db->get("question", array("uid" => $result['?question_code']->label)); |
---|
93 | $answers[] = new Answer($result['?uid']->label, $questionResult[0], $values); |
---|
94 | } |
---|
95 | } |
---|
96 | return $answers; |
---|
97 | |
---|
98 | } |
---|
99 | /** |
---|
100 | * function getValues() |
---|
101 | * @param type $uid : The uid of the Answer for which the values should be gotten. |
---|
102 | */ |
---|
103 | private function getValues($uid) |
---|
104 | { |
---|
105 | $querystring = ' |
---|
106 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
107 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
108 | SELECT DISTINCT ?answered |
---|
109 | WHERE |
---|
110 | { |
---|
111 | _answer predicates:resource_type resources:answer ; |
---|
112 | predicates:answered ?answered ; |
---|
113 | predicates:uid \''. $uid . '\' |
---|
114 | }'; |
---|
115 | $values = $this->model->sparqlQuery($querystring); |
---|
116 | $returnArray = array(); |
---|
117 | if(!empty($values)) |
---|
118 | { |
---|
119 | foreach($values as $value) |
---|
120 | { |
---|
121 | $returnArray[] = $value['?answered']->label; |
---|
122 | } |
---|
123 | } |
---|
124 | return $returnArray; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * function set() |
---|
129 | * @param type $rToolObject : The ResearchToolObject to be saved. |
---|
130 | */ |
---|
131 | public function set($rToolObject) |
---|
132 | { |
---|
133 | $this->load(); |
---|
134 | |
---|
135 | $resourceAnswer = new Resource(ANSWER.'/'.$rToolObject->uid); |
---|
136 | //Remove the old value stored with the given id |
---|
137 | $this->model->subtract($this->model->find($resourceAnswer, null, null)); |
---|
138 | |
---|
139 | //Save all the new values |
---|
140 | $resourceAnswerType = new Resource(ANSWER); |
---|
141 | $predicateRType = new Resource(RTYPE); |
---|
142 | $this->model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType)); |
---|
143 | |
---|
144 | $answerId = new Literal($rToolObject->uid); |
---|
145 | $predicateId = new Resource(UID); |
---|
146 | $this->model->add(new Statement($resourceAnswer, $predicateId, $answerId)); |
---|
147 | |
---|
148 | $questionId = new Literal($rToolObject->question->uid); |
---|
149 | $predicateQId = new Resource(QCODE); |
---|
150 | $this->model->add(new Statement($resourceAnswer, $predicateQId, $questionId)); |
---|
151 | |
---|
152 | if(isset($rToolObject->values)) |
---|
153 | { |
---|
154 | foreach($rToolObject->values as $value) |
---|
155 | { |
---|
156 | $answerValue = new Literal($value); |
---|
157 | $predicateAnswer = new Resource(ANSWERED); |
---|
158 | $this->model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue)); |
---|
159 | } |
---|
160 | } |
---|
161 | $this->save(); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | ?> |
---|