@@ -41,6 +41,11 @@ class PluginFormcreatorFormAnswer extends CommonDBTM
4141 public $ usenotepad = true ;
4242 public $ usenotepadrights = true ;
4343
44+ /**
45+ * Generated targets after creation of a form answer
46+ *
47+ * @var array
48+ */
4449 public $ targetList = [];
4550
4651 const SOPTION_ANSWER = 900000 ;
@@ -1064,7 +1069,7 @@ public function post_addItem() {
10641069 }
10651070 }
10661071 $ this ->createIssue ();
1067- $ minimalStatus = $ formAnswer ->getMinimalStatus ();
1072+ $ minimalStatus = $ formAnswer ->getAggregatedStatus ();
10681073 if ($ minimalStatus !== null ) {
10691074 $ this ->updateStatus ($ minimalStatus );
10701075 }
@@ -1114,7 +1119,7 @@ public function post_updateItem($history = 1) {
11141119 }
11151120 }
11161121 $ this ->updateIssue ();
1117- $ minimalStatus = $ formAnswer ->getMinimalStatus ();
1122+ $ minimalStatus = $ formAnswer ->getAggregatedStatus ();
11181123 if ($ minimalStatus !== null ) {
11191124 $ this ->updateStatus ($ minimalStatus );
11201125 }
@@ -1801,6 +1806,7 @@ public function getCurrentApprovers(): ?array {
18011806
18021807 /**
18031808 * get all generated targets by the form answer
1809+ * populates the generated targets associated to the instance
18041810 *
18051811 * @return array An array of target itemtypes to track
18061812 */
@@ -1818,7 +1824,8 @@ public function getGeneratedTargets($itemtypes = []): array {
18181824 $ itemtypes = array_intersect (PluginFormcreatorForm::getTargetTypes (), $ itemtypes );
18191825 }
18201826 /** @var PluginFormcreatorTargetInterface $targetType */
1821- foreach ($ itemtypes as $ targetType ) {
1827+ $ this ->targetList = [];
1828+ foreach (PluginFormcreatorForm::getTargetTypes () as $ targetType ) {
18221829 $ targetItem = new $ targetType ();
18231830 $ generatedType = $ targetItem ->getTargetItemtypeName ();
18241831 $ relationType = $ targetItem ->getItem_Item ();
@@ -1841,12 +1848,16 @@ public function getGeneratedTargets($itemtypes = []): array {
18411848 "$ relationTable.items_id " => $ this ->getID (),
18421849 ],
18431850 ]);
1844- foreach ($ iterator as $ row ) {
1851+ foreach ($ iterator as $ row ) {
18451852 /** @var $item CommonDBTM */
18461853 $ item = new $ generatedType ();
18471854 $ item ->getFromResultSet ($ row );
1848- $ targets [] = $ item ;
18491855 $ this ->targetList [] = clone $ item ;
1856+ // skip not wanted itemtypes
1857+ if (!in_array ($ targetType , $ itemtypes )) {
1858+ continue ;
1859+ }
1860+ $ targets [] = $ item ;
18501861 }
18511862 }
18521863
@@ -1858,9 +1869,13 @@ public function getGeneratedTargets($itemtypes = []): array {
18581869 *
18591870 * @return null|int
18601871 */
1861- public function getMinimalStatus (): ?int {
1872+ public function getAggregatedStatus (): ?int {
18621873 $ generatedTargets = $ this ->getGeneratedTargets ([PluginFormcreatorTargetTicket::getType ()]);
18631874
1875+ $ isWaiting = false ;
1876+ $ isAssigned = false ;
1877+ $ isProcessing = false ;
1878+
18641879 // Find the minimal status of the first generated tickets in the array (deleted items excluded)
18651880 $ generatedTarget = array_shift ($ generatedTargets );
18661881 while ($ generatedTarget !== null && $ generatedTarget ->fields ['is_deleted ' ]) {
@@ -1871,8 +1886,19 @@ public function getMinimalStatus(): ?int {
18711886 return null ;
18721887 }
18731888
1874- // Find the minimal status of all (not deleted) generated targets
1875- $ minimalStatus = PluginFormcreatorCommon::getTicketStatusForIssue ($ generatedTarget );
1889+ // Find status of the first ticket in the array
1890+ $ aggregatedStatus = PluginFormcreatorCommon::getTicketStatusForIssue ($ generatedTarget );
1891+ if ($ aggregatedStatus == CommonITILObject::ASSIGNED ) {
1892+ $ isAssigned = true ;
1893+ }
1894+ if ($ aggregatedStatus == CommonITILObject::PLANNED ) {
1895+ $ isProcessing = true ;
1896+ }
1897+ if ($ aggregatedStatus == CommonITILObject::WAITING ) {
1898+ $ isWaiting = true ;
1899+ }
1900+
1901+ // Traverse all other tickets and set the minimal status
18761902 foreach ($ generatedTargets as $ generatedTarget ) {
18771903 /** @var Ticket $generatedTarget */
18781904 if ($ generatedTarget ::getType () != Ticket::getType ()) {
@@ -1885,10 +1911,33 @@ public function getMinimalStatus(): ?int {
18851911 if ($ ticketStatus >= PluginFormcreatorFormAnswer::STATUS_WAITING ) {
18861912 continue ;
18871913 }
1888- $ minimalStatus = min ($ minimalStatus , $ ticketStatus );
1914+
1915+ if ($ ticketStatus == CommonITILObject::ASSIGNED ) {
1916+ $ isAssigned = true ;
1917+ }
1918+ if ($ ticketStatus == CommonITILObject::PLANNED ) {
1919+ $ isProcessing = true ;
1920+ }
1921+ if ($ ticketStatus == CommonITILObject::WAITING ) {
1922+ $ isWaiting = true ;
1923+ }
1924+ $ aggregatedStatus = min ($ aggregatedStatus , $ ticketStatus );
1925+ }
1926+
1927+ // Assigned status takes precedence
1928+ if ($ isAssigned ) {
1929+ $ aggregatedStatus = CommonITILObject::ASSIGNED ;
1930+ }
1931+ // Planned status takes precedence
1932+ if ($ isProcessing ) {
1933+ $ aggregatedStatus = CommonITILObject::PLANNED ;
1934+ }
1935+ // Waiting status takes precedence to inform the requester his feedback is required
1936+ if ($ isWaiting ) {
1937+ $ aggregatedStatus = CommonITILObject::WAITING ;
18891938 }
18901939
1891- return $ minimalStatus ;
1940+ return $ aggregatedStatus ;
18921941 }
18931942
18941943 /**
0 commit comments