Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * JavaScript library for the quiz module.
18
 *
19
 * @package    mod
20
 * @subpackage questionnaire
21
 * @copyright
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/*
26
 * A workaround for MSIE versions < 10 which do not recognize classList. Answer by Paulpro at:
27
 * http://stackoverflow.com/questions/6787383/what-is-the-solution-to-remove-add-a-class-in-pure-javascript.
28
 * */
29
 
30
function addClass(el, aclass){
31
    el.className += ' ' + aclass;
32
}
33
 
34
function removeClass(el, aclass){
35
    var elClass = ' ' + el.className + ' ';
36
    while(elClass.indexOf(' ' + aclass + ' ') != - 1) {
37
         elClass = elClass.replace(' ' + aclass + ' ', '');
38
    }
39
    el.className = elClass;
40
}
41
// End classList workaround.
42
 
43
/**
44
 * Javascript for hiding/displaying children questions on preview page of
45
 * questionnaire with conditional branching.
46
 */
47
 
48
function depend(children, choices) {
49
    children = children.split(',');
50
    choices = choices.split(',');
51
    var childrenlength = children.length;
52
    var choiceslength = choices.length;
53
    var child = null;
54
    var choice = null;
55
    for (var i = 0; i < childrenlength; i++) {
56
        child = children[i];
57
        var q = document.getElementById(child);
58
        if (q) {
59
            var radios = q.getElementsByTagName('input');
60
            var radiolength = radios.length;
61
            var droplists = q.getElementsByTagName('select');
62
            var droplistlength = droplists.length;
63
            var textareas = q.getElementsByTagName('textarea');
64
            var textarealength = textareas.length;
65
            for (var k = 0; k < choiceslength; k++) {
66
                var j, m, n;
67
                choice = choices[k];
68
                if (child == choice) {
69
                    // If this browser version accepts classList.
70
                    if (typeof document !== "undefined" && ("classList" in document.createElement("a"))) {
71
                        q.classList.add('qn-container');
72
                        // If this browser version DOES NOT accept classList (e.g. MSIE < 10)
73
                    } else {
74
                        addClass(q, 'qn-container');
75
                    }
76
                    for (j = 0; j < radiolength; j++) {
77
                        var radio = radios[j];
78
                        radio.disabled = false;
79
                    }
80
                    for (m = 0; m < droplistlength; m++) {
81
                        var droplist = droplists[m];
82
                        droplist.disabled = false;
83
                    }
84
                    delete children[i];
85
                } else if (children[i]){
86
                    if (typeof document !== "undefined" && ("classList" in document.createElement("a"))) {
87
                        q.classList.remove('qn-container');
88
                        q.classList.add('hidedependquestion');
89
                    } else {
90
                        removeClass(q, 'qn-container');
91
                    }
92
                    addClass(q, 'hidedependquestion');
93
                    for (j = 0; j < radiolength; j++) {
94
                        var radio = radios[j];
95
                        radio.disabled = true;
96
                        radio.checked = false;
97
                        radio.value = '';
98
                    }
99
                    for (m = 0; m < droplistlength; m++) {
100
                        var droplist = droplists[m];
101
                        droplist.selectedIndex = 0;
102
                        droplist.disabled = true;
103
                        droplist.checked = false;
104
                    }
105
                    for (n = 0; n < textarealength; n++) {
106
                        var textarea = textareas[n];
107
                        textarea.value = '';
108
                    }
109
                }
110
            }
111
        }
112
    }
113
}
114
 
115
/* exported dependdrop */
116
 
117
function dependdrop(qId, children) {
118
    var e = document.getElementById(qId);
119
    var choice = e.options[e.selectedIndex].value;
120
    depend(children, choice);
121
}
122
// End conditional branching functions.
123
 
124
// When respondent enters text in !other field, corresponding
125
// radio button OR check box is automatically checked.
126
/* exported other_check */
127
function other_check(name) {
128
    var other = name.split("_");
129
    var other = name.slice(name.indexOf("o")+1);
130
    if (other.indexOf("]") != -1) {
131
        other = other.slice(0, other.indexOf("]"));
132
    }
133
    var f = document.getElementById("phpesp_response");
134
    for (var i = 0; i <= f.elements.length; i++) {
135
        if (f.elements[i].value == other) {
136
            f.elements[i].checked = true;
137
            break;
138
        }
139
    }
140
}
141
 
