From 76ca5ac6ac4fbd76c9441e81137d361bf94a6094 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Thu, 23 Apr 2020 16:42:45 +0200 Subject: [PATCH] feat(condition): condition on visibility of a question Signed-off-by: Thierry Bugier --- inc/condition.class.php | 4 ++ inc/fields.class.php | 137 ++++++++++++++++++++++++---------------- 2 files changed, 86 insertions(+), 55 deletions(-) diff --git a/inc/condition.class.php b/inc/condition.class.php index 838a4d08d..49d89b63f 100644 --- a/inc/condition.class.php +++ b/inc/condition.class.php @@ -50,6 +50,8 @@ class PluginFormcreatorCondition extends CommonDBTM implements PluginFormcreator const SHOW_CONDITION_GT = 4; const SHOW_CONDITION_LE = 5; const SHOW_CONDITION_GE = 6; + const SHOW_CONDITION_QUESTION_VISIBLE = 7; + const SHOW_CONDITION_QUESTION_INVISIBLE = 8; public function prepareInputForAdd($input) { // generate a unique id @@ -76,6 +78,8 @@ public static function getEnumShowCondition() { self::SHOW_CONDITION_GT => '>', self::SHOW_CONDITION_LE => '≤', self::SHOW_CONDITION_GE => '≥', + self::SHOW_CONDITION_QUESTION_VISIBLE => __('is visible', 'formcreator'), + self::SHOW_CONDITION_QUESTION_INVISIBLE => __('is not visible', 'formcreator'), ]; } diff --git a/inc/fields.class.php b/inc/fields.class.php index 9976327b9..996734d29 100644 --- a/inc/fields.class.php +++ b/inc/fields.class.php @@ -196,81 +196,108 @@ public static function isVisible(PluginFormcreatorConditionnableInterface $item, $conditionField = $fields[$condition['field']]; $value = false; - if (self::isVisible($conditionField->getQuestion(), $fields)) { + if (in_array($condition['operator'], [PluginFormcreatorCondition::SHOW_CONDITION_QUESTION_VISIBLE, PluginFormcreatorCondition::SHOW_CONDITION_QUESTION_INVISIBLE])) { switch ($condition['operator']) { - case PluginFormcreatorCondition::SHOW_CONDITION_NE : - if (!$conditionField->isPrerequisites()) { - $evalItem[$itemtype][$itemId] = true; - return $evalItem[$itemtype][$itemId]; - } - try { - $value = $conditionField->notEquals($condition['value']); - } catch (PluginFormcreatorComparisonException $e) { - $value = false; - } - break; - - case PluginFormcreatorCondition::SHOW_CONDITION_EQ : + case PluginFormcreatorCondition::SHOW_CONDITION_QUESTION_VISIBLE: if (!$conditionField->isPrerequisites()) { $evalItem[$itemtype][$itemId] = false; return $evalItem[$itemtype][$itemId]; } try { - $value = $conditionField->equals($condition['value']); + $value = self::isVisible($conditionField->getQuestion(), $fields); } catch (PluginFormcreatorComparisonException $e) { $value = false; } break; - - case PluginFormcreatorCondition::SHOW_CONDITION_GT: + case PluginFormcreatorCondition::SHOW_CONDITION_QUESTION_INVISIBLE: if (!$conditionField->isPrerequisites()) { $evalItem[$itemtype][$itemId] = false; return $evalItem[$itemtype][$itemId]; } try { - $value = $conditionField->greaterThan($condition['value']); + $value = !self::isVisible($conditionField->getQuestion(), $fields); } catch (PluginFormcreatorComparisonException $e) { $value = false; } break; - - case PluginFormcreatorCondition::SHOW_CONDITION_LT: - if (!$conditionField->isPrerequisites()) { - $evalItem[$itemtype][$itemId] = false; - return $evalItem[$itemtype][$itemId]; - } - try { - $value = $conditionField->lessThan($condition['value']); - } catch (PluginFormcreatorComparisonException $e) { - $value = false; - } - break; - - case PluginFormcreatorCondition::SHOW_CONDITION_GE: - if (!$conditionField->isPrerequisites()) { - $evalItem[$itemtype][$itemId] = false; - return $evalItem[$itemtype][$itemId]; - } - try { - $value = ($conditionField->greaterThan($condition['value']) - || $conditionField->equals($condition['value'])); - } catch (PluginFormcreatorComparisonException $e) { - $value = false; - } - break; - - case PluginFormcreatorCondition::SHOW_CONDITION_LE: - if (!$conditionField->isPrerequisites()) { - $evalItem[$itemtype][$itemId] = false; - return $evalItem[$itemtype][$itemId]; - } - try { - $value = ($conditionField->lessThan($condition['value']) - || $conditionField->equals($condition['value'])); - } catch (PluginFormcreatorComparisonException $e) { - $value = false; + } + } else { + if (self::isVisible($conditionField->getQuestion(), $fields)) { + switch ($condition['operator']) { + case PluginFormcreatorCondition::SHOW_CONDITION_NE : + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = true; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = $conditionField->notEquals($condition['value']); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; + + case PluginFormcreatorCondition::SHOW_CONDITION_EQ : + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = false; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = $conditionField->equals($condition['value']); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; + + case PluginFormcreatorCondition::SHOW_CONDITION_GT: + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = false; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = $conditionField->greaterThan($condition['value']); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; + + case PluginFormcreatorCondition::SHOW_CONDITION_LT: + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = false; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = $conditionField->lessThan($condition['value']); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; + + case PluginFormcreatorCondition::SHOW_CONDITION_GE: + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = false; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = ($conditionField->greaterThan($condition['value']) + || $conditionField->equals($condition['value'])); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; + + case PluginFormcreatorCondition::SHOW_CONDITION_LE: + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = false; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = ($conditionField->lessThan($condition['value']) + || $conditionField->equals($condition['value'])); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; } - break; } } // Combine all condition with respect of operator precedence