-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathmulti.js
More file actions
358 lines (294 loc) · 9.59 KB
/
Copy pathmulti.js
File metadata and controls
358 lines (294 loc) · 9.59 KB
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
import $ from 'jquery';
import React from 'react';
import {
LegacyIncorrectDialog,
LegacyTooFewDialog,
} from '@cdo/apps/legacySharedComponents/LegacyDialogContents';
import {reportTeacherReviewingStudentNonLabLevel} from '@cdo/apps/metrics/analyticsUtils';
import {TestResults} from '../../constants';
import Sounds from '../../Sounds';
import {sourceForLevel} from '../clientState';
import {
registerGetResult,
onAnswerChanged,
resetContainedLevel,
} from './codeStudioLevels';
var Multi = function (
levelId,
id,
app,
standalone,
numAnswers,
answers,
answersFeedback,
lastAttemptString,
containedMode,
allowMultipleAttempts
) {
// The dashboard levelId.
this.levelId = levelId;
// The DOM id.
this.id = id;
// The dashboard app name.
this.app = app;
// Whether this multi is the only puzzle on a page, or part of a group of them.
this.standalone = standalone;
// The index of the last selection.
this.lastSelectionIndex = -1;
// How many answers should there be?
this.numAnswers = numAnswers;
// A boolean array of the answers. true is correct.
this.answers = answers;
// An array of the feedback strings for each of the answers (correct or incorrect).
this.answersFeedback = answersFeedback;
// A string of the last result. Looks like "1" or "2,3".
this.lastAttemptString = lastAttemptString;
// Whether this multi is running in contained mode.
this.containedMode = containedMode;
// Tracking which answers are currently selected.
this.selectedAnswers = [];
// Tracking which answers have been crossed out.
this.crossedAnswers = [];
this.submitAllowed = true;
this.allowMultipleAttempts = !!allowMultipleAttempts;
$(document).ready(() => this.ready());
};
Multi.prototype.enableButton = function (enable) {
$('#' + this.id + ' .submitButton').attr('disabled', !enable);
};
Multi.prototype.choiceClicked = function (button) {
if (!this.submitAllowed) {
return;
}
var index = parseInt($(button).attr('index'));
Sounds.getSingleton().play('click');
this.clickItem(index);
onAnswerChanged(this.levelId, true);
};
Multi.prototype.clickItem = function (index) {
// If this button is already crossed, do nothing more.
if (this.crossedAnswers.indexOf(index) !== -1) {
return;
}
// If single answer, and this button is already selected, do nothing more.
if (this.numAnswers === 1 && $.inArray(index, this.selectedAnswers) !== -1) {
return;
}
// If multiple answer, and this button is already selected, deselect it.
if (this.numAnswers > 1 && $.inArray(index, this.selectedAnswers) !== -1) {
this.unclickItem(index);
return;
}
this.enableButton(true);
this.lastSelectionIndex = index;
// Unchecked->checked.
$('#' + this.id + ' #unchecked_' + index).hide();
$('#' + this.id + ' #checked_' + index).show();
// Add this answer to the list of selected answers.
this.selectedAnswers.unshift(index);
// Unselect previously selected answer if there are now too many selected.
if (this.selectedAnswers.length > this.numAnswers) {
var unselectIndex = this.selectedAnswers.pop();
// Although don't uncheck it if it's already crossed out.
if (this.crossedAnswers.indexOf(unselectIndex) === -1) {
$('#' + this.id + ' #unchecked_' + unselectIndex).show();
$('#' + this.id + ' #checked_' + unselectIndex).hide();
}
}
return true;
};
Multi.prototype.unclickItem = function (index) {
var selectedItemIndex = this.selectedAnswers.indexOf(index);
this.selectedAnswers.splice(selectedItemIndex, 1);
// Checked->unchecked.
$('#' + this.id + ' #unchecked_' + index).show();
$('#' + this.id + ' #checked_' + index).hide();
};
// called on $.ready
Multi.prototype.ready = function () {
// Are we read-only? This can be because we're a teacher OR because an answer
// has been previously submitted.
if (window.appOptions.readonlyWorkspace && !this.containedMode) {
// hide the Submit buttons.
$('.submitButton').hide();
// grey out the marks
$('#' + this.id + '.item-mark').css('opacity', 0.5);
this.submitAllowed = false;
// Are we a student viewing their own previously-submitted work?
if (window.appOptions.submitted) {
// show the Unsubmit button.
$('#' + this.id + ' .unsubmitButton').show();
}
if (this.standalone) {
reportTeacherReviewingStudentNonLabLevel();
}
}
$('#' + this.id + ' .answerbutton').click(
$.proxy(function (event) {
this.choiceClicked($(event.currentTarget));
}, this)
);
$('#' + this.id + ' #voteform img').on(
'dragstart',
$.proxy(function (event) {
// Prevent button images from being dragged, click the button instead.
var button = $(event.currentTarget).parent().parent().parent();
this.choiceClicked(button);
event.preventDefault();
event.stopPropagation();
}, this)
);
this.enableButton(false);
// If we are relying on the containing page's submission buttons/dialog, then
// we need to provide a getResult function.
if (this.standalone) {
registerGetResult(this.getResult.bind(this));
}
// Pre-select previously submitted response if available.
var lastAttempt =
this.lastAttemptString ||
sourceForLevel(window.appOptions.scriptName, this.levelId);
if (lastAttempt) {
var previousResult = lastAttempt.split(',');
for (var i = 0; i < previousResult.length; i++) {
this.clickItem(parseInt(previousResult[i]));
}
}
if (this.standalone) {
$('.submitButton').click($.proxy(this.submitButtonClick, this));
} else {
var resetButton = $('#reset-predict-progress-button');
if (resetButton) {
resetButton.click(() => resetContainedLevel());
}
}
if (this.correctNumberAnswersSelected() && !this.allowMultipleAttempts) {
this.lockAnswers();
}
};
Multi.prototype.lockAnswers = function () {
if (this.allowMultipleAttempts) {
return;
}
$('#' + this.id + ' .answerbutton')
.addClass('lock-answers')
.attr('tabindex', '-1');
$('#reset-predict-progress-button')?.prop('disabled', false);
};
Multi.prototype.correctNumberAnswersSelected = function () {
return this.selectedAnswers.length === this.numAnswers;
};
Multi.prototype.resetAnswers = function () {
$('#' + this.id + ' .answerbutton').removeClass('lock-answers');
$('#reset-predict-progress-button')?.prop('disabled', true);
this.selectedAnswers.forEach(idx => this.unclickItem(idx));
};
Multi.prototype.getAppName = function () {
return this.app;
};
// called by external result-posting code
Multi.prototype.getResult = function (dontAllowSubmit) {
let answer;
let valid;
if (this.numAnswers === 1) {
answer = this.lastSelectionIndex;
valid = answer !== -1;
} else {
answer = this.selectedAnswers;
valid = this.selectedAnswers.length === this.numAnswers;
}
let result;
let submitted;
let errorDialog;
let testResult;
const answerIsCorrect = this.validateAnswers();
const tooFewAnswers = !this.correctNumberAnswersSelected();
if (tooFewAnswers) {
errorDialog = <LegacyTooFewDialog />;
} else if (!this.allowMultipleAttempts && !answerIsCorrect) {
errorDialog = <LegacyIncorrectDialog />;
}
if (
!dontAllowSubmit &&
(window.appOptions.level.submittable || this.forceSubmittable)
) {
result = true;
submitted = true;
} else if (tooFewAnswers) {
result = false;
submitted = false;
} else if (!this.allowMultipleAttempts) {
submitted = false;
result = answerIsCorrect;
// This isn't a great enum for this, but its description suggests it's the best option.
// Particularly: "Not validated, but should be treated as a success"
testResult = TestResults.CONTAINED_LEVEL_RESULT;
} else {
result = answerIsCorrect;
submitted = false;
}
return {
response: answer,
result,
errorDialog,
submitted,
valid,
testResult,
};
};
// called by external code that will display answer feedback
Multi.prototype.getCurrentAnswerFeedback = function () {
if (!this.answersFeedback) {
return;
}
if (this.selectedAnswers.length === 0) {
return;
}
var feedbackStrings = [];
for (var i = 0; i < this.selectedAnswers.length; i++) {
feedbackStrings.push(this.answersFeedback[this.selectedAnswers[i]]);
}
return feedbackStrings.join('\n');
};
// This behavior should only be available when this is a standalone Multi.
Multi.prototype.submitButtonClick = function () {
// Don't show right/wrong answers for submittable.
if (window.appOptions.level.submittable || this.forceSubmittable) {
return;
}
if (!this.allowMultipleAttempts && this.correctNumberAnswersSelected()) {
this.lockAnswers();
$('.submitButton')?.hide();
$('.nextLevelButton')?.show();
}
// If the solution only takes one answer, and it's wrong, and it's not
// already crossed out, then mark it as answered wrong.
if (
this.numAnswers === 1 &&
this.crossedAnswers.indexOf(this.lastSelectionIndex) === -1 &&
!this.validateAnswers()
) {
$('#' + this.id + ' #checked_' + this.lastSelectionIndex).hide();
$('#' + this.id + ' #cross_' + this.lastSelectionIndex).show();
this.crossedAnswers.unshift(this.lastSelectionIndex);
}
};
/**
* @returns {boolean} True if this Multi has been provided with answers, and the
* selected answer(s) are the correct one(s).
*/
Multi.prototype.validateAnswers = function () {
if (!this.answers) {
return false;
}
if (this.selectedAnswers.length === this.numAnswers) {
for (var i = 0; i < this.numAnswers; i++) {
if (!this.answers[this.selectedAnswers[i]]) {
return false;
}
}
return true;
}
return false;
};
export default Multi;