Activating 'hidden' visibility flag.
[platal.git] / modules / survey / question.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 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 interface SurveyQuestionContainer
23 {
24 public function addQuestion(SurveyQuestion $question, $pos = null);
25 public function newQuestion($type, $pos = null);
26 public function reassignQuestionIds();
27 }
28
29 class 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 {
50 require_once dirname(__FILE__) . '/' . $type . '.inc.php';
51 $class = 'SurveyQuestion' . $type;
52 return new $class($survey);
53 }
54
55 public function voteTemplate()
56 {
57 return 'survey/question.' . $this->type . '.tpl';
58 }
59
60 public function editTemplate()
61 {
62 return 'survey/edit.' . $this->type . '.tpl';
63 }
64
65 protected function buildAnswer(SurveyAnswer $answer, PlDict $answers)
66 {
67 Platal::assert(false, "This should not happen");
68 }
69
70 public function vote(SurveyVote $vote, PlDict $answers)
71 {
72 if ($this->flags->hasFlag('noanswer')) {
73 if ($answers->has($this->qid)) {
74 throw new Exception("Des réponses ont été données à une question n'en attendant pas");
75 }
76 return null;
77 }
78 $answer = $vote->getAnswer($this);
79 if (is_null($answer)) {
80 return null;
81 }
82 if (!$this->buildAnswer($answer, $answers)) {
83 return $answer;
84 }
85 if ($this->flags->hasFlag('mandatory') && is_null($answer->answer)) {
86 $answer->inError = 'Tu dois répondre à cette question';
87 }
88 return $answer;
89 }
90 }
91
92 class SurveyQuestionGroup extends SurveyQuestion implements SurveyQuestionContainer
93 {
94 public $children = array();
95
96 public function __construct(Survey $survey)
97 {
98 parent::__construct($survey);
99 }
100
101 public function addQuestion(SurveyQuestion $question, $pos = null)
102 {
103 $question->parentQuestion = $this;
104 if (is_null($pos)) {
105 $this->children[] = $question;
106 } else {
107 array_splice($this->children, $pos, 0, $question);
108 }
109 }
110
111 public function newQuestion($type, $pos = null)
112 {
113 $question = SurveyQuestion::instanceForType($this->survey, $type);
114 $this->addQuestion($question, $pos);
115 return $question;
116 }
117
118 public function reassignQuestionIds()
119 {
120 $id = $this->qid + 1;
121 foreach ($this->children as $question) {
122 $question->qid = $id;
123 if ($question instanceof SurveyQuestionContainer) {
124 $id = $question->reassignQuestionIds();
125 } else {
126 $id++;
127 }
128 }
129 return $id;
130 }
131
132 protected function postSave()
133 {
134 foreach ($this->children as $question) {
135 $question->sid = $this->sid;
136 $question->parent = $this->qid;
137 $question->insert();
138 }
139 }
140
141 public function export()
142 {
143 $export = parent::export();
144 $export['children'] = array();
145 foreach ($this->children as $child) {
146 $export['children'][] = $child->export();
147 }
148 return $export;
149 }
150
151 public function vote(SurveyVote $vote, PlDict $answers)
152 {
153 $a = parent::vote($vote, $answers);
154 foreach ($this->children as $child) {
155 $child->vote($vote, $answers);
156 }
157 return $a;
158 }
159
160 public function child($qid)
161 {
162 $prev = null;
163 foreach ($this->children as $question) {
164 if ($qid == $question->qid) {
165 return $question;
166 } else if ($qid < $question->qid) {
167 Platal::assert($prev instanceof SurveyQuestionGroup);
168 return $prev->child($qid);
169 }
170 $prev = $question;
171 }
172 Platal::assert($prev instanceof SurveyQuestionGroup);
173 return $prev->child($qid);
174 }
175 }
176
177 // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8:
178 ?>