Skip to content

Commit

Permalink
fix(targetticket): last valid category ignored visibility state
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Jul 30, 2020
1 parent 24dacd2 commit f6e09f0
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 124 deletions.
3 changes: 2 additions & 1 deletion inc/fields.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public static function getNames() {
}

/**
* Reset the cache of visibility of hide-able items
* Reset cache of evaluated visibility
* used for unit tests
*
* @return void
*/
Expand Down
6 changes: 6 additions & 0 deletions inc/fields/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,15 @@ public function prepareQuestionInputForSave($input) {
if (is_a($input['dropdown_values'], "CommonTreeDropdown", true)) {
// Specific param for ITILCategory
if ($input['dropdown_values'] == ITILCategory::class) {
if (!isset($input['show_ticket_categories'])) {
$input['show_ticket_categories'] = 'all';
}
$input['values']['show_ticket_categories'] = $input['show_ticket_categories'];
}

if (!isset($input['show_ticket_categories_depth'])) {
$input['show_ticket_categories_depth'] = 0;
}
if ($input['show_ticket_categories_depth'] != (int) $input['show_ticket_categories_depth']) {
$input['values']['show_ticket_categories_depth'] = 0;
} else {
Expand Down
2 changes: 1 addition & 1 deletion inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2171,7 +2171,7 @@ public function getFromDBByQuestion(PluginFormcreatorQuestion $question) {
/**
* Get an array of instances of all fields for the form
*
* @return array
* @return PluginFormcreatorFields[]
*/
public function getFields() {
$fields = [];
Expand Down
67 changes: 33 additions & 34 deletions inc/targetbase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ protected function setTargetEntity($data, PluginFormcreatorFormAnswer $formanswe
return $data;
}

protected function setTargetCategory($data, $formanswer) {
protected function setTargetCategory($data, PluginFormcreatorFormAnswer $formanswer) {
global $DB;

$category = null;
$category = 0;

switch ($this->fields['category_rule']) {
case self::CATEGORY_RULE_ANSWER:
Expand All @@ -398,54 +398,53 @@ protected function setTargetCategory($data, $formanswer) {
$category = $this->fields['category_question'];
break;
case self::CATEGORY_RULE_LAST_ANSWER:
$form_id = $formanswer->fields['id'];

// Get all answers for dropdown questions of this form, ordered
// from last to first displayed
$answers = $DB->request([
'SELECT' => ['answer.answer', 'question.values'],
'FROM' => PluginFormcreatorAnswer::getTable() . ' AS answer',
'JOIN' => [
PluginFormcreatorQuestion::getTable() . ' AS question' => [
'ON' => [
'answer' => 'plugin_formcreator_questions_id',
'question' => 'id',
]
]
],
'WHERE' => [
'answer.plugin_formcreator_formanswers_id' => $form_id,
'question.fieldtype' => "dropdown",
],
'ORDER' => [
'row DESC',
'col DESC',
]
]);
$questionFields = $formanswer->getForm()->getFields();
$answers_values = $formanswer->getAnswers($formanswer->getID());
foreach ($questionFields as $id => $question) {
$questionFields[$id]->deserializeValue($answers_values['formcreator_field_' . $id]);
}

// filter questions; keep DropdownFields only
$filteredFields = array_filter($questionFields, function($item) {
return get_class($item) === PluginFormcreatorDropdownField::class;
});

// Sort question in reverse order
uasort($filteredFields, function($a, $b) {
$orderA = $a->getQuestion()->fields['order'];
$orderB = $b->getQuestion()->fields['order'];
if ($orderA == $orderB) {
return 0;
}
return ($orderA > $orderB) ? -1 : 1;
});

foreach ($answers as $answer) {
foreach ($filteredFields as $questionField) {
// Decode dropdown settings
$itemtype = \PluginFormcreatorDropdownField::getSubItemtypeForValues($answer['values']);
$itemtype = \PluginFormcreatorDropdownField::getSubItemtypeForValues($questionField->getQuestion()->fields['values']);

// Skip if not a dropdown on categories
if ($itemtype !== "ITILCategory") {
if ($itemtype !== ITILCategory::class) {
continue;
}

// Skip if question is invisible
if (!PluginFormcreatorFields::isVisible($questionField->getQuestion(), $questionFields)) {
continue;
}

// Skip if question was not answered
if (empty($answer['answer'])) {
if (empty($questionField->getValueForDesign())) {
continue;
}

// Found a valid answer, stop here
$category = $answer['answer'];
$category = $questionField->getValueForDesign();
break;
}
break;
}
if ($category !== null) {
$data['itilcategories_id'] = $category;
}
$data['itilcategories_id'] = $category;

return $data;
}
Expand Down
Loading

0 comments on commit f6e09f0

Please sign in to comment.