2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class SurveyVote
extends PlDBTableEntry
27 private $answers = array();
28 private $fetchAnswers;
30 public function __construct(Survey
$survey, User
$user)
32 parent
::__construct('survey_votes');
33 $this->survey
= $survey;
35 $this->sid
= $survey->id
;
38 protected function postSave()
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
);
47 $selector = new SurveyAnswer($this);
51 foreach ($this->answers
as $key=>$answer) {
52 if (!is_null($answer)) {
53 $answer->vid
= $this->vid
;
57 PlDBTableEntry
::insertBatch($answers);
61 protected function postFetch()
63 $selector = new SurveyAnswer($this);
64 foreach ($selector as $answer) {
65 $question = $this->survey
->questionForId($answer->qid
);
66 $this->answers
[$answer->qid
] = $answer;
71 public function inError()
73 foreach ($this->answers
as $answer) {
74 if ($answer->inError
!== false
) {
81 public function getAnswer(SurveyQuestion
$question)
83 if (!isset($this->answers
[$question->qid
])) {
84 $val = new SurveyAnswer($this);
85 $val->qid
= $question->qid
;
86 $this->answers
[$question->qid
] = $val;
88 return $this->answers
[$question->qid
];
91 public function export()
94 foreach ($this->answers
as $qid=>$answer) {
95 $export[$qid] = $answer->export();
100 public static function getVote(Survey
$survey, User
$user, $fetchAnswers = true
)
102 $vid = XDB
::query('SELECT vid
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;
111 $vid = $vid->fetchOneCell();
113 /* User already vote, but survey is anonymous and the vote
118 $vote = new SurveyVote($survey, $user);
120 $vote->fetchAnswers
= $fetchAnswers;
126 class SurveyAnswer
extends PlDBTableEntry
128 public $inError = false
;
131 public function __construct(SurveyVote
$vote)
133 parent
::__construct('survey_vote_answers');
134 $this->registerFieldFormatter('answer', 'JSonFieldFormatter');
136 if (!is_null($vote->vid
)) {
137 $this->vid
= $vote->vid
;
141 protected function preSave()
143 Platal
::assert(!$this->inError
, "Cannot save an invalid answer");
144 $this->sid
= $this->vote
->sid
;
145 $this->vid
= $this->vote
->vid
;
149 public function export()
152 if (!is_null($this->answer
)) {
153 $export['value'] = $this->answer
->export();
155 if ($this->inError
!== false
) {
156 $export['error'] = $this->inError
;
162 // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: