diff --git a/setup.php b/setup.php index 8474f0a..7763483 100644 --- a/setup.php +++ b/setup.php @@ -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', + ]; } /** diff --git a/src/Controller.php b/src/Controller.php index f5ca861..e20bbe1 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -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); + } + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index fc5fa92..ba00792 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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';