24require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
36 public $element =
'opensurvey_formquestions';
41 public $table_element =
'opensurvey_formquestions';
46 public $picto =
'poll';
48 const TYPE_TEXT =
'text';
49 const TYPE_TEXTAREA =
'textarea';
50 const TYPE_RADIO =
'radio';
51 const TYPE_CHECKBOX =
'checkbox';
66 public $id_sondage =
'';
71 public $question =
'';
76 public $type = self::TYPE_RADIO;
86 public $mandatory = 0;
91 public $available_answers =
'';
112 $langs->load(
'opensurvey');
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'),
129 return array(self::TYPE_RADIO, self::TYPE_CHECKBOX);
141 foreach (preg_split(
'/\r\n|\r|\n/', (
string) $text) as $line) {
143 if ($line !==
'' && !in_array($line, $list,
true)) {
158 if (!in_array($this->
type, self::getTypesWithAnswers())) {
172 $this->question = trim($this->question);
173 if ($this->question ===
'') {
174 $this->error =
'ErrorOpenSurveyQuestionEmpty';
177 if (!in_array($this->
type, array(self::TYPE_TEXT, self::TYPE_TEXTAREA, self::TYPE_RADIO, self::TYPE_CHECKBOX),
true)) {
178 $this->error =
'ErrorOpenSurveyQuestionBadType';
181 if (empty($this->id_sondage)) {
182 $this->error =
'ErrorOpenSurveyQuestionNoSurvey';
186 if (in_array($this->
type, self::getTypesWithAnswers())) {
188 if (count($answers) < 2) {
189 $this->error =
'ErrorOpenSurveyQuestionNeedTwoAnswers';
192 $this->available_answers = implode(
"\n", $answers);
194 $this->available_answers =
'';
198 $this->mandatory = empty($this->mandatory) ? 0 : 1;
214 if (is_array($posted)) {
215 if ($this->
type != self::TYPE_CHECKBOX) {
216 $this->error =
'ErrorOpenSurveyAnswerNotValid';
220 foreach ($posted as $value) {
221 if (!is_string($value)) {
222 $this->error =
'ErrorOpenSurveyAnswerNotValid';
225 $values[] = trim($value);
228 $values = array(trim((
string) $posted));
230 $values = array_values(array_filter($values,
function (
string $value):
bool {
231 return $value !==
'';
234 if (empty($values)) {
235 if ($this->mandatory) {
236 $this->error =
'ErrorOpenSurveyAnswerMandatory';
242 switch ($this->
type) {
243 case self::TYPE_TEXT:
244 case self::TYPE_TEXTAREA:
248 $this->error =
'ErrorOpenSurveyAnswerTooLong';
252 case self::TYPE_RADIO:
253 case self::TYPE_CHECKBOX:
255 if ($this->
type == self::TYPE_RADIO && count($values) > 1) {
256 $this->error =
'ErrorOpenSurveyAnswerNotValid';
259 foreach ($values as $value) {
260 if (!in_array($value, $available,
true)) {
261 $this->error =
'ErrorOpenSurveyAnswerNotValid';
265 return implode(
"\n", array_values(array_unique($values)));
268 $this->error =
'ErrorOpenSurveyAnswerNotValid';
279 if ($this->
check() < 0) {
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").
")";
288 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
289 $resql = $this->db->query($sql);
291 $this->error = $this->db->lasterror();
294 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
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);
311 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
312 $resql = $this->db->query($sql);
314 $this->error = $this->db->lasterror();
317 $obj = $this->db->fetch_object($resql);
318 $this->db->free($resql);
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";
341 dol_syslog(get_class($this).
"::fetchAllBySurvey", LOG_DEBUG);
342 $resql = $this->db->query($sql);
344 $this->error = $this->db->lasterror();
349 while ($obj = $this->db->fetch_object($resql)) {
351 $question->setFromObject($obj);
354 $this->db->free($resql);
366 if ($this->
check() < 0) {
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).
"'";
379 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
380 $resql = $this->db->query($sql);
382 $this->error = $this->db->lasterror();
394 public function delete()
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);
402 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
403 $resql = $this->db->query($sql);
406 $this->error = $this->db->lasterror();
407 $this->db->rollback();
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;
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
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.
__construct($db)
Constructor.
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.