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 Answer |
---|
9 | * |
---|
10 | * @author jkraaijeveld |
---|
11 | */ |
---|
12 | class Answer extends ResearchToolObject { |
---|
13 | public static $filename = 'data/results/answers.rdf'; |
---|
14 | |
---|
15 | public $question; |
---|
16 | public $values; |
---|
17 | |
---|
18 | /** |
---|
19 | * Constructor for an Answer object |
---|
20 | * @param type $uid : The uid of the Answer object. |
---|
21 | * @param type $question : The Question object this Answer answers. |
---|
22 | * @param type $values : An array of strings, containing the answers. |
---|
23 | */ |
---|
24 | public function __construct($uid = null, $question = null, $values = null) { |
---|
25 | if(!isset($uid)) |
---|
26 | { |
---|
27 | $uid = md5(uniqid(rand(), true)); |
---|
28 | } |
---|
29 | $this->uid = $uid; |
---|
30 | $this->question = $question; |
---|
31 | $this->values = $values; |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * function evaluate() |
---|
36 | * Evaluates the references and fills in the objects for them instead. |
---|
37 | */ |
---|
38 | public function evaluate() |
---|
39 | { |
---|
40 | if(is_string($this->question)) |
---|
41 | { |
---|
42 | $result = Question::get(array("uid" => $this->question)); |
---|
43 | if(!isset($result[0])) |
---|
44 | return false; |
---|
45 | $this->question = $result[0]; |
---|
46 | } |
---|
47 | return true; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | /** |
---|
52 | * function get() |
---|
53 | * @param type $arguments: An array having one or more of the following elements: |
---|
54 | * 'uid', 'question', 'values'. |
---|
55 | */ |
---|
56 | public static function get($arguments) |
---|
57 | { |
---|
58 | $model = ResearchToolObject::load(Answer::$filename); |
---|
59 | //Create the querystring |
---|
60 | $querystring = ' |
---|
61 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
62 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
63 | SELECT DISTINCT ?uid, ?question_code |
---|
64 | WHERE |
---|
65 | { |
---|
66 | _answer predicates:resource_type resources:answer; |
---|
67 | |
---|
68 | predicates:uid ?uid ; |
---|
69 | predicates:question_code ?question_code ; |
---|
70 | ' . ResearchToolObject::createArguments($arguments) . ' |
---|
71 | }'; |
---|
72 | |
---|
73 | //Query the model |
---|
74 | $results = $model->sparqlQuery($querystring); |
---|
75 | //An answer can have multiple values, get all these values for the answer instances found. |
---|
76 | $answers = array(); |
---|
77 | if(!empty($results)) |
---|
78 | { |
---|
79 | foreach($results as $result) |
---|
80 | { |
---|
81 | $answers[] = new Answer($result['?uid']->label, $result['?question_code']->label, Answer::getValues($model, $result['?uid']->label)); |
---|
82 | } |
---|
83 | } |
---|
84 | return $answers; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * function getValues() |
---|
89 | * @param type $uid : The uid of the Answer for which the values should be gotten. |
---|
90 | */ |
---|
91 | private static function getValues($model, $uid) |
---|
92 | { |
---|
93 | $result = $model->findRegex("[(".$uid.")]", "[(answered)]", null); |
---|
94 | $iterator = $result->getStatementIterator(); |
---|
95 | $values = array(); |
---|
96 | while($iterator->hasNext()) |
---|
97 | { |
---|
98 | $element = $iterator->next(); |
---|
99 | $values[] = $element->getLabelObject(); |
---|
100 | } |
---|
101 | return $values; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * function save() |
---|
106 | * Saves the current object into the database. |
---|
107 | */ |
---|
108 | public function save() |
---|
109 | { |
---|
110 | //If evaluation fails, some references are incorrect. |
---|
111 | //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed. |
---|
112 | //TODO: Decide how to fix invalid references graciously. |
---|
113 | if(!$this->evaluate()) |
---|
114 | throw new Exception('Evaluation failed.'); |
---|
115 | |
---|
116 | //Ensure the required folder exists. |
---|
117 | if(!is_dir('data/results')) |
---|
118 | mkdir('data/results'); |
---|
119 | $model = ResearchToolObject::load(Answer::$filename); |
---|
120 | |
---|
121 | $resourceAnswer = new Resource(ANSWER.'/'.$this->uid); |
---|
122 | //Remove the old value stored with the given id |
---|
123 | $model->subtract($model->find($resourceAnswer, null, null)); |
---|
124 | |
---|
125 | //Save all the new values |
---|
126 | $resourceAnswerType = new Resource(ANSWER); |
---|
127 | $predicateRType = new Resource(RTYPE); |
---|
128 | $model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType)); |
---|
129 | |
---|
130 | $answerId = new Literal($this->uid); |
---|
131 | $predicateId = new Resource(UID); |
---|
132 | $model->add(new Statement($resourceAnswer, $predicateId, $answerId)); |
---|
133 | |
---|
134 | $questionId = new Literal($this->question->uid); |
---|
135 | $predicateQId = new Resource(QCODE); |
---|
136 | $model->add(new Statement($resourceAnswer, $predicateQId, $questionId)); |
---|
137 | |
---|
138 | if(isset($this->values)) |
---|
139 | { |
---|
140 | foreach($this->values as $value) |
---|
141 | { |
---|
142 | $answerValue = new Literal($value); |
---|
143 | $predicateAnswer = new Resource(ANSWERED); |
---|
144 | $model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue)); |
---|
145 | } |
---|
146 | } |
---|
147 | $model->saveAs(Answer::$filename, 'rdf'); |
---|
148 | return true; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | ?> |
---|