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