-
Notifications
You must be signed in to change notification settings - Fork 0
/
prendus-view-question.ts
325 lines (297 loc) · 11.4 KB
/
prendus-view-question.ts
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
import {
Question,
BuiltQuestion,
UserInput,
UserVariable,
UserCheck,
UserRadio,
UserEssay
} from './prendus-question-elements.d';
import {
buildQuestion,
checkAnswer
} from './services/question-service';
import {
createUUID
} from '../prendus-shared/services/utilities-service';
import {
getAstObjects,
compileToHTML
} from '../assessml/assessml';
import {
AST,
Variable,
Input,
Essay,
Radio,
Check,
Drag,
Drop,
Image
} from '../assessml/assessml.d';
import {
execute,
subscribe,
extendSchema,
addIsTypeOf
} from '../graphsm/graphsm';
import {
loadQuestion
} from './services/shared-service';
const PRENDUS_VIEW_QUESTION = 'PrendusViewQuestion';
extendSchema(`
type ${PRENDUS_VIEW_QUESTION} implements ComponentState {
componentId: String!
componentType: String!
loaded: Boolean
question: Question!
questionId: String!
builtQuestion: Any
showSolution: Boolean!
showEmbedCode: Boolean!
checkAnswerResponse: String!
solutionButtonText: String!
}
`);
addIsTypeOf('ComponentState', PRENDUS_VIEW_QUESTION, (value: any) => {
return value.componentType === PRENDUS_VIEW_QUESTION;
});
export class PrendusViewQuestion extends Polymer.Element {
shadowRoot: ShadowRoot;
componentId: string;
question: Question;
questionId: string;
builtQuestion: BuiltQuestion;
userToken: string | null;
loaded: boolean;
showEmbedCode: boolean;
checkAnswerResponse: string;
solutionButtonText: 'Solution' | 'Question';
static get is() { return 'prendus-view-question'; }
static get properties() {
return {
question: {
observer: 'questionInfoChanged'
},
questionId: {
observer: 'questionInfoChanged'
}
};
}
constructor() {
super();
this.componentId = createUUID();
subscribe(this.render.bind(this));
execute(`
mutation initialSetup($componentId: String!, $props: Any) {
updateComponentState(componentId: $componentId, props: $props)
}
`, {
initialSetup: (previousResult) => {
return {
componentId: this.componentId,
props: {
componentType: PRENDUS_VIEW_QUESTION,
question: {
text: '',
code: ''
},
questionId: '',
solutionButtonText: 'Solution',
showSolution: false,
showEmbedCode: false,
checkAnswerResponse: ''
}
};
}
}, this.userToken);
}
getThis() {
return this;
}
getQuestionDefined(builtQuestion) {
return builtQuestion ? builtQuestion.html === '' ? null : builtQuestion.html : null;
}
async showEmbedCodeClick() {
await execute(`
mutation setShowEmbedCode($componentId: String!, $props: Any) {
updateComponentState(componentId: $componentId, props: $props)
}
`, {
setShowEmbedCode: (previousResult) => {
return {
componentId: this.componentId,
props: {
showEmbedCode: !this.showEmbedCode
}
};
}
}, this.userToken);
//allow the template with the input to be stamped
setTimeout(() => {
this.shadowRoot.querySelector('#embedInput').select();
}, 0);
}
async questionInfoChanged(newValue: any, oldValue: any) {
if (!this.question && !this.questionId) {
return;
}
await loadQuestion(this.componentId, PRENDUS_VIEW_QUESTION, this.question, this.questionId, this.userToken);
//TODO this causes issues with the secureEval messaging, probably won't be hard to fix
//this is so that if the question is being viewed from within an iframe, the iframe can resize itself
// window.parent.postMessage({
// type: 'prendus-view-question-resize',
// height: document.body.scrollHeight,
// width: document.body.scrollWidth
// }, '*');
this.dispatchEvent(new CustomEvent('question-loaded'));
}
getSanitizedHTML(html: string) {
const sanitizedHTML = DOMPurify.sanitize(html, {
ADD_ATTR: ['contenteditable', 'fontsize', 'data'],
ADD_TAGS: ['juicy-ace-editor', 'function-plot'],
SANITIZE_DOM: false // This allows DOMPurify.sanitize to be called multiple times in succession without changing the output (it was removing ids before)
});
return sanitizedHTML;
}
async checkAnswer() {
const astVariables: Variable[] = getAstObjects(this.builtQuestion.ast, 'VARIABLE', ['SOLUTION']);
const astInputs: Input[] = getAstObjects(this.builtQuestion.ast, 'INPUT', ['SOLUTION']);
const astEssays: Essay[] = getAstObjects(this.builtQuestion.ast, 'ESSAY', ['SOLUTION']);
const astCodes: Code[] = getAstObjects(this.builtQuestion.ast, 'CODE', ['SOLUTION']);
const astChecks: Check[] = getAstObjects(this.builtQuestion.ast, 'CHECK', ['SOLUTION']);
const astRadios: Radio[] = getAstObjects(this.builtQuestion.ast, 'RADIO', ['SOLUTION']);
const astDrags: Drag[] = getAstObjects(this.builtQuestion.ast, 'DRAG', ['SOLUTION']);
const astDrops: Drop[] = getAstObjects(this.builtQuestion.ast, 'DROP', ['SOLUTION']);
const astImages: Image[] = getAstObjects(this.builtQuestion.ast, 'IMAGE', ['SOLUTION']);
const astGraphs: Graph[] = getAstObjects(this.builtQuestion.ast, 'GRAPH', ['SOLUTION']);
const userVariables: UserVariable[] = astVariables;
const userImages: UserImage[] = astImages;
const userGraphs: UserGraph[] = astGraphs;
const userInputs: UserInput[] = astInputs.map((astInput) => {
return {
varName: astInput.varName,
value: this.shadowRoot.querySelector(`#${astInput.varName}`).textContent
};
});
const userEssays: UserEssay[] = astEssays.map((astEssay) => {
return {
varName: astEssay.varName,
value: this.shadowRoot.querySelector(`#${astEssay.varName}`).value
};
});
const userCodes: UserCode[] = astCodes.map((astCode) => {
return {
varName: astCode.varName,
value: this.shadowRoot.querySelector(`#${astCode.varName}`).value
};
});
const userChecks: UserCheck[] = astChecks.map((astCheck) => {
return {
varName: astCheck.varName,
checked: this.shadowRoot.querySelector(`#${astCheck.varName}`).checked
};
});
const userRadios: UserRadio[] = astRadios.map((astRadio) => {
return {
varName: astRadio.varName,
checked: this.shadowRoot.querySelector(`#${astRadio.varName}`).checked
};
});
const checkAnswerInfo = await checkAnswer(this._question.code, this.builtQuestion.originalVariableValues, userVariables, userInputs, userEssays, userCodes, userChecks, userRadios, userImages, userGraphs);
await execute(`
mutation setCheckAnswerResponse($componentId: String!, $props: Any) {
updateComponentState(componentId: $componentId, props: $props)
}
`, {
setCheckAnswerResponse: (previousResult) => {
return {
componentId: this.componentId,
props: {
checkAnswerResponse: checkAnswerInfo.answer === true ? 'Correct' : checkAnswerInfo.error ? `This question has errors:\n\n${checkAnswerInfo.error}` : 'Incorrect'
}
};
}
}, this.userToken);
this.dispatchEvent(new CustomEvent('question-response', {
detail: {
userVariables,
userInputs,
userEssays,
userChecks,
userRadios,
userCodes
}
}));
this.shadowRoot.querySelector('#checkAnswerResponseToast').open();
}
async showSolutionClick() {
await execute(`
mutation setSolutionTemplateInfo($componentId: String!, $props: Any) {
updateComponentState(componentId: $componentId, props: $props)
}
`, {
setSolutionTemplateInfo: (previousResult) => {
const solutionTemplate = <HTMLTemplateElement> this.shadowRoot.querySelector('#solution1');
const props = solutionTemplate ? {
builtQuestion: {
...this.builtQuestion,
html: `${solutionTemplate.innerHTML}<template>${this._question.text}</template>`
},
solutionButtonText: 'Question'
} : {
builtQuestion: {
...this.builtQuestion,
html: compileToHTML(this.builtQuestion.ast, () => NaN, () => '')
},
solutionButtonText: 'Solution'
};
return {
componentId: this.componentId,
props
};
}
}, this.userToken);
}
render(state) {
const componentState = state.components[this.componentId];
if (componentState) {
this._question = componentState.question;
this._questionId = componentState.questionId;
this.loaded = componentState.loaded;
this.builtQuestion = componentState.builtQuestion;
this.showSolution = componentState.showSolution;
this.showEmbedCode = componentState.showEmbedCode;
this.checkAnswerResponse = componentState.checkAnswerResponse;
this.solutionButtonText = componentState.solutionButtonText;
const contentDiv = this.shadowRoot.querySelector('#contentDiv');
if (contentDiv) {
setTimeout(() => {
window.renderMathInElement(contentDiv, {
delimiters: [
{left: "$$", right: "$$", display: false}
]
});
});
}
else {
//TODO this seems to me to be a bad way to do this...the problem is that the contentDiv is not defined, and I do not know how to know when it will be defined. It is inside of a dom-if, and that gets stamped when the loaded property is true
setTimeout(() => {
this.render(state);
});
}
}
}
}
window.customElements.define(PrendusViewQuestion.is, PrendusViewQuestion);
async function createNotFoundQuestion(questionId: string) {
const notFoundQuestion = {
id: questionId,
text: 'This question does not exist',
code: 'answer = false;'
};
return {
question: notFoundQuestion,
builtQuestion: await buildQuestion(notFoundQuestion.text, notFoundQuestion.code)
};
}