[149] | 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 | */ |
---|
[171] | 12 | class AnswerConnector extends Connector{ |
---|
[149] | 13 | |
---|
| 14 | /** |
---|
| 15 | * Constructor for AnswerConnector |
---|
| 16 | */ |
---|
| 17 | public function __construct() { |
---|
| 18 | //Ensure the required folder for this connector exists |
---|
| 19 | if (!is_dir('data/answers/')) |
---|
[171] | 20 | mkdir('data/answers/'); |
---|
| 21 | $this->fileName = 'data/answers/answers.rdf'; |
---|
[149] | 22 | } |
---|
| 23 | |
---|
| 24 | /** |
---|
[158] | 25 | * function get() |
---|
| 26 | * @param type $arguments: An array having one or more of the following elements: |
---|
| 27 | * 'uid', 'question', 'values'. |
---|
[149] | 28 | */ |
---|
| 29 | public function get($arguments) |
---|
| 30 | { |
---|
| 31 | $this->load(); |
---|
| 32 | //Create the querystring |
---|
| 33 | $querystring = ' |
---|
| 34 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 35 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 36 | SELECT DISTINCT ?uid, ?question_code |
---|
| 37 | WHERE |
---|
| 38 | { |
---|
| 39 | _answer predicates:resource_type resources:answer; |
---|
| 40 | |
---|
| 41 | predicates:uid ?uid ; |
---|
[157] | 42 | predicates:question_code ?question_code ; |
---|
[171] | 43 | ' . $this->createArguments($arguments) . ' |
---|
[149] | 44 | }'; |
---|
| 45 | |
---|
| 46 | //Query the model |
---|
| 47 | $results = $this->model->sparqlQuery($querystring); |
---|
| 48 | //An answer can have multiple values, get all these values for the answer instances found. |
---|
| 49 | $answers = array(); |
---|
| 50 | if(!empty($results)) |
---|
| 51 | { |
---|
| 52 | foreach($results as $result) |
---|
| 53 | { |
---|
[186] | 54 | $answers[] = new Answer($result['?uid']->label, $result['?question_code']->label, $this->getValues($result['?uid']->label)); |
---|
[149] | 55 | } |
---|
| 56 | } |
---|
| 57 | return $answers; |
---|
| 58 | } |
---|
[171] | 59 | |
---|
[149] | 60 | /** |
---|
[158] | 61 | * function getValues() |
---|
| 62 | * @param type $uid : The uid of the Answer for which the values should be gotten. |
---|
[149] | 63 | */ |
---|
| 64 | private function getValues($uid) |
---|
| 65 | { |
---|
[171] | 66 | $result = $this->model->findRegex("[(".$uid.")]", "[(answered)]", null); |
---|
| 67 | $iterator = $result->getStatementIterator(); |
---|
| 68 | $values = array(); |
---|
| 69 | while($iterator->hasNext()) |
---|
[149] | 70 | { |
---|
[171] | 71 | $element = $iterator->next(); |
---|
| 72 | $values[] = $element->getLabelObject(); |
---|
[149] | 73 | } |
---|
[171] | 74 | return $values; |
---|
[149] | 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
[158] | 78 | * function set() |
---|
| 79 | * @param type $rToolObject : The ResearchToolObject to be saved. |
---|
[149] | 80 | */ |
---|
| 81 | public function set($rToolObject) |
---|
| 82 | { |
---|
[186] | 83 | $this->load(); |
---|
| 84 | //If evaluation fails, some references are incorrect. |
---|
| 85 | //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed. |
---|
| 86 | //TODO: Decide how to fix invalid references graciously. |
---|
| 87 | if(!$rToolObject->evaluate()) |
---|
| 88 | return false; |
---|
[149] | 89 | $resourceAnswer = new Resource(ANSWER.'/'.$rToolObject->uid); |
---|
| 90 | //Remove the old value stored with the given id |
---|
| 91 | $this->model->subtract($this->model->find($resourceAnswer, null, null)); |
---|
| 92 | |
---|
| 93 | //Save all the new values |
---|
| 94 | $resourceAnswerType = new Resource(ANSWER); |
---|
| 95 | $predicateRType = new Resource(RTYPE); |
---|
| 96 | $this->model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType)); |
---|
| 97 | |
---|
| 98 | $answerId = new Literal($rToolObject->uid); |
---|
| 99 | $predicateId = new Resource(UID); |
---|
| 100 | $this->model->add(new Statement($resourceAnswer, $predicateId, $answerId)); |
---|
| 101 | |
---|
| 102 | $questionId = new Literal($rToolObject->question->uid); |
---|
| 103 | $predicateQId = new Resource(QCODE); |
---|
| 104 | $this->model->add(new Statement($resourceAnswer, $predicateQId, $questionId)); |
---|
| 105 | |
---|
| 106 | if(isset($rToolObject->values)) |
---|
| 107 | { |
---|
| 108 | foreach($rToolObject->values as $value) |
---|
| 109 | { |
---|
| 110 | $answerValue = new Literal($value); |
---|
| 111 | $predicateAnswer = new Resource(ANSWERED); |
---|
| 112 | $this->model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue)); |
---|
| 113 | } |
---|
| 114 | } |
---|
[186] | 115 | $this->save(); |
---|
| 116 | return true; |
---|
[149] | 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | ?> |
---|