-
Notifications
You must be signed in to change notification settings - Fork 182
/
module.js
389 lines (354 loc) · 13.7 KB
/
module.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* JavaScript library for the quiz module.
*
* @package mod
* @subpackage questionnaire
* @copyright
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/*
* A workaround for MSIE versions < 10 which do not recognize classList. Answer by Paulpro at:
* http://stackoverflow.com/questions/6787383/what-is-the-solution-to-remove-add-a-class-in-pure-javascript.
* */
/**
* @param {HTMLElement} el
* @param {string} aclass
*/
function addClass(el, aclass) {
el.className += ' ' + aclass;
}
/**
* @param {HTMLElement} el
* @param {string} aclass
*/
function removeClass(el, aclass) {
var elClass = ' ' + el.className + ' ';
while (elClass.indexOf(' ' + aclass + ' ') != -1) {
elClass = elClass.replace(' ' + aclass + ' ', '');
}
el.className = elClass;
}
// End classList workaround.
/**
* Javascript for hiding/displaying children questions on preview page of
* questionnaire with conditional branching.
*/
function depend(children, choices) {
children = children.split(',');
choices = choices.split(',');
var childrenlength = children.length;
var choiceslength = choices.length;
var child = null;
var choice = null;
for (var i = 0; i < childrenlength; i++) {
child = children[i];
var q = document.getElementById(child);
if (q) {
var radios = q.getElementsByTagName('input');
var radiolength = radios.length;
var droplists = q.getElementsByTagName('select');
var droplistlength = droplists.length;
var textareas = q.getElementsByTagName('textarea');
var textarealength = textareas.length;
for (var k = 0; k < choiceslength; k++) {
var j, m, n;
choice = choices[k];
if (child == choice) {
// If this browser version accepts classList.
if (typeof document !== "undefined" && ("classList" in document.createElement("a"))) {
q.classList.add('qn-container');
// If this browser version DOES NOT accept classList (e.g. MSIE < 10)
} else {
addClass(q, 'qn-container');
}
for (j = 0; j < radiolength; j++) {
var radio = radios[j];
radio.disabled = false;
}
for (m = 0; m < droplistlength; m++) {
var droplist = droplists[m];
droplist.disabled = false;
}
delete children[i];
} else if (children[i]) {
if (typeof document !== "undefined" && ("classList" in document.createElement("a"))) {
q.classList.remove('qn-container');
q.classList.add('hidedependquestion');
} else {
removeClass(q, 'qn-container');
}
addClass(q, 'hidedependquestion');
for (j = 0; j < radiolength; j++) {
var radio = radios[j];
radio.disabled = true;
radio.checked = false;
radio.value = '';
}
for (m = 0; m < droplistlength; m++) {
var droplist = droplists[m];
droplist.selectedIndex = 0;
droplist.disabled = true;
droplist.checked = false;
}
for (n = 0; n < textarealength; n++) {
var textarea = textareas[n];
textarea.value = '';
}
}
}
}
}
}
/* exported dependdrop */
/**
* @param {string} qId
* @param {*} children
*/
function dependdrop(qId, children) {
var e = document.getElementById(qId);
var choice = e.options[e.selectedIndex].value;
depend(children, choice);
}
// End conditional branching functions.
// When respondent enters text in !other field, corresponding
// radio button OR check box is automatically checked.
/* exported other_check */
/**
* @param {string | void} name
*/
function other_check(name) {
var other = name.split("_");
var other = name.slice(name.indexOf("o") + 1);
if (other.indexOf("]") != -1) {
other = other.slice(0, other.indexOf("]"));
}
var f = document.getElementById("phpesp_response");
for (var i = 0; i <= f.elements.length; i++) {
if (f.elements[i].value == other) {
f.elements[i].checked = true;
break;
}
}
}
// Automatically empty an !other text input field if another Radio button is clicked.
/* exported other_check_empty */
/**
* @param {string} name
*/
function other_check_empty(name, value) {
var f = document.getElementById("phpesp_response");
var i;
for (i = 0; i < f.elements.length; i++) {
if ((f.elements[i].name == name) && f.elements[i].value.substr(0, 6) == "other_") {
f.elements[i].checked = true;
var otherid = f.elements[i].name + "_" + f.elements[i].value.substring(6);
var other = document.getElementsByName(otherid);
if (value.substr(0, 6) != "other_") {
other[0].value = "";
} else {
other[0].focus();
}
var actualbuttons = document.getElementsByName(name);
for (i = 0; i <= actualbuttons.length; i++) {
if (actualbuttons[i].value == value) {
actualbuttons[i].checked = true;
break;
}
}
break;
}
}
}
// In a Rate question type of sub-type Order : automatically uncheck a Radio button
// when another radio button in the same column is clicked.
/* exported other_rate_uncheck */
/**
* @param {string} name
* @param {string} value
*/
function other_rate_uncheck(name, value) {
var col_name = name.substr(0, name.indexOf("_"));
var inputbuttons = document.getElementsByTagName("input");
for (var i = 0; i <= inputbuttons.length - 1; i++) {
var button = inputbuttons[i];
if (button.type == "radio" && button.name != name && button.value == value
&& button.name.substr(0, name.indexOf("_")) == col_name) {
button.checked = false;
}
}
}
// Empty an !other text input when corresponding Check Box is clicked (supposedly to empty it).
/* exported checkbox_empty */
/**
* @param {string} name
*/
function checkbox_empty(name) {
var actualbuttons = document.getElementsByName(name);
for (var i = 0; i <= actualbuttons.length; i++) {
if (actualbuttons[i].value.substr(0, 6) == "other_") {
name = name.substring(0, name.length - 2) + actualbuttons[i].value.substring(5);
var othertext = document.getElementsByName(name);
if (othertext[0].value == "" && actualbuttons[i].checked == true) {
othertext[0].focus();
} else {
othertext[0].value = "";
}
break;
}
}
}
M.mod_questionnaire = M.mod_questionnaire || {};
/* exported Y */
/* exported e */
M.mod_questionnaire.init_attempt_form = function() {
require(['core_form/changechecker'], function(FormChangeChecker) {
FormChangeChecker.watchFormById('phpesp_response');
});
};
M.mod_questionnaire.init_sendmessage = function(Y) {
Y.on('click', function() {
Y.all('input.usercheckbox').each(function() {
this.set('checked', 'checked');
});
}, '#checkall');
Y.on('click', function() {
Y.all('input.usercheckbox').each(function() {
this.set('checked', '');
});
}, '#checknone');
Y.on('click', function() {
Y.all('input.usercheckbox').each(function() {
if (this.get('alt') == 0) {
this.set('checked', 'checked');
} else {
this.set('checked', '');
}
});
}, '#checknotstarted');
Y.on('click', function() {
Y.all('input.usercheckbox').each(function() {
if (this.get('alt') == 1) {
this.set('checked', 'checked');
} else {
this.set('checked', '');
}
});
}, '#checkstarted');
};
M.mod_questionnaire.init_slider = function() {
const allRanges = document.querySelectorAll(".question-slider");
allRanges.forEach(wrap => {
const range = wrap.querySelector("input.questionnaire-slider");
const bubble = wrap.querySelector(".bubble");
const labels = {
leftlabel: wrap.querySelector(".left-side-label"),
middlelabel: wrap.querySelector(".middle-side-label"),
rightlabel: wrap.querySelector(".right-side-label")
};
range.addEventListener("input", () => {
setBubble(range, bubble);
createAccessibilityHeading(range, bubble, labels);
});
setBubble(range, bubble);
createAccessibilityHeading(range, bubble, labels);
});
function setBubble(range, bubble) {
const val = range.value;
const min = range.min ? range.min : 0;
const max = range.max ? range.max : 100;
var newVal = Number(((val - min) * 100) / (max - min));
var positiveVal = '';
if (range.min && range.min < 0) {
if (range.max && range.max > 0) {
if (val > 0) {
positiveVal = '+';
}
}
}
bubble.innerHTML = positiveVal + val;
// Sorta magic numbers based on size of the native UI thumb
bubble.style.left = `calc(${newVal}% + (${8 - newVal * 0.15}px))`;
}
/**
* Adds accessibility support by generating an h2 element with relevant text.
* @param {HTMLElement} range - The range input element.
* @param {HTMLElement} bubble - The bubble element.
* @param {Object} labels - The object containing label elements.
*/
function createAccessibilityHeading(range, bubble, labels) {
const min = range.min ? range.min : 0;
const max = range.max ? range.max : 100;
const step = range.step ? range.step : 1;
const centerValues = calculateCenterValues(range);
const accesshideElement = document.createElement('h2');
accesshideElement.classList.add('accesshide');
const a = {
min,
max,
leftlabel: labels.leftlabel.innerHTML,
rightlabel: labels.rightlabel.innerHTML,
middlelabel: labels.middlelabel.innerHTML,
centreval: centerValues[0],
centreval1: centerValues[0],
centreval2: centerValues[1],
};
const rangeNum = max - min;
const numSteps = rangeNum / step;
const middleLabel = (numSteps % 2 !== 0)
? (labels.middlelabel.innerHTML
? M.util.get_string('middlepartwithtwovalues', 'questionnaire', a)
: M.util.get_string('middlepartwithtwovaluesdefault', 'questionnaire', a))
: (labels.middlelabel.innerHTML
? M.util.get_string('middlepart', 'questionnaire', a)
: M.util.get_string('middlepartdefault', 'questionnaire', a));
const leftPart = labels.leftlabel.innerHTML ? M.util.get_string('leftpart', 'questionnaire', a)
: M.util.get_string('leftpartdefault', 'questionnaire', a);
const middlePart = middleLabel ? middleLabel : '';
const rightPart = labels.rightlabel.innerHTML
? M.util.get_string('rightpart', 'questionnaire', a) : M.util.get_string('rightpartdefault', 'questionnaire', a);
// Include a whitespace to accommodate a setting of the NVDA screen reader.
const whitespace = '\xa0';
accesshideElement.textContent = whitespace + M.util.get_string('where', 'questionnaire', a) + leftPart + middlePart + rightPart;
bubble.appendChild(accesshideElement);
}
/**
* Calculates the center value(s) based on the given range.
* @param {Object} range - The range object containing properties: min, max, step.
* @returns {Array} - An array containing the center value(s).
*/
function calculateCenterValues(range) {
const min = parseInt(range.min);
const max = parseInt(range.max);
const step = parseInt(range.step);
const rangeNum = max - min;
// Calculate the number of steps.
const numSteps = rangeNum / step;
// Calculate the center value(s).
const centerValues = [];
if (numSteps % 2 === 0) {
// Even number of steps, return single center value.
const center = (min + max) / 2;
centerValues.push(center);
} else {
// Odd number of steps, calculate lower and upper center values.
const lowerCenter = min + Math.floor(numSteps / 2) * step;
const upperCenter = lowerCenter + step;
centerValues.push(lowerCenter, upperCenter);
}
return centerValues;
}
};