9b3bfee20979fbf5911f7adedd9e2396b843c184
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 ***************************************************************************/
23 questions
: function() {
24 return $('.q_edit:not(#questions)');
27 lastQuestion
: function() {
28 return $.questions().last();
31 renumberQuestions
: function() {
32 var q
= $.questions();
33 q
.each(function(idx
) {
35 var old_id
= elt
.attr('id');
36 var new_id
= 'q_edit[' + idx
+ ']';
37 if (old_id
== new_id
) {
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
));
54 children
= children
.children(':not(.q_edit)');
56 elt
.attr('id', new_id
);
60 debugPrintQuestions
: function() {
61 var q
= $.questions();
64 str
+= $(this).attr('id') + '\n';
71 showQuestions
: function(questions
) {
72 var data
= $('#question_base').tmpl(questions
);
79 prepareQuestions
: function(questions
) {
80 for (var q
in questions
) {
82 var child
= this.addQuestion(q
);
83 if ($.isArray(q
.children
)) {
84 child
.prepareQuestions(q
.children
);
90 isQuestion
: function() {
91 return this.hasClass('q_edit');
94 isRootSection
: function() {
95 return this.attr('id') == 'questions';
98 question
: function() {
99 return this.isQuestion() ? this : this.parentQuestion();
102 questionForm
: function() {
103 return this.question().children('.q_edit_form');
107 var question
= this.question();
108 if (question
.get(0) == undefined
) {
111 var id
= question
.attr('id');
112 if (id
.substr(0, 7) != 'q_edit[') {
115 if (id
.charAt(id
.length
- 1) != ']') {
118 id
= id
.substr(7, id
.length
- 8);
122 parentQuestion
: function() {
123 return this.parent().closest('.q_edit');
126 childrenContainer
: function() {
127 var question
= this.question();
128 return question
.isRootSection() ? question
: question
.questionForm().children();
131 childrenQuestion
: function() {
132 return this.childrenContainer().children('.q_edit');
135 addQuestion
: function(q
) {
136 var id
= $.lastQuestion().qid();
137 if (id
== undefined
) {
145 var question
= $("#q_edit_new").tmpl(q
);
148 .change(function () {
149 var type
= $(this).val();
150 var form
= question
.children('.q_edit_form');
153 $("#q_edit_base").tmpl({ qid
: id
, type
: type
})
159 var dest
= this.question();
160 var res
= this.childrenContainer().children('.add_question').before(question
);
161 $.renumberQuestions();
165 bindQuestion
: function(type
) {
166 var name
= type
+ '_bindQuestion';
167 if ($.isFunction(this[name
])) {
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 ?')) {
182 if (question
.isRootSection()) {
183 res
= question
.empty();
185 res
= question
.remove();
187 $.renumberQuestions();
191 buildParentsQuestions
: function() {
193 $.questions().each(function() {
194 var parent
= $(this).parentQuestion();
195 if (!parent
.isRootSection()) {
198 name
: 'q_edit[' + $(this).qid() + '][parent]',
206 /* Multiple choices questions */
207 multiple_bindQuestion
: function() {
211 multiple_addAnswer
: function() {
212 var answer
= $("#q_edit_multiple_answer").tmpl({ qid
: this.qid() });
213 this.childrenContainer().children('.add_answer').before(answer
);
221 $(".datepicker").datepicker({
222 hideIfNoPrevNext
: true,
228 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: