first view of the results of a survey
authorx2004laborde <x2004laborde@839d8a87-29fc-0310-9880-83ba4fa771e5>
Tue, 10 Apr 2007 21:20:06 +0000 (21:20 +0000)
committerx2004laborde <x2004laborde@839d8a87-29fc-0310-9880-83ba4fa771e5>
Tue, 10 Apr 2007 21:20:06 +0000 (21:20 +0000)
git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1682 839d8a87-29fc-0310-9880-83ba4fa771e5

modules/survey.php
modules/survey/survey.inc.php
templates/survey/confirm.tpl
templates/survey/error.tpl
templates/survey/show_checkbox.tpl
templates/survey/show_radio.tpl
templates/survey/show_root.tpl
templates/survey/show_text.tpl
templates/survey/show_textarea.tpl
templates/survey/success.tpl
upgrade/0.9.14/07_survey.sql

index b53577f..1051117 100644 (file)
@@ -27,6 +27,7 @@ class SurveyModule extends PLModule
         return array(
             'survey'              => $this->make_hook('index', AUTH_PUBLIC),
             'survey/vote'         => $this->make_hook('vote', AUTH_PUBLIC),
+            'survey/result'       => $this->make_hook('result', AUTH_PUBLIC),
             'survey/edit'         => $this->make_hook('edit', AUTH_COOKIE),
             'survey/ajax'         => $this->make_hook('ajax', AUTH_COOKIE),
             'survey/admin'        => $this->make_hook('admin', AUTH_MDP, 'admin'),
@@ -48,7 +49,7 @@ class SurveyModule extends PLModule
     }
     // }}}
 
