Skip to content

Commit

Permalink
feat(formanswer): priority to waiting and processing status
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Dec 23, 2021
1 parent 13638cd commit 4fbdfa4
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 18 deletions.
21 changes: 14 additions & 7 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ function plugin_formcreator_hook_update_ticket(CommonDBTM $item) {
}

// set the minimal status to the form answer (which will forward the status to the issue)
$minimalStatus = $formAnswer->getMinimalStatus();
$minimalStatus = $formAnswer->getAggregatedStatus();
if ($minimalStatus === null) {
return;
}
Expand All @@ -434,8 +434,11 @@ function plugin_formcreator_hook_delete_ticket(CommonDBTM $item) {
])
]);
if (!$formAnswer->isNewItem()) {
$minimalStatus = $formAnswer->getMinimalStatus();
if ($minimalStatus !== null) {
$minimalStatus = $formAnswer->getAggregatedStatus();
if ($minimalStatus === null) {
// There is no more ticket in the form anwer
$formAnswer->updateStatus(CommonITILObject::CLOSED);
} else {
$formAnswer->updateStatus($minimalStatus);
}
return;
Expand Down Expand Up @@ -467,7 +470,7 @@ function plugin_formcreator_hook_restore_ticket(CommonDBTM $item) {
return;
}

$minimalStatus = $formAnswer->getMinimalStatus();
$minimalStatus = $formAnswer->getAggregatedStatus();
if ($minimalStatus !== null) {
$formAnswer->updateStatus($minimalStatus);
}
Expand All @@ -493,13 +496,17 @@ function plugin_formcreator_hook_purge_ticket(CommonDBTM $item) {
])
]);
if (!$formAnswer->isNewItem()) {
$minimalStatus = $formAnswer->getMinimalStatus();
if ($minimalStatus !== null) {
$minimalStatus = $formAnswer->getAggregatedStatus();
if ($minimalStatus === null) {
// There is no more ticket in the form anwer
$formAnswer->updateStatus(CommonITILObject::CLOSED);
} else {
$formAnswer->updateStatus($minimalStatus);
}
return;
}

// delete issue if any
// delete issue if any
$issue = new PluginFormcreatorIssue();
$issue->deleteByCriteria([
'items_id' => $id,
Expand Down
29 changes: 29 additions & 0 deletions inc/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,4 +902,33 @@ public static function getAvailableCategoriesCriterias($helpdeskHome = 1): array
]
];
}

/**
* remove form answer from associatable items to tickets when viewing a form answer
* this removes the button "add a ticket for this item"
*
* @param array $options
* @return void
*/
public static function hookPreShowTab(array $options) {
if ($options['item']::getType() == PluginFormcreatorFormAnswer::getType()) {
$_SESSION['plugin_formcreator']['helpdesk_item_type_backup'] = $_SESSION["glpiactiveprofile"]["helpdesk_item_type"];
$_SESSION["glpiactiveprofile"]["helpdesk_item_type"] = array_diff(
$_SESSION["glpiactiveprofile"]["helpdesk_item_type"],
[PluginFormcreatorFormAnswer::getType()]
);
}
}

/**
* Restore the associatable items to tickets into the session
*
* @param array $options
* @return void
*/
public static function hookPostShowTab(array $options) {
if ($options['item']::getType() == PluginFormcreatorFormAnswer::getType()) {
$_SESSION["glpiactiveprofile"]["helpdesk_item_type"] = $_SESSION['plugin_formcreator']['helpdesk_item_type_backup'];
}
}
}
69 changes: 59 additions & 10 deletions inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class PluginFormcreatorFormAnswer extends CommonDBTM
public $usenotepad = true;
public $usenotepadrights = true;

/**
* Generated targets after creation of a form answer
*
* @var array
*/
public $targetList = [];

const SOPTION_ANSWER = 900000;
Expand Down Expand Up @@ -1064,7 +1069,7 @@ public function post_addItem() {
}
}
$this->createIssue();
$minimalStatus = $formAnswer->getMinimalStatus();
$minimalStatus = $formAnswer->getAggregatedStatus();
if ($minimalStatus !== null) {
$this->updateStatus($minimalStatus);
}
Expand Down Expand Up @@ -1114,7 +1119,7 @@ public function post_updateItem($history = 1) {
}
}
$this->updateIssue();
$minimalStatus = $formAnswer->getMinimalStatus();
$minimalStatus = $formAnswer->getAggregatedStatus();
if ($minimalStatus !== null) {
$this->updateStatus($minimalStatus);
}
Expand Down Expand Up @@ -1801,6 +1806,7 @@ public function getCurrentApprovers(): ?array {

/**
* get all generated targets by the form answer
* populates the generated targets associated to the instance
*
* @return array An array of target itemtypes to track
*/
Expand All @@ -1818,7 +1824,8 @@ public function getGeneratedTargets($itemtypes = []): array {
$itemtypes = array_intersect(PluginFormcreatorForm::getTargetTypes(), $itemtypes);
}
/** @var PluginFormcreatorTargetInterface $targetType */
foreach ($itemtypes as $targetType) {
$this->targetList = [];
foreach (PluginFormcreatorForm::getTargetTypes() as $targetType) {
$targetItem = new $targetType();
$generatedType = $targetItem->getTargetItemtypeName();
$relationType = $targetItem->getItem_Item();
Expand All @@ -1841,12 +1848,16 @@ public function getGeneratedTargets($itemtypes = []): array {
"$relationTable.items_id" => $this->getID(),
],
]);
foreach($iterator as $row) {
foreach ($iterator as $row) {
/** @var $item CommonDBTM */
$item = new $generatedType();
$item->getFromResultSet($row);
$targets[] = $item;
$this->targetList[] = clone $item;
// skip not wanted itemtypes
if (!in_array($targetType, $itemtypes)) {
continue;
}
$targets[] = $item;
}
}

Expand All @@ -1858,9 +1869,13 @@ public function getGeneratedTargets($itemtypes = []): array {
*
* @return null|int
*/
public function getMinimalStatus(): ?int {
public function getAggregatedStatus(): ?int {
$generatedTargets = $this->getGeneratedTargets([PluginFormcreatorTargetTicket::getType()]);

$isWaiting = false;
$isAssigned = false;
$isProcessing = false;

// Find the minimal status of the first generated tickets in the array (deleted items excluded)
$generatedTarget = array_shift($generatedTargets);
while ($generatedTarget!== null && $generatedTarget->fields['is_deleted']) {
Expand All @@ -1871,8 +1886,19 @@ public function getMinimalStatus(): ?int {
return null;
}

// Find the minimal status of all (not deleted) generated targets
$minimalStatus = PluginFormcreatorCommon::getTicketStatusForIssue($generatedTarget);
// Find status of the first ticket in the array
$aggregatedStatus = PluginFormcreatorCommon::getTicketStatusForIssue($generatedTarget);
if ($aggregatedStatus == CommonITILObject::ASSIGNED) {
$isAssigned = true;
}
if ($aggregatedStatus == CommonITILObject::PLANNED) {
$isProcessing = true;
}
if ($aggregatedStatus == CommonITILObject::WAITING) {
$isWaiting = true;
}

// Traverse all other tickets and set the minimal status
foreach ($generatedTargets as $generatedTarget) {
/** @var Ticket $generatedTarget */
if ($generatedTarget::getType() != Ticket::getType()) {
Expand All @@ -1885,10 +1911,33 @@ public function getMinimalStatus(): ?int {
if ($ticketStatus >= PluginFormcreatorFormAnswer::STATUS_WAITING) {
continue;
}
$minimalStatus = min($minimalStatus, $ticketStatus);

if ($ticketStatus == CommonITILObject::ASSIGNED) {
$isAssigned = true;
}
if ($ticketStatus == CommonITILObject::PLANNED) {
$isProcessing = true;
}
if ($ticketStatus == CommonITILObject::WAITING) {
$isWaiting = true;
}
$aggregatedStatus = min($aggregatedStatus, $ticketStatus);
}

// Assigned status takes precedence
if ($isAssigned) {
$aggregatedStatus = CommonITILObject::ASSIGNED;
}
// Planned status takes precedence
if ($isProcessing) {
$aggregatedStatus = CommonITILObject::PLANNED;
}
// Waiting status takes precedence to inform the requester his feedback is required
if ($isWaiting) {
$aggregatedStatus = CommonITILObject::WAITING;
}

return $minimalStatus;
return $aggregatedStatus;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ function plugin_formcreator_hook() {
// Add specific CSS
$PLUGIN_HOOKS['add_css']['formcreator'][] = PluginFormcreatorCommon::getCssFilename();

$PLUGIN_HOOKS['pre_show_tab']['formcreator'] = [
PluginFormcreatorCommon::class, 'hookPreShowTab',
];
$PLUGIN_HOOKS['post_show_tab']['formcreator'] = [
PluginFormcreatorCommon::class, 'hookPostShowTab',
];

// Load JS and CSS files if we are on a page which need them
if (strpos($_SERVER['REQUEST_URI'], 'formcreator') !== false
|| strpos($_SERVER['REQUEST_URI'], 'central.php') !== false
Expand Down
Loading

0 comments on commit 4fbdfa4

Please sign in to comment.