Support multiline text answers.
[platal.git] / htdocs / javascript / survey.js
CommitLineData
d0b836c9
FB
1/***************************************************************************
2 * Copyright (C) 2003-2011 Polytechnique.org *
3 * http://opensource.polytechnique.org/ *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************/
20
21(function($) {
b09bfb14 22 var dispatchType = function(method) {
2a0c59cf 23 return function(type) {
0cc986db 24 var name = type + '_' + method;
2a0c59cf 25 var args = Array.prototype.slice.call(arguments, 1);
0cc986db 26 if ($.isFunction(this[name])) {
2a0c59cf 27 return this[name].apply(this, args);
0cc986db
FB
28 }
29 return this;
30 };
31 };
32
5f2f6b6d
FB
33 $.extend({
34 questions: function() {
35 return $('.q_edit:not(#questions)');
36 },
37
38 lastQuestion: function() {
39 return $.questions().last();
40 },
41
42 renumberQuestions: function() {
43 var q = $.questions();
44 q.each(function(idx) {
45 var elt = $(this);
46 var old_id = elt.attr('id');
47 var new_id = 'q_edit[' + idx + ']';
0cc986db
FB
48 var fixAttrs;
49
50 if (old_id === new_id) {
5f2f6b6d
FB
51 return;
52 }
53
0cc986db
FB
54 fixAttrs = function(attr) {
55 elt.find('[' + attr + '^="' + old_id + '"]').each(function() {
56 var cid = $(this).attr(attr);
57 if (cid.startsWith(old_id)) {
58 $(this).attr(attr, new_id + cid.substring(old_id.length, cid.length));
5f2f6b6d 59 }
5f2f6b6d 60 });
0cc986db
FB
61 };
62 fixAttrs('id');
63 fixAttrs('name');
64 elt.children().children('.q_edit_label').text('Question ' + (idx + 1));
5f2f6b6d
FB
65 elt.attr('id', new_id);
66 });
67 },
68
69 debugPrintQuestions: function() {
70 var q = $.questions();
71 var str = '';
72 q.each(function() {
73 str += $(this).attr('id') + '\n';
74 });
75 alert(str);
76 }
77 });
78
d0b836c9 79 $.fn.extend({
47001402 80 showQuestions: function(questions) {
d0b836c9 81 var data = $('#question_base').tmpl(questions);
99ec711f 82 this.empty();
d0b836c9
FB
83 data.appendTo(this);
84 return this;
47001402
FB
85 },
86
5f2f6b6d 87 /* Edition form */
99ec711f 88 prepareQuestions: function(questions) {
0cc986db
FB
89 var q, child;
90 for (q in questions) {
91 q = questions[q];
92 child = this.addQuestion(q);
eb9bea42
FB
93 if ($.isArray(q.children)) {
94 child.prepareQuestions(q.children);
95 }
96 }
99ec711f
FB
97 return this;
98 },
99
5f2f6b6d
FB
100 isQuestion: function() {
101 return this.hasClass('q_edit');
102 },
103
104 isRootSection: function() {
0cc986db 105 return this.attr('id') === 'questions';
5f2f6b6d
FB
106 },
107
108 question: function() {
109 return this.isQuestion() ? this : this.parentQuestion();
110 },
111
43f0c040
FB
112 questionForm: function() {
113 return this.question().children('.q_edit_form');
114 },
115
5f2f6b6d
FB
116 qid: function() {
117 var question = this.question();
0cc986db
FB
118 if (typeof question.get(0) === 'undefined') {
119 return;
5f2f6b6d
FB
120 }
121 var id = question.attr('id');
0cc986db
FB
122 if (id.substr(0, 7) !== 'q_edit[') {
123 return;
5f2f6b6d 124 }
0cc986db
FB
125 if (id.charAt(id.length - 1) !== ']') {
126 return;
5f2f6b6d
FB
127 }
128 id = id.substr(7, id.length - 8);
0cc986db 129 return parseInt(id, 10);
5f2f6b6d
FB
130 },
131
132 parentQuestion: function() {
99ec711f 133 return this.parent().closest('.q_edit');
5f2f6b6d
FB
134 },
135
136 childrenContainer: function() {
137 var question = this.question();
43f0c040 138 return question.isRootSection() ? question : question.questionForm().children();
5f2f6b6d
FB
139 },
140
141 childrenQuestion: function() {
142 return this.childrenContainer().children('.q_edit');
143 },
144
eb9bea42 145 addQuestion: function(q) {
5f2f6b6d 146 var id = $.lastQuestion().qid();
0cc986db 147 if (!id) {
5f2f6b6d 148 id = 0;
0cc986db 149 } else {
5f2f6b6d
FB
150 id++;
151 }
0cc986db
FB
152 if (!q) {
153 q = { qid: id };
eb9bea42 154 }
ced5514d 155 console.log(q);
eb9bea42 156 var question = $("#q_edit_new").tmpl(q);
e1bf2a63 157 var type = question
2a0c59cf 158 .find('select[name$="[type]"]')
47001402
FB
159 .change(function () {
160 var type = $(this).val();
161 var form = question.children('.q_edit_form');
b09bfb14 162 var qid = $(this).qid();
47001402 163 form.empty();
0cc986db 164 if (type) {
b09bfb14
FB
165 $("#q_edit_base").tmpl({ qid: qid, type: type })
166 .bindQuestion(type, qid)
43f0c040
FB
167 .appendTo(form);
168 }
47001402
FB
169 return true;
170 });
e1bf2a63
FB
171 if (type.val()) {
172 question.children('.q_edit_form')
2a0c59cf 173 .bindQuestion(type.val(), q.qid, q.parameters)
e1bf2a63 174 }
0cc986db 175 this.childrenContainer().children('.add_question').before(question);
5f2f6b6d 176 $.renumberQuestions();
eb9bea42 177 return question;
5f2f6b6d
FB
178 },
179
0cc986db 180 bindQuestion: dispatchType('bindQuestion'),
43f0c040 181
5f2f6b6d
FB
182 removeQuestion: function(force) {
183 var question = this.parentQuestion();
184 if (!force && question.children('.q_edit_form').children().children('.q_edit').length > 0) {
185 if (!alert('Vous avez demander la suppression d\'une section contenant des questions. '
186 + 'Ces questions seront supprimées. Etes-vous sur de vouloir continuer ?')) {
187 return this;
188 }
189 }
190 var res;
191 if (question.isRootSection()) {
192 res = question.empty();
193 } else {
194 res = question.remove();
195 }
196 $.renumberQuestions();
197 return res;
198 },
99ec711f
FB
199
200 buildParentsQuestions: function() {
201 var $this = $(this);
202 $.questions().each(function() {
203 var parent = $(this).parentQuestion();
204 if (!parent.isRootSection()) {
205 $('<input>', {
206 type: 'hidden',
207 name: 'q_edit[' + $(this).qid() + '][parent]',
208 value: parent.qid()
209 }).appendTo($this);
210 }
211 });
212 return $this;
43f0c040
FB
213 },
214
215 /* Multiple choices questions */
b09bfb14
FB
216 multiple_selectSubtype: function() {
217 return this.find('select[name$="[subtype]"]');
218 },
219
2a0c59cf 220 multiple_bindQuestion: function(id, parameters) {
b09bfb14 221 var $question = this;
2a0c59cf
FB
222 var answer;
223 var value;
b09bfb14 224 this.multiple_selectSubtype()
2a0c59cf 225 .assertLength(1)
b09bfb14
FB
226 .change(function() {
227 $question.find('.q_edit_answer_box')
228 .empty()
229 .append($('<input>', {
230 type: $(this).val(),
231 disabled: "disabled"
232 }));
2a0c59cf
FB
233 });
234 if (parameters) {
235 for (answer = 0; answer < parameters.answers.length; answer++) {
236 this.multiple_addAnswer(parameters.answers[answer]);
237 }
238 }
43f0c040
FB
239 return this;
240 },
241
2a0c59cf 242 multiple_addAnswer: function(value) {
b09bfb14 243 var question = this.question();
2a0c59cf
FB
244 var answer = $("#q_edit_multiple_answer").tmpl({ qid: question.qid(), value: value });
245 question.childrenContainer().children('.add_answer').before(answer);
e1bf2a63 246 question.multiple_selectSubtype().change();
43f0c040 247 return answer;
2a0c59cf
FB
248 },
249
250 multiple_removeAnswer: function() {
251 return this.parent().remove();
99ec711f 252 }
d0b836c9 253 });
0cc986db 254}(jQuery));
d0b836c9
FB
255
256
31d63ae1
FB
257$(function() {
258 $(".datepicker").datepicker({
259 hideIfNoPrevNext: true,
260 minDate: new Date()
261 });
262});
263
264
d0b836c9 265// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: