forked from h5p/h5p-php-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5p-report.class.php
300 lines (252 loc) · 8.19 KB
/
h5p-report.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
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
<?php
/**
* Class H5PReport
* @property fillInProcessor
*/
class H5PReport {
private static $version = '1.1.0';
private static $processorMap = array(
'compound' => 'CompoundProcessor',
'fill-in' => 'FillInProcessor',
'true-false' => 'TrueFalseProcessor',
'matching' => 'MatchingProcessor',
'choice' => 'ChoiceProcessor',
'long-choice' => 'LongChoiceProcessor',
);
private static $versionExtension = 'https://h5p.org/x-api/h5p-reporting-version';
private static $contentTypeExtension = 'https://h5p.org/x-api/h5p-machine-name';
public static $contentTypeProcessors = array(
'H5P.DocumentationTool' => 'DocumentationToolProcessor',
'H5P.GoalsPage' => 'GoalsPageProcessor',
'H5P.GoalsAssessmentPage' => 'GoalsAssessmentPageProcessor',
'H5P.StandardPage' => 'StandardPageProcessor',
'H5P.FreeTextQuestion' => 'IVOpenEndedQuestionProcessor',
);
private $processors = array();
/**
* Generate the proper report depending on xAPI data.
*
* @param object $xapiData
* @param string $forcedProcessor Force a processor type
* @param bool $disableScoring Disables scoring for the report
*
* @return string A report
*/
public function generateReport($xapiData, $forcedProcessor = null, $disableScoring = false) {
$interactionType = $xapiData->interaction_type;
if (!self::isSupportedVersion($xapiData)) {
return self::renderUnsupportedVersionPage($xapiData);
}
$contentTypeProcessor = self::getContentTypeProcessor($xapiData);
if (isset($contentTypeProcessor)) {
$interactionType = $contentTypeProcessor;
}
if (isset($forcedProcessor)) {
$interactionType = $forcedProcessor;
}
if (!isset(self::$processorMap[$interactionType]) && !isset(self::$contentTypeProcessors[$interactionType])) {
return ''; // No processor found
}
if (!isset($this->processors[$interactionType])) {
// Not used before. Initialize new processor
if (array_key_exists($interactionType, self::$contentTypeProcessors)) {
$this->processors[$interactionType] = new self::$contentTypeProcessors[$interactionType]();
}
else {
$this->processors[$interactionType] = new self::$processorMap[$interactionType]();
}
}
// Generate and return report from xAPI data
// Allow compound content types to have styles in case they are rendering gradable containers
return $this->processors[$interactionType]
->generateReport($xapiData, $disableScoring, ($interactionType == "compound" ? true : false));
}
/**
* Generate the proper report for dynamically gradable content types depending on xAPI data.
*
* @param object $xapiData
* @return string A report
*/
public function generateGradableReports($xapiData) {
$results = array();
foreach ($xapiData as $childData) {
$interactionType = self::getContentTypeProcessor($childData);
if (!isset($this->processors[$interactionType])) {
// Not used before. Initialize new processor
if (array_key_exists($interactionType, self::$contentTypeProcessors)) {
$this->processors[$interactionType] = new self::$contentTypeProcessors[$interactionType]();
}
}
if ($interactionType == 'H5P.FreeTextQuestion') {
array_push($results, $childData);
}
}
if (count($results) > 0) {
return self::buildContainer($results);
}
// Return nothing if there are no reports
return ' ';
}
/**
* Generate the wrapping element for a grading container
*
* @param object $results
*
* @return string HTML of the container and within it, gradable elements
*/
private function buildContainer($results) {
$container = '<div id="gradable-container" class="h5p-iv-open-ended-grading-container">';
foreach ($results as $index=>$child) {
$container .= self::buildChild($child, $index);
}
$container .= '</div>';
return $container;
}
/**
* Generate each of the gradable elements
*
* @param object $data
* @param int $index
*
* @return string HTML of a gradable element
*/
private function buildChild($data, $index) {
// Generate and return report from xAPI data
$interactionType = self::getContentTypeProcessor($data);
return $this->processors[$interactionType]
->generateReport($data, false, true);
}
/**
* Removes gradable children from xAPI data
*
* @return array
*/
public function stripGradableChildren($xapiData) {
return array_filter($xapiData, function ($data) {
$contentTypeProcessor = H5PReport::getContentTypeProcessor($data);
$interactionType = $contentTypeProcessor;
return $interactionType !== 'H5P.FreeTextQuestion';
});
}
/**
* List of CSS stylesheets used by the processors when rendering the report.
*
* @return array
*/
public function getStylesUsed() {
$styles = array(
'styles/shared-styles.css'
);
// Fetch style used by each report processor
foreach ($this->processors as $processor) {
$style = $processor->getStyle();
if (!empty($style)) {
$styles[] = $style;
}
}
return $styles;
}
/**
* List of JS scripts to be used by the processors when rendering the report.
*
* @return array
*/
public function getScriptsUsed() {
$scripts = [];
// Fetch scripts used by each report processor
foreach ($this->processors as $processor) {
$script = $processor->getScript();
if (!empty($script)) {
$scripts[] = $script;
}
}
return $scripts;
}
/**
* Caches instance of report generator.
* @return \H5PReport
*/
public static function getInstance() {
static $instance;
if (!$instance) {
$instance = new H5PReport();
}
return $instance;
}
/**
* Attempts to retrieve content type processor from xapi data
* @param object $xapiData
*
* @return string|null Content type processor
*/
public static function getContentTypeProcessor($xapiData) {
if (!isset($xapiData->additionals)) {
return null;
}
$extras = json_decode($xapiData->additionals);
if (!isset($extras->extensions) || !isset($extras->extensions->{self::$contentTypeExtension})) {
return null;
}
$processor = $extras->extensions->{self::$contentTypeExtension};
if (!array_key_exists($processor, self::$contentTypeProcessors)) {
return null;
}
return $processor;
}
/**
* Get required reporting module version from statement
*
* @param $xapiData
*
* @return string Defaults to 1.0.0
*/
public static function getVersion($xapiData) {
if (!isset($xapiData->additionals)) {
return '1.0.0';
}
$additionals = json_decode($xapiData->additionals);
if (!isset($additionals->contextExtensions->{self::$versionExtension})) {
return '1.0.0';
}
return $additionals->contextExtensions->{self::$versionExtension};
}
/**
* Check is render report from statement is supported
*
* @param $xapiData
*
* @return bool
*/
public static function isSupportedVersion($xapiData) {
$reportingVersion = array_map('intval', explode('.', self::$version));
$statementVersion = array_map('intval', explode('.', self::getVersion($xapiData)));
// Sanitation
if (!count($statementVersion) === 3) {
return false;
}
// Check major version
if ($reportingVersion[0] < $statementVersion[0]) {
return false;
}
// Check minor version
$hasOutdatedMinorVersion = $reportingVersion[0] === $statementVersion[0]
&& $reportingVersion[1] < $statementVersion[1];
if ($hasOutdatedMinorVersion) {
return false;
}
// Patch versions are assumed to be compatible
return true;
}
/**
* Display message saying that report could not be rendered
*
* @param $xapiData
*
* @return string
*/
public static function renderUnsupportedVersionPage($xapiData) {
$requiredVersion = self::getVersion($xapiData);
$installedVersion = self::$version;
return "<div>Version {$requiredVersion} of the reporting module is required to render this report. Currently installed: {$installedVersion}</div>";
}
}