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