-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionUtils.js
More file actions
220 lines (194 loc) · 7.65 KB
/
QuestionUtils.js
File metadata and controls
220 lines (194 loc) · 7.65 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
/**
* Returns the specified question with only the three following fields:
*
* - id
* - theme
* - points
*
* @param {Object} question - the question to filter
*/
function getQuestion(question) {
return {
_id: question._id,
theme: question.theme,
points: question.points,
};
}
/**
* Returns the specified question without the answer fields.
*
* @param {Object} question - the question to filter
*/
function getActiveQuestion(question) {
return {
_id: question._id,
questionType: question.questionType,
label: question.label,
answers: question.answers.map(({label}) => { return { label } }),
matchingFields: question.matchingFields.map(({label, answers}) => { return { label, answers: answers.map(({label}) => { return { label } }) } }),
theme: question.theme,
points: question.points,
time: question.time
};
}
const correctByQuestionType = {
multipleChoice: correctMultipleChoiceQuestion,
openEnded: correctOpenEndedQuestion,
matching: correctMatchingQuestion
};
const feedbackByQuestionType = {
multipleChoice: getMultipleChoiceFeedback,
openEnded: getOpenEndedFeedback,
matching: getMatchingFeedback
}
function equals(str1, str2) {
return str1 === str2;
}
function includes(str1, str2) {
return str1.includes(str2);
}
/**
* Checks the specified multiple choice question against the original question to ensure it is not malformed.
*
* @param {Object} studentQuestion - the question returned by the team of students
* @param {Object} originalQuestion - the original question
*/
function checkMultipleChoiceQuestion(studentQuestion, originalQuestion) {
return studentQuestion
&& Array.isArray(studentQuestion.answers)
&& studentQuestion.answers.length === originalQuestion.answers.length
&& studentQuestion.answers.filter(e => e).length === studentQuestion.answers.length;
}
/**
* Checks the specified open-ended question to ensure it is not malformed.
*
* @param {Object} studentQuestion - the question returned by the team of students
*/
function checkOpenEndedQuestion(studentQuestion) {
return studentQuestion && typeof studentQuestion.openEndedAnswer === 'string';
}
/**
* Checks the specified matching question against the original question to ensure it is not malformed.
*
* @param {Object} studentQuestion - the question returned by the team of students
* @param {Object} originalQuestion - the original question
*/
function checkMatchingQuestion(studentQuestion, originalQuestion) {
return studentQuestion
&& Array.isArray(studentQuestion.matchingFields)
&& studentQuestion.matchingFields.length === originalQuestion.matchingFields.length
&& studentQuestion.matchingFields.every((field, i) => {
return field
&& Array.isArray(field.answers)
&& field.answers.length === originalQuestion.matchingFields[i].answers.length
&& field.answers.filter(e => e).length == field.answers.length;
});
}
/**
* Checks the answer to the specified multiple choice question against the original question.
* Returns true if the answer is correct, false otherwise.
*
* @param {Object} studentQuestion - the question returned by the team of students
* @param {Object} originalQuestion - the original question
*/
function correctMultipleChoiceQuestion(studentQuestion, originalQuestion) {
return checkMultipleChoiceQuestion(studentQuestion, originalQuestion)
&& originalQuestion.answers.every(({correct}, i) => correct === !!studentQuestion.answers[i].correct);
}
/**
* Checks the answer to the specified open-ended question against the original question.
* Returns true if the answer is correct, false otherwise.
*
* @param {Object} studentQuestion - the question returned by the team of students
* @param {Object} originalQuestion - the original question
*/
function correctOpenEndedQuestion(studentQuestion, originalQuestion) {
const match = originalQuestion.exactMatch ? equals : includes;
return checkOpenEndedQuestion(studentQuestion)
&& originalQuestion.words.some(word => match(studentQuestion.openEndedAnswer.toLowerCase(), word.toLowerCase()));
}
/**
* Checks the answer to the specified matching question against the original question.
* Returns true if the answer is correct, false otherwise.
*
* @param {Object} studentQuestion - the question returned by the team of students
* @param {Object} originalQuestion - the original question
*/
function correctMatchingQuestion(studentQuestion, originalQuestion) {
return checkMatchingQuestion(studentQuestion, originalQuestion)
&& originalQuestion.matchingFields.every((field, i) => {
return field.answers.every(({correct}, j) => correct === !!studentQuestion.matchingFields[i].answers[j].correct);
});
}
/**
* Checks the answer to the specified question against the original question.
* Returns true if the answer is correct, false otherwise.
*
* @param {Object} studentQuestion - the question returned by the team of students
* @param {Object} originalQuestion - the original question
*/
function correctQuestion(studentQuestion, originalQuestion) {
return correctByQuestionType[originalQuestion.questionType](studentQuestion, originalQuestion);
}
/**
* Builds the feedback to respond to the team of students.
* The answer to the specified multiple choice question is first checked, and the feedback is built accordingly.
*
* @param {Object} studentQuestion - the question returned by the team of students
* @param {Object} originalQuestion - the original question
*/
function getMultipleChoiceFeedback(studentQuestion, originalQuestion) {
let feedback = {};
if (checkMultipleChoiceQuestion(studentQuestion, originalQuestion)) {
const studentAnswerIndex = studentQuestion.answers.findIndex(answer => answer.correct);
if (studentAnswerIndex != -1 && originalQuestion.answers[studentAnswerIndex].feedback) {
feedback.specific = originalQuestion.answers[studentAnswerIndex].feedback;
}
feedback.general = originalQuestion.feedback;
}
return feedback;
}
/**
* Builds the feedback to respond to the team of students.
* The answer to the specified open-ended question is first checked, and the feedback is built accordingly.
*
* @param {Object} studentQuestion - the question returned by the team of students
* @param {Object} originalQuestion - the original question
*/
function getOpenEndedFeedback(studentQuestion, originalQuestion) {
let feedback = {};
if (checkMultipleChoiceQuestion(studentQuestion, originalQuestion)) {
if (correctOpenEndedQuestion(studentQuestion, originalQuestion)) {
feedback.specific = originalQuestion.positiveFeedback;
} else {
feedback.specific = originalQuestion.negativeFeedback;
}
feedback.general = originalQuestion.feedback;
}
return feedback;
}
/**
* Builds the feedback to respond to the team of students.
* The answer to the specified matching question is first checked, and the feedback is built accordingly.
*
* @param {Object} studentQuestion - the question returned by the team of students
* @param {Object} originalQuestion - the original question
*/
function getMatchingFeedback(studentQuestion, originalQuestion) {
let feedback = {};
if (checkMatchingQuestion(studentQuestion, originalQuestion)) {
feedback.general = originalQuestion.feedback;
}
return feedback;
}
/**
* Builds the feedback to respond to the team of students.
* The answer to the specified question is first checked, and the feedback is built accordingly.
*
* @param {Object} studentQuestion - the question returned by the team of students
* @param {Object} originalQuestion - the original question
*/
function getFeedback(studentQuestion, originalQuestion) {
return feedbackByQuestionType[originalQuestion.questionType](studentQuestion, originalQuestion);
}
module.exports = { getQuestion, getActiveQuestion, correctQuestion, getFeedback };