Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ function plugin_init_moreoptions(): void
$PLUGIN_HOOKS[Hooks::ITEM_UPDATE]['moreoptions'][Problem::class] = [
Controller::class, 'updateItemActors',
];

$PLUGIN_HOOKS[Hooks::ITEM_ADD]['moreoptions'][TicketTask::class] = [
Controller::class, 'assignTechnicianFromTask',
];

$PLUGIN_HOOKS[Hooks::ITEM_ADD]['moreoptions'][ChangeTask::class] = [
Controller::class, 'assignTechnicianFromTask',
];

$PLUGIN_HOOKS[Hooks::ITEM_ADD]['moreoptions'][ProblemTask::class] = [
Controller::class, 'assignTechnicianFromTask',
];
}

/**
Expand Down
77 changes: 77 additions & 0 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,81 @@ public static function updateItemActors(CommonITILObject $item): CommonITILObjec
}
return $item;
}

/**
* Assign technician from task to parent ITIL object
* When a task is created with a technician assigned, this method will
* automatically assign that technician to the parent ticket/change/problem
*
* @param CommonDBTM $item The task item (TicketTask, ChangeTask, or ProblemTask)
* @return void
*/
public static function assignTechnicianFromTask(CommonDBTM $item): void
{
$conf = Config::getCurrentConfig();
if ($conf->fields['is_active'] != 1) {
return;
}

// Check if a technician is assigned to the task
if (!isset($item->fields['users_id_tech']) || empty($item->fields['users_id_tech'])) {
return;
}

$users_id_tech = $item->fields['users_id_tech'];

// Determine the parent ITIL object and user link class based on task type
switch (get_class($item)) {
case TicketTask::class:
if (!isset($item->fields['tickets_id'])) {
return;
}
$itilObject = new Ticket();
$userLinkClass = Ticket_User::class;
$itilIdField = 'tickets_id';
$itilId = $item->fields['tickets_id'];
break;

case ChangeTask::class:
if (!isset($item->fields['changes_id'])) {
return;
}
$itilObject = new Change();
$userLinkClass = Change_User::class;
$itilIdField = 'changes_id';
$itilId = $item->fields['changes_id'];
break;

case ProblemTask::class:
if (!isset($item->fields['problems_id'])) {
return;
}
$itilObject = new Problem();
$userLinkClass = Problem_User::class;
$itilIdField = 'problems_id';
$itilId = $item->fields['problems_id'];
break;

default:
return;
}

// Get the parent ITIL object
if (!$itilObject->getFromDB($itilId)) {
return;
}

// Check if the technician is already assigned to the parent ITIL object
$userLink = new $userLinkClass();
$criteria = [
'users_id' => $users_id_tech,
'type' => CommonITILActor::ASSIGN,
$itilIdField => $itilId,
];

// If the technician is not already assigned, add them
if (!$userLink->getFromDBByCrit($criteria)) {
$userLink->add($criteria);
}
}
}
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

define('GLPI_LOG_DIR', __DIR__ . '/files/_logs');

require_once __DIR__ . '/../../../phpunit/GLPITestCase.php';
require_once __DIR__ . '/../../../phpunit/DbTestCase.php';
require_once __DIR__ . '/../../../tests/GLPITestCase.php';
require_once __DIR__ . '/../../../tests/DbTestCase.php';
require_once __DIR__ . '/../../../vendor/autoload.php';
require_once __DIR__ . '/MoreOptionsTestCase.php';

Expand Down