'update'
[qomet.git] / public / javascripts / components / statements.js
CommitLineData
43828378
BA
1/*
2 * questions group by index prefix 1.2.3 1.1 ...etc --> '1'
3
4NOTE: questions can contain parameterized exercises (how ?
5--> describe variables (syntax ?)
6--> write javascript script (OK, users trusted ? ==> safe mode possible if public website)
7Imaginary example: (using math.js)
8 <params> (avant l'exo)
9 x: math.random()
10 y: math.random()
11 M: math.matrix([[7, x], [y, -3]]);
12 res: math.det(M)
13 </params>
14 <div>Calculer le déterminant de
15 $$\begin{matrix}7 & x\\y & -3\end{matrix}$$</div>
16 * ...
73609d3b 17
a3080c33
BA
18+ fixed + question time (syntax ?)
19
73609d3b
BA
20--> input of type text (number, or vector, or matrix e.g. in R syntax)
21--> parameter stored in question.param (TODO)
22
43828378
BA
23*/
24
435371c7 25Vue.component("statements", {
73609d3b 26 // 'inputs': object with key = question index and value = text or boolean array
a80c6a3b
BA
27 // display: 'all', 'one', 'solution'
28 // iidx: current level-0 integer index (can match a group of questions / inputs)
14c9c66a 29 props: ['questions','inputs','answers','display','iidx'],
a80c6a3b
BA
30 data: function() {
31 return {
32 displayStyle: "compact", //or "all": all on same page
14c9c66a 33 parameters: 0, //TODO: DO NOT re-draw parameters for already answered questions
a80c6a3b
BA
34 };
35 }
435371c7
BA
36 // Full questions tree is rendered, but some parts hidden depending on display settings
37 render(h) {
73609d3b
BA
38 // Prepare questions groups, ordered
39 let questions = this.questions || [ ]
40 let questionGroups = _.groupBy(questions, q => {
41 const dotPos = q.index.indexOf(".");
42 return dotPos === -1 ? q.index : q.index.substring(0,dotPos);
43 });
44 let domTree = questionGroups.map( (qg,i) => {
45 // Re-order questions 1.1.1 then 1.1.2 then...
46 const orderedQg = qg.sort( (a,b) => {
47 let aParts = a.split('.').map(Number);
48 let bParts = b.split('.').map(Number);
49 const La = aParts.length, Lb = bParts.length;
50 for (let i=0; i<Math.min(La,Lb); i++)
51 {
52 if (aParts[i] != bParts[i])
53 return aParts[i] - bParts[i];
54 }
55 return La - Lb; //the longer should appear after
56 });
57 let qgDom = orderedQg.map( q => {
58 let questionContent = [ ];
59 questionContent.push(
60 h(
61 "h4",
62 {
63 "class": {
64 "questionIndex": true,
65 }
435371c7 66 },
73609d3b
BA
67 q.index
68 )
69 );
70 questionContent.push(
71 h(
72 "div",
73 {
74 "class": {
75 wording: true,
435371c7 76 },
73609d3b
BA
77 domProps: {
78 innerHTML: q.wording,
79 },
80 }
81 )
82 );
83 if (!!q.options)
84 {
85 // quiz-like question
86 let optionsOrder = _.range(q.options.length);
87 if (!q.fixed)
88 optionsOrder = _.shuffle(optionsOrder);
89 let optionList = [ ];
90 optionsOrder.forEach( idx => {
91 let option = [ ];
92 option.push(
93 h(
94 "input",
95 {
96 domProps: {
a3080c33
BA
97 checked: !!this.inputs && this.inputs[q.index][idx],
98 disabled: monitoring || this.display == "solution",
73609d3b
BA
99 },
100 attrs: {
101 id: this.inputId(q.index,idx),
102 type: "checkbox",
103 },
104 on: {
105 change: e => { this.inputs[q.index][idx] = e.target.checked; },
106 },
a80c6a3b 107 },
73609d3b
BA
108 [ '' ] //to work in Firefox 45.9 ESR @ ENSTA...
109 )
110 );
111 option.push(
112 h(
113 "label",
114 {
115 domProps: {
116 innerHTML: q.options[idx],
117 },
118 attrs: {
119 "for": this.inputId(q.index,idx),
120 },
121 }
122 )
123 );
14c9c66a 124 const aIdx = (this.answers || [ ]).findIndex( item => { return item.index == q.index; });
73609d3b
BA
125 optionList.push(
126 h(
127 "div",
128 {
129 "class": {
130 option: true,
14c9c66a
BA
131 choiceCorrect: this.display == "solution" && this.answers[aIdx].includes(idx),
132 choiceWrong: this.display == "solution" && !!this.inputs && this.inputs[q.index][idx] && !this.answers[aIdx].includes(idx),
73609d3b 133 },
a80c6a3b 134 },
73609d3b
BA
135 option
136 )
137 );
138 });
139 questionContent.push(
a80c6a3b
BA
140 h(
141 "div",
142 {
143 "class": {
73609d3b 144 optionList: true,
a80c6a3b 145 },
435371c7 146 },
73609d3b 147 optionList
a80c6a3b
BA
148 )
149 );
73609d3b 150 }
14c9c66a
BA
151 else
152 {
153 // Open question, or parameterized: TODO
154 }
73609d3b
BA
155 const depth = (q.index.match(/\./g) || []).length;
156 return h(
157 "div",
158 {
159 "class": {
160 "question": true,
161 "depth" + depth: true,
435371c7 162 },
73609d3b
BA
163 },
164 questionContent
435371c7 165 );
73609d3b 166 });
435371c7
BA
167 return h(
168 "div",
169 {
170 "class": {
73609d3b 171 "questionGroup": true,
a80c6a3b 172 "hide": this.display == "one" && this.iidx != i,
435371c7 173 },
73609d3b
BA
174 }
175 qgDom
435371c7
BA
176 );
177 });
a80c6a3b
BA
178 const navigator = h(
179 "div",
180 {
181 "class": {
182 "hide": this.displayStyle == "all"
183 },
184 },
185 [
186 h(
187 "button",
188 {
189 "class": {
190 "btn": true,
191 },
192 on: {
193 click: () => {
194 this.index = Math.max(0, this.index - 1);
195 },
196 },
197 },
198 [ h("span", { "class": { "material-icon": true } }, "fast_rewind") ]
73609d3b 199 ),
a80c6a3b
BA
200 h("span",{ },(this.iidx+1).toString()),
201 h(
202 "button",
203 {
204 "class": {
205 "btn": true,
206 },
207 on: {
208 click: () => {
209 this.index = Math.min(this.index+1, this.questions.length-1)
210 },
211 },
212 },
213 [ h("span", { "class": { "material-icon": true } }, "fast_forward") ]
214 )
215 ]
216 );
217 domTree.push(navigator);
218 domTree.push(
219 h(
220 "button",
221 {
222 on: {
223 click: () => {
224 this.displayStyle = displayStyle == "compact" ? "all" : "compact";
225 },
226 },
227 },
228 this.displayStyle == "compact" ? "Show all" : "Navigator"
229 )
230 );
435371c7
BA
231 return h(
232 "div",
233 {
234 attrs: {
235 id: "statements",
236 },
237 },
71d1ca9c 238 domTree
435371c7
BA
239 );
240 },
3b8117c5
BA
241 mounted: function() {
242 statementsLibsRefresh();
243 },
435371c7 244 updated: function() {
435371c7
BA
245 statementsLibsRefresh();
246 },
247 methods: {
248 inputId: function(i,j) {
249 return "q" + i + "_" + "input" + j;
250 },
251 },
252});