/
questionModel.js
337 lines (261 loc) · 11.1 KB
/
questionModel.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
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
define([
'core/js/adapt',
'core/js/models/componentModel',
'core/js/enums/buttonStateEnum'
], function(Adapt, ComponentModel, BUTTON_STATE) {
var QuestionModel = ComponentModel.extend({
//////
// Setup question types
////
// Used to set model defaults
defaults: function() {
// Extend from the ComponentModel defaults
return ComponentModel.resultExtend("defaults", {
_isQuestionType: true,
_shouldDisplayAttempts: false,
_canShowModelAnswer: true,
_canShowFeedback: true,
_canShowMarking: true,
_isSubmitted: false,
_questionWeight: Adapt.config.get("_questionWeight"),
});
},
// Extend from the ComponentModel trackable
trackable: ComponentModel.resultExtend("trackable", [
'_isSubmitted',
'_score',
'_isCorrect',
'_attemptsLeft'
]),
init: function() {
this.setupDefaultSettings();
this.listenToOnce(Adapt, "adapt:initialize", this.onAdaptInitialize);
},
// Calls default methods to setup on questions
setupDefaultSettings: function() {
// Not sure this is needed anymore, keeping to maintain API
this.setupWeightSettings();
this.setupButtonSettings();
},
// Used to setup either global or local button text
setupButtonSettings: function() {
var globalButtons = Adapt.course.get("_buttons");
// Check if '_buttons' attribute exists and if not use the globally defined buttons.
if (!this.has("_buttons")) {
this.set("_buttons", globalButtons);
} else {
// Check all the components buttons.
// If they are empty use the global defaults.
var componentButtons = this.get("_buttons");
for (var key in componentButtons) {
if (typeof componentButtons[key] === 'object') {
// Button text.
if (!componentButtons[key].buttonText && globalButtons[key].buttonText) {
componentButtons[key].buttonText = globalButtons[key].buttonText;
}
// ARIA labels.
if (!componentButtons[key].ariaLabel && globalButtons[key].ariaLabel) {
componentButtons[key].ariaLabel = globalButtons[key].ariaLabel;
}
}
if (!componentButtons[key] && globalButtons[key]) {
componentButtons[key] = globalButtons[key];
}
}
}
},
// Used to setup either global or local question weight/score
setupWeightSettings: function() {
// Not needed as handled by model defaults, keeping to maintain API
},
//////
// Selection restoration process
////
// Used to add post-load changes to the model
onAdaptInitialize: function() {
this.restoreUserAnswers();
},
// Used to restore the user answers
restoreUserAnswers: function() {},
//////
// Submit process
////
// Use to check if the user is allowed to submit the question
// Maybe the user has to select an item?
canSubmit: function() {},
// Used to update the amount of attempts the user has left
updateAttempts: function() {
if (!this.get('_attemptsLeft')) {
this.set("_attemptsLeft", this.get('_attempts'));
}
this.set("_attemptsLeft", this.get('_attemptsLeft') - 1);
},
// Used to set _isEnabled and _isSubmitted on the model
setQuestionAsSubmitted: function() {
this.set({
_isEnabled: false,
_isSubmitted: true
});
},
// This is important for returning or showing the users answer
// This should preserve the state of the users answers
storeUserAnswer: function() {},
// Sets _isCorrect:true/false based upon isCorrect method below
markQuestion: function() {
if (this.isCorrect()) {
this.set('_isCorrect', true);
} else {
this.set('_isCorrect', false);
}
},
// Should return a boolean based upon whether to question is correct or not
isCorrect: function() {},
// Used to set the score based upon the _questionWeight
setScore: function() {},
// Checks if the question should be set to complete
// Calls setCompletionStatus and adds complete classes
checkQuestionCompletion: function() {
var isComplete = (this.get('_isCorrect') || this.get('_attemptsLeft') === 0);
if (isComplete) {
this.setCompletionStatus();
}
return isComplete;
},
// Updates buttons based upon question state by setting
// _buttonState on the model which buttonsView listens to
updateButtons: function() {
var isInteractionComplete = this.get('_isInteractionComplete');
var isCorrect = this.get('_isCorrect');
var isEnabled = this.get('_isEnabled');
var buttonState = this.get('_buttonState');
var canShowModelAnswer = this.get('_canShowModelAnswer');
if (isInteractionComplete) {
if (isCorrect || !canShowModelAnswer) {
// Use correct instead of complete to signify button state
this.set('_buttonState', BUTTON_STATE.CORRECT);
} else {
switch (buttonState) {
case BUTTON_STATE.SUBMIT:
case BUTTON_STATE.HIDE_CORRECT_ANSWER:
this.set('_buttonState', BUTTON_STATE.SHOW_CORRECT_ANSWER);
break;
default:
this.set('_buttonState', BUTTON_STATE.HIDE_CORRECT_ANSWER);
}
}
} else {
if (isEnabled) {
this.set('_buttonState', BUTTON_STATE.SUBMIT);
} else {
this.set('_buttonState', BUTTON_STATE.RESET);
}
}
},
// Used to setup the correct, incorrect and partly correct feedback
setupFeedback: function() {
if (this.get('_isCorrect')) {
this.setupCorrectFeedback();
} else if (this.isPartlyCorrect()) {
this.setupPartlyCorrectFeedback();
} else {
this.setupIncorrectFeedback();
}
},
// Used by the question to determine if the question is incorrect or partly correct
// Should return a boolean
isPartlyCorrect: function() {},
setupCorrectFeedback: function() {
var feedback = this.get("_feedback");
this.set({
feedbackTitle: this.getFeedbackTitle(feedback),
feedbackMessage: feedback.correct || ""
});
},
setupPartlyCorrectFeedback: function() {
var feedback = this.get("_feedback");
if (!feedback) return;
if (feedback._partlyCorrect) {
if (this.get('_attemptsLeft') === 0 || !feedback._partlyCorrect.notFinal) {
if (feedback._partlyCorrect.final) {
this.set({
feedbackTitle: this.getFeedbackTitle(feedback),
feedbackMessage: feedback._partlyCorrect.final
});
} else {
this.setupIncorrectFeedback();
}
} else {
this.set({
feedbackTitle: this.getFeedbackTitle(feedback),
feedbackMessage: feedback._partlyCorrect.notFinal ? feedback._partlyCorrect.notFinal : ""
});
}
} else {
this.setupIncorrectFeedback();
}
},
setupIncorrectFeedback: function() {
var feedback = this.get("_feedback");
if (this.get('_attemptsLeft') === 0 || feedback && !feedback._incorrect.notFinal) {
this.set({
feedbackTitle: this.getFeedbackTitle(feedback),
feedbackMessage: feedback ? feedback._incorrect.final : ""
});
} else {
this.set({
feedbackTitle: this.getFeedbackTitle(feedback),
feedbackMessage: feedback ? feedback._incorrect.notFinal : ""
});
}
},
getFeedbackTitle: function(feedback) {
return feedback.title || this.get('displayTitle') || this.get('title') || "";
},
// Reset the model to let the user have another go (not the same as attempts)
reset: function(type, force) {
if (!this.get("_canReset") && !force) return;
type = type || true; //hard reset by default, can be "soft", "hard"/true
ComponentModel.prototype.reset.call(this, type, force);
var attempts = this.get('_attempts');
this.set({
_attemptsLeft: attempts,
_isCorrect: undefined,
_isSubmitted: false,
_buttonState: BUTTON_STATE.SUBMIT
});
},
// Reset question for subsequent attempts
setQuestionAsReset: function() {
this.set({
_isEnabled: true,
_isSubmitted: false
});
},
// Used by the question view to reset the stored user answer
resetUserAnswer: function() {},
refresh: function() {
this.trigger('question:refresh');
},
getButtonState: function() {
if (this.get('_isCorrect')) {
return BUTTON_STATE.CORRECT;
}
if (this.get('_attemptsLeft') === 0) {
return this.get('_canShowModelAnswer') ? BUTTON_STATE.SHOW_CORRECT_ANSWER : BUTTON_STATE.INCORRECT;
}
return this.get('_isSubmitted') ? BUTTON_STATE.RESET : BUTTON_STATE.SUBMIT;
},
// Returns an object specific to the question type, e.g. if the question
// is a 'choice' this should contain an object with:
// - correctResponsesPattern[]
// - choices[]
getInteractionObject: function() {
return {};
},
// Returns a string detailing how the user answered the question.
getResponse: function() {},
// Returns a string describing the type of interaction: "choice" and "matching" supported (see scorm wrapper)
getResponseType: function() {}
});
return QuestionModel;
});