[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 | |
---|
[105] | 33 | public function readQuestionByCode($questionCode) |
---|
[82] | 34 | { |
---|
| 35 | $result = array(); |
---|
[105] | 36 | $result['title'] = $this->readQuestionTitle($questionCode); |
---|
| 37 | $result['description'] = $this->readQuestionDescription($questionCode); |
---|
| 38 | $result['type'] = $this->readQuestionType($questionCode); |
---|
| 39 | $result['code'] = $this->readQuestionCode($questionCode); |
---|
| 40 | $result['category'] = $this->readQuestionCategory($questionCode); |
---|
| 41 | $result['answers'] = $this->readQuestionAnswers($questionCode); |
---|
[82] | 42 | |
---|
| 43 | return $result; |
---|
| 44 | } |
---|
[105] | 45 | |
---|
[91] | 46 | public function readQuestionCodes() |
---|
| 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'); |
---|
[82] | 56 | |
---|
[91] | 57 | $querystring = ' |
---|
| 58 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 59 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 60 | SELECT ?questionCode |
---|
| 61 | WHERE |
---|
| 62 | { |
---|
| 63 | _question predicates:resource_type resources:question ; |
---|
| 64 | predicates:question_code ?questionCode |
---|
| 65 | }'; |
---|
| 66 | |
---|
| 67 | $result = $tempmodel->sparqlQuery($querystring); |
---|
| 68 | } |
---|
| 69 | return $result; |
---|
| 70 | } |
---|
| 71 | |
---|
[105] | 72 | public function readQuestionTitle($questionCode) |
---|
[82] | 73 | { |
---|
| 74 | $querystring = ' |
---|
| 75 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 76 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 77 | SELECT ?questionTitle |
---|
| 78 | WHERE |
---|
[105] | 79 | { |
---|
[82] | 80 | _question predicates:resource_type resources:question ; |
---|
[105] | 81 | predicates:question_code "' . $questionCode . '" ; |
---|
[82] | 82 | predicates:title ?questionTitle |
---|
| 83 | }'; |
---|
| 84 | |
---|
| 85 | $result = $this->model->sparqlQuery($querystring); |
---|
| 86 | |
---|
| 87 | return $result; |
---|
| 88 | } |
---|
| 89 | |
---|
[105] | 90 | public function readQuestionDescription($questionCode) |
---|
[82] | 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 ; |
---|
[105] | 99 | predicates:question_code "' . $questionCode . '" ; |
---|
[82] | 100 | predicates:description ?questionDescription |
---|
| 101 | }'; |
---|
| 102 | |
---|
| 103 | $result = $this->model->sparqlQuery($querystring); |
---|
| 104 | |
---|
| 105 | return $result; |
---|
| 106 | } |
---|
| 107 | |
---|
[105] | 108 | public function readQuestionType($questionCode) |
---|
[82] | 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 ; |
---|
[105] | 117 | predicates:question_code "' . $questionCode . '" ; |
---|
[82] | 118 | predicates:question_type ?questionType |
---|
| 119 | }'; |
---|
| 120 | |
---|
| 121 | $result = $this->model->sparqlQuery($querystring); |
---|
| 122 | |
---|
| 123 | return $result; |
---|
| 124 | } |
---|
[105] | 125 | |
---|
| 126 | public function readQuestionCategory($questionCode) |
---|
[82] | 127 | { |
---|
| 128 | $querystring = ' |
---|
| 129 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 130 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 131 | SELECT ?questionCategory |
---|
| 132 | WHERE |
---|
| 133 | { |
---|
| 134 | _question predicates:resource_type resources:question ; |
---|
[105] | 135 | predicates:question_code "' . $questionCode . '" ; |
---|
[82] | 136 | predicates:question_category ?questionCategory |
---|
| 137 | }'; |
---|
| 138 | |
---|
| 139 | $result = $this->model->sparqlQuery($querystring); |
---|
| 140 | |
---|
| 141 | return $result; |
---|
| 142 | } |
---|
| 143 | |
---|
[105] | 144 | public function readQuestionAnswers($questionCode) |
---|
[82] | 145 | { |
---|
| 146 | $querystring = ' |
---|
| 147 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 148 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 149 | SELECT ?answerDescription |
---|
| 150 | WHERE |
---|
| 151 | { |
---|
| 152 | _question predicates:resource_type resources:question ; |
---|
[105] | 153 | predicates:question_code "' . $questionCode . '" ; |
---|
[85] | 154 | predicates:has_answer ?answerDescription |
---|
[82] | 155 | }'; |
---|
| 156 | |
---|
| 157 | $result = $this->model->sparqlQuery($querystring); |
---|
| 158 | |
---|
| 159 | return $result; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | ?> |
---|