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'),
}
// }}}
- // {{{ 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
} 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;
}
// }}}
+ // {{{ 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)
{
}
// }}}
+ // {{{ 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)
{
}
// }}}
- // {{{ 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,
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;
}
// }}}
- // {{{ 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)
{
$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);
+ }
}
}
}
// {{{ 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
{
public function checkAnswer($ans)
{
- return intval($ans);
+ return array(intval($ans));
}
protected function getQuestionType()
// }}}
// }}}
-// {{{ 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;
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()
{
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()
{
if (intval($ans) == 1) {
// requete mysql qvb
- return "";
+ return null;
} else {
- return "";
+ return null;
}
}
{* 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)}
{* 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">
{* *}
{**************************************************************************}
-{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: *}
{* *}
{**************************************************************************}
-{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: *}
{if $survey_warning neq ''}
<br/>{$survey_warning}
{/if}
+{if $survey_resultmode}
+ <br/>{$survey.votes} personnes ont répondu à ce sondage.
+{/if}
<br/>
{if $survey_editmode}
{assign var="survey_rooteditmode" value=true}
{* *}
{**************************************************************************}
+{if $survey_resultmode}
+ Quelques réponses données par les personnes sondé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: *}
{* *}
{**************************************************************************}
+{if $survey_resultmode}
+ Quelques réponses données par les personnes sondé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: *}
{* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *}
{* *}
{**************************************************************************}
-<h1>Sondage : succès</h1>
+<h1>Sondages : succès</h1>
{if $survey_message neq ""}
{$survey_message}
`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;
`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: