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