-
Notifications
You must be signed in to change notification settings - Fork 17
/
questionModel.js
434 lines (365 loc) · 11.9 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import Adapt from 'core/js/adapt';
import components from 'core/js/components';
import ComponentModel from 'core/js/models/componentModel';
import BUTTON_STATE from 'core/js/enums/buttonStateEnum';
class QuestionModel extends ComponentModel {
/// ///
// Setup question types
/// /
// Used to set model defaults
defaults() {
// Extend from the ComponentModel defaults
return ComponentModel.resultExtend('defaults', {
_isQuestionType: true,
_shouldDisplayAttempts: false,
_shouldShowMarking: false,
_canShowCorrectness: false,
_canShowModelAnswer: true,
_canShowFeedback: true,
_canShowMarking: true,
_canSubmit: true,
_isSubmitted: false,
_isCorrectAnswerShown: false,
_questionWeight: Adapt.config.get('_questionWeight'),
_hasItemScoring: false,
_items: []
});
}
// Extend from the ComponentModel trackable
trackable() {
return ComponentModel.resultExtend('trackable', [
'_isSubmitted',
'_score',
'_isCorrect',
'_attemptsLeft',
'_isPartlyCorrect'
]);
}
trackableType() {
return ComponentModel.resultExtend('trackableType', [
Boolean,
Number,
Boolean,
Number,
Boolean
]);
}
lockedAttributes() {
return ComponentModel.resultExtend('lockedAttributes', {
_canSubmit: true
});
}
/**
* Returns a string of the model type group.
* @returns {string}
*/
getTypeGroup() {
return 'question';
}
init() {
this.setupDefaultSettings();
this.updateRawScore();
super.init();
}
// Calls default methods to setup on questions
setupDefaultSettings() {
// Not sure this is needed anymore, keeping to maintain API
this.setupWeightSettings();
this.setupButtonSettings();
}
// Used to setup either global or local button text
setupButtonSettings() {
const 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.
const componentButtons = this.get('_buttons');
for (const 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() {
// Not needed as handled by model defaults, keeping to maintain API
}
/// ///
// Submit process
/// /
// Use to check if the user is allowed to submit the question
// Maybe the user has to select an item?
canSubmit() {}
checkCanSubmit() {
this.set('_canSubmit', this.canSubmit(), { pluginName: 'adapt' });
}
// Used to update the amount of attempts the user has left
updateAttempts() {
const attemptsLeft = this.get('_attemptsLeft') || this.get('_attempts');
this.set('_attemptsLeft', attemptsLeft - 1);
}
// Used to set _isEnabled and _isSubmitted on the model
setQuestionAsSubmitted() {
this.set({
_isEnabled: false,
_isSubmitted: true,
_shouldShowMarking: this.shouldShowMarking
});
}
// Sets _isCorrect:true/false based upon isCorrect method below
markQuestion() {
this.set({
_isCorrect: this.isCorrect(),
_isPartlyCorrect: this.isPartlyCorrect()
});
this.updateRawScore();
}
// Should return a boolean based upon whether to question is correct or not
isCorrect() {}
// Used by the question to determine if the question is incorrect or partly correct
// Should return a boolean
isPartlyCorrect() {}
/**
* Used to set the legacy _score property based upon the _questionWeight and correctness
* @deprecated Please use get score, get maxScore and get minScore instead
*/
setScore() {
const questionWeight = this.get('_questionWeight');
const answeredCorrectly = this.get('_isCorrect');
const score = answeredCorrectly ? questionWeight : 0;
this.set('_score', score);
}
updateRawScore() {
this.set({
_rawScore: this.score,
_maxScore: this.maxScore,
_minScore: this.minScore
});
}
/**
* Returns a numerical value between maxScore and minScore
* @type {number}
*/
get score() {
return this.get('_isCorrect') ? this.maxScore : 0;
}
/**
* @type {number}
*/
get maxScore() {
return this.get('_questionWeight');
}
/**
* @type {number}
*/
get minScore() {
return 0;
}
// Checks if the question should be set to complete
// Calls setCompletionStatus and adds complete classes
checkQuestionCompletion() {
const 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() {
const isInteractionComplete = this.get('_isInteractionComplete');
const isCorrect = this.get('_isCorrect');
const isEnabled = this.get('_isEnabled');
const buttonState = this.get('_buttonState');
const canShowModelAnswer = this.get('_canShowModelAnswer');
const canShowCorrectness = this.get('_canShowCorrectness');
if (isInteractionComplete) {
if (isCorrect || (!canShowModelAnswer || canShowCorrectness)) {
// 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);
}
}
}
getFeedback(feedback = this.get('_feedback')) {
if (!feedback) return {};
const isFinal = (this.get('_attemptsLeft') === 0);
const isCorrect = this.get('_isCorrect');
const correctness = isCorrect
? 'correct'
: this.isPartlyCorrect()
? 'partlyCorrect'
: 'incorrect';
const isLegacyConfig = (typeof feedback.correct === 'string') ||
(typeof feedback._partlyCorrect === 'object') ||
(typeof feedback._incorrect === 'object');
const getLegacyConfigObject = () => {
const subPart = isFinal ? 'final' : 'notFinal';
return {
body: (
isCorrect
? feedback.correct
: feedback[`_${correctness}`]?.[subPart] ||
feedback[`_${correctness}`]?.final ||
feedback._incorrect?.final
) || ''
};
};
const getConfigObject = () => {
const subPart = isFinal ? 'Final' : 'NotFinal';
return (
isCorrect
? feedback._correct
: feedback[`_${correctness}${subPart}`] ||
feedback[`_${correctness}Final`] ||
feedback._incorrectFinal
) || {};
};
const altFeedbackTitle = Adapt.course.get('_globals')._accessibility.altFeedbackTitle;
const hasTitle = Boolean(feedback.title || this.get('title'));
const isAltTitle = Boolean(feedback.altTitle) || (!hasTitle && altFeedbackTitle);
const title = (feedback.altTitle || feedback.title || this.get('title') || altFeedbackTitle || '');
const feedbackConfig = {
isAltTitle,
title: Handlebars.compile(title)(this.toJSON()),
_classes: feedback._classes,
...(isLegacyConfig
? getLegacyConfigObject()
: getConfigObject()
)
};
if (feedbackConfig._graphic?._src && !feedbackConfig._imageAlignment) {
feedbackConfig._imageAlignment = 'right';
}
return feedbackConfig;
}
// Used to setup the correct, incorrect and partly correct feedback
setupFeedback() {
if (!this.has('_feedback')) return;
const { title = '', body = '' } = this.getFeedback();
this.set({
feedbackTitle: Handlebars.compile(title)(this.toJSON()),
feedbackMessage: Handlebars.compile(body)(this.toJSON())
});
}
/**
* Used to determine whether the learner is allowed to interact with the question component or not.
* @return {Boolean}
*/
isInteractive() {
return !this.get('_isComplete') || (this.get('_isEnabled') && !this.get('_isSubmitted'));
}
checkIfResetOnRevisit() {
super.checkIfResetOnRevisit();
// Setup button view state
this.set('_buttonState', this.get('_isInteractionComplete')
? BUTTON_STATE.HIDE_CORRECT_ANSWER
: BUTTON_STATE.SUBMIT
);
}
/**
* Reset the model to let the user have another go (not the same as attempts)
* @param {string} [type] 'hard' resets _isComplete and _isInteractionComplete, 'soft' resets _isInteractionComplete only.
* @param {boolean} [canReset] Defaults to this.get('_canReset')
* @returns {boolean}
*/
reset(type = 'hard', canReset = this.get('_canReset')) {
const wasReset = super.reset(type, canReset);
if (!wasReset) return false;
const attempts = this.get('_attempts');
this.set({
_attemptsLeft: attempts,
_isCorrect: undefined,
_isPartlyCorrect: undefined,
_isCorrectAnswerShown: false,
_isSubmitted: false,
_buttonState: BUTTON_STATE.SUBMIT,
_shouldShowMarking: this.shouldShowMarking
});
return true;
}
// Reset question for subsequent attempts
setQuestionAsReset() {
this.set({
_isEnabled: true,
_isSubmitted: false,
_shouldShowMarking: this.shouldShowMarking
});
this.resetQuestion();
}
/**
* Used by the question view to reset the options of the component.
* This is triggered when the reset button is clicked so it shouldn't be a full reset.
*/
resetQuestion() {}
refresh() {
this.trigger('question:refresh');
}
getButtonState() {
if (this.get('_isCorrect')) {
return BUTTON_STATE.CORRECT;
}
if (this.get('_attemptsLeft') === 0) {
const canShowModelAnswer = this.get('_canShowModelAnswer');
const canShowCorrectness = this.get('_canShowCorrectness');
return canShowModelAnswer && !canShowCorrectness
? 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() {
return {};
}
// Returns a string detailing how the user answered the question.
getResponse() {}
// Returns a string describing the type of interaction: "choice" and "matching" supported (see scorm wrapper)
getResponseType() {}
/**
* Called at the end of the onSubmitClicked view function.
*/
onSubmitted() {
// Stores the current attempt state
if (this.get('_shouldStoreAttempts')) this.addAttemptObject();
this.set('_shouldShowMarking', this.shouldShowMarking);
}
/** @type {boolean} */
get shouldShowMarking() {
return (!this.isInteractive() && this.get('_canShowMarking') && this.get('_isInteractionComplete'));
}
}
// This abstract model needs to registered to support deprecated view-only questions
components.register('question', { model: QuestionModel });
export default QuestionModel;