Skip to content

Commit 079a120

Browse files
committed
fix(formanswer): status update on ticket change
1 parent 6af1766 commit 079a120

File tree

5 files changed

+95
-128
lines changed

5 files changed

+95
-128
lines changed

hook.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@ function plugin_formcreator_hook_update_ticket(CommonDBTM $item) {
489489
'requester_id' => $requester,
490490
'comment' => addslashes($item->fields['content']),
491491
]);
492-
return;
493492
}
494493

495494
// No issue linked to the ticket,

inc/formanswer.class.php

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,76 +1945,77 @@ public function getGeneratedTargets($itemtypes = []): array {
19451945
}
19461946

19471947
/**
1948-
* Get the lowest status among the associated tickets
1948+
* Get the most appropriate status among the associated tickets
1949+
*
1950+
* Conversion matrix between the temporary status of the form answer
1951+
* and the status of the ticket under process. The matrix below is subjective
1952+
* and is designed in a way to give priority to requester's action.
1953+
*
1954+
* Status of the ticket under process
1955+
* +----------+-- -------+---------+---------+--------+--------+
1956+
* | INCOMING | ASSIGNED | PLANNED | WAITING | SOLVED | CLOSED
1957+
* + ---------+----------+----------+---------+---------+--------+--------+
1958+
* | (null) | INCOMING ASSIGNED PLANNED WAITING SOLVED CLOSED
1959+
* S | INCOMING | INCOMING ASSIGNED PLANNED WAITING INCOMING INCOMING
1960+
* T t | ASSIGNED | ASSIGNED ASSIGNED PLANNED WAITING ASSIGNED ASSIGNED
1961+
* e a | PLANNED | PLANNED PLANNED PLANNED WAITING PLANNED PLANNED
1962+
* m t | WAITING | WAITING WAITING WAITING WAITING WAITING WAITING
1963+
* p u | SOLVED | INCOMING ASSIGNED PLANNED WAITING SOLVED SOLVED
1964+
* s | CLOSED | INCOMING ASSIGNED PLANNED WAITING SOLVED CLOSED
1965+
*
1966+
* T = status picked from Ticket
1967+
* V = status picked from Validation
19491968
*
19501969
* @return null|int
19511970
*/
19521971
public function getAggregatedStatus(): ?int {
19531972
$generatedTargets = $this->getGeneratedTargets([PluginFormcreatorTargetTicket::getType()]);
1954-
1955-
$isWaiting = false;
1956-
$isAssigned = false;
1957-
$isProcessing = false;
1958-
1959-
// Find the minimal status of the first generated tickets in the array (deleted items excluded)
1960-
$generatedTarget = array_shift($generatedTargets);
1961-
while ($generatedTarget!== null && $generatedTarget->fields['is_deleted']) {
1962-
$generatedTarget = array_shift($generatedTargets);
1963-
}
1964-
if ($generatedTarget === null) {
1973+
if (count($generatedTargets) === 0) {
19651974
// No target found, nothing to do
19661975
return null;
19671976
}
19681977

1969-
// Find status of the first ticket in the array
1970-
$aggregatedStatus = PluginFormcreatorCommon::getTicketStatusForIssue($generatedTarget);
1971-
if ($aggregatedStatus == CommonITILObject::ASSIGNED) {
1972-
$isAssigned = true;
1973-
}
1974-
if ($aggregatedStatus == CommonITILObject::PLANNED) {
1975-
$isProcessing = true;
1976-
}
1977-
if ($aggregatedStatus == CommonITILObject::WAITING) {
1978-
$isWaiting = true;
1979-
}
1978+
$aggregatedStatus = null;
19801979

1981-
// Traverse all other tickets and set the minimal status
1980+
// Traverse all tickets and set the minimal status
19821981
foreach ($generatedTargets as $generatedTarget) {
19831982
/** @var Ticket $generatedTarget */
19841983
if ($generatedTarget::getType() != Ticket::getType()) {
19851984
continue;
19861985
}
19871986
if ($generatedTarget->isDeleted()) {
1987+
// Ignore deleted tickets
19881988
continue;
19891989
}
19901990
$ticketStatus = PluginFormcreatorCommon::getTicketStatusForIssue($generatedTarget);
19911991
if ($ticketStatus >= PluginFormcreatorFormAnswer::STATUS_WAITING) {
1992+
// Ignore tickets refused or pending for validation
1993+
// getTicketStatusForIssue() does not returns STATUS_ACCEPTED
19921994
continue;
19931995
}
19941996

1995-
if ($ticketStatus == CommonITILObject::ASSIGNED) {
1996-
$isAssigned = true;
1997+
if ($ticketStatus == CommonITILObject::WAITING) {
1998+
$aggregatedStatus = CommonITILObject::WAITING;
1999+
break;
19972000
}
19982001
if ($ticketStatus == CommonITILObject::PLANNED) {
1999-
$isProcessing = true;
2002+
$aggregatedStatus = CommonITILObject::PLANNED;
2003+
continue;
20002004
}
2001-
if ($ticketStatus == CommonITILObject::WAITING) {
2002-
$isWaiting = true;
2005+
if ($ticketStatus == CommonITILObject::ASSIGNED) {
2006+
$aggregatedStatus = CommonITILObject::ASSIGNED;
2007+
continue;
2008+
}
2009+
if ($ticketStatus == CommonITILObject::INCOMING) {
2010+
$aggregatedStatus = CommonITILObject::INCOMING;
2011+
continue;
2012+
}
2013+
if ($aggregatedStatus === null) {
2014+
$aggregatedStatus = $ticketStatus;
2015+
continue;
20032016
}
2004-
$aggregatedStatus = min($aggregatedStatus, $ticketStatus);
2005-
}
20062017

2007-
// Assigned status takes precedence
2008-
if ($isAssigned) {
2009-
$aggregatedStatus = CommonITILObject::ASSIGNED;
2010-
}
2011-
// Planned status takes precedence
2012-
if ($isProcessing) {
2013-
$aggregatedStatus = CommonITILObject::PLANNED;
2014-
}
2015-
// Waiting status takes precedence to inform the requester his feedback is required
2016-
if ($isWaiting) {
2017-
$aggregatedStatus = CommonITILObject::WAITING;
2018+
$aggregatedStatus = min($aggregatedStatus, $ticketStatus);
20182019
}
20192020

20202021
return $aggregatedStatus;

tests/3-unit/PluginFormcreatorFormAnswer.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function beforeTestMethod($method) {
6969
case 'testGetTargets':
7070
case 'testGetGeneratedTargets':
7171
case 'testGetAggregatedStatus':
72+
case 'testStatus':
7273
$this->login('glpi', 'glpi');
7374
}
7475
}
@@ -613,20 +614,6 @@ public function testGetTargets() {
613614
]);
614615
}
615616

616-
public function testUpdateStatus() {
617-
global $CFG_GLPI;
618-
619-
$CFG_GLPI['use_notifications'] = 0;
620-
621-
// Prepare test context
622-
$form = $this->getForm();
623-
624-
$formAnswer = $this->newTestedInstance();
625-
$formAnswer->add([
626-
'plugin_formcreator_forms_id' => $form->getID(),
627-
]);
628-
}
629-
630617
public function testGetGeneratedTargets() {
631618
$form = $this->getForm();
632619
$targets = [];
@@ -720,7 +707,7 @@ public function testGetAggregatedStatus() {
720707
'status' => CommonITILObject::INCOMING,
721708
]);
722709
$output = $instance->getAggregatedStatus();
723-
$this->integer($output)->isEqualTo(CommonITILObject::ASSIGNED);
710+
$this->integer($output)->isEqualTo(CommonITILObject::INCOMING);
724711

725712
$tickets[0]->update([
726713
'id' => $tickets[0]->getID(),

tests/3-unit/PluginFormcreatorIssue.php

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
namespace tests\units;
3333
use GlpiPlugin\Formcreator\Tests\CommonTestCase;
3434
use PluginFormcreatorFormAnswer;
35+
use PluginFormcreatorTargetTicket;
36+
use PluginFormcreatorTarget_Actor;
37+
use CommonITILActor;
3538
use RuleAction;
3639
use User;
3740
use Rule;
@@ -153,7 +156,7 @@ public function providerGetsyncIssuesRequest_simpleFormanswers() {
153156
public function providerGetSyncIssuesRequest_formAnswerWithOneTicket() {
154157
// case 1
155158
$form = $this->getForm();
156-
$targetTicket1 = new \PluginFormcreatorTargetTicket();
159+
$targetTicket1 = new PluginFormcreatorTargetTicket();
157160
$targetTicket1->add([
158161
'plugin_formcreator_forms_id' => $form->getID(),
159162
'name' => 'foo',
@@ -207,7 +210,7 @@ public function providerGetSyncIssuesRequest_formAnswerWithOneTicket() {
207210
$this->boolean($formAnswer->isNewItem())->isFalse();
208211
$formAnswer->getFromDB($formAnswer->getID());
209212
$ticket2 = array_shift($formAnswer->targetList);
210-
$this->object($ticket)->isInstanceOf(Ticket::getType());
213+
$this->object($ticket2)->isInstanceOf(Ticket::getType());
211214

212215
return [
213216
'formAnswerWithOneTicket' => [
@@ -241,13 +244,13 @@ public function providerGetSyncIssuesRequest_formAnswerWithOneTicket() {
241244

242245
public function providerGetSyncIssuesRequest_formAnswerWithSeveralTickets() {
243246
$form = $this->getForm();
244-
$targetTicket1 = new \PluginFormcreatorTargetTicket();
247+
$targetTicket1 = new PluginFormcreatorTargetTicket();
245248
$targetTicket1->add([
246249
'plugin_formcreator_forms_id' => $form->getID(),
247250
'name' => 'foo',
248251
]);
249252
$this->boolean($targetTicket1->isNewItem())->isFalse();
250-
$targetTicket2 = new \PluginFormcreatorTargetTicket();
253+
$targetTicket2 = new PluginFormcreatorTargetTicket();
251254
$targetTicket2->add([
252255
'plugin_formcreator_forms_id' => $form->getID(),
253256
'name' => 'bar',
@@ -277,42 +280,6 @@ public function providerGetSyncIssuesRequest_formAnswerWithSeveralTickets() {
277280
];
278281
}
279282

280-
public function providerGetSyncIssuesRequest_formAnswerWithOneTickets() {
281-
$form = $this->getForm();
282-
$targetTicket1 = new \PluginFormcreatorTargetTicket();
283-
$targetTicket1->add([
284-
'plugin_formcreator_forms_id' => $form->getID(),
285-
'name' => 'foo',
286-
]);
287-
$this->boolean($targetTicket1->isNewItem())->isFalse();
288-
289-
$formAnswer = new PluginFormcreatorFormAnswer();
290-
$formAnswer->add([
291-
'plugin_formcreator_forms_id' => $form->getID(),
292-
]);
293-
$this->boolean($formAnswer->isNewItem())->isFalse();
294-
$formAnswer->getFromDB($formAnswer->getID());
295-
296-
/** @var Ticket */
297-
$ticket = array_pop($formAnswer->targetList);
298-
$this->object($ticket)->isInstanceOf(Ticket::class);
299-
return [
300-
'formAnswerWithOneTickets' => [
301-
'item' => $formAnswer,
302-
'expected' => [
303-
'itemtype' => PluginFormcreatorFormAnswer::getType(),
304-
'items_id' => $ticket->getID(),
305-
'display_id' => 't_' . $ticket->getID(),
306-
'name' => $formAnswer->fields['name'],
307-
'status' => $formAnswer->fields['status'],
308-
'requester_id' => $formAnswer->fields['requester_id'],
309-
'date_creation' => $formAnswer->fields['request_date'],
310-
'date_mod' => $formAnswer->fields['request_date'],
311-
],
312-
],
313-
];
314-
}
315-
316283
public function providerGetSyncIssuesRequest_formanswerUnderValidation() {
317284
$form = $this->getForm([
318285
'validation_required' => PluginFormcreatorForm::VALIDATION_USER,
@@ -466,29 +433,29 @@ public function providerGetsyncIssuesRequest_validatedTicket() {
466433

467434
public function providerGetSyncIssuesRequest_FormAnswerWithSeveralRequesters() {
468435
$form = $this->getForm();
469-
$targetTicket1 = new \PluginFormcreatorTargetTicket();
436+
$targetTicket1 = new PluginFormcreatorTargetTicket();
470437
$targetTicket1->add([
471438
'plugin_formcreator_forms_id' => $form->getID(),
472439
'name' => 'foo',
473440
]);
474441
$this->boolean($targetTicket1->isNewItem())->isFalse();
475442

476-
$actor1 = new \PluginFormcreatorTarget_Actor();
443+
$actor1 = new PluginFormcreatorTarget_Actor();
477444
$actor1->add([
478445
'itemtype' => $targetTicket1->getType(),
479446
'items_id' => $targetTicket1->getID(),
480-
'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON,
481-
'actor_type' => \CommonITILActor::REQUESTER,
447+
'actor_role' => PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON,
448+
'actor_type' => CommonITILActor::REQUESTER,
482449
'actor_value' => 3,
483450
'use_notification' => '1',
484451
]);
485452
$this->boolean($actor1->isNewItem())->isFalse();
486-
$actor2 = new \PluginFormcreatorTarget_Actor();
453+
$actor2 = new PluginFormcreatorTarget_Actor();
487454
$actor2->add([
488455
'itemtype' => $targetTicket1->getType(),
489456
'items_id' => $targetTicket1->getID(),
490-
'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON,
491-
'actor_type' => \CommonITILActor::REQUESTER,
457+
'actor_role' => PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON,
458+
'actor_type' => CommonITILActor::REQUESTER,
492459
'actor_value' => 5,
493460
'use_notification' => '1',
494461
]);

0 commit comments

Comments
 (0)