-    // {{{ function handler_vote()
+    // {{{ function handler_vote() : handles the vote to a survey
     function handler_vote(&$page, $id = -1)
     {
         if (Post::has('survey_cancel')) { // if the user cancels, returns to index
@@ -65,15 +66,8 @@ class SurveyModule extends PLModule
         } elseif ($survey->isEnded()) {
             return $this->show_error($page, "Le sondage ".$survey->getTitle()." est termin&#233;.");
         }
-        if (!$survey->isMode(Survey::MODE_ALL)) { // if the survey is reserved to alumni
-            global $globals;
-            if (!call_user_func(array($globals->session, 'doAuth'))) { // checks authentification
-                global $platal;
-                $platal->force_login($page);
-            }
-            if ($survey->isMode(Survey::MODE_XIDENT) && !$survey->checkPromo(S::v('promo'))) { // checks promotion
-                return $this->show_error($page, "Tu n'as pas acc&#232;s &#224; ce sondage car il est r&#233;serv&#233; &#224; d'autres promotions.");
-            }
+        if (!$this->check_surveyPerms($page, $survey)) {
+            return;
         }
         if (Post::has('survey_submit')) { // checks if the survey has already been filled in
             $uid = 0;
@@ -97,6 +91,28 @@ class SurveyModule extends PLModule
     }
     // }}}
 
+    // {{{ function handler_result() : show the results of the votes to a survey
+    function handler_result($page, $id = -1, $qid = 'all')
+    {
+        $id = intval($id);
+        if ($id == -1) {
+            return $this->show_error($page, "Un identifiant de sondage doit &#234;tre pr&#233;cis&#233;.", '');
+        }
+        require_once dirname(__FILE__).'/survey/survey.inc.php';
+        $survey = Survey::retrieveSurvey($id); // retrieves the survey object structure
+        if ($survey == null || !$survey->isValid()) {
+            return $this->show_error($page, "Sondage ".$id." introuvable.", '');
+        } elseif (!$survey->isEnded()) {
+            return $this->show_error($page, "Le sondage ".$survey->getTitle()." n'est pas encore termin&#233;.");
+        }
+        if (!$this->check_surveyPerms($page, $survey)) {
+            return;
+        }
+        $page->assign('survey_resultmode', true);
+        $this->show_survey($page, $survey);
+    }
+    // }}}
+
     // {{{ function handler_admin() : index of admin mode
     function handler_admin(&$page, $id = -1)
     {
@@ -348,6 +364,25 @@ class SurveyModule extends PLModule
     }
     // }}}
 
+    // {{{ function check_surveyPerms() : checks the particular surveys access permissions
+    function check_surveyPerms(&$page, $survey)
+    {
+        require_once dirname(__FILE__).'/survey/survey.inc.php';
+        if (!$survey->isMode(Survey::MODE_ALL)) { // if the survey is reserved to alumni
+            global $globals;
+            if (!call_user_func(array($globals->session, 'doAuth'))) { // checks authentification
+                global $platal;
+                $platal->force_login($page);
+            }
+            if (!$survey->checkPromo(S::v('promo'))) { // checks promotion
+                $this->show_error($page, "Tu n'as pas acc&#232;s &#224; ce sondage car il est r&#233;serv&#233; &#224; d'autres promotions.");
+                return false;
+            }
+        }
+        return true;
+    }
+    // }}}
+
     // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results
     function show_survey(&$page, $survey)
     {
index 0b2f4cd..9b92360 100644 (file)
@@ -126,17 +126,17 @@ class Survey
     }
     // }}}
 
-    // {{{ function toArray() : converts a question (or the whole survey) to array
+    // {{{ function toArray() : converts a question (or the whole survey) to array, with results if the survey is ended
     public function toArray($i = 'all')
     {
-        if ($i != 'all' && $i != 'root') {
+        if ($i != 'all' && $i != 'root') { // if a specific question is requested, then just returns this question converted to array
             $i = intval($i);
             if (array_key_exists($i, $this->questions)) {
                 return $this->questions[$i]->toArray();
             } else {
                 return null;
             }
-        } else {
+        } else { // else returns the root converted to array in any case
             $a = array('title'       => $this->title,
                        'description' => $this->description,
                        'end'         => $this->end,
@@ -147,11 +147,24 @@ class Survey
             if ($this->id != -1) {
                 $a['id'] = $this->id;
             }
-            if ($i == 'all' && count($this->questions) > 0) {
+            if ($this->isEnded()) { // if the survey is ended, then adds here the number of votes
+                $sql = 'SELECT COUNT(id)
+                          FROM survey_votes
+                         WHERE survey_id={?};';
+                $tot = XDB::query($sql, $this->id);
+                $a['votes'] = $tot->fetchOneCell();
+            }
+            if ($i == 'all' && count($this->questions) > 0) { // if the whole survey is requested, then returns all the questions converted to array
                 $qArr = array();
                 for ($k = 0; $k < count($this->questions); $k++) {
                     $q = $this->questions[$k]->toArray();
                     $q['id'] = $k;
+                    if ($this->isEnded()) { // if the survey is ended, then adds here the results of this question
+                        $sql = $this->questions[$k]->buildResultRequest();
+                        if ($sql != '') {
+                            $q['result'] = XDB::iterator($sql, $this->id, $k);
+                        }
+                    }
                     $qArr[$k] = $q;
                 }
                 $a['questions'] = $qArr;
@@ -246,7 +259,7 @@ class Survey
     }
     // }}}
 
-    // {{{ functions that manipulates surveys in database
+    // {{{ functions that manipulate surveys in database
     // {{{ static function retrieveList() : gets the list of available survey (current, old and not validated surveys)
     public static function retrieveList($type, $tpl = true)
     {
@@ -356,11 +369,13 @@ class Survey
         $vid = XDB::insertId();
         for ($i = 0; $i < count($this->questions); $i++) {
             $ans = $this->questions[$i]->checkAnswer($args[$i]);
-            if ($ans != "") {
-                XDB::execute('INSERT INTO survey_answers
-                                      SET vote_id     = {?},
-                                          question_id = {?},
-                                          answer      = {?}', $vid, $i, $ans);
+            if (!is_null($ans) && is_array($ans)) {
+                foreach ($ans as $a) {
+                    XDB::execute('INSERT INTO survey_answers
+                                          SET vote_id     = {?},
+                                              question_id = {?},
+                                              answer      = {?}', $vid, $i, $a);
+                }
             }
         }
     }
@@ -442,24 +457,40 @@ abstract class SurveyQuestion
     // {{{ function checkAnswer : returns a correctly formatted answer (or nothing empty string if error)
     public function checkAnswer($ans)
     {
-        return "";
+        return null;
     }
     // }}}
 
-    // {{{ function resultArray() : statistics on the results of the survey
-    //abstract protected function resultArray($sid, $where);
+    // {{{ function buildResultRequest() : statistics on the results of the survey
+    public function buildResultRequest()
+    {
+        return '';
+    }
     // }}}
 }
 // }}}
 
-// {{{ abstract class SurveySimple extends SurveyQuestion : "opened" questions
+// {{{ abstract class SurveySimple and its derived classes : "opended" questions
+// {{{ abstract class SurveySimple extends SurveyQuestion
 abstract class SurveySimple extends SurveyQuestion
 {
     public function checkAnswer($ans)
     {
-        return $ans;
+        return array($ans);
+    }
+
+    public function buildResultRequest()
+    {
+        $sql = 'SELECT answer
+                  FROM survey_answers
+                 WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?})
+                   AND question_id={?}
+              ORDER BY RAND()
+                 LIMIT 5;';
+        return $sql;
     }
 }
+// }}}
 
 // {{{ class SurveyText extends SurveySimple : simple text field, allowing a few words
 class SurveyText extends SurveySimple
@@ -486,7 +517,7 @@ class SurveyNum extends SurveySimple
 {
     public function checkAnswer($ans)
     {
-        return intval($ans);
+        return array(intval($ans));
     }
 
     protected function getQuestionType()
@@ -497,7 +528,8 @@ class SurveyNum extends SurveySimple
 // }}}
 // }}}
 
-// {{{ abstract class SurveyList extends SurveyTreeable : restricted questions that allows only a list of possible answers
+// {{{ abstract class SurveyList and its derived classes : restricted questions that allows only a list of possible answers
+// {{{ abstract class SurveyList extends SurveyQuestion
 abstract class SurveyList extends SurveyQuestion
 {
     protected $choices;
@@ -516,14 +548,24 @@ abstract class SurveyList extends SurveyQuestion
         return $rArr;
     }
 
+    public function buildResultRequest()
+    {
+        $sql = 'SELECT answer, COUNT(id) AS count
+                  FROM survey_answers
+                 WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?})
+                   AND question_id={?}
+              GROUP BY answer ASC';
+        return $sql;
+    }
 }
+// }}}
 
 // {{{ class SurveyRadio extends SurveyList : radio question, allows one answer among the list offered
 class SurveyRadio extends SurveyList
 {
     public function checkAnswer($ans)
     {
-        return (array_key_exists($ans, $this->choices)) ? $ans : "";
+        return (array_key_exists($ans, $this->choices))? array($ans) : null;
     }
 
     protected function getQuestionType()
@@ -538,13 +580,14 @@ class SurveyCheckbox extends SurveyList
 {
     public function checkAnswer($ans)
     {
-        $rep = "|";
+        $rep = array();
         foreach ($ans as $a) {
-            if (array_key_exists($a,$this->choices)) {
-                $rep .= $a . "|";
+            $a = intval($a);
+            if (array_key_exists($a, $this->choices)) {
+                $rep[] = $a;
             }
         }
-        return $rep;
+        return (count($rep) == 0)? null : $rep;
     }
 
     protected function getQuestionType()
@@ -574,9 +617,9 @@ class SurveyPersonal extends SurveyQuestion
     {
         if (intval($ans) == 1) {
             // requete mysql qvb
-            return "";
+            return null;
         } else {
-            return "";
+            return null;
         }
     }
 
index d25fbd8..30586e2 100644 (file)
@@ -19,7 +19,7 @@
 {*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
 {*                                                                        *}
 {**************************************************************************}
-<h1>Sondage : confirmation</h1>
+<h1>Sondages : confirmation</h1>
 
 <form action="{$survey_formaction}" method="post">
   {if is_array($survey_formhidden)}
index fce9b05..695e714 100644 (file)
@@ -19,7 +19,7 @@
 {*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
 {*                                                                        *}
 {**************************************************************************}
-<h1>Sondage : erreur</h1>
+<h1>Sondages : erreur</h1>
 
 {if !is_null($survey_errors) && is_array($survey_errors)}
 <table class="bicol">
index ebc01e1..32181c2 100644 (file)
 {*                                                                        *}
 {**************************************************************************}
 
-{assign var=sid value=$survey.id}
-{assign var=sqid value=$squestion.id}
-{if $survey_votemode}
-  {html_checkboxes name="survey$sid[$sqid]" options=$squestion.choices separator='<br/>'}
+{if $survey_resultmode}
+  <ul>
+  {iterate item=sresult from=$squestion.result}
+    <li>{$squestion.choices[$sresult.answer]} : {$sresult.count*100/$survey.votes|string_format:"%.1f"}% ({$sresult.count} votes)</li>
+  {/iterate}
+  </ul>
 {else}
-  {html_checkboxes name="survey$sid[$sqid]" options=$squestion.choices separator='<br/>' disabled='disabled'}
+  {assign var=sid value=$survey.id}
+  {assign var=sqid value=$squestion.id}
+  {if $survey_votemode}
+    {html_checkboxes name="survey$sid[$sqid]" options=$squestion.choices separator='<br/>'}
+  {else}
+    {html_checkboxes name="survey$sid[$sqid]" options=$squestion.choices separator='<br/>' disabled='disabled'}
+  {/if}
 {/if}
 
 {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *}
index 1ee53d1..5ac0a87 100644 (file)
 {*                                                                        *}
 {**************************************************************************}
 
-{assign var=sid value=$survey.id}
-{assign var=sqid value=$squestion.id}
-{if $survey_votemode}
-  {html_radios name="survey$sid[$sqid]" options=$squestion.choices separator='<br/>'}
+{if $survey_resultmode}
+  <ul>
+  {iterate item=sresult from=$squestion.result}
+    <li>{$squestion.choices[$sresult.answer]} : {$sresult.count*100/$survey.votes|string_format:"%.1f"}% ({$sresult.count} votes)</li>
+  {/iterate}
+  </ul>
 {else}
-  {html_radios name="survey$sid[$sqid]" options=$squestion.choices separator='<br/>' disabled='disabled'}
+  {assign var=sid value=$survey.id}
+  {assign var=sqid value=$squestion.id}
+  {if $survey_votemode}
+    {html_radios name="survey$sid[$sqid]" options=$squestion.choices separator='<br/>'}
+  {else}
+    {html_radios name="survey$sid[$sqid]" options=$squestion.choices separator='<br/>' disabled='disabled'}
+  {/if}
 {/if}
 
 {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *}
index 209cc5b..32e9357 100644 (file)
@@ -46,6 +46,9 @@
 {if $survey_warning neq ''}
   <br/>{$survey_warning}
 {/if}
+{if $survey_resultmode}
+  <br/>{$survey.votes} personnes ont r&#233;pondu &#224; ce sondage.
+{/if}
 <br/>
 {if $survey_editmode}
   {assign var="survey_rooteditmode" value=true}
index c103293..fb5710d 100644 (file)
 {*                                                                        *}
 {**************************************************************************}
 
+{if $survey_resultmode}
+  Quelques r&#233;ponses donn&#233;es par les personnes sond&#233;es :
+  <ul>
+  {iterate item=sresult from=$squestion.result}
+    <li>{$sresult.answer}</li>
+  {/iterate}
+  </ul>
+{else}
   <input type="text" name="survey{$survey.id}[{$squestion.id}]" value="" size="50" maxlength="200" {if !$survey_votemode}disabled="disabled"{/if}/>
+{/if}
 
 {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *}
index 6a6ff69..88a42b4 100644 (file)
 {*                                                                        *}
 {**************************************************************************}
 
+{if $survey_resultmode}
+  Quelques r&#233;ponses donn&#233;es par les personnes sond&#233;es :
+  <ul>
+  {iterate item=sresult from=$squestion.result}
+    <li>{$sresult.answer}</li>
+  {/iterate}
+  </ul>
+{else}
   <textarea name="survey{$survey.id}[{$squestion.id}]" rows="5" cols="60" {if !$survey_votemode}disabled="disabled"{/if}></textarea>
+{/if}
 
 {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *}
index 8f1fad4..3262927 100644 (file)
@@ -19,7 +19,7 @@
 {*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
 {*                                                                        *}
 {**************************************************************************}
-<h1>Sondage : succ&#232;s</h1>
+<h1>Sondages : succ&#232;s</h1>
 
 {if $survey_message neq ""}
   {$survey_message}
index 6528dca..b118916 100644 (file)
@@ -4,7 +4,7 @@ CREATE TABLE `survey_answers` (
   `question_id` smallint(3) unsigned NOT NULL,
   `answer` text NOT NULL,
   PRIMARY KEY  (`id`),
-  UNIQUE KEY `vote` (`vote_id`,`question_id`)
+  KEY `vote` (`vote_id`,`question_id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 
 
@@ -27,6 +27,7 @@ CREATE TABLE `survey_votes` (
   `survey_id` smallint(4) unsigned NOT NULL,
   `user_id` smallint(5) unsigned NOT NULL,
   PRIMARY KEY  (`id`),
+  KEY `voter` (`survey_id`,`user_id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 
 # vim: set syntax=mysql: