-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathpresave.js
94 lines (83 loc) · 2.5 KB
/
presave.js
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
var H5PEditor = H5PEditor || {};
var H5PPresave = H5PPresave || {};
/**
* Resolve the presave logic for the content type Drag Question
*
* @param {object} content
* @param finished
* @constructor
*/
H5PPresave['H5P.DragQuestion'] = function (content, finished) {
var presave = H5PEditor.Presave;
var score = 0;
var correctDropZones = [];
if (isContentInvalid()) {
throw new presave.exceptions.InvalidContentSemanticsException('Invalid Drag and Drop Error');
}
if (hasDropZones()) {
correctDropZones = content.question.task.dropZones
.map(function (dropzone) {
return dropzone.correctElements;
})
.filter(function (correctElements) {
return correctElements.length;
})
.reduce(function (previous, current, dropZone) {
current.forEach(function (element) {
if (!Array.isArray(previous[element])) {
previous[element] = [];
}
previous[element].push(dropZone);
});
return previous;
}, []);
}
if (correctDropZones.length === 0 || isSinglePoint()) {
score = 1;
}
else if (hasElements()) {
score = content.question.task.elements
.filter(function (element, index) {
return Array.isArray(correctDropZones[index]) && correctDropZones.length > 0;
})
.map(function (element) {
if (element.multiple === true) {
return correctDropZones.length;
}
return 1;
})
.reduce(function (previous, current) {
return previous + current;
}, 0);
}
presave.validateScore(score);
finished({maxScore: score});
/**
* Check if required parameters is present
* @return {boolean}
*/
function isContentInvalid() {
return !presave.checkNestedRequirements(content, 'content.question.task');
}
/**
* Check if tasks has drop zones
* @return {boolean}
*/
function hasDropZones() {
return presave.checkNestedRequirements(content, 'content.question.task.dropZones') && Array.isArray(content.question.task.dropZones);
}
/**
* Check if tasks has elements
* @return {boolean}
*/
function hasElements() {
return presave.checkNestedRequirements(content, 'content.question.task.elements') && Array.isArray(content.question.task.elements);
}
/**
* Check if task should give 1 point as score
* @return {boolean}
*/
function isSinglePoint() {
return presave.checkNestedRequirements(content, 'content.behaviour.singlePoint') && content.behaviour.singlePoint === true;
}
};