From 99ec711f235ebfe45b689661efde4f4c0d77c663 Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Sun, 9 Jan 2011 22:55:43 +0100 Subject: [PATCH] Can save a new survey \o/ Still some bugs, but, hey, you can propose a survey and vote. Signed-off-by: Florent Bruneau --- htdocs/javascript/survey.js | 25 ++++++++++++++-- modules/survey.php | 59 ++++++++++++++++++------------------- templates/survey/edit.questions.tpl | 10 +++++-- templates/survey/edit.tpl | 28 ++++++++++++++---- templates/survey/vote.tpl | 2 +- 5 files changed, 81 insertions(+), 43 deletions(-) diff --git a/htdocs/javascript/survey.js b/htdocs/javascript/survey.js index 834b9e5..a0ef4e2 100644 --- a/htdocs/javascript/survey.js +++ b/htdocs/javascript/survey.js @@ -70,12 +70,18 @@ $.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'); }, @@ -105,7 +111,7 @@ }, parentQuestion: function() { - return this.closest('.q_edit'); + return this.parent().closest('.q_edit'); }, childrenContainer: function() { @@ -157,6 +163,21 @@ $.renumberQuestions(); return res; }, + + buildParentsQuestions: function() { + var $this = $(this); + $.questions().each(function() { + var parent = $(this).parentQuestion(); + if (!parent.isRootSection()) { + $('', { + type: 'hidden', + name: 'q_edit[' + $(this).qid() + '][parent]', + value: parent.qid() + }).appendTo($this); + } + }); + return $this; + } }); })(jQuery); diff --git a/modules/survey.php b/modules/survey.php index 1d0c014..d730af5 100644 --- a/modules/survey.php +++ b/modules/survey.php @@ -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); } diff --git a/templates/survey/edit.questions.tpl b/templates/survey/edit.questions.tpl index 8182563..960382d 100644 --- a/templates/survey/edit.questions.tpl +++ b/templates/survey/edit.questions.tpl @@ -22,17 +22,19 @@ {literal} +{/literal} + {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *} diff --git a/templates/survey/vote.tpl b/templates/survey/vote.tpl index e76065e..0a771da 100644 --- a/templates/survey/vote.tpl +++ b/templates/survey/vote.tpl @@ -40,7 +40,7 @@ //exportQuestionsToJSON()|smarty:nodefaults}; - $($("#questions").surveyQuestions(questions)); + $($("#questions").showQuestions(questions)); //]]> -- 2.1.4