From 9f01f40bbb3074f13b228d98f3a9db8f23ea4992 Mon Sep 17 00:00:00 2001 From: x2004laborde Date: Tue, 10 Apr 2007 21:20:06 +0000 Subject: [PATCH] first view of the results of a survey git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1682 839d8a87-29fc-0310-9880-83ba4fa771e5 --- modules/survey.php | 55 ++++++++++++++++++----- modules/survey/survey.inc.php | 91 ++++++++++++++++++++++++++++---------- templates/survey/confirm.tpl | 2 +- templates/survey/error.tpl | 2 +- templates/survey/show_checkbox.tpl | 18 +++++--- templates/survey/show_radio.tpl | 18 +++++--- templates/survey/show_root.tpl | 3 ++ templates/survey/show_text.tpl | 9 ++++ templates/survey/show_textarea.tpl | 9 ++++ templates/survey/success.tpl | 2 +- upgrade/0.9.14/07_survey.sql | 3 +- 11 files changed, 164 insertions(+), 48 deletions(-) diff --git a/modules/survey.php b/modules/survey.php index b53577f..1051117 100644 --- a/modules/survey.php +++ b/modules/survey.php @@ -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é."); } - 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ès à ce sondage car il est réservé à 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 être précisé.", ''); + } + 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é."); + } + 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ès à ce sondage car il est réservé à 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) { diff --git a/modules/survey/survey.inc.php b/modules/survey/survey.inc.php index 0b2f4cd..9b92360 100644 --- a/modules/survey/survey.inc.php +++ b/modules/survey/survey.inc.php @@ -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; } } diff --git a/templates/survey/confirm.tpl b/templates/survey/confirm.tpl index d25fbd8..30586e2 100644 --- a/templates/survey/confirm.tpl +++ b/templates/survey/confirm.tpl @@ -19,7 +19,7 @@ {* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *} {* *} {**************************************************************************} -

Sondage : confirmation

+

Sondages : confirmation

{if is_array($survey_formhidden)} diff --git a/templates/survey/error.tpl b/templates/survey/error.tpl index fce9b05..695e714 100644 --- a/templates/survey/error.tpl +++ b/templates/survey/error.tpl @@ -19,7 +19,7 @@ {* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *} {* *} {**************************************************************************} -

Sondage : erreur

+

Sondages : erreur

{if !is_null($survey_errors) && is_array($survey_errors)} diff --git a/templates/survey/show_checkbox.tpl b/templates/survey/show_checkbox.tpl index ebc01e1..32181c2 100644 --- a/templates/survey/show_checkbox.tpl +++ b/templates/survey/show_checkbox.tpl @@ -20,12 +20,20 @@ {* *} {**************************************************************************} -{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='
'} +{if $survey_resultmode} + {else} - {html_checkboxes name="survey$sid[$sqid]" options=$squestion.choices separator='
' 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='
'} + {else} + {html_checkboxes name="survey$sid[$sqid]" options=$squestion.choices separator='
' disabled='disabled'} + {/if} {/if} {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *} diff --git a/templates/survey/show_radio.tpl b/templates/survey/show_radio.tpl index 1ee53d1..5ac0a87 100644 --- a/templates/survey/show_radio.tpl +++ b/templates/survey/show_radio.tpl @@ -20,12 +20,20 @@ {* *} {**************************************************************************} -{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='
'} +{if $survey_resultmode} + {else} - {html_radios name="survey$sid[$sqid]" options=$squestion.choices separator='
' 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='
'} + {else} + {html_radios name="survey$sid[$sqid]" options=$squestion.choices separator='
' disabled='disabled'} + {/if} {/if} {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *} diff --git a/templates/survey/show_root.tpl b/templates/survey/show_root.tpl index 209cc5b..32e9357 100644 --- a/templates/survey/show_root.tpl +++ b/templates/survey/show_root.tpl @@ -46,6 +46,9 @@ {if $survey_warning neq ''}
{$survey_warning} {/if} +{if $survey_resultmode} +
{$survey.votes} personnes ont répondu à ce sondage. +{/if}
{if $survey_editmode} {assign var="survey_rooteditmode" value=true} diff --git a/templates/survey/show_text.tpl b/templates/survey/show_text.tpl index c103293..fb5710d 100644 --- a/templates/survey/show_text.tpl +++ b/templates/survey/show_text.tpl @@ -20,6 +20,15 @@ {* *} {**************************************************************************} +{if $survey_resultmode} + Quelques réponses données par les personnes sondées : + +{else} +{/if} {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *} diff --git a/templates/survey/show_textarea.tpl b/templates/survey/show_textarea.tpl index 6a6ff69..88a42b4 100644 --- a/templates/survey/show_textarea.tpl +++ b/templates/survey/show_textarea.tpl @@ -20,6 +20,15 @@ {* *} {**************************************************************************} +{if $survey_resultmode} + Quelques réponses données par les personnes sondées : + +{else} +{/if} {* vim:set et sw=2 sts=2 ts=8 enc=utf-8: *} diff --git a/templates/survey/success.tpl b/templates/survey/success.tpl index 8f1fad4..3262927 100644 --- a/templates/survey/success.tpl +++ b/templates/survey/success.tpl @@ -19,7 +19,7 @@ {* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *} {* *} {**************************************************************************} -

Sondage : succès

+

Sondages : succès

{if $survey_message neq ""} {$survey_message} diff --git a/upgrade/0.9.14/07_survey.sql b/upgrade/0.9.14/07_survey.sql index 6528dca..b118916 100644 --- a/upgrade/0.9.14/07_survey.sql +++ b/upgrade/0.9.14/07_survey.sql @@ -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: -- 2.1.4