142
// Automatically empty an !other text input field if another Radio button is clicked.
143
/* exported other_check_empty */
144
function other_check_empty(name, value) {
145
    var f = document.getElementById("phpesp_response");
146
    var i;
147
    for (i = 0; i < f.elements.length; i++) {
148
        if ((f.elements[i].name == name) && f.elements[i].value.substr(0, 6) == "other_") {
149
            f.elements[i].checked = true;
150
            var otherid = f.elements[i].name + "_" + f.elements[i].value.substring(6);
151
            var other = document.getElementsByName(otherid);
152
            if (value.substr(0,6) != "other_") {
153
                other[0].value = "";
154
            } else {
155
                other[0].focus();
156
            }
157
            var actualbuttons = document.getElementsByName(name);
158
            for (i = 0; i <= actualbuttons.length; i++) {
159
                if (actualbuttons[i].value == value) {
160
                    actualbuttons[i].checked = true;
161
                    break;
162
                }
163
            }
164
            break;
165
        }
166
    }
167
}
168
 
169
// In a Rate question type of sub-type Order : automatically uncheck a Radio button
170
// when another radio button in the same column is clicked.
171
/* exported other_rate_uncheck */
172
function other_rate_uncheck(name, value) {
173
    var col_name = name.substr(0, name.indexOf("_"));
174
    var inputbuttons = document.getElementsByTagName("input");
175
    for (var i = 0; i <= inputbuttons.length - 1; i++) {
176
        var button = inputbuttons[i];
177
        if (button.type == "radio" && button.name != name && button.value == value
178
                    && button.name.substr(0, name.indexOf("_")) == col_name) {
179
            button.checked = false;
180
        }
181
    }
182
}
183
 
184
// Empty an !other text input when corresponding Check Box is clicked (supposedly to empty it).
185
/* exported checkbox_empty */
186
function checkbox_empty(name) {
187
    var actualbuttons = document.getElementsByName(name);
188
    for (var i = 0; i <= actualbuttons.length; i++) {
189
        if (actualbuttons[i].value.substr(0, 6) == "other_") {
190
            name = name.substring(0, name.length - 2) + actualbuttons[i].value.substring(5);
191
            var othertext = document.getElementsByName(name);
192
            if (othertext[0].value == "" && actualbuttons[i].checked == true) {
193
                othertext[0].focus();
194
            } else {
195
                othertext[0].value = "";
196
            }
197
            break;
198
        }
199
    }
200
}
201
 
202
 
203
M.mod_questionnaire = M.mod_questionnaire || {};
204
 
205
/* exported Y */
206
/* exported e */
207
M.mod_questionnaire.init_attempt_form = function(Y) {
208
    M.core_formchangechecker.init({formid: 'phpesp_response'});
209
};
210
 
211
M.mod_questionnaire.init_sendmessage = function(Y) {
212
    Y.on('click', function(e) {
213
        Y.all('input.usercheckbox').each(function() {
214
            this.set('checked', 'checked');
215
        });
216
    }, '#checkall');
217
 
218
    Y.on('click', function(e) {
219
        Y.all('input.usercheckbox').each(function() {
220
            this.set('checked', '');
221
        });
222
    }, '#checknone');
223
 
224
    Y.on('click', function(e) {
225
        Y.all('input.usercheckbox').each(function() {
226
            if (this.get('alt') == 0) {
227
                this.set('checked', 'checked');
228
            } else {
229
                this.set('checked', '');
230
            }
231
        });
232
    }, '#checknotstarted');
233
 
234
    Y.on('click', function(e) {
235
        Y.all('input.usercheckbox').each(function() {
236
            if (this.get('alt') == 1) {
237
                this.set('checked', 'checked');
238
            } else {
239
                this.set('checked', '');
240
            }
241
        });
242
    }, '#checkstarted');
243
 
244
};
245
M.mod_questionnaire.init_slider = function(Y) {
246
    const allRanges = document.querySelectorAll(".slider");
247
    allRanges.forEach(wrap => {
248
        const range = wrap.querySelector("input.questionnaire-slider");
249
        const bubble = wrap.querySelector(".bubble");
250
 
251
        range.addEventListener("input", () => {
252
            setBubble(range, bubble);
253
        });
254
        setBubble(range, bubble);
255
    });
256
 
257
    function setBubble(range, bubble) {
258
        const val = range.value;
259
        const min = range.min ? range.min : 0;
260
        const max = range.max ? range.max : 100;
261
        var newVal = Number(((val - min) * 100) / (max - min));
262
        var positiveVal = '';
263
        if (range.min && range.min < 0) {
264
            if (range.max && range.max > 0) {
265
                if (val > 0) {
266
                    positiveVal = '+';
267
                }
268
            }
269
        }
270
        bubble.innerHTML = positiveVal + val;
271
 
272
        // Sorta magic numbers based on size of the native UI thumb
273
        bubble.style.left = `calc(${newVal}% + (${8 - newVal * 0.15}px))`;
274
    }
275
};