Start working on multiple choice questions.
[platal.git] / modules / survey.php
index 7a34573..d1626a8 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -26,8 +26,9 @@ class SurveyModule extends PLModule
         return array(
             'survey'              => $this->make_hook('index',         AUTH_COOKIE),
             'survey/vote'         => $this->make_hook('vote',          AUTH_COOKIE),
-    /*        'survey/result'       => $this->make_hook('result',        AUTH_COOKIE),
             'survey/edit'         => $this->make_hook('edit',          AUTH_COOKIE),
+            /*
+            'survey/result'       => $this->make_hook('result',        AUTH_COOKIE),
             'survey/ajax'         => $this->make_hook('ajax',          AUTH_COOKIE),
             'survey/admin'        => $this->make_hook('admin',         AUTH_MDP, 'admin'),
             'survey/admin/edit'   => $this->make_hook('adminEdit',     AUTH_MDP, 'admin'),
@@ -36,39 +37,20 @@ class SurveyModule extends PLModule
       */  );
     }
 
-    function handler_index(&$page, $action = null)
+    private function setup_page(PlPage $page)
     {
         $this->load('survey.inc.php');
+        $page->addJsLink('jquery.ui.core.js');
+        $page->addJsLink('jquery.ui.widget.js');
+        $page->addJsLink('jquery.ui.datepicker.js');
+        $page->addJsLink('jquery.ui.datepicker-fr.js');
+        $page->addJsLink('jquery.tmpl.js');
+        $page->addJsLink('survey.js');
+    }
 
-        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);
+    function handler_index(&$page, $action = null)
+    {
+        $this->setup_page($page);
 
         $page->changeTpl('survey/index.tpl');
         $page->assign('active', Survey::iterActive());
@@ -76,8 +58,9 @@ class SurveyModule extends PLModule
 
     function handler_vote(PlPage $page, $name)
     {
-        $this->load('survey.inc.php');
+        $this->setup_page($page);
         $page->changeTpl('survey/vote.tpl');
+
         $survey = Survey::get($name);
         if (is_null($survey)) {
             return PL_NOT_FOUND;
@@ -99,6 +82,49 @@ class SurveyModule extends PLModule
         }
         $page->assign('survey', $survey);
     }
+
+    function handler_edit(PlPage $page, $name = null)
+    {
+        $this->setup_page($page);
+        $page->changeTpl('survey/edit.tpl');
+
+        if (!is_null($name)) {
+            $survey = Survey::get($name);
+        } else {
+            $survey = new Survey();
+            $survey->id = null;
+            $survey->uid = S::user()->id();
+        }
+        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';
+            $survey->flags->addFlag('anonymous', Post::b('anonymous'));
+
+            $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']];
+                } 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);
+    }
 }
 
 // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: