aad469f992cbd4f9b5c8270ad7b7fa35b1f455ec
1 /***************************************************************************
2 * Copyright (C) 2003-2011 Polytechnique.org *
3 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************/
22 var dispatchType(method
) {
23 return function(type
) {
24 var name
= type
+ '_' + method
;
25 if ($.isFunction(this[name
])) {
33 questions
: function() {
34 return $('.q_edit:not(#questions)');
37 lastQuestion
: function() {
38 return $.questions().last();
41 renumberQuestions
: function() {
42 var q
= $.questions();
43 q
.each(function(idx
) {
45 var old_id
= elt
.attr('id');
46 var new_id
= 'q_edit[' + idx
+ ']';
49 if (old_id
=== new_id
) {
53 fixAttrs
= function(attr
) {
54 elt
.find('[' + attr
+ '^="' + old_id
+ '"]').each(function() {
55 var cid
= $(this).attr(attr
);
56 if (cid
.startsWith(old_id
)) {
57 $(this).attr(attr
, new_id
+ cid
.substring(old_id
.length
, cid
.length
));
63 elt
.children().children('.q_edit_label').text('Question ' + (idx
+ 1));
64 elt
.attr('id', new_id
);
68 debugPrintQuestions
: function() {
69 var q
= $.questions();
72 str
+= $(this).attr('id') + '\n';
79 showQuestions
: function(questions
) {
80 var data
= $('#question_base').tmpl(questions
);
87 prepareQuestions
: function(questions
) {
89 for (q
in questions
) {
91 child
= this.addQuestion(q
);
92 if ($.isArray(q
.children
)) {
93 child
.prepareQuestions(q
.children
);
99 isQuestion
: function() {
100 return this.hasClass('q_edit');
103 isRootSection
: function() {
104 return this.attr('id') === 'questions';
107 question
: function() {
108 return this.isQuestion() ? this : this.parentQuestion();
111 questionForm
: function() {
112 return this.question().children('.q_edit_form');
116 var question
= this.question();
117 if (typeof question
.get(0) === 'undefined') {
120 var id
= question
.attr('id');
121 if (id
.substr(0, 7) !== 'q_edit[') {
124 if (id
.charAt(id
.length
- 1) !== ']') {
127 id
= id
.substr(7, id
.length
- 8);
128 return parseInt(id
, 10);
131 parentQuestion
: function() {
132 return this.parent().closest('.q_edit');
135 childrenContainer
: function() {
136 var question
= this.question();
137 return question
.isRootSection() ? question
: question
.questionForm().children();
140 childrenQuestion
: function() {
141 return this.childrenContainer().children('.q_edit');
144 addQuestion
: function(q
) {
145 var id
= $.lastQuestion().qid();
154 var question
= $("#q_edit_new").tmpl(q
);
157 .change(function () {
158 var type
= $(this).val();
159 var form
= question
.children('.q_edit_form');
162 $("#q_edit_base").tmpl({ qid
: id
, type
: type
})
168 this.childrenContainer().children('.add_question').before(question
);
169 $.renumberQuestions();
173 bindQuestion
: dispatchType('bindQuestion'),
175 removeQuestion
: function(force
) {
176 var question
= this.parentQuestion();
177 if (!force
&& question
.children('.q_edit_form').children().children('.q_edit').length
> 0) {
178 if (!alert('Vous avez demander la suppression d\'une section contenant des questions. '
179 + 'Ces questions seront supprimées. Etes-vous sur de vouloir continuer ?')) {
184 if (question
.isRootSection()) {
185 res
= question
.empty();
187 res
= question
.remove();
189 $.renumberQuestions();
193 buildParentsQuestions
: function() {
195 $.questions().each(function() {
196 var parent
= $(this).parentQuestion();
197 if (!parent
.isRootSection()) {
200 name
: 'q_edit[' + $(this).qid() + '][parent]',
208 /* Multiple choices questions */
209 multiple_bindQuestion
: function() {
213 multiple_addAnswer
: function() {
214 var answer
= $("#q_edit_multiple_answer").tmpl({ qid
: this.qid() });
215 this.childrenContainer().children('.add_answer').before(answer
);
223 $(".datepicker").datepicker({
224 hideIfNoPrevNext
: true,
230 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: