-
Notifications
You must be signed in to change notification settings - Fork 6
/
class-xapidata.php
346 lines (294 loc) · 10.6 KB
/
class-xapidata.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
340
341
342
343
344
345
346
<?php
namespace H5PXAPIKATCHU;
/**
* Pseudo xAPI
* The goal of this class is not to comply to the xAPI specification, but to
* create output that's useful for the user.
*
* https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md
*
* @package H5PXAPIKATCHU
* @since 0.1
*/
class XAPIDATA {
private $raw;
private $data;
/**
* Constructor
* @param string $xapi Data to work with.
*/
public function __construct( $xapi ) {
// Change from JavaScript
$xapi = str_replace( '\"', '"', $xapi );
$xapi = str_replace( "\'", "'", $xapi );
$xapi = str_replace( '\\\\"', '"', $xapi );
$this->raw = $xapi;
$this->data = json_decode( $xapi, true );
}
/**
* Get raw xAPI data.
*/
public function get_raw_xapi() {
return $this->$raw;
}
/**
* Get actor data from xAPI statement.
* @return array Actor data.
*/
public function get_actor() {
if ( is_array( $this->data ) && array_key_exists( 'actor', $this->data ) ) {
$actor = $this->data['actor'];
$object_type = ( array_key_exists( 'objectType', $actor ) ) ? $actor['objectType'] : '';
$inverse_functional_identifier = $this->flatten_inverse_functional_identifier( $actor );
$name = ( array_key_exists( 'name', $actor ) ) ? $actor['name'] : '';
$members = ( array_key_exists( 'member', $actor ) ) ? $this->flatten_members( $actor['member'] ) : '';
// Identified Group or Anonymous Group (we don't need to distinguish here)
if ( 'Group' === $object_type ) {
$name = ( '' === $name ) ? $name : $name . ' (' . __( 'Group', 'H5PXAPIKATCHU' ) . ')';
}
//Agent
if ( 'Agent' === $object_type || '' === $object_type ) {
// Not really neccessary, but according to xAPI specs agents have no member data
$members = '';
}
}
return array(
'inverseFunctionalIdentifier' => isset( $inverse_functional_identifier ) ? $inverse_functional_identifier : null,
'name' => isset( $name ) ? $name : null,
'members' => isset( $members ) ? $members : null,
);
}
/**
* Get verb data from xAPI statement.
* @return array Verb data.
*/
public function get_verb() {
if ( is_array( $this->data ) && array_key_exists( 'verb', $this->data ) ) {
$verb = $this->data['verb'];
$id = array_key_exists( 'id', $verb ) ? $verb['id'] : '';
$display = array_key_exists( 'display', $verb ) ? $this->get_locale_string( $verb['display'] ) : '';
}
return array(
'id' => ( isset( $id ) ) ? $id : null,
'display' => ( isset( $display ) ) ? $display : null,
);
}
/**
* Get object data from xAPI statement.
* @return array Object data.
*/
public function get_object() {
if ( is_array( $this->data ) && array_key_exists( 'object', $this->data ) ) {
$object = $this->data['object'];
$id = array_key_exists( 'id', $object ) ? $object['id'] : '';
$definition = array_key_exists( 'definition', $object ) ? $this->get_definition( $object['definition'] ) : '';
if ( '' !== $definition ) {
$name = $definition['name'];
$description = $definition['description'];
$choices = $definition['choices'];
$correct_responses_pattern = $definition['correctResponsesPattern'];
}
}
return array(
'id' => isset( $id ) ? $id : '',
'name' => isset( $name ) ? $name : '',
'description' => isset( $description ) ? $description : '',
'choices' => isset( $choices ) ? $choices : '',
'correctResponsesPattern' => isset( $correct_responses_pattern ) ? $correct_responses_pattern : '',
);
}
/**
* Get result data from xAPI statement.
* @return array Result data.
*/
public function get_result() {
if ( is_array( $this->data ) && array_key_exists( 'result', $this->data ) ) {
$result = $this->data['result'];
$response = array_key_exists( 'response', $result ) ? $result['response'] : '';
$scores = array_key_exists( 'score', $result ) ? $this->get_scores( $result['score'] ) : '';
if ( '' !== $scores ) {
$score_raw = $scores['score_raw'];
$score_scaled = $scores['score_scaled'];
}
$completion = array_key_exists( 'completion', $result ) ? $result['completion'] : '';
$success = array_key_exists( 'success', $result ) ? $result['success'] : '';
$duration = array_key_exists( 'duration', $result ) ? $result['duration'] : '';
}
return array(
'response' => isset( $response ) ? $response : null,
'score_raw' => isset( $score_raw ) ? $score_raw : null,
'score_scaled' => isset( $score_scaled ) ? $score_scaled : null,
'completion' => isset( $completion ) ? $completion : false,
'success' => isset( $success ) ? $success : false,
'duration' => isset( $duration ) ? $duration : null,
);
}
/**
* Flatten xAPI member object.
* @param array $members The members object.
* @return string Flattened member object.
*/
private function flatten_members( $members ) {
if ( ! is_array( $members ) || empty( $members ) ) {
return '';
}
$output = array();
foreach ( $members as $member ) {
array_push( $output, $this->flatten_agent( $member ) );
}
return implode( ', ', $output );
}
/**
* Flatten xAPI agent object.
* @param array $agent The agent object.
* @return string Agent data.
*/
private function flatten_agent( $agent ) {
if ( ! is_array( $agent ) || empty( $agent ) ) {
return '';
}
$name = ( array_key_exists( 'name', $agent ) ) ? $agent['name'] : '';
$ifi = $this->flatten_inverse_functional_identifier( $agent );
if ( '' !== $name && '' !== $ifi ) {
$name = ' (' . $name . ')';
}
return $ifi . $name;
}
/**
* Flatten xAPI InverseFunctionalIdentifier object.
* @param array $actor The actor object.
* @return string Flattened InverseFunctionalIdentifier.
*/
private function flatten_inverse_functional_identifier( $actor ) {
if ( ! is_array( $actor ) || empty( $actor ) ) {
return '';
}
$inverse_functional_identifier = array();
if ( array_key_exists( 'mbox', $actor ) ) {
array_push( $inverse_functional_identifier, __( 'email', 'H5PXAPIKATCHU' ) . ': ' . $actor['mbox'] );
}
if ( array_key_exists( 'mbox_sha1sum', $actor ) ) {
array_push( $inverse_functional_identifier, __( 'email hash', 'H5PXAPIKATCHU' ) . ': ' . $actor['mbox_sha1sum'] );
}
if ( array_key_exists( 'openid', $actor ) ) {
array_push( $inverse_functional_identifier, __( 'openid', 'H5PXAPIKATCHU' ) . ': ' . $actor['openid'] );
}
if ( array_key_exists( 'account', $actor ) ) {
array_push( $inverse_functional_identifier, __( 'account', 'H5PXAPIKATCHU' ) . ': ' . $this->flatten_account( $actor['account'] ) );
}
return ( empty( $inverse_functional_identifier ) ) ? '' : implode( ', ', $inverse_functional_identifier );
}
/**
* Flatten xAPI account object.
* @param array $account The accout object.
* @return string Flattened account object.
*/
private function flatten_account( $account ) {
if ( ! is_array( $account ) || empty( $account ) ) {
return '';
}
$name = ( array_key_exists( 'name', $account ) ) ? $account['name'] : '';
$homepage = ( array_key_exists( 'homePage', $account ) ) ? $account['homePage'] : '';
if ( '' !== $name && '' !== $homepage ) {
$homepage = ' (' . $homepage . ')';
}
return $name . $homepage;
}
/**
* Get local string from xAPI language map object.
* @param array $language_map The language map.
* @return string Local string.
*/
private function get_locale_string( $language_map ) {
if ( ! is_array( $language_map ) || empty( $language_map ) ) {
return '';
}
$locale_default = 'en-US';
$locale = str_replace( '_', '-', get_locale() );
if ( array_key_exists( $locale, $language_map ) ) {
return $language_map[ $locale ];
}
if ( array_key_exists( $locale, $language_map ) ) {
return $language_map[ $locale_default ];
}
return array_values( $language_map )[0];
}
/**
* Get xAPI description object.
* @param array $definition The definition.
* @return array Description object.
*/
private function get_definition( $definition ) {
if ( is_array( $definition ) ) {
$name = array_key_exists( 'name', $definition ) ?
$this->get_locale_string( $definition['name'] ) :
'';
$description = array_key_exists( 'description', $definition ) ?
$this->get_locale_string( $definition['description'] ) :
'';
$choices = array_key_exists( 'choices', $definition ) ?
$this->flatten_choices( $definition['choices'] ) :
'';
$correct_responses_pattern = array_key_exists( 'correctResponsesPattern', $definition ) ?
$this->flatten_correct_responses_pattern( $definition['correctResponsesPattern'] ) :
'';
}
return array(
'name' => isset( $name ) ? $name : '',
'description' => isset( $description ) ? $description : '',
'choices' => isset( $choices ) ? $choices : '',
'correctResponsesPattern' => isset( $correct_responses_pattern ) ? $correct_responses_pattern : '',
);
}
/**
* Flatten xAPI choices object.
* @param array $choices Choices object.
* @return string Flattened choices object.
*/
private function flatten_choices( $choices ) {
if ( ! is_array( $choices ) || empty( $choices ) ) {
return '';
}
$output = array();
foreach ( $choices as $choice ) {
$id = array_key_exists( 'id', $choice ) ?
$choice['id'] :
'';
$description = array_key_exists( 'description', $choice ) ?
$this->get_locale_string( $choice['description'] ) :
'';
array_push( $output, '[' . $id . '] ' . $description );
}
return implode( ', ', $output );
}
/**
* Flatten xAPI correctResponsesPattern object.
* @param array $correct_responses_patterns Correct pattern object.
* @return string Flattened correct responses pattern.
*/
private function flatten_correct_responses_pattern( $correct_responses_patterns ) {
if ( ! is_array( $correct_responses_patterns ) || empty( $correct_responses_patterns ) ) {
return '';
}
$output = array();
foreach ( $correct_responses_patterns as $key => $pattern ) {
array_push( $output, '[' . $key . ']: ' . $pattern );
}
return implode( ', ', $output );
}
/**
* Get score details from xAPI score object.
* @param array $scores The scores.
* @return array scores.
*/
private function get_scores( $scores ) {
if ( is_array( $scores ) ) {
$score_raw = array_key_exists( 'raw', $scores ) ? $scores['raw'] : '';
$score_scaled = array_key_exists( 'scaled', $scores ) ? $scores['scaled'] : '';
}
return array(
'score_raw' => isset( $score_raw ) ? $score_raw : '',
'score_scaled' => isset( $score_scaled ) ? $score_scaled : '',
);
}
}