Skip to content

Commit 4fbdfa4

Browse files
committed
feat(formanswer): priority to waiting and processing status
1 parent 13638cd commit 4fbdfa4

File tree

5 files changed

+310
-18
lines changed

5 files changed

+310
-18
lines changed

hook.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ function plugin_formcreator_hook_update_ticket(CommonDBTM $item) {
407407
}
408408

409409
// set the minimal status to the form answer (which will forward the status to the issue)
410-
$minimalStatus = $formAnswer->getMinimalStatus();
410+
$minimalStatus = $formAnswer->getAggregatedStatus();
411411
if ($minimalStatus === null) {
412412
return;
413413
}
@@ -434,8 +434,11 @@ function plugin_formcreator_hook_delete_ticket(CommonDBTM $item) {
434434
])
435435
]);
436436
if (!$formAnswer->isNewItem()) {
437-
$minimalStatus = $formAnswer->getMinimalStatus();
438-
if ($minimalStatus !== null) {
437+
$minimalStatus = $formAnswer->getAggregatedStatus();
438+
if ($minimalStatus === null) {
439+
// There is no more ticket in the form anwer
440+
$formAnswer->updateStatus(CommonITILObject::CLOSED);
441+
} else {
439442
$formAnswer->updateStatus($minimalStatus);
440443
}
441444
return;
@@ -467,7 +470,7 @@ function plugin_formcreator_hook_restore_ticket(CommonDBTM $item) {
467470
return;
468471
}
469472

470-
$minimalStatus = $formAnswer->getMinimalStatus();
473+
$minimalStatus = $formAnswer->getAggregatedStatus();
471474
if ($minimalStatus !== null) {
472475
$formAnswer->updateStatus($minimalStatus);
473476
}
@@ -493,13 +496,17 @@ function plugin_formcreator_hook_purge_ticket(CommonDBTM $item) {
493496
])
494497
]);
495498
if (!$formAnswer->isNewItem()) {
496-
$minimalStatus = $formAnswer->getMinimalStatus();
497-
if ($minimalStatus !== null) {
499+
$minimalStatus = $formAnswer->getAggregatedStatus();
500+
if ($minimalStatus === null) {
501+
// There is no more ticket in the form anwer
502+
$formAnswer->updateStatus(CommonITILObject::CLOSED);
503+
} else {
498504
$formAnswer->updateStatus($minimalStatus);
499505
}
506+
return;
500507
}
501508

502-
// delete issue if any
509+
// delete issue if any
503510
$issue = new PluginFormcreatorIssue();
504511
$issue->deleteByCriteria([
505512
'items_id' => $id,

inc/common.class.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,4 +902,33 @@ public static function getAvailableCategoriesCriterias($helpdeskHome = 1): array
902902
]
903903
];
904904
}
905+
906+
/**
907+
* remove form answer from associatable items to tickets when viewing a form answer
908+
* this removes the button "add a ticket for this item"
909+
*
910+
* @param array $options
911+
* @return void
912+
*/
913+
public static function hookPreShowTab(array $options) {
914+
if ($options['item']::getType() == PluginFormcreatorFormAnswer::getType()) {
915+
$_SESSION['plugin_formcreator']['helpdesk_item_type_backup'] = $_SESSION["glpiactiveprofile"]["helpdesk_item_type"];
916+
$_SESSION["glpiactiveprofile"]["helpdesk_item_type"] = array_diff(
917+
$_SESSION["glpiactiveprofile"]["helpdesk_item_type"],
918+
[PluginFormcreatorFormAnswer::getType()]
919+
);
920+
}
921+
}
922+
923+
/**
924+
* Restore the associatable items to tickets into the session
925+
*
926+
* @param array $options
927+
* @return void
928+
*/
929+
public static function hookPostShowTab(array $options) {
930+
if ($options['item']::getType() == PluginFormcreatorFormAnswer::getType()) {
931+
$_SESSION["glpiactiveprofile"]["helpdesk_item_type"] = $_SESSION['plugin_formcreator']['helpdesk_item_type_backup'];
932+
}
933+
}
905934
}

inc/formanswer.class.php

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

setup.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ function plugin_formcreator_hook() {
348348
// Add specific CSS
349349
$PLUGIN_HOOKS['add_css']['formcreator'][] = PluginFormcreatorCommon::getCssFilename();
350350

351+
$PLUGIN_HOOKS['pre_show_tab']['formcreator'] = [
352+
PluginFormcreatorCommon::class, 'hookPreShowTab',
353+
];
354+
$PLUGIN_HOOKS['post_show_tab']['formcreator'] = [
355+
PluginFormcreatorCommon::class, 'hookPostShowTab',
356+
];
357+
351358
// Load JS and CSS files if we are on a page which need them
352359
if (strpos($_SERVER['REQUEST_URI'], 'formcreator') !== false
353360
|| strpos($_SERVER['REQUEST_URI'], 'central.php') !== false

0 commit comments

Comments
 (0)