Support multiline text answers.
[platal.git] / modules / survey / question.inc.php
CommitLineData
90343d1e
FB
1<?php
2/***************************************************************************
5e1513f6 3 * Copyright (C) 2003-2011 Polytechnique.org *
90343d1e
FB
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
22interface SurveyQuestionContainer
23{
24 public function addQuestion(SurveyQuestion $question, $pos = null);
25 public function newQuestion($type, $pos = null);
26 public function reassignQuestionIds();
27}
28
29class SurveyQuestion extends PlDBTableEntry
30{
31 protected $survey;
32 protected $parentQuestion;
33
34 public function __construct(Survey $survey)
35 {
36 parent::__construct('survey_questions');
37 $this->registerFieldFormatter('parameters', 'JSonFieldFormatter');
38 $this->survey = $survey;
39 }
40
41 public function typedInstance()
42 {
43 $instance = self::instanceForType($this->survey, $this->type);
44 $instance->copy($this);
45 return $instance;
46 }
47
48 public static function instanceForType(Survey $survey, $type)
49 {
3ddf2584
FB
50 $file = dirname(__FILE__) . '/' . $type . '.inc.php';
51 if (!file_exists($file)) {
52 throw new Exception("Unknown question type \"$type\"");
53 }
54 require_once $file;
90343d1e
FB
55 $class = 'SurveyQuestion' . $type;
56 return new $class($survey);
57 }
58
59 public function voteTemplate()
60 {
61 return 'survey/question.' . $this->type . '.tpl';
62 }
63
64 public function editTemplate()
65 {
66 return 'survey/edit.' . $this->type . '.tpl';
67 }
68
69 protected function buildAnswer(SurveyAnswer $answer, PlDict $answers)
70 {
71 Platal::assert(false, "This should not happen");
72 }
73
74 public function vote(SurveyVote $vote, PlDict $answers)
75 {
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");
79 }
80 return null;
81 }
82 $answer = $vote->getAnswer($this);
83 if (is_null($answer)) {
84 return null;
85 }
86 if (!$this->buildAnswer($answer, $answers)) {
87 return $answer;
88 }
89 if ($this->flags->hasFlag('mandatory') && is_null($answer->answer)) {
90 $answer->inError = 'Tu dois répondre à cette question';
91 }
92 return $answer;
93 }
ced5514d
FB
94
95 public function export()
96 {
97 $export = parent::export();
98 if (isset($export['parameters'])) {
99 foreach ($export['parameters'] as $key=>$value) {
100 $export[$key] = $value;
101 }
102 unset($export['parameters']);
103 }
104 return $export;
105 }
106
90343d1e
FB
107}
108
109class SurveyQuestionGroup extends SurveyQuestion implements SurveyQuestionContainer
110{
111 public $children = array();
112
113 public function __construct(Survey $survey)
114 {
115 parent::__construct($survey);
116 }
117
118 public function addQuestion(SurveyQuestion $question, $pos = null)
119 {
120 $question->parentQuestion = $this;
121 if (is_null($pos)) {
122 $this->children[] = $question;
123 } else {
124 array_splice($this->children, $pos, 0, $question);
125 }
126 }
127
128 public function newQuestion($type, $pos = null)
129 {
130 $question = SurveyQuestion::instanceForType($this->survey, $type);
131 $this->addQuestion($question, $pos);
132 return $question;
133 }
134
135 public function reassignQuestionIds()
136 {
137 $id = $this->qid + 1;
138 foreach ($this->children as $question) {
139 $question->qid = $id;
140 if ($question instanceof SurveyQuestionContainer) {
141 $id = $question->reassignQuestionIds();
142 } else {
143 $id++;
144 }
145 }
146 return $id;
147 }
148
149 protected function postSave()
150 {
151 foreach ($this->children as $question) {
152 $question->sid = $this->sid;
153 $question->parent = $this->qid;
154 $question->insert();
155 }
156 }
157
158 public function export()
159 {
160 $export = parent::export();
161 $export['children'] = array();
162 foreach ($this->children as $child) {
163 $export['children'][] = $child->export();
164 }
165 return $export;
166 }
167
168 public function vote(SurveyVote $vote, PlDict $answers)
169 {
170 $a = parent::vote($vote, $answers);
171 foreach ($this->children as $child) {
172 $child->vote($vote, $answers);
173 }
174 return $a;
175 }
176
177 public function child($qid)
178 {
179 $prev = null;
180 foreach ($this->children as $question) {
181 if ($qid == $question->qid) {
182 return $question;
183 } else if ($qid < $question->qid) {
184 Platal::assert($prev instanceof SurveyQuestionGroup);
185 return $prev->child($qid);
186 }
187 $prev = $question;
188 }
189 Platal::assert($prev instanceof SurveyQuestionGroup);
190 return $prev->child($qid);
191 }
192}
193
194// vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8:
195?>