Skip to content

Commit

Permalink
fix(install): populate issues table on upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Sep 20, 2021
1 parent 2eebe5c commit 76550d2
Showing 1 changed file with 95 additions and 1 deletion.
96 changes: 95 additions & 1 deletion install/upgrade_to_2.10.2.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,100 @@ public function upgrade(Migration $migration) {
// Versioin 2.10.2 contains fixes on counters requiring repopulation of issues table
$table = 'glpi_plugin_formcreator_issues';
$DB->query("TRUNCATE `$table`");
PluginFormcreatorIssue::cronSyncIssues(new CronTask());
$this->syncIssues(new CronTask());
}

/**
* This is a copy of PluginFormcreatorIssue::cronSyncIssues as it is in 2.10.2
*
* @param CronTask $task
* @return void
*/
public function syncIssues(CronTask $task) {
global $DB;

$task->log("Sync issues from forms answers and tickets");
$volume = 0;

// Request which merges tickets and formanswers
// 1 ticket not linked to a formanswer => 1 issue which is the ticket sub_itemtype
// 1 form_answer not linked to a ticket => 1 issue which is the formanswer sub_itemtype
// 1 ticket linked to 1 form_answer => 1 issue which is the ticket sub_itemtype
// several tickets linked to the same form_answer => 1 issue which is the form_answer sub_itemtype
$query = "SELECT DISTINCT
NULL AS `id`,
`f`.`name` AS `name`,
CONCAT('f_',`fanswer`.`id`) AS `display_id`,
`fanswer`.`id` AS `original_id`,
'PluginFormcreatorFormAnswer' AS `sub_itemtype`,
`fanswer`.`status` AS `status`,
`fanswer`.`request_date` AS `date_creation`,
`fanswer`.`request_date` AS `date_mod`,
`fanswer`.`entities_id` AS `entities_id`,
`fanswer`.`is_recursive` AS `is_recursive`,
`fanswer`.`requester_id` AS `requester_id`,
`fanswer`.`users_id_validator` AS `users_id_validator`,
`fanswer`.`groups_id_validator` AS `groups_id_validator`,
`fanswer`.`comment` AS `comment`
FROM `glpi_plugin_formcreator_formanswers` AS `fanswer`
LEFT JOIN `glpi_plugin_formcreator_forms` AS `f`
ON`f`.`id` = `fanswer`.`plugin_formcreator_forms_id`
LEFT JOIN `glpi_items_tickets` AS `itic`
ON `itic`.`items_id` = `fanswer`.`id`
AND `itic`.`itemtype` = 'PluginFormcreatorFormAnswer'
GROUP BY `original_id`
HAVING COUNT(`itic`.`tickets_id`) != 1
UNION
SELECT DISTINCT
NULL AS `id`,
`tic`.`name` AS `name`,
CONCAT('t_',`tic`.`id`) AS `display_id`,
`tic`.`id` AS `original_id`,
'Ticket' AS `sub_itemtype`,
if(`tv`.`status` IS NULL,`tic`.`status`, if(`tv`.`status` = 2, 101, if(`tv`.`status` = 3, `tic`.`status`, 102))) AS `status`,
`tic`.`date` AS `date_creation`,
`tic`.`date_mod` AS `date_mod`,
`tic`.`entities_id` AS `entities_id`,
0 AS `is_recursive`,
`tu`.`users_id` AS `requester_id`,
`tv`.`users_id_validate` AS `users_id_validator`,
0 AS `groups_id_validator`,
`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 DISTINCT `users_id`, `tickets_id`
FROM `glpi_tickets_users` AS `tu`
WHERE `tu`.`type` = '" . CommonITILActor::REQUESTER . "'
ORDER BY `id` ASC
) AS `tu` ON (`tic`.`id` = `tu`.`tickets_id`)
LEFT JOIN `glpi_ticketvalidations` as `tv`
ON (`tic`.`id` = `tv`.`tickets_id`)
WHERE `tic`.`is_deleted` = 0
GROUP BY `original_id`
HAVING COUNT(`itic`.`items_id`) <= 1";

$countQuery = "SELECT COUNT(*) AS `cpt` FROM ($query) AS `issues`";
$result = $DB->query($countQuery);
if ($result !== false) {
if (version_compare(GLPI_VERSION, '9.5') < 0) {
$fa = 'fetch_assoc';
} else {
$fa = 'fetchAssoc';
}
$count = $DB->$fa($result);
$table = 'glpi_plugin_formcreator_issues';
if (countElementsInTable($table) != $count['cpt']) {
if ($DB->query("TRUNCATE `$table`")) {
$DB->query("INSERT INTO `$table` SELECT * FROM ($query) as `dt`");
$volume = 1;
}
}
}
$task->setVolume($volume);

return 1;
}
}

0 comments on commit 76550d2

Please sign in to comment.