-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathor_xform_results.php
339 lines (299 loc) · 11 KB
/
or_xform_results.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* ORXFormResults
*/
class OR_xform_results {
/**
* Language to use when parsing the file.
* To lighten the process, the data is returned in only one language
* defined by the preferred languages.
*/
protected $preferred_language = 'English';
/**
* Original SimpleXMLElement
*/
protected $xform = NULL;
/**
* Existing languages.
*/
protected $languages = array();
/**
* The xform after being flatten.
* Will be an array keyed with the question's path.
* EG:
* /survey_7_xls/q0_3
* machine_name - The question's machine name.
* system - Whether is a system question. Like start_date
* type - Type of the question. Like int, select1
* label - The label. Will be an array if there are multiple languages
* lang - Key is the language, value is the translation.
* items - If is a select question, an array with the items
* key - Item key. Will be an array if there are multiple languages
* lang
*/
protected $flat_xform = array();
/**
* Constructor function.
* Parses the xform with the questions and flattens it to allow
* result parsing later on.
*
* @param string $xform_file
* Path to the file.
*
* @throws Exception
* If the given file is does not exist or is not readable.
*/
function __construct($xform_file) {
// Sanity check.
if (!is_readable($xform_file)) {
throw new Exception('The xform file does not exist or is not readable.');
}
// Load the survey definition file.
$this->xform = simplexml_load_file($xform_file);
// Namespaces are needed in order to do queries.
// Returns all the namespaces used by the elements
$ns = $this->xform->getNamespaces(TRUE);
// When querying we need to provide the namespace (Ex: ns:tag).
// The default namespace has no prefix and that's a problem because
// we can't use it to query. This is solved by registering the default
// namespace giving it a prefix.
$this->xform->registerXPathNamespace('d', $ns['']);
// Extract all the languages available in the file.
$translation_nodes = $this->xform->xpath('//d:itext/d:translation');
foreach ($translation_nodes as $node) {
$this->languages[] = (string)$node['lang'];
}
// Set preferred language.
// By default, English is the preferred language, however if
// it is not available, use the first one found.
if (!empty($this->languages)) {
if (in_array("English", $this->languages)) {
$this->preferred_language = "English";
}
else if (in_array("english", $this->languages)) {
$this->preferred_language = "english";
}
else {
$this->preferred_language = reset($this->languages);
}
}
// Get all questions by querying the bind tag.
// The questions always have a @type attribute and are not @readonly.
$questions = $this->xform->xpath('//d:model/d:bind[not(@readonly) and @type]');
// Create a list with all the questions keyed by the path
// storing the label and, if they exist, the options.
// Note: All the values need to be converted to string, otherwise
// they are stores as xml object.
$list = array();
foreach($questions as $k => $v){
$path = (string)$v['nodeset'];
$type = (string)$v['type'];
$path_pieces = explode('/', $path);
$machine_label = end($path_pieces);
// Store the question's machine name.
$list[$path]['machine_label'] = $machine_label;
// Store the question's type
$list[$path]['type'] = $type;
// Search the question to get the label and value.
// It will return an array of results.
$question = $this->xform->xpath(sprintf('h:body//*[@ref="%s"]', $path));
if (!$question) {
// The question was not found. Probably a system value
// like the start date. Set the label as the machine name.
$list[$path]['label'] = $machine_label;
$list[$path]['system'] = TRUE;
}
else {
// Only need the first result.
$question = $question[0];
$list[$path]['label'] = $this->_label_translation($question->label);
// Search for items. (EG: Options for checkboxes)
if (isset($question->item)) {
foreach($question->item as $item) {
$list[$path]['items'][(string)$item->value] = $this->_label_translation($item->label);
}
}
}
}
$this->flat_xform = $list;
}
/**
* Set the language for the parsing.
* If there are multiple languages per file, the default will be
* English. If not available will be the first one found.
* @param string $language.
*/
public function set_language($language) {
if (in_array($language, $this->languages)) {
$this->preferred_language = $language;
}
}
/**
* Returns the preferred language or false if it is not translated.
* @return mixed
*/
public function get_preferred_language() {
if ($this->is_translated()) {
return $this->preferred_language;
}
return FALSE;
}
/**
* Checks if the xform is multi-language.
* @return boolean
* Whether there are multiple languages.
*/
public function is_translated() {
return count($this->languages) > 0;
}
/**
* Returns the xform after being flatten.
* @return array
*/
public function get_flatten() {
return $this->flat_xform;
}
/**
* Parses the result file returning the data in the following format:
*
* [index] =>
* label => human readable label
* machine_label => unique, machine readable label
* value => human readable value
* machine_value => answer key
*
* In a case of an open ended question, the value and machine value will be
* the same.
*
* @param string $result_file_path
* Path to the file.
* @throws Exception
* If result file does not exist or is not readable.
*/
public function parse_result_file($result_file_path) {
// Sanity check.
if (!is_readable($result_file_path)) {
throw new Exception('The result file does not exist or is not readable.');
}
// Load the result file.
$result_file_sxe = simplexml_load_file($result_file_path);
return $this->_normalize_result_item($result_file_sxe);
}
protected function _normalize_result_item($item, $path = NULL) {
// Normalised data.
$normalised = array();
// Initialise current path for the first time.
// The current path is need when drilling down.
// it is used to match the questions.
if ($path === NULL) {
$path = '/' . $item->getName();
}
foreach($item as $key => $item_data) {
// Update current_path
$current_path = $path . '/' . (string)$key;
if ($item_data->count()) {
// The node has children. Start recursion.
$normalised = array_merge($normalised, $this->_normalize_result_item($item_data, $current_path));
}
else {
// No children.
// Does the question exist?
if (isset($this->flat_xform[$current_path])) {
$question = $this->flat_xform[$current_path];
// Store results for this question.
$norm = array();
// Store label
$norm['label'] = $this->_get_question_label_translation($question);
$norm['machine_label'] = $question['machine_label'];
// Check if question was answered.
if ($item_data) {
// Check if is a question with items.
if (isset($question['items'])) {
$answer_key = trim((string)$item_data);
// Check if the key exists in the items array.
if (isset($question['items'][$answer_key])) {
// Translation.
$norm['value'] = $this->_get_question_item_translation($question, $answer_key);
$norm['machine_value'] = $answer_key;
}
else {
// The value was not found in the item array.
// This may mean that its a multiple answer question.
// Split by space and search each term.
$answer_pieces = explode(' ', $answer_key);
$norm['value'] = array();
foreach ($answer_pieces as $value) {
if (isset($question['items'][$value])) {
$norm['value'][] = $this->_get_question_item_translation($question, $value);
$norm['machine_value'][] = $value;
}
}
}
}
else {
// It's an open ended question.
// Store value.
$norm['value'] = (string)$item_data;
$norm['machine_value'] = (string)$item_data;
}
}
else {
// The question was not answered
$norm['value'] = NULL;
$norm['machine_value'] = NULL;
}
$normalised[] = $norm;
}
// else skip
}
}
// Return results.
return $normalised;
}
protected function _get_question_label_translation($question) {
if (isset($question['system']) && $question['system']) {
return $question['label'];
}
else if ($this->is_translated()) {
return $question['label'][$this->preferred_language];
}
else {
return $question['label'];
}
}
protected function _get_question_item_translation($question, $item_key) {
return $this->is_translated() ? $question['items'][$item_key][$this->preferred_language] : $question['items'][$item_key];
}
/**
* Gets the translation for the given label.
* It assumes that the label has a @ref attribute with the translation path.
* If the file has no translations the label value is returned.
* @param SimpleXMLElement
* The label element.
* @return mixed
* Array keyed by language with the translated value if there
* are translations, the label value otherwise.
*/
protected function _label_translation($label) {
if (!$this->is_translated()) {
return (string)$label;
}
// The @ref attribute will look like:
// jr:itext('/survey_7_xls/section1/q11_other:label')
$ref = (string)$label['ref'];
// Extract path: /survey_7_xls/section1/q11_other:label
if (!preg_match("/jr:itext\\(\'(.*)\'\\)/", $ref, $matches)) {
return NULL;
}
// Translation path will be the first captured result.
$translation_path = $matches[1];
$translations = array();
foreach ($this->languages as $lang) {
$translation_value = $this->xform->xpath(sprintf('//d:itext/d:translation[@lang="%s"]//*[@id="%s"]/d:value', $lang, $translation_path));
$translations[$lang] = empty($translation_value) ? NULL : (string)$translation_value[0];
}
return $translations;
}
}
// ------------------------------------------------------------------------
/* End of file or_xform_results.php */