Skip to content

Commit

Permalink
fix(issue): distinguish requester and author
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Apr 6, 2020
1 parent 2121328 commit 051c1fd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 25 deletions.
56 changes: 37 additions & 19 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
32 changes: 28 additions & 4 deletions inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1353,16 +1355,27 @@ 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'),
'date_creation' => $ticket->getField('date'),
'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')),
]);
Expand Down Expand Up @@ -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,
Expand All @@ -1424,15 +1448,15 @@ 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'),
'date_creation' => $ticket->getField('date'),
'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')),
]);
Expand Down
9 changes: 8 additions & 1 deletion inc/issue.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 051c1fd

Please sign in to comment.