80614a9c36d4467499b54506a32aa52c284abc1f
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
= function(method
) {
23 return function(type
) {
24 var name
= type
+ '_' + method
;
25 var args
= Array
.prototype.slice
.call(arguments
, 1);
26 if ($.isFunction(this[name
])) {
27 return this[name
].apply(this, args
);
34 questions
: function() {
35 return $('.q_edit:not(#questions)');
38 lastQuestion
: function() {
39 return $.questions().last();
42 renumberQuestions
: function() {
43 var q
= $.questions();
44 q
.each(function(idx
) {
46 var old_id
= elt
.attr('id');
47 var new_id
= 'q_edit[' + idx
+ ']';
50 if (old_id
=== new_id
) {
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
));
64 elt
.children().children('.q_edit_label').text('Question ' + (idx
+ 1));
65 elt
.attr('id', new_id
);
69 debugPrintQuestions
: function() {
70 var q
= $.questions();
73 str
+= $(this).attr('id') + '\n';
80 showQuestions
: function(questions
) {
81 var data
= $('#question_base').tmpl(questions
);
88 prepareQuestions
: function(questions
) {
90 for (q
in questions
) {
92 child
= this.addQuestion(q
);
93 if ($.isArray(q
.children
)) {
94 child
.prepareQuestions(q
.children
);
100 isQuestion
: function() {
101 return this.hasClass('q_edit');
104 isRootSection
: function() {
105 return this.attr('id') === 'questions';
108 question
: function() {
109 return this.isQuestion() ? this : this.parentQuestion();
112 questionForm
: function() {
113 return this.question().children('.q_edit_form');
117 var question
= this.question();
118 if (typeof question
.get(0) === 'undefined') {
121 var id
= question
.attr('id');
122 if (id
.substr(0, 7) !== 'q_edit[') {
125 if (id
.charAt(id
.length
- 1) !== ']') {
128 id
= id
.substr(7, id
.length
- 8);
129 return parseInt(id
, 10);
132 parentQuestion
: function() {
133 return this.parent().closest('.q_edit');
136 childrenContainer
: function() {
137 var question
= this.question();
138 return question
.isRootSection() ? question
: question
.questionForm().children();
141 childrenQuestion
: function() {
142 return this.childrenContainer().children('.q_edit');
145 addQuestion
: function(q
) {
146 var id
= $.lastQuestion().qid();
156 var question
= $("#q_edit_new").tmpl(q
);
158 .find('select[name$="[type]"]')
159 .change(function () {
160 var type
= $(this).val();
161 var form
= question
.children('.q_edit_form');
162 var qid
= $(this).qid();
165 $("#q_edit_base").tmpl({ qid
: qid
, type
: type
})
166 .bindQuestion(type
, qid
)
172 question
.children('.q_edit_form')
173 .bindQuestion(type
.val(), q
.qid
, q
.parameters
)
175 this.childrenContainer().children('.add_question').before(question
);
176 $.renumberQuestions();
180 bindQuestion
: dispatchType('bindQuestion'),
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 ?')) {
191 if (question
.isRootSection()) {
192 res
= question
.empty();
194 res
= question
.remove();
196 $.renumberQuestions();
200 buildParentsQuestions
: function() {
202 $.questions().each(function() {
203 var parent
= $(this).parentQuestion();
204 if (!parent
.isRootSection()) {
207 name
: 'q_edit[' + $(this).qid() + '][parent]',
215 /* Multiple choices questions */
216 multiple_selectSubtype
: function() {
217 return this.find('select[name$="[subtype]"]');
220 multiple_bindQuestion
: function(id
, parameters
) {
221 var $question
= this;
224 this.multiple_selectSubtype()
227 $question
.find('.q_edit_answer_box')
229 .append($('<input>', {
235 for (answer
= 0; answer
< parameters
.answers
.length
; answer
++) {
236 this.multiple_addAnswer(parameters
.answers
[answer
]);
242 multiple_addAnswer
: function(value
) {
243 var question
= this.question();
244 var answer
= $("#q_edit_multiple_answer").tmpl({ qid
: question
.qid(), value
: value
});
245 question
.childrenContainer().children('.add_answer').before(answer
);
246 question
.multiple_selectSubtype().change();
250 multiple_removeAnswer
: function() {
251 return this.parent().remove();
258 $(".datepicker").datepicker({
259 hideIfNoPrevNext
: true,
265 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: