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 interface SurveyQuestionContainer
24 public function addQuestion(SurveyQuestion
$question, $pos = null
);
25 public function newQuestion($type, $pos = null
);
26 public function reassignQuestionIds();
29 class SurveyQuestion
extends PlDBTableEntry
32 protected $parentQuestion;
34 public function __construct(Survey
$survey)
36 parent
::__construct('survey_questions');
37 $this->registerFieldFormatter('parameters', 'JSonFieldFormatter');
38 $this->survey
= $survey;
41 public function typedInstance()
43 $instance = self
::instanceForType($this->survey
, $this->type
);
44 $instance->copy($this);
48 public static function instanceForType(Survey
$survey, $type)
50 $file = dirname(__FILE__
) . '/' . $type . '.inc.php';
51 if (!file_exists($file)) {
52 throw new Exception("Unknown question type \"$type\"");
55 $class = 'SurveyQuestion' . $type;
56 return new $class($survey);
59 public function voteTemplate()
61 return 'survey/question.' . $this->type
. '.tpl';
64 public function editTemplate()
66 return 'survey/edit.' . $this->type
. '.tpl';
69 protected function buildAnswer(SurveyAnswer
$answer, PlDict
$answers)
71 Platal
::assert(false
, "This should not happen");
74 public function vote(SurveyVote
$vote, PlDict
$answers)
76 if ($this->flags
->hasFlag('noanswer')) {
77 if ($answers->has($this->qid
)) {
78 throw new Exception("Des réponses ont été données à une question n'en attendant pas");
82 $answer = $vote->getAnswer($this);
83 if (is_null($answer)) {
86 if (!$this->buildAnswer($answer, $answers)) {
89 if ($this->flags
->hasFlag('mandatory') && is_null($answer->answer
)) {
90 $answer->inError
= 'Tu dois répondre à cette question';
96 class SurveyQuestionGroup
extends SurveyQuestion
implements SurveyQuestionContainer
98 public $children = array();
100 public function __construct(Survey
$survey)
102 parent
::__construct($survey);
105 public function addQuestion(SurveyQuestion
$question, $pos = null
)
107 $question->parentQuestion
= $this;
109 $this->children
[] = $question;
111 array_splice($this->children
, $pos, 0, $question);
115 public function newQuestion($type, $pos = null
)
117 $question = SurveyQuestion
::instanceForType($this->survey
, $type);
118 $this->addQuestion($question, $pos);
122 public function reassignQuestionIds()
124 $id = $this->qid +
1;
125 foreach ($this->children
as $question) {
126 $question->qid
= $id;
127 if ($question instanceof SurveyQuestionContainer
) {
128 $id = $question->reassignQuestionIds();
136 protected function postSave()
138 foreach ($this->children
as $question) {
139 $question->sid
= $this->sid
;
140 $question->parent
= $this->qid
;
145 public function export()
147 $export = parent
::export();
148 $export['children'] = array();
149 foreach ($this->children
as $child) {
150 $export['children'][] = $child->export();
155 public function vote(SurveyVote
$vote, PlDict
$answers)
157 $a = parent
::vote($vote, $answers);
158 foreach ($this->children
as $child) {
159 $child->vote($vote, $answers);
164 public function child($qid)
167 foreach ($this->children
as $question) {
168 if ($qid == $question->qid
) {
170 } else if ($qid < $question->qid
) {
171 Platal
::assert($prev instanceof SurveyQuestionGroup
);
172 return $prev->child($qid);
176 Platal
::assert($prev instanceof SurveyQuestionGroup
);
177 return $prev->child($qid);
181 // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: