-
Notifications
You must be signed in to change notification settings - Fork 83
/
upgrades.js
164 lines (145 loc) · 6.26 KB
/
upgrades.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
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
var H5PUpgrades = H5PUpgrades || {};
H5PUpgrades['H5P.DragQuestion'] = (function () {
return {
1: {
1: {
contentUpgrade: function (parameters, finished) {
// Moved all behavioural settings into "behaviour" group.
parameters.behaviour = {
enableRetry: parameters.enableTryAgain === undefined ? true : parameters.enableTryAgain,
preventResize: parameters.preventResize === undefined ? true : parameters.preventResize,
singlePoint: parameters.singlePoint === undefined ? true : parameters.singlePoint,
showSolutionsRequiresInput: parameters.showSolutionsRequiresInput === undefined ? true : parameters.showSolutionsRequiresInput
};
delete parameters.enableTryAgain;
delete parameters.preventResize;
delete parameters.singlePoint;
delete parameters.showSolutionsRequiresInput;
finished(null, parameters);
}
},
/**
* Asynchronous content upgrade hook.
* Upgrades content parameters to support DQ 1.4.
*
* Converts H5P.Text elements into H5P.AdvancedText. This is to support
* more styling options for text.
*
* @params {Object} parameters
* @params {function} finished
*/
4: function (parameters, finished) {
if (parameters.question !== undefined && parameters.question.task !== undefined && parameters.question.task.elements !== undefined) {
var elements = parameters.question.task.elements;
// Go through elements
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
// Check if element type is text
if (element && element.type && element.type.library &&
element.type.library.split(' ')[0] === 'H5P.Text') {
element.type.library = 'H5P.AdvancedText 1.0';
}
}
}
finished(null, parameters);
},
/**
* Asynchronous content upgrade hook.
* Upgrades content parameters to support Drag Question 1.11
*
* 1. Move old feedback message to the new overall feedback system.
* 2. Group tip with feedback
* 3. Do not show the new score points for old content being upgraded.
* 4. Relocate fields in the editor
*
* @param {object} parameters
* @param {function} finished
*/
11: function (parameters, finished) {
// Move old feedback message to the new overall feedback system.
if (parameters && parameters.feedback) {
parameters.overallFeedback = [
{
'from': 0,
'to': 100,
'feedback': parameters.feedback
}
];
delete parameters.feedback;
}
// Group tip with feedback
if (parameters.question !== undefined &&
parameters.question.task !== undefined &&
parameters.question.task.dropZones !== undefined ) {
var dropZones = parameters.question.task.dropZones;
for (var i = 0; i < dropZones.length; i++) {
var dropZone = dropZones[i];
var tip = (dropZone !== undefined && dropZone.tip !== undefined && typeof dropZone.tip === 'string') ? dropZone.tip : '';
// Create the new group-structure
delete dropZone.tip;
dropZone.tipsAndFeedback = {
tip: tip,
feedbackOnCorrect: '',
feedbackOnIncorrect: ''
};
}
}
// Hide score points for old content
if (!parameters.behaviour) {
parameters.behaviour = {};
}
// Move fields into behaviour and remove the old values
parameters.behaviour.backgroundOpacity = parameters.backgroundOpacity;
delete parameters.backgroundOpacity;
if (parameters.question !== undefined && parameters.question.settings !== undefined) {
parameters.behaviour.dropZoneHighlighting = parameters.question.settings.dropZoneHighlighting;
parameters.behaviour.autoAlignSpacing = parameters.question.settings.autoAlignSpacing;
parameters.behaviour.enableFullScreen = parameters.question.settings.enableFullScreen;
delete parameters.question.settings.dropZoneHighlighting;
delete parameters.question.settings.autoAlignSpacing;
delete parameters.question.settings.enableFullScreen;
}
// Done
finished(null, parameters);
},
13: function (parameters, finished, extras) {
var metadata = extras.metadata || {};
if (parameters.question && parameters.question.settings) {
// Set new show title parameter
if (parameters.behaviour) {
parameters.behaviour.showTitle = parameters.question.settings.showTitle || false;
}
metadata.title = parameters.question.settings.questionTitle || ((extras && extras.metadata) ? extras.metadata.title : undefined);
// Remove old parameter
delete parameters.question.settings.questionTitle;
delete parameters.question.settings.showTitle;
}
extras.metadata = metadata;
finished(null, parameters, extras);
},
14: function (parameters, finished, extras) {
const taskParams = parameters.question.task;
if (taskParams.dropZones && taskParams.elements) {
const dropZones = taskParams.dropZones;
// Go through and check if there are any draggables that should not be
// there.
const draggables = taskParams.elements;
dropZones.forEach((dropzone, index) => {
dropzone.correctElements = dropzone.correctElements.filter((draggableId) => {
// Check for existence first
const draggableExists = draggables.length > parseInt(draggableId);
if (!draggableExists) {
return false;
}
// Check if the draggable can be dropped in the dropzone
const draggableParams = draggables[parseInt(draggableId)];
const canBeDropped = draggableParams.dropZones.includes(index.toString());
return canBeDropped;
});
});
}
finished(null, parameters, extras);
}
}
};
})();