dolibarr 25.0.0-alpha
opensurveyquestion.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Dolibarr development team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
24require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
25
26
32{
36 public $element = 'opensurvey_formquestions';
37
41 public $table_element = 'opensurvey_formquestions';
42
46 public $picto = 'poll';
47
48 const TYPE_TEXT = 'text'; // Short free text
49 const TYPE_TEXTAREA = 'textarea'; // Long free text
50 const TYPE_RADIO = 'radio'; // One answer among the available answers
51 const TYPE_CHECKBOX = 'checkbox'; // One or several answers among the available answers
52
56 const MAXLENGTH_TEXT = 255;
57
61 const MAXLENGTH_TEXTAREA = 5000;
62
66 public $id_sondage = '';
67
71 public $question = '';
72
76 public $type = self::TYPE_RADIO;
77
81 public $position = 0;
82
86 public $mandatory = 0;
87
91 public $available_answers = '';
92
93
99 public function __construct($db)
100 {
101 $this->db = $db;
102 }
103
110 public static function getTypes($langs)
111 {
112 $langs->load('opensurvey');
113
114 return array(
115 self::TYPE_TEXT => $langs->trans('OpenSurveyQuestionTypeText'),
116 self::TYPE_TEXTAREA => $langs->trans('OpenSurveyQuestionTypeTextarea'),
117 self::TYPE_RADIO => $langs->trans('OpenSurveyQuestionTypeRadio'),
118 self::TYPE_CHECKBOX => $langs->trans('OpenSurveyQuestionTypeCheckbox'),
119 );
120 }
121
127 public static function getTypesWithAnswers()
128 {
129 return array(self::TYPE_RADIO, self::TYPE_CHECKBOX);
130 }
131
138 public static function parseAnswers($text)
139 {
140 $list = array();
141 foreach (preg_split('/\r\n|\r|\n/', (string) $text) as $line) {
142 $line = trim($line);
143 if ($line !== '' && !in_array($line, $list, true)) {
144 $list[] = $line;
145 }
146 }
147
148 return $list;
149 }
150
156 public function getAvailableAnswers()
157 {
158 if (!in_array($this->type, self::getTypesWithAnswers())) {
159 return array();
160 }
161
162 return self::parseAnswers($this->available_answers);
163 }
164
170 public function check()
171 {
172 $this->question = trim($this->question);
173 if ($this->question === '') {
174 $this->error = 'ErrorOpenSurveyQuestionEmpty';
175 return -1;
176 }
177 if (!in_array($this->type, array(self::TYPE_TEXT, self::TYPE_TEXTAREA, self::TYPE_RADIO, self::TYPE_CHECKBOX), true)) {
178 $this->error = 'ErrorOpenSurveyQuestionBadType';
179 return -1;
180 }
181 if (empty($this->id_sondage)) {
182 $this->error = 'ErrorOpenSurveyQuestionNoSurvey';
183 return -1;
184 }
185
186 if (in_array($this->type, self::getTypesWithAnswers())) {
187 $answers = self::parseAnswers($this->available_answers);
188 if (count($answers) < 2) {
189 $this->error = 'ErrorOpenSurveyQuestionNeedTwoAnswers';
190 return -1;
191 }
192 $this->available_answers = implode("\n", $answers);
193 } else {
194 $this->available_answers = '';
195 }
196
197 $this->position = (int) $this->position;
198 $this->mandatory = empty($this->mandatory) ? 0 : 1;
199
200 return 1;
201 }
202
210 public function checkAnswer($posted)
211 {
212 $this->error = '';
213
214 if (is_array($posted)) {
215 if ($this->type != self::TYPE_CHECKBOX) {
216 $this->error = 'ErrorOpenSurveyAnswerNotValid';
217 return false;
218 }
219 $values = array();
220 foreach ($posted as $value) {
221 if (!is_string($value)) {
222 $this->error = 'ErrorOpenSurveyAnswerNotValid';
223 return false;
224 }
225 $values[] = trim($value);
226 }
227 } else {
228 $values = array(trim((string) $posted));
229 }
230 $values = array_values(array_filter($values, function (string $value): bool {
231 return $value !== '';
232 }));
233
234 if (empty($values)) {
235 if ($this->mandatory) {
236 $this->error = 'ErrorOpenSurveyAnswerMandatory';
237 return false;
238 }
239 return null;
240 }
241
242 switch ($this->type) {
243 case self::TYPE_TEXT:
244 case self::TYPE_TEXTAREA:
245 $value = $values[0];
246 $maxlength = ($this->type == self::TYPE_TEXT ? self::MAXLENGTH_TEXT : self::MAXLENGTH_TEXTAREA);
247 if (dol_strlen($value) > $maxlength) {
248 $this->error = 'ErrorOpenSurveyAnswerTooLong';
249 return false;
250 }
251 return $value;
252 case self::TYPE_RADIO:
253 case self::TYPE_CHECKBOX:
254 $available = $this->getAvailableAnswers();
255 if ($this->type == self::TYPE_RADIO && count($values) > 1) {
256 $this->error = 'ErrorOpenSurveyAnswerNotValid';
257 return false;
258 }
259 foreach ($values as $value) {
260 if (!in_array($value, $available, true)) {
261 $this->error = 'ErrorOpenSurveyAnswerNotValid';
262 return false;
263 }
264 }
265 return implode("\n", array_values(array_unique($values)));
266 }
267
268 $this->error = 'ErrorOpenSurveyAnswerNotValid';
269 return false;
270 }
271
277 public function create()
278 {
279 if ($this->check() < 0) {
280 return -1;
281 }
282
283 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (id_sondage, question, type, position, mandatory, available_answers)";
284 $sql .= " VALUES ('".$this->db->escape($this->id_sondage)."', '".$this->db->escape($this->question)."', '".$this->db->escape($this->type)."',";
285 $sql .= " ".((int) $this->position).", ".((int) $this->mandatory).", ";
286 $sql .= ($this->available_answers !== '' ? "'".$this->db->escape($this->available_answers)."'" : "null").")";
287
288 dol_syslog(get_class($this)."::create", LOG_DEBUG);
289 $resql = $this->db->query($sql);
290 if (!$resql) {
291 $this->error = $this->db->lasterror();
292 return -1;
293 }
294 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
295
296 return $this->id;
297 }
298
305 public function fetch($id)
306 {
307 $sql = "SELECT rowid, id_sondage, question, type, position, mandatory, available_answers";
308 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
309 $sql .= " WHERE rowid = ".((int) $id);
310
311 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
312 $resql = $this->db->query($sql);
313 if (!$resql) {
314 $this->error = $this->db->lasterror();
315 return -1;
316 }
317 $obj = $this->db->fetch_object($resql);
318 $this->db->free($resql);
319 if (!$obj) {
320 return 0;
321 }
322
323 $this->setFromObject($obj);
324
325 return 1;
326 }
327
334 public function fetchAllBySurvey($idsondage)
335 {
336 $sql = "SELECT rowid, id_sondage, question, type, position, mandatory, available_answers";
337 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
338 $sql .= " WHERE id_sondage = '".$this->db->escape($idsondage)."'";
339 $sql .= " ORDER BY position, rowid";
340
341 dol_syslog(get_class($this)."::fetchAllBySurvey", LOG_DEBUG);
342 $resql = $this->db->query($sql);
343 if (!$resql) {
344 $this->error = $this->db->lasterror();
345 return -1;
346 }
347
348 $list = array();
349 while ($obj = $this->db->fetch_object($resql)) {
350 $question = new Opensurveyquestion($this->db);
351 $question->setFromObject($obj);
352 $list[] = $question;
353 }
354 $this->db->free($resql);
355
356 return $list;
357 }
358
364 public function update()
365 {
366 if ($this->check() < 0) {
367 return -1;
368 }
369
370 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
371 $sql .= " question = '".$this->db->escape($this->question)."',";
372 $sql .= " type = '".$this->db->escape($this->type)."',";
373 $sql .= " position = ".((int) $this->position).",";
374 $sql .= " mandatory = ".((int) $this->mandatory).",";
375 $sql .= " available_answers = ".($this->available_answers !== '' ? "'".$this->db->escape($this->available_answers)."'" : "null");
376 $sql .= " WHERE rowid = ".((int) $this->id);
377 $sql .= " AND id_sondage = '".$this->db->escape($this->id_sondage)."'";
378
379 dol_syslog(get_class($this)."::update", LOG_DEBUG);
380 $resql = $this->db->query($sql);
381 if (!$resql) {
382 $this->error = $this->db->lasterror();
383 return -1;
384 }
385
386 return 1;
387 }
388
394 public function delete()
395 {
396 $this->db->begin();
397
398 $sql = "DELETE FROM ".MAIN_DB_PREFIX."opensurvey_user_formanswers WHERE fk_question = ".((int) $this->id);
399 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
400 $resql = $this->db->query($sql);
401 if ($resql) {
402 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id);
403 $resql = $this->db->query($sql);
404 }
405 if (!$resql) {
406 $this->error = $this->db->lasterror();
407 $this->db->rollback();
408 return -1;
409 }
410
411 $this->db->commit();
412
413 return 1;
414 }
415
422 private function setFromObject($obj)
423 {
424 $this->id = (int) $obj->rowid;
425 $this->id_sondage = $obj->id_sondage;
426 $this->question = $obj->question;
427 $this->type = $obj->type;
428 $this->position = (int) $obj->position;
429 $this->mandatory = (int) $obj->mandatory;
430 $this->available_answers = (string) $obj->available_answers;
431 }
432}
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition propal.php:281
print $object position
Definition edit.php:206
Class for a question of a survey of format 'F' (form).
static getTypesWithAnswers()
Return the types of question that have a list of available answers.
update()
Update the question into the database.
checkAnswer($posted)
Check an answer given by a respondent for this question, and return the value to store.
static getTypes($langs)
Return the types of question, with the label of each type.
static parseAnswers($text)
Convert a text with one answer per line into a clean list of answers (trimmed, no empty line,...
const MAXLENGTH_TEXTAREA
Max length of an answer in long free text.
getAvailableAnswers()
Return the list of available answers of the question.
const MAXLENGTH_TEXT
Max length of an answer in free text.
fetch($id)
Load a question from the database.
check()
Check the properties of the question and clean the list of available answers.
setFromObject($obj)
Set the properties from a row of the database.
create()
Create the question into the database.
fetchAllBySurvey($idsondage)
Return all the questions of a survey, in the order of the survey.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.