Can save a new survey \o/
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 9 Jan 2011 21:55:43 +0000 (22:55 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 9 Jan 2011 21:55:43 +0000 (22:55 +0100)
Still some bugs, but, hey, you can propose a survey and vote.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
htdocs/javascript/survey.js
modules/survey.php
templates/survey/edit.questions.tpl
templates/survey/edit.tpl
templates/survey/vote.tpl

index 834b9e5..a0ef4e2 100644 (file)
     $.fn.extend({
         showQuestions: function(questions) {
             var data = $('#question_base').tmpl(questions);
-            this.children().remove();
+            this.empty();
             data.appendTo(this);
             return this;
         },
 
         /* Edition form */
+        prepareQuestions: function(questions) {
+            var data = $('#q_edit_new').tmpl(questions);
+            data.prependTo(this);
+            return this;
+        },
+
         isQuestion: function() {
             return this.hasClass('q_edit');
         },
         },
 
         parentQuestion: function() {
-            return this.closest('.q_edit');
+            return this.parent().closest('.q_edit');
         },
 
         childrenContainer: function() {
             $.renumberQuestions();
             return res;
         },
+
+        buildParentsQuestions: function() {
+            var $this = $(this);
+            $.questions().each(function() {
+                var parent = $(this).parentQuestion();
+                if (!parent.isRootSection()) {
+                    $('<input>', {
+                        type: 'hidden',
+                        name: 'q_edit[' + $(this).qid() + '][parent]',
+                        value: parent.qid()
+                    }).appendTo($this);
+                }
+            });
+            return $this;
+        }
     });
 })(jQuery);
 
index 1d0c014..d730af5 100644 (file)
@@ -41,36 +41,6 @@ class SurveyModule extends PLModule
     {
         $this->load('survey.inc.php');
 
-        XDB::execute("DELETE FROM surveys");
-
-        $survey = new Survey();
-        $survey->id = null;
-        $survey->shortname = "blah";
-        $survey->title = "Blah";
-        $survey->description = "Blih blih blih blih";
-        $survey->uid = S::user()->id();
-        $survey->begin = "09/09/2010";
-        $survey->end   = "30/12/2011";
-
-        $qpage = $survey->newQuestion("section");
-        $qpage->parameters = array('type' => 'page');
-        $qpage->label = 'Première page';
-
-        $question = $qpage->newQuestion("text");
-        $question->label = "Super question";
-        $question->flags = "mandatory";
-        $question->parameters = array("type" => "text", "limit" => 256);
-
-        $question = $qpage->newQuestion("text");
-        $question->label = "Super question 2";
-
-        $qpage = $survey->newQuestion("section");
-        $qpage->parameters = array('type' => 'page');
-        $qpage->label = 'Deuxième page';
-
-        $survey->flags = 'validated';
-        $survey->insert(true);
-
         $page->changeTpl('survey/index.tpl');
         $page->assign('active', Survey::iterActive());
     }
@@ -121,7 +91,34 @@ class SurveyModule extends PLModule
             $survey->id = null;
             $survey->uid = S::user()->id();
         }
-        if (Post::has('save')) {
+        if (Post::has('valid')) {
+            $survey->title = Post::t('title');
+            $survey->shortname = Post::t('shortname');
+            $survey->description = Post::t('description');
+            $survey->begin     = Post::t('begin');
+            $survey->end       = Post::t('end');
+            $survey->flags     = 'validated';
+            if (Post::b('anonymous')) {
+                $survey->flags->addFlag('anonymous');
+            }
+
+            $q_edit = Post::v('q_edit');
+            $qs = array();
+            foreach ($q_edit as $qid => $q_desc) {
+                if (isset($q_desc['parent'])) {
+                    $parent = $qs[$q_desc['parent']];
+                } else {
+                    $parent = $survey;
+                }
+                $question = $parent->newQuestion($q_desc['type']);
+                $question->label = $q_desc['label'];
+                unset($q_desc['type']);
+                unset($q_desc['parent']);
+                unset($q_desc['label']);
+                $question->parameters = $q_desc;
+                $qs[$qid] = $question;
+            }
+            $survey->insert('true');
         }
         $page->assign('survey', $survey);
     }
