-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccm.quiz.mjs
More file actions
133 lines (123 loc) · 3.61 KB
/
ccm.quiz.mjs
File metadata and controls
133 lines (123 loc) · 3.61 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
export const component = {
name: "quiz",
ccm: "././libs/ccmjs/ccm.js",
config: {
ui: ["ccm.load", "././libs/ccm-ui/ccm-ui.mjs"],
html: ["ccm.load", "././resources/templates.mjs"],
css: ["ccm.load", "././resources/styles.css"],
questions: [
{
text: "Question 1",
description: "Does this demo work?",
type: "radio",
answers: [
{
text: "Yes",
correct: true,
},
{
text: "No",
comment: "Example comment for wrong answer",
},
],
},
{
text: "Question 2",
description: "Which question is this",
type: "checkbox",
answers: [
{
text: "First",
comment:
"This answer is wrong, because this is the second and last question.",
},
{
text: "Second",
correct: true,
},
{
text: "Last",
correct: true,
},
],
},
],
feedback: true,
labels: {
submit: "Submit",
next: "Next",
finish: "Finish",
},
onaction: [
// ["ccm.load", "././resources/actions.mjs#restore"],
// ["ccm.load", "././resources/actions.mjs#skippable"],
// ["ccm.load", "././resources/actions.mjs#startButton"],
// ["ccm.load", "././resources/actions.mjs#escapeHTML"],
// ["ccm.load", "././resources/actions.mjs#shuffleQuestions"],
// ["ccm.load", "././resources/actions.mjs#randomAnswers"],
// ["ccm.load", "././resources/actions.mjs#prevButton"],
// ["ccm.load", "././resources/actions.mjs#anytimeFinish"],
// ["ccm.load", "././resources/actions.mjs#noFinishButton"],
// ["ccm.load", "././resources/actions.mjs#store"],
["ccm.load", "././resources/actions.mjs#analytics"],
["ccm.load", "././resources/actions.mjs#restart"],
],
},
Instance: function () {
this.init = async () => {
await this.emit("init");
};
this.ready = async () => {
await this.emit("ready");
};
this.start = async () => {
await this.emit("before-start");
this.state = { items: this.questions.map(() => ({})) };
this.current = 0;
this.renderQuestion(false);
await this.emit("start");
};
this.events = {
submit: () => {
if (!this.feedback) return;
this.evaluate();
this.renderQuestion(true);
this.emit("submit");
},
next: () => {
if (this.current >= this.questions.length - 1) return;
if (!this.feedback) this.evaluate();
this.current++;
this.renderQuestion(false);
this.emit("next");
},
finish: () => {
if (!this.feedback) this.evaluate();
this.emit("finish");
},
};
this.renderQuestion = (showFeedback) => {
this.ui.render(
this.html.question(this, showFeedback),
this.element,
this,
);
};
this.evaluate = () => {
const item = this.state.items[this.current];
const inputs = [...this.element.querySelectorAll(".input")];
item.input = [];
inputs.forEach((input, i) => input.checked && item.input.push(i));
item.solution = [];
this.questions[this.current].answers.forEach(
(answer, i) => answer.correct && item.solution.push(i),
);
};
this.emit = async (type, data) => {
if (!this.onaction) return;
const event = { instance: this, type, data };
if (!Array.isArray(this.onaction)) this.onaction(event);
for (const fn of this.onaction) fn && (await fn(event));
};
},
};