+// {{{ abstract class SurveyTable and its derived classes : table question, each column represents a choice, each line represents a question
+// {{{ abstract class SurveyTable extends SurveyList
+abstract class SurveyTable extends SurveyList
+{
+ protected $subquestions;
+
+ public function update($args)
+ {
+ parent::update($args);
+ $this->subquestions = array();
+ foreach ($args['subquestions'] as $val) {
+ if (trim($val) || trim($val) == '0') {
+ $this->subquestions[] = $val;
+ }
+ }
+ }
+
+ public function toArray()
+ {
+ $rArr = parent::toArray();
+ $rArr['subquestions'] = $this->subquestions;
+ return $rArr;
+ }
+
+ public function getResultArray($sid, $qid)
+ {
+ $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';
+ $res = XDB::iterator($sql, $sid, $qid);
+ $result = array();
+ for ($i = 0; $i < count($this->subquestions); $i++) {
+ $result[$i] = array_fill(0, count($this->choices), 0);
+ }
+ while ($r = $res->next()) {
+ list($i, $j) = explode(':', $r['answer']);
+ $result[$i][$j] = $r['count'];
+ }
+ return $result;
+ }
+
+ public function formatAnswer($ans)
+ {
+ list($q, $c) = explode(':', $ans);
+ if (array_key_exists($q, $this->subquestions) && array_key_exists($c, $this->choices)) {
+ return array('id' => $q, 'answer' => $this->choices[$c]);
+ } else {
+ return null;
+ }
+ }
+
+ public function getCSVColumns()
+ {
+ $q = parent::getCSVColumns();
+ if (empty($this->subquestions)) {
+ return $q;
+ }
+ $a = array();
+ for ($k = 0; $k < count($this->subquestions); $k++) {
+ $a[$k] = $q.' : '.$this->subquestions[$k];
+ }
+ return $a;
+ }
+}
+// }}}
+
+// {{{ class SurveyRadioTable extends SurveyTable : SurveyTable with radio type choices
+class SurveyRadioTable extends SurveyTable
+{
+ public function checkAnswer($ans)
+ {
+ $rep = array();
+ foreach ($ans as $k => $a) {
+ if (!array_key_exists($k, $this->subquestions)) {
+ continue;
+ }
+ $a = intval($a);
+ if (array_key_exists($a, $this->choices)) {
+ $rep[] = $k . ':' . $a;
+ }
+ }
+ return (count($rep) == 0)? null : $rep;
+ }
+
+ protected function getQuestionType()
+ {
+ return "radiotable";
+ }
+
+}
+// }}}
+
+// {{{ class SurveyCheckboxTable extends SurveyTable : SurveyTable with checkbox type choices
+class SurveyCheckboxTable extends SurveyTable
+{
+ public function checkAnswer($ans)
+ {
+ $rep = array();
+ foreach ($ans as $k => $aa) {
+ if (!array_key_exists($k, $this->subquestions)) {
+ continue;
+ }
+ foreach ($aa as $a) {
+ $a = intval($a);
+ if (array_key_exists($a, $this->choices)) {
+ $rep[] = $k . ':' . $a;
+ }
+ }
+ }
+ return (count($rep) == 0)? null : $rep;
+ }
+
+ protected function getQuestionType()
+ {
+ return "checkboxtable";
+ }
+
+}
+// }}}
+// }}}
+
+// vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: