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