Improvements and fixes for the surveys.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 30 Jan 2011 11:03:54 +0000 (12:03 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 30 Jan 2011 11:32:03 +0000 (12:32 +0100)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
htdocs/javascript/survey.js
templates/survey/edit.questions.tpl
templates/survey/vote.questions.tpl

index 7823baa..1b30014 100644 (file)
 
 (function($) {
     var dispatchType = function(method) {
-        return function(type, id) {
+        return function(type) {
             var name = type + '_' + method;
+            var args = Array.prototype.slice.call(arguments, 1);
             if ($.isFunction(this[name])) {
-                return this[name](id);
+                return this[name].apply(this, args);
             }
             return this;
         };
             }
             var question = $("#q_edit_new").tmpl(q);
             var type = question
-                .children('select[name$="[type]"]')
+                .find('select[name$="[type]"]')
                 .change(function () {
                     var type = $(this).val();
                     var form = question.children('.q_edit_form');
                 });
             if (type.val()) {
                 question.children('.q_edit_form')
-                        .bindQuestion(type.val(), q.qid);
+                        .bindQuestion(type.val(), q.qid, q.parameters)
             }
             this.childrenContainer().children('.add_question').before(question);
             $.renumberQuestions();
             return this.find('select[name$="[subtype]"]');
         },
 
-        multiple_bindQuestion: function(id) {
+        multiple_bindQuestion: function(id, parameters) {
             var $question = this;
+            var answer;
+            var value;
             this.multiple_selectSubtype()
+                .assertLength(1)
                 .change(function() {
                     $question.find('.q_edit_answer_box')
                         .empty()
                             type: $(this).val(),
                             disabled: "disabled"
                         }));
-                })
-                .change();
+                });
+            if (parameters) {
+                for (answer = 0; answer < parameters.answers.length; answer++) {
+                    this.multiple_addAnswer(parameters.answers[answer]);
+                }
+            }
             return this;
         },
 
-        multiple_addAnswer: function() {
+        multiple_addAnswer: function(value) {
             var question = this.question();
-            var answer = $("#q_edit_multiple_answer").tmpl({ qid: question.qid() });
-            question.childrenContainer().children('.add_answer').after(answer);
+            var answer = $("#q_edit_multiple_answer").tmpl({ qid: question.qid(), value: value });
+            question.childrenContainer().children('.add_answer').before(answer);
             question.multiple_selectSubtype().change();
             return answer;
+        },
+
+        multiple_removeAnswer: function() {
+            return this.parent().remove();
         }
     });
 }(jQuery));
index 5559ebf..be29c27 100644 (file)
 
 {literal}
 <script id="q_edit_new" type="text/x-jquery-tmpl">
-  <div style="clear: both; padding-top: 1em" class="q_edit" id="q_edit[${qid}]">
-    <div>
+  <fieldset style="clear: both; padding-top: 1em; background: inherit; color: inherit"
+            class="q_edit" id="q_edit[${qid}]">
+    <legend>
       <span class="q_edit_label" style="font-weight: bold">Question ${qid + 1}</span> 
+      <input type="text" name="q_edit[${qid}][label]" value="{{if label}}${label}{{/if}}" />
       (<a onclick="$(this).removeQuestion()" style="text-decoration: none">
         {/literal}{icon name="delete"}{literal} Supprimer
-      </a>)
-    </div>
-    Titre&nbsp;: <input type="text" name="q_edit[${qid}][label]"
-                        value="{{if label}}${label}{{/if}}" /><br />
-    Type de question&nbsp;: <select name="q_edit[${qid}][type]">
-      <option value=""></option>
-      <option value="section" {{if type}}{{if type == 'section'}}selected="selected"{{/if}}{{/if}}>
-        Section
-      </option>
-      <option value="text" {{if type}}{{if type == 'text'}}selected="selected"{{/if}}{{/if}}>
-        Texte
-      </option>
-      <option value="multiple" {{if type}}{{if type == 'multiple'}}selected="selected"{{/if}}{{/if}}>
-        Question à choix multiples
-      </option>
-    </select>
+      </a>)<br />
+      Type de question&nbsp;: <select name="q_edit[${qid}][type]">
+        <option value=""></option>
+        <option value="section" {{if type}}{{if type == 'section'}}selected="selected"{{/if}}{{/if}}>
+          Section
+        </option>
+        <option value="text" {{if type}}{{if type == 'text'}}selected="selected"{{/if}}{{/if}}>
+          Texte
+        </option>
+        <option value="multiple" {{if type}}{{if type == 'multiple'}}selected="selected"{{/if}}{{/if}}>
+          Question à choix multiples
+        </option>
+      </select>
+    </legend>
     <div class="q_edit_form">
       {{tmpl "#q_edit_base"}}
     </div>
-  </div>
+  </fieldset>
 </script>
 
 <script id="q_edit_base" type="text/x-jquery-tmpl">
@@ -59,7 +59,7 @@
 </script>
 
 <script id="q_edit_section" type="text/x-jquery-tmpl">
-  <div id="q_edit[${qid}][section]" style="padding-left: 4ex; border-left: 1px solid white">
+  <div id="q_edit[${qid}][section]">
     <div class="add_question">
       <a onclick="$(this).addQuestion()" style="text-decoration: none">
         {/literal}{icon name="add"}{literal} Ajouter une question
 
 <script id="q_edit_multiple" type="text/x-jquery-tmpl">
   <div id="q_edit[${qid}][answers]">
-    <div class="add_answer">
-      <a onclick="$(this).multiple_addAnswer()">
-        {/literal}{icon name="add"}{literal} Ajouter une réponse
-      </a>
-    </div>
-    {{if parameters}}{{if parameters.answer}}
-      {{tmpl(parameters.answer) "#q_edit_multiple_answer"}}
-    {{/if}}{{/if}}
     <div>
       Permettre la sélection de plusieurs réponses ?
       <select name="q_edit[${qid}][subtype]">
         <option value="radio" selected="selected">Non</option>
       </select>
     </div>
+    <div class="add_answer">
+      <a onclick="$(this).multiple_addAnswer()">
+        {/literal}{icon name="add"}{literal} Ajouter une réponse
+      </a>
+    </div>
     <div>
       Ajouter une case Autre ?
       <select name="q_edit[${qid}][allow_other]">
@@ -98,7 +95,8 @@
 <script id="q_edit_multiple_answer" type="text/x-jquery-tmpl">
   <div>
     <span class="q_edit_answer_box"></span>
-    Réponse&nbsp;: <input type="text" name="q_edit[${qid}][answer][][value]" value="${value}" />
+    <input type="text" name="q_edit[${qid}][answers][]" value="${value}" />
+    <a onclick="$(this).multiple_removeAnswer()">{/literal}{icon name="delete"}{literal}</a>
   </div>
 </script>
 
index a42428b..87cb26a 100644 (file)
 
 <script id="question_text" type="text/x-jquery-tmpl">
   <div>
-    <div>${label}</div>
+    <div><strong>${label}</strong></div>
     <input type="text" name="qid[${qid}]" value="" />
   </div>
 </script>
+
+<script id="question_multiple" type="text/x-jquery-tmpl">
+  <div>
+    <div><strong>${label}</strong></div>
+    {{tmpl(parameters.answers) "#question_multiple_answer"}}
+  </div>
+</script>
+
+<script id="question_multiple_answer" type="text/x-jquery-tmpl">
+  <input type="checkbox" name="qid[${qid}][]" value="${$data}" /> ${$data} <br />
+</script>
 {/literal}
 
 {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *}