Rewrite form generation.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 8 Jan 2011 15:18:11 +0000 (16:18 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 8 Jan 2011 15:22:08 +0000 (16:22 +0100)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
modules/survey.php
modules/survey/survey.inc.php
templates/survey/index.tpl
templates/survey/question.text.tpl [deleted file]
templates/survey/questions.tpl [moved from templates/survey/question.section.tpl with 75% similarity]
templates/survey/vote.tpl

index a3bd012..f40ce5b 100644 (file)
@@ -77,6 +77,7 @@ class SurveyModule extends PLModule
     function handler_vote(PlPage $page, $name)
     {
         $this->load('survey.inc.php');
+        $page->addJsLink('jquery.tmpl.js');
         $page->changeTpl('survey/vote.tpl');
         $survey = Survey::get($name);
         if (is_null($survey)) {
index df0a9f9..865a148 100644 (file)
@@ -156,13 +156,24 @@ class Survey extends PlDBTableEntry implements SurveyQuestionContainer
     public function export()
     {
         $export = parent::export();
-        $export['questions'] = array();
+        $export['questions'] = $this->exportQuestions();
+        return $export;
+    }
+
+    public function exportQuestions()
+    {
+        $export = array();
         foreach ($this->questions as $question) {
-            $export['questions'][] = $question->export();
+            $export[] = $question->export();
         }
         return $export;
     }
 
+    public function exportQuestionsToJSON()
+    {
+        return json_encode($this->exportQuestions());
+    }
+
     /* Return an indicator of the progression of the survey:
      *  negative values means 'the survey is not started'
      *  0 means 'the survey is in progress'
index 503f372..0291a3a 100644 (file)
@@ -23,7 +23,7 @@
 <h1>Sondages</h1>
 
 {if $active->total() > 0}
-<table class="bicol">
+<table class="bicol" id="surveyList">
   <tr>
     <th>
       Sondages en cours
diff --git a/templates/survey/question.text.tpl b/templates/survey/question.text.tpl
deleted file mode 100644 (file)
index 5e0d231..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-{**************************************************************************}
-{*                                                                        *}
-{*  Copyright (C) 2003-2011 Polytechnique.org                             *}
-{*  http://opensource.polytechnique.org/                                  *}
-{*                                                                        *}
-{*  This program is free software; you can redistribute it and/or modify  *}
-{*  it under the terms of the GNU General Public License as published by  *}
-{*  the Free Software Foundation; either version 2 of the License, or     *}
-{*  (at your option) any later version.                                   *}
-{*                                                                        *}
-{*  This program is distributed in the hope that it will be useful,       *}
-{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
-{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
-{*  GNU General Public License for more details.                          *}
-{*                                                                        *}
-{*  You should have received a copy of the GNU General Public License     *}
-{*  along with this program; if not, write to the Free Software           *}
-{*  Foundation, Inc.,                                                     *}
-{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
-{*                                                                        *}
-{**************************************************************************}
-
-<div>
-  <div>{$question->label}</div>
-  <input type="text" name="qid[{$question->qid}]" value="" />
-</div>
-
-{* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *}
similarity index 75%
rename from templates/survey/question.section.tpl
rename to templates/survey/questions.tpl
index cb4d2af..e7c8709 100644 (file)
 {*                                                                        *}
 {**************************************************************************}
 
-<fieldset>
-  <legend>{$question->label}</legend>
+{literal}
+<script id="question_base" type="text/x-jquery-tmpl">
+  {{if type == 'text'}}
+    {{tmpl "#question_text"}}
+  {{else type == 'section'}}
+    {{tmpl "#question_section"}}
+  {{/if}}
+</script>
 
-{foreach from=$question->children item=child}
-  {include file=$child->voteTemplate() question=$child}
-{/foreach}
-</fieldset>
+<script id="question_section" type="text/x-jquery-tmpl">
+  <fieldset>
+    <legend>${label}</legend>
+
+    {{tmpl(children) "#question_base"}}
+  </fieldset>
+</script>
+
+<script id="question_text" type="text/x-jquery-tmpl">
+  <div>
+    <div>${label}</div>
+    <input type="text" name="qid[${qid}]" value="" />
+  </div>
+</script>
+{/literal}
 
 {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *}
index 2dbe32e..5b91735 100644 (file)
 
 <p>{$survey->description|miniwiki}</p>
 
-<div>
 <form action="survey/vote/{$survey->shortname}" method="post">
-  {foreach from=$survey->questions item=question}
-    {include file=$question->voteTemplate() question=$question}
-  {/foreach}
+  <div id="questions">
+  </div>
 
   <div class="center">
     {xsrf_token_field}
     <input type="submit" name="vote" value="Enregister mon vote" />
   </div>
 </form>
-</div>
+
+{include file="survey/questions.tpl"}
+
+<script type="text/javascript">
+  //<![CDATA[
+  var questions = {$survey->exportQuestionsToJSON()|smarty:nodefaults};
+
+  {literal}
+  $(function() {
+    $("#question_base").tmpl(questions).appendTo("#questions");
+  });
+  {/literal}
+  //]]>
+</script>
 
 {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *}