-
Notifications
You must be signed in to change notification settings - Fork 34
/
viewer-component.js
267 lines (222 loc) · 6.17 KB
/
viewer-component.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
import './viewer-component.scss'
import Common from 'obojobo-document-engine/src/scripts/common'
import MCAssessmentAnswerChoices from './mc-assessment-answer-choices'
import React from 'react'
import Viewer from 'obojobo-document-engine/src/scripts/viewer'
import shuffle from 'obojobo-document-engine/src/scripts/common/util/shuffle'
import isOrNot from 'obojobo-document-engine/src/scripts/common/util/isornot'
const { OboModel } = Common.models
const { OboComponent, OboQuestionAssessmentComponent } = Viewer.components
const { QuestionUtil } = Viewer.util
const { focus } = Common.page
const PICK_ALL_INCORRECT_MESSAGE =
'You have either missed some correct answers or selected some incorrect answers.'
export default class MCAssessment extends OboQuestionAssessmentComponent {
static focusOnContent(model, opts = {}) {
const el = model.getDomEl()
if (!el) return false
focus(el, opts.scroll)
return true
}
static isResponseEmpty(response) {
return response.ids.length === 0
}
constructor(props) {
super(props)
this.answerChoicesRef = React.createRef()
this.sortIds()
}
getInstructions(questionModel, questionAssessmentModel) {
const responseType = questionAssessmentModel.modelState.responseType
const questionType = questionModel.modelState.type
let instructions = ''
if (questionType === 'survey') {
switch (responseType) {
case 'pick-one':
case 'pick-one-multiple-correct':
instructions = <span>Choose one</span>
break
case 'pick-all':
instructions = <span>Choose one or more</span>
break
}
} else {
switch (responseType) {
case 'pick-one':
instructions = <span>Pick the correct answer</span>
break
case 'pick-one-multiple-correct':
instructions = <span>Pick one of the correct answers</span>
break
case 'pick-all':
instructions = (
<span>
Pick <b>all</b> of the correct answers
</span>
)
break
}
}
return (
<React.Fragment>
<span className="for-screen-reader-only">{`Multiple choice form with ${questionAssessmentModel.children.models.length} choices. `}</span>
{instructions}
</React.Fragment>
)
}
getDetails(questionModel, questionAssessmentModel, score) {
if (questionAssessmentModel.modelState.responseType === 'pick-all' && score !== 100) {
return PICK_ALL_INCORRECT_MESSAGE
}
return null
}
getResponseData() {
const questionResponse = this.props.response || { ids: [] }
const correct = new Set()
const responses = new Set()
let childId
for (const child of Array.from(this.props.model.children.models)) {
childId = child.get('id')
if (child.modelState.score === 100) {
correct.add(childId)
}
if (questionResponse.ids.indexOf(childId) !== -1) {
responses.add(childId)
}
}
return {
correct,
responses
}
}
calculateScore() {
const responseData = this.getResponseData()
const { correct } = responseData
const { responses } = responseData
switch (this.props.model.modelState.responseType) {
case 'pick-all': {
if (correct.size !== responses.size) {
return { score: 0, details: null }
}
let score = 100
correct.forEach(function(id) {
if (!responses.has(id)) {
score = 0
}
})
return { score, details: null }
}
default: {
// pick-one | pick-one-multiple-correct
const correctArr = [...correct]
for (const id of correctArr) {
if (responses.has(id)) {
return { score: 100, details: null }
}
}
return { score: 0, details: null }
}
}
}
retry() {
QuestionUtil.retryQuestion(
this.props.questionModel.get('id'),
this.props.moduleData.navState.context
)
}
handleFormChange(event, prevResponse) {
if (this.props.score !== null) {
this.retry()
prevResponse = { ids: [] }
}
const mcChoiceId = event.target.value
let response
switch (this.props.model.modelState.responseType) {
case 'pick-all': {
response = prevResponse || { ids: [] }
const responseIndex = response.ids.indexOf(mcChoiceId)
if (responseIndex === -1) {
response.ids.push(mcChoiceId)
} else {
response.ids.splice(responseIndex, 1)
}
break
}
default:
response = {
ids: [mcChoiceId]
}
break
}
return {
state: response,
targetId: mcChoiceId,
sendResponseImmediately: true
}
}
componentDidUpdate() {
this.sortIds()
}
sortIds() {
if (!this.getSortedIds()) {
let ids = this.props.model.children.models.map(model => model.get('id'))
if (this.props.model.modelState.shuffle) ids = shuffle(ids)
QuestionUtil.setData(
this.props.model.get('id'),
this.props.moduleData.navState.context,
'sortedIds',
ids
)
}
}
getSortedIds() {
return QuestionUtil.getData(
this.props.moduleData.questionState,
this.props.model,
this.props.moduleData.navState.context,
'sortedIds'
)
}
getSortedChoiceModels() {
const sortedIds = this.getSortedIds()
if (!sortedIds) return []
return sortedIds
.map(mcChoiceId => OboModel.models[mcChoiceId])
.filter(model => model.get('type') === 'ObojoboDraft.Chunks.MCAssessment.MCChoice')
}
render() {
const responseType = this.props.model.modelState.responseType
const score = this.props.score
const scoreClass = this.props.scoreClass
const sortedChoiceModels = this.getSortedChoiceModels()
const hasResponse = this.props.hasResponse
const className =
'obojobo-draft--chunks--mc-assessment' +
` is-response-type-${this.props.model.modelState.responseType}` +
` is-mode-${this.props.mode}` +
` is-type-${this.props.type}` +
isOrNot(hasResponse, 'responded-to') +
` ${scoreClass}` +
isOrNot(score !== null, 'scored')
return (
<OboComponent
model={this.props.model}
moduleData={this.props.moduleData}
className={className}
>
<MCAssessmentAnswerChoices
ref={this.answerChoicesRef}
models={sortedChoiceModels}
questionModel={this.props.questionModel}
responseType={responseType}
score={score}
mode={this.props.mode}
isAnswerRevealed={this.props.isAnswerRevealed}
type={this.props.type}
moduleData={this.props.moduleData}
feedbackText={this.props.feedbackText}
/>
</OboComponent>
)
}
}