1 | <?php |
---|
2 | |
---|
3 | class QuestionRDFReader |
---|
4 | { |
---|
5 | protected $model; |
---|
6 | |
---|
7 | protected $filePath; |
---|
8 | |
---|
9 | public function __construct() |
---|
10 | { |
---|
11 | // Create empty MemModel |
---|
12 | $factory = new ModelFactory(); |
---|
13 | $this->model = $factory->getDefaultModel(); |
---|
14 | |
---|
15 | $this->filePath = 'data/questions/'; |
---|
16 | if (!is_dir($this->filePath)) |
---|
17 | mkdir($this->filePath); |
---|
18 | } |
---|
19 | |
---|
20 | public function loadQuestions() |
---|
21 | { |
---|
22 | if(file_exists($this->filePath.'questions.rdf')) |
---|
23 | { |
---|
24 | $this->model->load($this->filePath.'questions.rdf'); |
---|
25 | return true; |
---|
26 | } |
---|
27 | else |
---|
28 | { |
---|
29 | return false; |
---|
30 | } |
---|
31 | } |
---|
32 | |
---|
33 | public function readQuestionByID($questionID) |
---|
34 | { |
---|
35 | $result = array(); |
---|
36 | $result['title'] = $this->readQuestionTitle($questionID); |
---|
37 | $result['description'] = $this->readQuestionDescription($questionID); |
---|
38 | $result['type'] = $this->readQuestionType($questionID); |
---|
39 | $result['code'] = $this->readQuestionCode($questionID); |
---|
40 | $result['category'] = $this->readQuestionCategory($questionID); |
---|
41 | $result['answers'] = $this->readQuestionAnswers($questionID); |
---|
42 | |
---|
43 | return $result; |
---|
44 | } |
---|
45 | |
---|
46 | public function readQuestionIDs() |
---|
47 | { |
---|
48 | $result = null; |
---|
49 | |
---|
50 | if(file_exists($this->filePath.'questions.rdf')) |
---|
51 | { |
---|
52 | // Create empty MemModel |
---|
53 | $factory = new ModelFactory(); |
---|
54 | $tempmodel= $factory->getDefaultModel(); |
---|
55 | $tempmodel->load($this->filePath.'questions.rdf'); |
---|
56 | |
---|
57 | $querystring = ' |
---|
58 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
59 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
60 | SELECT ?questionID |
---|
61 | WHERE |
---|
62 | { |
---|
63 | _question predicates:resource_type resources:question ; |
---|
64 | predicates:uid ?questionID |
---|
65 | }'; |
---|
66 | |
---|
67 | $result = $tempmodel->sparqlQuery($querystring); |
---|
68 | } |
---|
69 | return $result; |
---|
70 | } |
---|
71 | |
---|
72 | public function readQuestionTitle($questionID) |
---|
73 | { |
---|
74 | $querystring = ' |
---|
75 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
76 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
77 | SELECT ?questionTitle |
---|
78 | WHERE |
---|
79 | { |
---|
80 | _question predicates:resource_type resources:question ; |
---|
81 | predicates:uid "' . $questionID . '" ; |
---|
82 | predicates:title ?questionTitle |
---|
83 | }'; |
---|
84 | |
---|
85 | $result = $this->model->sparqlQuery($querystring); |
---|
86 | |
---|
87 | return $result; |
---|
88 | } |
---|
89 | |
---|
90 | public function readQuestionDescription($questionID) |
---|
91 | { |
---|
92 | $querystring = ' |
---|
93 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
94 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
95 | SELECT ?questionDescription |
---|
96 | WHERE |
---|
97 | { |
---|
98 | _question predicates:resource_type resources:question ; |
---|
99 | predicates:uid "' . $questionID . '" ; |
---|
100 | predicates:description ?questionDescription |
---|
101 | }'; |
---|
102 | |
---|
103 | $result = $this->model->sparqlQuery($querystring); |
---|
104 | |
---|
105 | return $result; |
---|
106 | } |
---|
107 | |
---|
108 | public function readQuestionType($questionID) |
---|
109 | { |
---|
110 | $querystring = ' |
---|
111 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
112 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
113 | SELECT ?questionType |
---|
114 | WHERE |
---|
115 | { |
---|
116 | _question predicates:resource_type resources:question ; |
---|
117 | predicates:uid "' . $questionID . '" ; |
---|
118 | predicates:question_type ?questionType |
---|
119 | }'; |
---|
120 | |
---|
121 | $result = $this->model->sparqlQuery($querystring); |
---|
122 | |
---|
123 | return $result; |
---|
124 | } |
---|
125 | |
---|
126 | public function readQuestionCode($questionID) |
---|
127 | { |
---|
128 | $querystring = ' |
---|
129 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
130 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
131 | SELECT ?questionCode |
---|
132 | WHERE |
---|
133 | { |
---|
134 | _question predicates:resource_type resources:question ; |
---|
135 | predicates:uid "' . $questionID . '" ; |
---|
136 | predicates:question_code ?questionCode |
---|
137 | }'; |
---|
138 | |
---|
139 | $result = $this->model->sparqlQuery($querystring); |
---|
140 | |
---|
141 | return $result; |
---|
142 | } |
---|
143 | |
---|
144 | public function readQuestionCategory($questionID) |
---|
145 | { |
---|
146 | $querystring = ' |
---|
147 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
148 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
149 | SELECT ?questionCategory |
---|
150 | WHERE |
---|
151 | { |
---|
152 | _question predicates:resource_type resources:question ; |
---|
153 | predicates:uid "' . $questionID . '" ; |
---|
154 | predicates:question_category ?questionCategory |
---|
155 | }'; |
---|
156 | |
---|
157 | $result = $this->model->sparqlQuery($querystring); |
---|
158 | |
---|
159 | return $result; |
---|
160 | } |
---|
161 | |
---|
162 | public function readQuestionAnswers($questionID) |
---|
163 | { |
---|
164 | $querystring = ' |
---|
165 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
166 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
167 | SELECT ?answerDescription |
---|
168 | WHERE |
---|
169 | { |
---|
170 | _question predicates:resource_type resources:question ; |
---|
171 | predicates:uid "' . $questionID . '" ; |
---|
172 | predicates:has_answer ?answerDescription |
---|
173 | }'; |
---|
174 | |
---|
175 | $result = $this->model->sparqlQuery($querystring); |
---|
176 | |
---|
177 | return $result; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | ?> |
---|