+ // }}}
+
+ // {{{ function proposeSurvey() : stores a proposition of survey in database (before validation)
+ public function proposeSurvey()
+ {
+ $surveyreq = new SurveyReq($this->title, $this->description, $this->end, $this->mode, $this->promos, $this->questions, S::user());
+ return $surveyreq->submit();
+ }
+ // }}}
+
+ // {{{ function updateSurvey() : updates a survey in database (before validation)
+ public function updateSurvey()
+ {
+ if ($this->valid) {
+ $sql = 'UPDATE surveys
+ SET questions={?},
+ title={?},
+ description={?},
+ end={?},
+ mode={?},
+ promos={?}
+ WHERE id={?};';
+ return XDB::execute($sql, serialize($this->questions), $this->title, $this->description, $this->end, $this->mode, $this->promos, $this->id);
+ } else {
+ $surveyreq = Validate::get_request_by_id($this->id);
+ if ($surveyreq == null) {
+ return false;
+ }
+ return $surveyreq->updateReq($this->title, $this->description, $this->end, $this->mode, $this->promos, $this->questions);
+ }
+ }
+ // }}}
+
+ // {{{ functions vote() and hasVoted() : handles vote to a survey
+ public function vote($uid, $args)
+ {
+ XDB::execute('INSERT INTO survey_votes
+ SET survey_id = {?}, uid = {?}',
+ $this->id, ($uid == 0) ? null : $uid); // notes the user as having voted
+ $vid = XDB::insertId();
+ for ($i = 0; $i < count($this->questions); $i++) {
+ $ans = $this->questions[$i]->checkAnswer($args[$i]);
+ 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);
+ }
+ }
+ }
+ }
+
+ public function hasVoted($uid)
+ {
+ $res = XDB::query('SELECT id
+ FROM survey_votes
+ WHERE survey_id = {?} AND uid = {?};', $this->id, $uid); // checks whether the user has already voted
+ return ($res->numRows() != 0);
+ }
+ // }}}
+
+ // {{{ static function deleteSurvey() : deletes a survey (and all its votes)
+ public static function deleteSurvey($sid)
+ {
+ $sql = 'DELETE s.*, v.*, a.*
+ FROM surveys AS s
+ LEFT JOIN survey_votes AS v
+ ON v.survey_id=s.id
+ LEFT JOIN survey_answers AS a
+ ON a.vote_id=v.id
+ WHERE s.id={?};';
+ return XDB::execute($sql, $sid);
+ }
+ // }}}