9b3bfee20979fbf5911f7adedd9e2396b843c184
[platal.git] / htdocs / javascript / survey.js
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($) {
22 $.extend({
23 questions: function() {
24 return $('.q_edit:not(#questions)');
25 },
26
27 lastQuestion: function() {
28 return $.questions().last();
29 },
30
31 renumberQuestions: function() {
32 var q = $.questions();
33 q.each(function(idx) {
34 var elt = $(this);
35 var old_id = elt.attr('id');
36 var new_id = 'q_edit[' + idx + ']';
37 if (old_id == new_id) {
38 return;
39 }
40
41 var children = elt.children(':not(.q_edit)');
42 while (children.length > 0) {
43 children.filter('.q_edit_label').text('Question ' + (idx + 1));
44 children.children('[name*="' + old_id + '"]').each(function() {
45 function replace(attr) {
46 var cid = $(this).attr(attr);
47 if (cid.substr(0, id.length) == old_id) {
48 $(this).attr(attr, new_id + cid.substring(old_id.length, cid.length));
49 }
50 }
51 replace('id');
52 replace('name');
53 });
54 children = children.children(':not(.q_edit)');
55 }
56 elt.attr('id', new_id);
57 });
58 },
59
60 debugPrintQuestions: function() {
61 var q = $.questions();
62 var str = '';
63 q.each(function() {
64 str += $(this).attr('id') + '\n';
65 });
66 alert(str);
67 }
68 });
69
70 $.fn.extend({
71 showQuestions: function(questions) {
72 var data = $('#question_base').tmpl(questions);
73 this.empty();
74 data.appendTo(this);
75 return this;
76 },
77
78 /* Edition form */
79 prepareQuestions: function(questions) {
80 for (var q in questions) {
81 var q = questions[q];
82 var child = this.addQuestion(q);
83 if ($.isArray(q.children)) {
84 child.prepareQuestions(q.children);
85 }
86 }
87 return this;
88 },
89
90 isQuestion: function() {
91 return this.hasClass('q_edit');
92 },
93
94 isRootSection: function() {
95 return this.attr('id') == 'questions';
96 },
97
98 question: function() {
99 return this.isQuestion() ? this : this.parentQuestion();
100 },
101
102 questionForm: function() {
103 return this.question().children('.q_edit_form');
104 },
105
106 qid: function() {
107 var question = this.question();
108 if (question.get(0) == undefined) {
109 return undefined;
110 }
111 var id = question.attr('id');
112 if (id.substr(0, 7) != 'q_edit[') {
113 return undefined;
114 }
115 if (id.charAt(id.length - 1) != ']') {
116 return undefined;
117 }
118 id = id.substr(7, id.length - 8);
119 return parseInt(id);
120 },
121
122 parentQuestion: function() {
123 return this.parent().closest('.q_edit');
124 },
125
126 childrenContainer: function() {
127 var question = this.question();
128 return question.isRootSection() ? question : question.questionForm().children();
129 },
130
131 childrenQuestion: function() {
132 return this.childrenContainer().children('.q_edit');
133 },
134
135 addQuestion: function(q) {
136 var id = $.lastQuestion().qid();
137 if (id == undefined) {
138 id = 0;
139 else {
140 id++;
141 }
142 if (q == null) {
143 q = { qid: id }
144 }
145 var question = $("#q_edit_new").tmpl(q);
146 question
147 .children('select')
148 .change(function () {
149 var type = $(this).val();
150 var form = question.children('.q_edit_form');
151 form.empty();
152 if (type != '') {
153 $("#q_edit_base").tmpl({ qid: id, type: type })
154 .bindQuestion(type)
155 .appendTo(form);
156 }
157 return true;
158 });
159 var dest = this.question();
160 var res = this.childrenContainer().children('.add_question').before(question);
161 $.renumberQuestions();
162 return question;
163 },
164
165 bindQuestion: function(type) {
166 var name = type + '_bindQuestion';
167 if ($.isFunction(this[name])) {
168 this[name]();
169 }
170 return this;
171 },
172
173 removeQuestion: function(force) {
174 var question = this.parentQuestion();
175 if (!force && question.children('.q_edit_form').children().children('.q_edit').length > 0) {
176 if (!alert('Vous avez demander la suppression d\'une section contenant des questions. '
177 + 'Ces questions seront supprimées. Etes-vous sur de vouloir continuer ?')) {
178 return this;
179 }
180 }
181 var res;
182 if (question.isRootSection()) {
183 res = question.empty();
184 } else {
185 res = question.remove();
186 }
187 $.renumberQuestions();
188 return res;
189 },
190
191 buildParentsQuestions: function() {
192 var $this = $(this);
193 $.questions().each(function() {
194 var parent = $(this).parentQuestion();
195 if (!parent.isRootSection()) {
196 $('<input>', {
197 type: 'hidden',
198 name: 'q_edit[' + $(this).qid() + '][parent]',
199 value: parent.qid()
200 }).appendTo($this);
201 }
202 });
203 return $this;
204 },
205
206 /* Multiple choices questions */
207 multiple_bindQuestion: function() {
208 return this;
209 },
210
211 multiple_addAnswer: function() {
212 var answer = $("#q_edit_multiple_answer").tmpl({ qid: this.qid() });
213 this.childrenContainer().children('.add_answer').before(answer);
214 return answer;
215 }
216 });
217 })(jQuery);
218
219
220 $(function() {
221 $(".datepicker").datepicker({
222 hideIfNoPrevNext: true,
223 minDate: new Date()
224 });
225 });
226
227
228 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: