Skip to content

Commit

Permalink
feat(condition): condition on visibility of a question
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Apr 24, 2020
1 parent faaecf4 commit 76ca5ac
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 55 deletions.
4 changes: 4 additions & 0 deletions inc/condition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'),
];
}

Expand Down
137 changes: 82 additions & 55 deletions inc/fields.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76ca5ac

Please sign in to comment.