}
// }}}
- // {{{ 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;
}
}