index 8182563..960382d 100644 (file)
 
 {literal}
 <script id="q_edit_new" type="text/x-jquery-tmpl">
-  <div style="clear: both" class="q_edit" id="q_edit[${qid}]">
+  <div style="clear: both; padding-top: 1em" class="q_edit" id="q_edit[${qid}]">
     <div>
       <span class="q_edit_label" style="font-weight: bold">Question ${qid + 1}</span> 
       (<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="text" {{if type}}{{if type == 'text'}}selected="selected"{{/if}}{{/if}}>Texte</option>
-      <option value="section" {{if type}}{{if type == 'section'}}selected="selected"{{/if}}{{/if}}>Section</option>
+      <option value="section"{{if type}}{{if type == 'section'}}selected="selected"{{/if}}{{/if}}>Section</option>
     </select>
     <div class="q_edit_form">
       {{tmpl "#q_edit_base"}}
 </script>
 
 <script id="q_edit_text" type="text/x-jquery-tmpl">
-  Question&nbsp;: <input type="text" name="q_edit[${qid}][label]" /><br />
 </script>
 
 <script id="q_edit_section" type="text/x-jquery-tmpl">
   <div id="section_${qid}" style="padding-left: 4ex; border-left: 1px solid white">
+    {{if children}}
+      {{tmpl(children) "#q_edit_new"}}
+    {{/if}}
     <div class="add_question">
       <a onclick="$(this).addQuestion()" style="text-decoration: none">
         {/literal}{icon name="add"}{literal} Ajouter une question
index f4a5828..d2abf46 100644 (file)
 
 <h1>Edition de sondage</h1>
 
-<form action="survey/edit/{$survey->shortname}" method="post">
+<form action="survey/edit/{$survey->shortname}" method="post" id="form">
   <fieldset>
     <legend>Description du sondage</legend>
 
-    Titre&nbsp;: <input type="text" name="title" /><br />
-    Nom&nbsp;: <input type="text" name="shortname" /><br />
-    Description&nbsp;:<br /><textarea name="description" style="width: 100%"></textarea>
+    Titre&nbsp;: <input type="text" name="title" value="{$survey->title}" /><br />
+    Nom&nbsp;: <input type="text" name="shortname" value="{$survey->shortname}" /><br />
+    Description&nbsp;:<br /><textarea name="description" style="width: 100%">{$survey->description}</textarea>
   </fieldset>
 
   <fieldset>
     <legend>Paramètre du sondage</legend>
-    Premier jour&nbsp;: <input type="text" class="datepicker" name="begin" /><br />
-    Dernier jour&nbsp;: <input type="text" class="datepicker" name="end" /><br />
+    Premier jour&nbsp;: <input type="text" class="datepicker" name="begin" value="{$survey->begin}" /><br />
+    Dernier jour&nbsp;: <input type="text" class="datepicker" name="end" value="{$survey->end}" /><br />
     Sondage anonyme&nbsp;: <label>Oui&nbsp;<input type="radio" name="anonymous" value="1" checked="checked" /></label>
     <label><input type="radio" name="anonymous" value="0" />&nbsp;Non</label>
   </fieldset>
 {include file="survey/vote.questions.tpl"}
 {include file="survey/edit.questions.tpl"}
 
+{literal}
+<script type="text/javascript">
+  //<![CDATA[
+  var questions = {/literal}{$survey->exportQuestionsToJSON()|smarty:nodefaults}{literal};
+
+  $(function() {
+    $('#form').submit(function() {
+      $(this).buildParentsQuestions();
+      return true;
+    });
+    $('#questions').prepareQuestions(questions);
+  });
+  //]]>
+</script>
+{/literal}
+
 {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *}
index e76065e..0a771da 100644 (file)
@@ -40,7 +40,7 @@
   //<![CDATA[
   var questions = {$survey->exportQuestionsToJSON()|smarty:nodefaults};
 
-  $($("#questions").surveyQuestions(questions));
+  $($("#questions").showQuestions(questions));
   //]]>
 </script>