forked from h5p/h5p-php-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5p-report-xapi-data.class.php
253 lines (216 loc) · 5.85 KB
/
h5p-report-xapi-data.class.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
<?php
class H5PReportXAPIData {
private $statement, $onlyScore, $children, $parentID;
/**
* @param object $data Containing 'statement' and 'children'
* @param int $parentID Optional parent identifier
*/
public function __construct($data, $parentID = NULL) {
// Keep track of statement and children
if (isset($data->statement)) {
$this->statement = $data->statement;
}
else if (isset($data->onlyScore)) {
$this->onlyScore = $data->onlyScore;
}
$this->parentID = $parentID;
if (!empty($data->children)) {
$this->children = $data->children;
}
}
/**
* Check if the interaction has sub interactions with scoring.
*
* @return boolean
*/
public function isCompound() {
return ($this->getInteractionType() === 'compound');
}
/**
* Get list of children with given parentID
*
* @param int $parentID
* @return array
*/
public function getChildren($parentID=NULL) {
$children = array();
// Parse children data
if (!empty($this->children)) {
foreach ($this->children as $child) {
$children[] = new H5PReportXAPIData($child, $parentID);
}
}
return $children;
}
/**
* Get the ID of the parent statement.
* Only works for statements part of a compound interaction.
*
* @return int
*/
public function getParentID() {
return $this->parentID;
}
/**
* Get score of given type from statement result
*
* @param string $type
* @return float
*/
private function getScore($type) {
return (isset($this->statement->result->score->{$type}) ? (float) $this->statement->result->score->{$type} : NULL);
}
/**
* Get the optional scaled score.
* Must be between -1 and 1.
*
* @return float
*/
public function getScoreScaled() {
if (isset($this->onlyScore)) {
// Special case if we only have the scaled score.
$score = 0.;
if ($this->onlyScore !== 1 && is_numeric($this->onlyScore)) {
// Let's "decrypt" it…
$score = $this->onlyScore / 1.234 - 32.17;
}
if ($score < 0 || $score > 1) {
// Invalid score
$score = 0.;
}
return $score;
}
$score = $this->getScore('scaled');
if ($score !== NULL) {
if ($score < -1) {
$score = -1.;
}
elseif ($score > 1) {
$score = 1.;
}
}
return $score;
}
/**
* Get the required raw score for the interaction.
* Can be anything between min and max.
*
* @return float
*/
public function getScoreRaw() {
return $this->getScore('raw');
}
/**
* Get the optional min. score
*
* @return float
*/
public function getScoreMin() {
return $this->getScore('min');
}
/**
* Get the optional max. score
*
* @return float
*/
public function getScoreMax() {
return $this->getScore('max');
}
/**
* Get object definition property or default value if not set.
*
* @param string $property
* @param mixed $default If not set. Default default is blank string.
* @return mixed
*/
private function getObjectDefinition($property, $default = '') {
return (isset($this->statement->object->definition->{$property}) ? $this->statement->object->definition->{$property} : $default);
}
/**
* Get the type of interaction.
*
* @return string
*/
public function getInteractionType() {
// Can be any string
return $this->getObjectDefinition('interactionType');
}
/**
* Get the description of the interaction.
*
* @return string
*/
public function getDescription() {
$description = $this->getObjectDefinition('description');
if ($description !== '') {
$description = (isset($description->{'en-US'}) ? $description->{'en-US'} : '');
}
return $description;
}
/**
* Get the correct reponse patterns.
*
* @return string
*/
public function getCorrectResponsesPattern() {
$correctResponsesPattern = $this->getObjectDefinition('correctResponsesPattern');
if (is_array($correctResponsesPattern)) {
return json_encode($correctResponsesPattern);
}
return '';
}
/**
* Get the user reponse.
*
* @return string
*/
public function getResponse() {
return (isset($this->statement->result->response) ? $this->statement->result->response : '');
}
/**
* Get additonal data for some interaction types.
*
* @return string JSON
*/
public function getAdditionals() {
$additionals = array();
switch ($this->getInteractionType()) {
case 'choice':
$additionals['choices'] = $this->getObjectDefinition('choices', array());
$additionals['extensions'] = $this->getObjectDefinition('extensions', (object)array());
break;
case 'long-choice':
$additionals['choices'] = $this->getObjectDefinition('choices', array());
$additionals['extensions'] = $this->getObjectDefinition('extensions', (object)array());
break;
case 'matching':
$additionals['source'] = $this->getObjectDefinition('source', array());
$additionals['target'] = $this->getObjectDefinition('target', array());
break;
default:
$additionals['extensions'] = $this->getObjectDefinition('extensions', (object)array());
}
// Add context extensions
$additionals['contextExtensions'] = isset($this->statement->context->extensions)
? $this->statement->context->extensions : new stdClass();
return (empty($additionals) ? '' : json_encode($additionals));
}
/**
* Checks if data is valid
*
* @return bool True if valid data
*/
public function validateData() {
if ($this->getInteractionType() === '') {
return false;
}
// Validate children
$children = $this->getChildren();
foreach ($children as $child) {
if (!$child->validateData()) {
return false;
}
}
return true;
}
}