[141] | 1 | <?php |
---|
| 2 | // Survey database interface class as intermediate for storing data from the site to the RDF database |
---|
| 3 | require_once 'rdfConstants.php'; |
---|
| 4 | // Include RAP Library to write RDF files |
---|
| 5 | include(RDFAPI_INCLUDE_DIR . "RDFAPI.php"); |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * Description of AnswerConnector |
---|
| 9 | * |
---|
| 10 | * @author jkraaijeveld |
---|
| 11 | */ |
---|
| 12 | class AnswerConnector implements IConnector{ |
---|
| 13 | protected $model; |
---|
[143] | 14 | protected $fileName = 'data/answers/answers.rdf'; |
---|
| 15 | protected $db; |
---|
[141] | 16 | |
---|
| 17 | /** |
---|
| 18 | * Constructor for AnswerConnector |
---|
| 19 | */ |
---|
| 20 | public function __construct() { |
---|
| 21 | //Ensure the required folder for this connector exists |
---|
| 22 | if (!is_dir('data/answers/')) |
---|
| 23 | mkdir('data/answers/'); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * function load() |
---|
| 28 | * Loads the file into the standard MemModel. |
---|
| 29 | */ |
---|
| 30 | public function load() { |
---|
| 31 | //Get the Memory Model from the ModelFactory |
---|
| 32 | $this->model = ModelFactory::getDefaultModel(); |
---|
| 33 | |
---|
| 34 | //Ensure the required file exists before loading |
---|
| 35 | if(file_exists($this->fileName)) |
---|
| 36 | $this->model->load($this->fileName); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * function save() |
---|
| 41 | * Saves the MemModel into the given file. |
---|
| 42 | */ |
---|
| 43 | public function save() { |
---|
| 44 | $this->model->saveAs($this->fileName,'rdf'); |
---|
| 45 | } |
---|
| 46 | |
---|
[143] | 47 | /** |
---|
| 48 | * Function get() |
---|
| 49 | * Get the answers corresponding to the arguments |
---|
| 50 | * 'Question' argument is supposed to be the question ID |
---|
| 51 | * 'Values' has to be an array to make sure multiple answers can be selected. |
---|
| 52 | */ |
---|
[141] | 53 | public function get($arguments) |
---|
[143] | 54 | { |
---|
| 55 | $this->load(); |
---|
| 56 | $keys = array_keys($arguments); |
---|
| 57 | $uid = "?uid"; $question = "?question_code"; $value = ""; |
---|
| 58 | if(in_array("uid", $keys)) |
---|
| 59 | $uid = '\''.$arguments["uid"].'\''; |
---|
| 60 | if(in_array("question", $keys)) |
---|
| 61 | $question = '\''.$arguments["question"].'\''; |
---|
| 62 | if(in_array("values", $keys)) |
---|
| 63 | { |
---|
| 64 | foreach ($arguments["values"] as $val) |
---|
| 65 | { |
---|
| 66 | $value = $value . 'predicates:answered \'' . $val . '\' '; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | //Create the querystring |
---|
| 70 | $querystring = ' |
---|
| 71 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 72 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 73 | SELECT DISTINCT ?uid, ?question_code |
---|
| 74 | WHERE |
---|
| 75 | { |
---|
| 76 | _answer predicates:resource_type resources:answer; |
---|
| 77 | |
---|
| 78 | predicates:uid ?uid ; |
---|
| 79 | predicates:question_code ?question_code; |
---|
| 80 | |
---|
| 81 | predicates:uid ' . $uid . ' |
---|
| 82 | predicates:question_code ' . $question . ' |
---|
| 83 | ' . $value . ' |
---|
| 84 | }'; |
---|
| 85 | |
---|
| 86 | //Query the model |
---|
| 87 | $results = $this->model->sparqlQuery($querystring); |
---|
| 88 | //An answer can have multiple values, get all these values for the answer instances found. |
---|
| 89 | $answers = array(); |
---|
| 90 | if(!empty($results)) |
---|
| 91 | { |
---|
| 92 | $this->db = new DatabaseInterface(); |
---|
| 93 | foreach($results as $result) |
---|
| 94 | { |
---|
| 95 | $values = $this->getValues($result['?uid']->label); |
---|
| 96 | $questionResult = $this->db->get("question", array("uid" => $result['?question_code']->label)); |
---|
| 97 | $answers[] = new Answer($result['?uid']->label, $questionResult[0], $values); |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | return $answers; |
---|
| 101 | |
---|
| 102 | } |
---|
| 103 | /** |
---|
| 104 | * Gets the values that belong to the answer |
---|
| 105 | * specified by the UID |
---|
| 106 | */ |
---|
| 107 | private function getValues($uid) |
---|
| 108 | { |
---|
| 109 | $querystring = ' |
---|
| 110 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 111 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 112 | SELECT DISTINCT ?answered |
---|
| 113 | WHERE |
---|
| 114 | { |
---|
| 115 | _answer predicates:resource_type resources:answer ; |
---|
| 116 | predicates:answered ?answered ; |
---|
| 117 | predicates:uid \''. $uid . '\' |
---|
| 118 | }'; |
---|
| 119 | $values = $this->model->sparqlQuery($querystring); |
---|
| 120 | $returnArray = array(); |
---|
| 121 | if(!empty($values)) |
---|
| 122 | { |
---|
| 123 | foreach($values as $value) |
---|
| 124 | { |
---|
| 125 | $returnArray[] = $value['?answered']->label; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | return $returnArray; |
---|
| 129 | } |
---|
| 130 | |
---|
[141] | 131 | /** |
---|
[143] | 132 | * Saves the answer Object to the RDF file. |
---|
[141] | 133 | * @param type $rToolObject |
---|
| 134 | */ |
---|
| 135 | public function set($rToolObject) |
---|
| 136 | { |
---|
[143] | 137 | $this->load(); |
---|
[141] | 138 | |
---|
[143] | 139 | $resourceAnswer = new Resource(ANSWER.'/'.$rToolObject->uid); |
---|
| 140 | //Remove the old value stored with the given id |
---|
| 141 | $this->model->subtract($this->model->find($resourceAnswer, null, null)); |
---|
| 142 | |
---|
| 143 | //Save all the new values |
---|
| 144 | $resourceAnswerType = new Resource(ANSWER); |
---|
| 145 | $predicateRType = new Resource(RTYPE); |
---|
| 146 | $this->model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType)); |
---|
| 147 | |
---|
| 148 | $answerId = new Literal($rToolObject->uid); |
---|
| 149 | $predicateId = new Resource(UID); |
---|
| 150 | $this->model->add(new Statement($resourceAnswer, $predicateId, $answerId)); |
---|
| 151 | |
---|
| 152 | $questionId = new Literal($rToolObject->question->uid); |
---|
| 153 | $predicateQId = new Resource(QCODE); |
---|
| 154 | $this->model->add(new Statement($resourceAnswer, $predicateQId, $questionId)); |
---|
| 155 | |
---|
| 156 | if(isset($rToolObject->values)) |
---|
| 157 | { |
---|
| 158 | foreach($rToolObject->values as $value) |
---|
| 159 | { |
---|
| 160 | $answerValue = new Literal($value); |
---|
| 161 | $predicateAnswer = new Resource(ANSWERED); |
---|
| 162 | $this->model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue)); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | $this->save(); |
---|
[141] | 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | ?> |
---|