diff --git a/hook.php b/hook.php index 753889096..5876c50b9 100644 --- a/hook.php +++ b/hook.php @@ -293,27 +293,45 @@ function plugin_formcreator_giveItem($itemtype, $ID, $data, $num) { } function plugin_formcreator_hook_add_ticket(CommonDBTM $item) { - global $CFG_GLPI; + global $CFG_GLPI, $DB; - if ($item instanceof Ticket) { - if (!isset($CFG_GLPI['plugin_formcreator_disable_hook_create_ticket'])) { - // run this hook only if the plugin is not generating tickets - $issue = new PluginFormcreatorIssue(); - $issue->add([ - 'original_id' => $item->getID(), - 'sub_itemtype' => 'Ticket', - 'name' => addslashes($item->fields['name']), - 'status' => $item->fields['status'], - 'date_creation' => $item->fields['date'], - 'date_mod' => $item->fields['date_mod'], - 'entities_id' => $item->fields['entities_id'], - 'is_recursive' => '0', - 'requester_id' => $item->fields['users_id_recipient'], - 'validator_id' => '0', - 'comment' => addslashes($item->fields['content']), - ]); - } + if (!($item instanceof Ticket)) { + return; + } + if (isset($CFG_GLPI['plugin_formcreator_disable_hook_create_ticket'])) { + return; + } + + // run this hook only if the plugin is not generating tickets + $requester = $DB->request([ + 'SELECT' => 'users_id', + 'FROM' => Ticket_User::getTable(), + 'WHERE' => [ + 'tickets_id' => $item->getID(), + 'type' => '1', + ], + 'ORDER' => ['id'], + 'LIMIT' => '1', + ])->next(); + if ($requester === null) { + $requester = [ + 'users_id' => 0, + ]; } + $issue = new PluginFormcreatorIssue(); + $issue->add([ + 'original_id' => $item->getID(), + 'sub_itemtype' => 'Ticket', + 'name' => addslashes($item->fields['name']), + 'status' => $item->fields['status'], + 'date_creation' => $item->fields['date'], + 'date_mod' => $item->fields['date_mod'], + 'entities_id' => $item->fields['entities_id'], + 'is_recursive' => '0', + 'requester_id' => $requester['users_id'], + 'validator_id' => '0', + 'comment' => addslashes($item->fields['content']), + ]); } function plugin_formcreator_hook_update_ticket(CommonDBTM $item) { diff --git a/inc/formanswer.class.php b/inc/formanswer.class.php index 79e17b92b..5d35a4d1f 100644 --- a/inc/formanswer.class.php +++ b/inc/formanswer.class.php @@ -716,6 +716,8 @@ public function prepareInputForAdd($input) { $input['is_recursive'] = $form->fields['is_recursive']; $input['plugin_formcreator_forms_id'] = $form->getID(); + // requester_id is actually the author + // TODO : rename this column $input['requester_id'] = isset($_SESSION['glpiID']) ? $_SESSION['glpiID'] : 0; @@ -1353,8 +1355,19 @@ private function createIssue() { if (!$ticket->getFromDB($itemTicket->fields['tickets_id'])) { throw new RuntimeException('Formcreator: Missing ticket ' . $itemTicket->fields['tickets_id'] . ' for formanswer ' . $this->getID()); } + $ticketId = $ticket->getID(); + $ticketUser = new Ticket_User(); + $ticketUserRow = $ticketUser->find([ + 'tickets_id' => $ticketId, + 'type' => CommonITILActor::REQUESTER, + ], [ + 'id ASC' + ], + 1 + ); + $ticketUserRow = array_pop($ticketUserRow); $issue->add([ - 'original_id' => $ticket->getID(), + 'original_id' => $ticketId, 'sub_itemtype' => Ticket::class, 'name' => addslashes($ticket->getField('name')), 'status' => $ticket->getField('status'), @@ -1362,7 +1375,7 @@ private function createIssue() { 'date_mod' => $ticket->getField('date_mod'), 'entities_id' => $ticket->getField('entities_id'), 'is_recursive' => '0', - 'requester_id' => $ticket->getField('users_id_recipient'), + 'requester_id' => $ticketUserRow['users_id'], 'validator_id' => '', 'comment' => addslashes($ticket->getField('content')), ]); @@ -1416,6 +1429,17 @@ private function updateIssue() { if (!$ticket->getFromDB($itemTicket->fields['tickets_id'])) { throw new RuntimeException('Formcreator: Missing ticket ' . $itemTicket->fields['tickets_id'] . ' for formanswer ' . $this->getID()); } + $ticketId = $ticket->getID(); + $ticketUser = new Ticket_User(); + $ticketUserRow = $ticketUser->find([ + 'tickets_id' => $ticketId, + 'type' => CommonITILActor::REQUESTER, + ], [ + 'id ASC' + ], + 1 + ); + $ticketUserRow = array_pop($ticketUserRow); $issue->getFromDBByCrit([ 'AND' => [ 'sub_itemtype' => PluginFormcreatorFormAnswer::class, @@ -1424,7 +1448,7 @@ private function updateIssue() { ]); $issue->update([ 'id' => $issue->getID(), - 'original_id' => $ticket->getID(), + 'original_id' => $ticketId, 'sub_itemtype' => Ticket::class, 'name' => addslashes($ticket->getField('name')), 'status' => $ticket->getField('status'), @@ -1432,7 +1456,7 @@ private function updateIssue() { 'date_mod' => $ticket->getField('date_mod'), 'entities_id' => $ticket->getField('entities_id'), 'is_recursive' => '0', - 'requester_id' => $ticket->getField('users_id_recipient'), + 'requester_id' => $ticketUserRow['users_id'], 'validator_id' => '', 'comment' => addslashes($ticket->getField('content')), ]); diff --git a/inc/issue.class.php b/inc/issue.class.php index 008832475..65c89e949 100644 --- a/inc/issue.class.php +++ b/inc/issue.class.php @@ -107,13 +107,20 @@ public static function cronSyncIssues(CronTask $task) { `tic`.`date_mod` AS `date_mod`, `tic`.`entities_id` AS `entities_id`, 0 AS `is_recursive`, - `tic`.`users_id_recipient` AS `requester_id`, + `tu`.`users_id` AS `requester_id`, 0 AS `validator_id`, `tic`.`content` AS `comment` FROM `glpi_tickets` AS `tic` LEFT JOIN `glpi_items_tickets` AS `itic` ON `itic`.`tickets_id` = `tic`.`id` AND `itic`.`itemtype` = 'PluginFormcreatorFormAnswer' + LEFT JOIN ( + SELECT `users_id`, `tickets_id` + FROM `glpi_tickets_users` AS `tu` + WHERE `tu`.`type` = '" . CommonITILActor::REQUESTER . "' + ORDER BY `id` ASC + LIMIT 1 + ) AS `tu` ON (`tic`.`id` = `tu`.`tickets_id`) WHERE `tic`.`is_deleted` = 0 GROUP BY `original_id` HAVING COUNT(`itic`.`items_id`) <= 1"; diff --git a/inc/targetticket.class.php b/inc/targetticket.class.php index 99003941b..f683a4e74 100644 --- a/inc/targetticket.class.php +++ b/inc/targetticket.class.php @@ -632,7 +632,7 @@ public function save(PluginFormcreatorFormAnswer $formanswer) { } } - $data['_users_id_recipient'] = $requesters_id; + $data['users_id_recipient'] = $formanswer->fields['requester_id']; $data['users_id_lastupdater'] = Session::getLoginUserID(); $data = $this->setTargetEntity($data, $formanswer, $requesters_id);