2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 Survey
extends PlDBTableEntry
implements SurveyQuestionContainer
24 private $fetchQuestions = true
;
25 public $questions = array();
27 public function __construct()
29 parent
::__construct('surveys');
30 $this->registerFieldValidator('shortname', 'ShortNameFieldValidator');
31 $this->registerFieldFormatter('voters', 'JSonFieldFormatter');
32 $this->registerFieldFormatter('viewers', 'JSonFieldFormatter');
35 protected function postFetch()
37 if (!$this->fetchQuestions
) {
40 $selector = new SurveyQuestion($this);
41 $selector->sid
= $this->id
;
44 foreach ($selector as $question) {
45 $question = $question->typedInstance();
46 if (is_null($question->parent
)) {
47 $this->addQuestion($question);
49 $pos = count($stack) - 1;
50 while ($stack[$pos]->qid
!= $question->parent
) {
54 Platal
::assert(count($stack) > 0, "Invalid question structure");
55 Platal
::assert($stack[$pos] instanceof SurveyQuestionContainer
, "Invalid question type");
56 $stack[$pos]->addQuestion($question);
58 array_push($stack, $question);
63 protected function postSave()
66 $selector = new SurveyQuestion($this);
67 $selector->sid
= $this->id
;
70 $this->reassignQuestionIds();
71 foreach ($this->questions
as $question) {
72 $question->sid
= $this->id
;
77 public function addQuestion(SurveyQuestion
$question, $pos = null
)
79 $question->parent
= null
;
81 $this->questions
[] = $question;
83 array_splice($this->questions
, $pos, 0, $question);
87 public function newQuestion($type, $pos = null
)
89 $question = SurveyQuestion
::instanceForType($this, $type);
90 $this->addQuestion($question, $pos);
94 public function reassignQuestionIds()
97 foreach ($this->questions
as $question) {
99 if ($question instanceof SurveyQuestionContainer
) {
100 $id = $question->reassignQuestionIds();
108 public function export()
110 $export = parent
::export();
111 $export['questions'] = array();
112 foreach ($this->questions
as $question) {
113 $export['questions'][] = $question->export();
118 public function canSee(User
$user)
120 if ($this->uid
== $user->id() ||
$user->hasFlag('admin')) {
126 public static function get($name, $fetchQuestions = true
)
128 if (is_array($name)) {
131 $survey = new Survey();
132 $survey->fetchQuestions
= $fetchQuestions;
133 if (can_convert_to_integer($name)) {
136 $survey->shortname
= $name;
138 if (!$survey->fetch()) {
144 public static function iterActive()
146 $survey = new Survey();
147 $survey->fetchQuestions
= false
;
148 return $survey->iterateOnCondition('begin <= CURDATE() AND end >= CURDATE()
149 AND FIND_IN_SET(\'validated\', flags)');
153 class ShortNameFieldValidator
implements PlDBTableFieldValidator
155 public function __construct(PlDBTableField
$field, $value)
157 if (can_convert_to_integer($value) ||
!preg_match('/^[a-z0-9]+[-_\.a-z0-9]*[a-z0-9]+$/i', $value)) {
158 throw new PlDBBadValueException($value, $field,
159 'The shortname can only contain alphanumerical caracters, dashes, underscores and dots');
164 interface SurveyQuestionContainer
166 public function addQuestion(SurveyQuestion
$question, $pos = null
);
167 public function newQuestion($type, $pos = null
);
168 public function reassignQuestionIds();
171 class SurveyQuestion
extends PlDBTableEntry
174 protected $parentQuestion;
176 public function __construct(Survey
$survey)
178 parent
::__construct('survey_questions');
179 $this->registerFieldFormatter('parameters', 'JSonFieldFormatter');
180 $this->survey
= $survey;
183 public function typedInstance()
185 $instance = self
::instanceForType($this->survey
, $this->type
);
186 $instance->copy($this);
190 public static function instanceForType(Survey
$survey, $type)
192 require_once dirname(__FILE__
) . '/' . $type . '.inc.php';
193 $class = 'SurveyQuestion' . $type;
194 return new $class($survey);
198 class SurveyQuestionGroup
extends SurveyQuestion
implements SurveyQuestionContainer
200 public $children = array();
202 public function __construct(Survey
$survey)
204 parent
::__construct($survey);
207 public function addQuestion(SurveyQuestion
$question, $pos = null
)
209 $question->parentQuestion
= $this;
211 $this->children
[] = $question;
213 array_splice($this->children
, $pos, 0, $question);
217 public function newQuestion($type, $pos = null
)
219 $question = SurveyQuestion
::instanceForType($this->survey
, $type);
220 $this->addQuestion($question, $pos);
224 public function reassignQuestionIds()
226 $id = $this->qid +
1;
227 foreach ($this->children
as $question) {
228 $question->qid
= $id;
229 if ($question instanceof SurveyQuestionContainer
) {
230 $id = $question->reassignQuestionIds();
238 protected function postSave()
240 foreach ($this->children
as $question) {
241 $question->sid
= $this->sid
;
242 $question->parent
= $this->qid
;
247 public function export()
249 $export = parent
::export();
250 $export['children'] = array();
251 foreach ($this->children
as $child) {
252 $export['children'][] = $child->export();
258 // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: