Start working on multiple choice questions.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 11 Jan 2011 22:06:05 +0000 (23:06 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 11 Jan 2011 22:06:05 +0000 (23:06 +0100)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
modules/survey.php
modules/survey/multiple.inc.php [new file with mode: 0644]
modules/survey/question.inc.php
modules/survey/survey.inc.php
templates/survey/edit.questions.tpl
templates/survey/index.tpl

index 832d3a4..d1626a8 100644 (file)
@@ -106,6 +106,7 @@ class SurveyModule extends PLModule
 
             $q_edit = Post::v('q_edit');
             $qs = array();
+            $survey->clearQuestions();
             foreach ($q_edit as $qid => $q_desc) {
                 if (isset($q_desc['parent'])) {
                     $parent = $qs[$q_desc['parent']];
diff --git a/modules/survey/multiple.inc.php b/modules/survey/multiple.inc.php
new file mode 100644 (file)
index 0000000..e9ce0fb
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+/***************************************************************************
+ *  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                *
+ ***************************************************************************/
+
+class SurveyQuestionMultiple extends SurveyQuestion
+{
+    public function __construct(Survey $survey)
+    {
+        parent::__construct($survey);
+        $this->type = "multiple";
+    }
+
+    protected function buildAnswer(SurveyAnswer $answer, PlDict $data)
+    {
+        $content = $data->v($this->qid);
+        $value   = $content['answer'];
+        if (empty($value)) {
+            $answer->answer = null;
+            return true;
+        }
+        $id = to_integer($value);
+        if ($id === false) {
+            if ($value != 'other') {
+                $answer->answers = null;
+                return false;
+            }
+            if (@$this->parameters['allow_other']) {
+                $answer->answer = array('other' => $content['text']);
+            }
+        } else {
+            if ($id >= count($this->parameters['answers'])) {
+                $answer->answers = null;
+                return false;
+            }
+            $answer->answer = array('answer' => $id);
+        }
+        return true;
+    }
+}
+
+?>
index f8cde84..bd206ad 100644 (file)
@@ -47,7 +47,11 @@ class SurveyQuestion extends PlDBTableEntry
 
     public static function instanceForType(Survey $survey, $type)
     {
-        require_once dirname(__FILE__) . '/' . $type . '.inc.php';
+        $file = dirname(__FILE__) . '/' . $type . '.inc.php';
+        if (!file_exists($file)) {
+            throw new Exception("Unknown question type \"$type\"");
+        }
+        require_once $file;
         $class = 'SurveyQuestion' . $type;
         return new $class($survey);
     }
index 865a148..7e715fd 100644 (file)
@@ -104,6 +104,12 @@ class Survey extends PlDBTableEntry implements SurveyQuestionContainer
         }
     }
 
+    public function clearQuestions()
+    {
+        $this->fetchQuestions = true;
+        $this->questions = array();
+    }
+
     public function addQuestion(SurveyQuestion $question, $pos = null)
     {
         $question->parent = null;
index df65dd1..a8a9646 100644 (file)
                         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>
+      <option value="text" {{if type}}{{if type == 'text'}}selected="selected"{{/if}}{{/if}}>
+        Texte
+      </option>
+      <option value="multiple" {{if type}}{{if type == 'multiple'}}selection="selected"{{/if}}{{/if}}>
+        Question à choix multiples
+      </option>
     </select>
     <div class="q_edit_form">
       {{tmpl "#q_edit_base"}}
     </div>
   </div>
 </script>
+
+<script id="q_edit_multiple" typex="text/x-jquery-tmpl">
+  <div id="q_edit[${qid}][answers]">
+    <div class="add_answer">
+      <a onclick="$(this).multipleAddAnswer()">
+        {/literal}{icon name="add"}{literal} Ajouter une réponse
+      </a>
+    </div>
+    <div>
+      Ajouter une case Autre ?
+      <select name="q_edit[${qid}][allow_other]">
+        <option name="1">Oui</option>
+        <option name="" selected="selected">Non</option>
+      </select>
+    </div>
+  </div>
+</script>
 {/literal}
 
 {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *}
index c0ade2f..6f8d914 100644 (file)
@@ -46,7 +46,7 @@
   {/iterate}
 </table>
 {/if}
-
+<br />
 <div class="center">
   <a href="survey/edit">{icon name=page_edit} Proposer un nouveau sondage</a>
 </div>