@@ -377,10 +377,10 @@ protected function setTargetEntity($data, PluginFormcreatorFormAnswer $formanswe
377
377
return $ data ;
378
378
}
379
379
380
- protected function setTargetCategory ($ data , $ formanswer ) {
380
+ protected function setTargetCategory ($ data , PluginFormcreatorFormAnswer $ formanswer ) {
381
381
global $ DB ;
382
382
383
- $ category = null ;
383
+ $ category = 0 ;
384
384
385
385
switch ($ this ->fields ['category_rule ' ]) {
386
386
case self ::CATEGORY_RULE_ANSWER :
@@ -398,54 +398,53 @@ protected function setTargetCategory($data, $formanswer) {
398
398
$ category = $ this ->fields ['category_question ' ];
399
399
break ;
400
400
case self ::CATEGORY_RULE_LAST_ANSWER :
401
- $ form_id = $ formanswer ->fields ['id ' ];
402
-
403
- // Get all answers for dropdown questions of this form, ordered
404
- // from last to first displayed
405
- $ answers = $ DB ->request ([
406
- 'SELECT ' => ['answer.answer ' , 'question.values ' ],
407
- 'FROM ' => PluginFormcreatorAnswer::getTable () . ' AS answer ' ,
408
- 'JOIN ' => [
409
- PluginFormcreatorQuestion::getTable () . ' AS question ' => [
410
- 'ON ' => [
411
- 'answer ' => 'plugin_formcreator_questions_id ' ,
412
- 'question ' => 'id ' ,
413
- ]
414
- ]
415
- ],
416
- 'WHERE ' => [
417
- 'answer.plugin_formcreator_formanswers_id ' => $ form_id ,
418
- 'question.fieldtype ' => "dropdown " ,
419
- ],
420
- 'ORDER ' => [
421
- 'row DESC ' ,
422
- 'col DESC ' ,
423
- ]
424
- ]);
401
+ $ questionFields = $ formanswer ->getForm ()->getFields ();
402
+ $ answers_values = $ formanswer ->getAnswers ($ formanswer ->getID ());
403
+ foreach ($ questionFields as $ id => $ question ) {
404
+ $ questionFields [$ id ]->deserializeValue ($ answers_values ['formcreator_field_ ' . $ id ]);
405
+ }
406
+
407
+ // filter questions; keep DropdownFields only
408
+ $ filteredFields = array_filter ($ questionFields , function ($ item ) {
409
+ return get_class ($ item ) === PluginFormcreatorDropdownField::class;
410
+ });
411
+
412
+ // Sort question in reverse order
413
+ uasort ($ filteredFields , function ($ a , $ b ) {
414
+ $ orderA = $ a ->getQuestion ()->fields ['order ' ];
415
+ $ orderB = $ b ->getQuestion ()->fields ['order ' ];
416
+ if ($ orderA == $ orderB ) {
417
+ return 0 ;
418
+ }
419
+ return ($ orderA > $ orderB ) ? -1 : 1 ;
420
+ });
425
421
426
- foreach ($ answers as $ answer ) {
422
+ foreach ($ filteredFields as $ questionField ) {
427
423
// Decode dropdown settings
428
- $ itemtype = \PluginFormcreatorDropdownField::getSubItemtypeForValues ($ answer ['values ' ]);
424
+ $ itemtype = \PluginFormcreatorDropdownField::getSubItemtypeForValues ($ questionField -> getQuestion ()-> fields ['values ' ]);
429
425
430
426
// Skip if not a dropdown on categories
431
- if ($ itemtype !== "ITILCategory " ) {
427
+ if ($ itemtype !== ITILCategory::class) {
428
+ continue ;
429
+ }
430
+
431
+ // Skip if question is invisible
432
+ if (!PluginFormcreatorFields::isVisible ($ questionField ->getQuestion (), $ questionFields )) {
432
433
continue ;
433
434
}
434
435
435
436
// Skip if question was not answered
436
- if (empty ($ answer [ ' answer ' ] )) {
437
+ if (empty ($ questionField -> getValueForDesign () )) {
437
438
continue ;
438
439
}
439
440
440
441
// Found a valid answer, stop here
441
- $ category = $ answer [ ' answer ' ] ;
442
+ $ category = $ questionField -> getValueForDesign () ;
442
443
break ;
443
444
}
444
445
break ;
445
446
}
446
- if ($ category !== null ) {
447
- $ data ['itilcategories_id ' ] = $ category ;
448
- }
447
+ $ data ['itilcategories_id ' ] = $ category ;
449
448
450
449
return $ data ;
451
450
}
0 commit comments