| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | class SurveyVote extends PlDBTableEntry |
| 23 | { |
| 24 | protected $survey; |
| 25 | protected $user; |
| 26 | |
| 27 | private $answers = array(); |
| 28 | private $fetchAnswers; |
| 29 | |
| 30 | public function __construct(Survey $survey, User $user) |
| 31 | { |
| 32 | parent::__construct('survey_votes'); |
| 33 | $this->survey = $survey; |
| 34 | $this->user = $user; |
| 35 | $this->sid = $survey->id; |
| 36 | } |
| 37 | |
| 38 | protected function postSave() |
| 39 | { |
| 40 | Platal::assert(!is_null($this->vid), "Cannot process a vote without its identifier"); |
| 41 | XDB::execute("REPLACE INTO survey_voters (sid, uid, vid) |
| 42 | VALUES ({?}, {?}, {?})", |
| 43 | $this->survey->id, $this->user->id(), |
| 44 | $this->survey->flags->hasFlag('anonymous') ? null : $this->vid); |
| 45 | |
| 46 | /* Save answers */ |
| 47 | $selector = new SurveyAnswer($this); |
| 48 | $selector->delete(); |
| 49 | |
| 50 | $answers = array(); |
| 51 | foreach ($this->answers as $key=>$answer) { |
| 52 | if (!is_null($answer)) { |
| 53 | $answer->vid = $this->vid; |
| 54 | $answers[] = $answer; |
| 55 | } |
| 56 | } |
| 57 | PlDBTableEntry::insertBatch($answers); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | protected function postFetch() |
| 62 | { |
| 63 | $selector = new SurveyAnswer($this); |
| 64 | foreach ($selector as $answer) { |
| 65 | $question = $this->survey->questionForId($answer->qid); |
| 66 | $this->answers[$answer->qid] = $answer; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | public function inError() |
| 72 | { |
| 73 | foreach ($this->answers as $answer) { |
| 74 | if ($answer->inError !== false) { |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | public function getAnswer(SurveyQuestion $question) |
| 82 | { |
| 83 | if (!isset($this->answers[$question->qid])) { |
| 84 | $val = new SurveyAnswer($this); |
| 85 | $val->qid = $question->qid; |
| 86 | $this->answers[$question->qid] = $val; |
| 87 | } |
| 88 | return $this->answers[$question->qid]; |
| 89 | } |
| 90 | |
| 91 | public function export() |
| 92 | { |
| 93 | $export = array(); |
| 94 | foreach ($this->answers as $qid=>$answer) { |
| 95 | $export[$qid] = $answer->export(); |
| 96 | } |
| 97 | return $export; |
| 98 | } |
| 99 | |
| 100 | public static function getVote(Survey $survey, User $user, $fetchAnswers = true) |
| 101 | { |
| 102 | $vid = XDB::query('SELECT vid |
| 103 | FROM survey_voters |
| 104 | WHERE sid = {?} AND uid = {?}', |
| 105 | $survey->id, $user->id()); |
| 106 | if ($vid->numRows() == 0) { |
| 107 | $vote = new SurveyVote($survey, $user); |
| 108 | $vote->fetchAnswers = $fetchAnswers; |
| 109 | return $vote; |
| 110 | } |
| 111 | $vid = $vid->fetchOneCell(); |
| 112 | if (is_null($vid)) { |
| 113 | /* User already vote, but survey is anonymous and the vote |
| 114 | * cannot be changed |
| 115 | */ |
| 116 | return null; |
| 117 | } |
| 118 | $vote = new SurveyVote($survey, $user); |
| 119 | $vote->vid = $vid; |
| 120 | $vote->fetchAnswers = $fetchAnswers; |
| 121 | $vote->fetch(); |
| 122 | return $vote; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | class SurveyAnswer extends PlDBTableEntry |
| 127 | { |
| 128 | public $inError = false; |
| 129 | public $vote; |
| 130 | |
| 131 | public function __construct(SurveyVote $vote) |
| 132 | { |
| 133 | parent::__construct('survey_vote_answers'); |
| 134 | $this->registerFieldFormatter('answer', 'JSonFieldFormatter'); |
| 135 | $this->vote = $vote; |
| 136 | if (!is_null($vote->vid)) { |
| 137 | $this->vid = $vote->vid; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | protected function preSave() |
| 142 | { |
| 143 | Platal::assert(!$this->inError, "Cannot save an invalid answer"); |
| 144 | $this->sid = $this->vote->sid; |
| 145 | $this->vid = $this->vote->vid; |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | public function export() |
| 150 | { |
| 151 | $export = array(); |
| 152 | if (!is_null($this->answer)) { |
| 153 | $export['value'] = $this->answer->export(); |
| 154 | } |
| 155 | if ($this->inError !== false) { |
| 156 | $export['error'] = $this->inError; |
| 157 | } |
| 158 | return $export; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: |
| 163 | ?> |