[82] | 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 | } |
---|
[91] | 71 | |
---|
| 72 | public function readQuestionCodes() |
---|
| 73 | { |
---|
| 74 | $result = null; |
---|
| 75 | |
---|
| 76 | if(file_exists($this->filePath.'questions.rdf')) |
---|
| 77 | { |
---|
| 78 | // Create empty MemModel |
---|
| 79 | $factory = new ModelFactory(); |
---|
| 80 | $tempmodel= $factory->getDefaultModel(); |
---|
| 81 | $tempmodel->load($this->filePath.'questions.rdf'); |
---|
[82] | 82 | |
---|
[91] | 83 | $querystring = ' |
---|
| 84 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 85 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 86 | SELECT ?questionCode |
---|
| 87 | WHERE |
---|
| 88 | { |
---|
| 89 | _question predicates:resource_type resources:question ; |
---|
| 90 | predicates:question_code ?questionCode |
---|
| 91 | }'; |
---|
| 92 | |
---|
| 93 | $result = $tempmodel->sparqlQuery($querystring); |
---|
| 94 | } |
---|
| 95 | return $result; |
---|
| 96 | } |
---|
| 97 | |
---|
[82] | 98 | public function readQuestionTitle($questionID) |
---|
| 99 | { |
---|
| 100 | $querystring = ' |
---|
| 101 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 102 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 103 | SELECT ?questionTitle |
---|
| 104 | WHERE |
---|
| 105 | { |
---|
| 106 | _question predicates:resource_type resources:question ; |
---|
| 107 | predicates:uid "' . $questionID . '" ; |
---|
| 108 | predicates:title ?questionTitle |
---|
| 109 | }'; |
---|
| 110 | |
---|
| 111 | $result = $this->model->sparqlQuery($querystring); |
---|
| 112 | |
---|
| 113 | return $result; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | public function readQuestionDescription($questionID) |
---|
| 117 | { |
---|
| 118 | $querystring = ' |
---|
| 119 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 120 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 121 | SELECT ?questionDescription |
---|
| 122 | WHERE |
---|
| 123 | { |
---|
| 124 | _question predicates:resource_type resources:question ; |
---|
| 125 | predicates:uid "' . $questionID . '" ; |
---|
| 126 | predicates:description ?questionDescription |
---|
| 127 | }'; |
---|
| 128 | |
---|
| 129 | $result = $this->model->sparqlQuery($querystring); |
---|
| 130 | |
---|
| 131 | return $result; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | public function readQuestionType($questionID) |
---|
| 135 | { |
---|
| 136 | $querystring = ' |
---|
| 137 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 138 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 139 | SELECT ?questionType |
---|
| 140 | WHERE |
---|
| 141 | { |
---|
| 142 | _question predicates:resource_type resources:question ; |
---|
| 143 | predicates:uid "' . $questionID . '" ; |
---|
| 144 | predicates:question_type ?questionType |
---|
| 145 | }'; |
---|
| 146 | |
---|
| 147 | $result = $this->model->sparqlQuery($querystring); |
---|
| 148 | |
---|
| 149 | return $result; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | public function readQuestionCode($questionID) |
---|
| 153 | { |
---|
| 154 | $querystring = ' |
---|
| 155 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 156 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 157 | SELECT ?questionCode |
---|
| 158 | WHERE |
---|
| 159 | { |
---|
| 160 | _question predicates:resource_type resources:question ; |
---|
| 161 | predicates:uid "' . $questionID . '" ; |
---|
| 162 | predicates:question_code ?questionCode |
---|
| 163 | }'; |
---|
| 164 | |
---|
| 165 | $result = $this->model->sparqlQuery($querystring); |
---|
| 166 | |
---|
| 167 | return $result; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | public function readQuestionCategory($questionID) |
---|
| 171 | { |
---|
| 172 | $querystring = ' |
---|
| 173 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 174 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 175 | SELECT ?questionCategory |
---|
| 176 | WHERE |
---|
| 177 | { |
---|
| 178 | _question predicates:resource_type resources:question ; |
---|
| 179 | predicates:uid "' . $questionID . '" ; |
---|
| 180 | predicates:question_category ?questionCategory |
---|
| 181 | }'; |
---|
| 182 | |
---|
| 183 | $result = $this->model->sparqlQuery($querystring); |
---|
| 184 | |
---|
| 185 | return $result; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | public function readQuestionAnswers($questionID) |
---|
| 189 | { |
---|
| 190 | $querystring = ' |
---|
| 191 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 192 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 193 | SELECT ?answerDescription |
---|
| 194 | WHERE |
---|
| 195 | { |
---|
| 196 | _question predicates:resource_type resources:question ; |
---|
| 197 | predicates:uid "' . $questionID . '" ; |
---|
[85] | 198 | predicates:has_answer ?answerDescription |
---|
[82] | 199 | }'; |
---|
| 200 | |
---|
| 201 | $result = $this->model->sparqlQuery($querystring); |
---|
| 202 | |
---|
| 203 | return $result; |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | ?> |
---|