Skip to content

Commit 3eb4e5b

Browse files
SebSeptcedric-anne
authored andcommitted
Group managed OLA
1 parent df8d30e commit 3eb4e5b

61 files changed

Lines changed: 6301 additions & 1892 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

front/ticket.form.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@
6666
}
6767
}
6868

69+
// form submitted (add & update)
6970
// as _actors virtual field stores json, bypass automatic escaping
7071
if (isset($_POST['_actors'])) {
7172
$_POST['_actors'] = json_decode($_POST['_actors'], true);
7273
$_REQUEST['_actors'] = $_POST['_actors'];
7374
}
7475

7576
if (isset($_POST["add"])) {
77+
// form submitted (add)
7678
$track->check(-1, CREATE, $_POST);
7779
$_POST = $track->enforceReadonlyFields($_POST, true);
7880

@@ -83,6 +85,7 @@
8385
}
8486
Html::back();
8587
} elseif (isset($_POST['update'])) {
88+
// form submitted (update)
8689
if (!$track::canUpdate()) {
8790
throw new AccessDeniedHttpException();
8891
}
@@ -123,6 +126,8 @@
123126
}
124127
Html::redirect(Ticket::getFormURLWithID($_POST["id"]) . $toadd);
125128
}
129+
130+
// in case user can update the ticket but not read it, redirect to the ticket list
126131
Session::addMessageAfterRedirect(
127132
__s('You have been redirected because you no longer have access to this ticket'),
128133
true,
@@ -181,20 +186,6 @@
181186
sprintf(__('%s updates an item'), $_SESSION["glpiname"])
182187
);
183188

184-
Html::redirect(Ticket::getFormURLWithID($_POST["id"]));
185-
} elseif (isset($_POST['ola_delete'])) {
186-
$track->check($_POST["id"], UPDATE);
187-
188-
$track->deleteLevelAgreement("OLA", $_POST["id"], $_POST['type'], $_POST['delete_date']);
189-
Event::log(
190-
$_POST["id"],
191-
"ticket",
192-
4,
193-
"tracking",
194-
//TRANS: %s is the user login
195-
sprintf(__('%s updates an item'), $_SESSION["glpiname"])
196-
);
197-
198189
Html::redirect(Ticket::getFormURLWithID($_POST["id"]));
199190
} elseif (isset($_POST['addme_as_actor'])) {
200191
$id = (int) $_POST['id'];
@@ -234,6 +225,7 @@
234225
Html::back();
235226
}
236227

228+
// show form when editing a ticket
237229
$id = (int) $_GET['id'];
238230
if ($id > 0) {
239231
$available_options = ['_openfollowup'];

inc/relation.constant.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@
750750
'glpi_tickettasks' => 'groups_id_tech',
751751
'glpi_users' => 'groups_id',
752752
'glpi_itilvalidationtemplates_targets' => 'groups_id',
753+
'glpi_olas' => 'groups_id',
753754
],
754755

755756
'glpi_holidays' => [
@@ -1033,15 +1034,11 @@
10331034
'_glpi_olalevelactions' => 'olalevels_id',
10341035
'_glpi_olalevelcriterias' => 'olalevels_id',
10351036
'_glpi_olalevels_tickets' => 'olalevels_id',
1036-
'glpi_tickets' => 'olalevels_id_ttr',
10371037
],
10381038

10391039
'glpi_olas' => [
10401040
'glpi_olalevels' => 'olas_id',
1041-
'glpi_tickets' => [
1042-
'olas_id_ttr',
1043-
'olas_id_tto',
1044-
],
1041+
'_glpi_items_olas' => 'olas_id',
10451042
],
10461043

10471044
'glpi_operatingsystemarchitectures' => [

install/empty_data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ public function getEmptyData(): array
756756
'hourmax' => 24,
757757
], [
758758
'id' => 32,
759-
'itemtype' => 'OlaLevel_Ticket',
759+
'itemtype' => 'Item_Ola',
760760
'name' => 'olaticket',
761761
'frequency' => 5 * MINUTE_TIMESTAMP,
762762
'param' => null,
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2025 Teclib' and contributors.
11+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
12+
* @var DBmysql $DB
13+
* @var Migration $migration
14+
*
15+
* ---------------------------------------------------------------------
16+
*
17+
* LICENSE
18+
*
19+
* This file is part of GLPI.
20+
*
21+
* This program is free software: you can redistribute it and/or modify
22+
* it under the terms of the GNU General Public License as published by
23+
* the Free Software Foundation, either version 3 of the License, or
24+
* (at your option) any later version.
25+
*
26+
* This program is distributed in the hope that it will be useful,
27+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
28+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29+
* GNU General Public License for more details.
30+
*
31+
* You should have received a copy of the GNU General Public License
32+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
33+
*
34+
* ---------------------------------------------------------------------
35+
*/
36+
37+
/**
38+
* @var DBmysql $DB
39+
* @var Migration $migration
40+
*/
41+
42+
use function Safe\json_encode;
43+
44+
$migration->log('Group managed olas', false);
45+
46+
add_groups_id_field_in_olas($migration);
47+
create_items_olas_table($migration);
48+
migrate_items_olas_data($migration);
49+
remove_olas_fields_in_tickets($migration);
50+
update_crontask($migration, $DB);
51+
52+
$migration->executeMigration();
53+
return;
54+
55+
// --- functions
56+
57+
function add_groups_id_field_in_olas(Migration $migration): void
58+
{
59+
$migration->addField(
60+
OLA::getTable(),
61+
Group::getForeignKeyField(),
62+
'fkey',
63+
[
64+
'value' => '0',
65+
'null' => false,
66+
'after' => 'slms_id',
67+
]
68+
);
69+
70+
// addKey requires the table to exist -> execute migration before
71+
$migration->executeMigration();
72+
$migration->addKey(OLA::getTable(), Group::getForeignKeyField());
73+
}
74+
75+
function remove_olas_fields_in_tickets(Migration $migration): void
76+
{
77+
$fields_to_remove = [
78+
'ola_waiting_duration',
79+
'olas_id_tto',
80+
'olas_id_ttr',
81+
'olalevels_id_ttr',
82+
'ola_tto_begin_date',
83+
'ola_ttr_begin_date',
84+
'internal_time_to_resolve',
85+
'internal_time_to_own',
86+
];
87+
88+
foreach ($fields_to_remove as $field) {
89+
$migration->dropField(Ticket::getTable(), $field);
90+
}
91+
}
92+
function create_items_olas_table(Migration $migration): void
93+
{
94+
$charset = DBConnection::getDefaultCharset();
95+
$collation = DBConnection::getDefaultCollation();
96+
$pk_sign = DBConnection::getDefaultPrimaryKeySignOption();
97+
98+
$query = "CREATE TABLE IF NOT EXISTS `glpi_items_olas` (
99+
`id` int {$pk_sign} NOT NULL AUTO_INCREMENT,
100+
`itemtype` varchar(255) NOT NULL,
101+
`items_id` int unsigned NOT NULL,
102+
`olas_id` int unsigned NOT NULL,
103+
`ola_type` tinyint NOT NULL, -- 1: TTO, 2: TTR
104+
`start_time` timestamp NULL DEFAULT NULL,
105+
`due_time` timestamp NULL DEFAULT NULL,
106+
`end_time` timestamp NULL DEFAULT NULL,
107+
`waiting_time` int NOT NULL DEFAULT 0,
108+
`waiting_start` timestamp,
109+
`is_late` tinyint NOT NULL DEFAULT 0,
110+
PRIMARY KEY (`id`)
111+
) ENGINE=InnoDB DEFAULT CHARSET=$charset COLLATE=$collation ROW_FORMAT=DYNAMIC;";
112+
$migration->addPreQuery($query);
113+
$migration->executeMigration();
114+
115+
$migration->addKey('glpi_items_olas', 'olas_id');
116+
$migration->addKey('glpi_items_olas', ['itemtype', 'items_id'], 'item');
117+
}
118+
119+
function migrate_items_olas_data(Migration $migration): void
120+
{
121+
$_ticket = new Ticket();
122+
if (!$_ticket->isField('olas_id_tto')) {
123+
// olas_id_tto field is removed : considere migration as done
124+
return;
125+
}
126+
$tickets_with_ola = $_ticket->find(['OR'
127+
=> [
128+
['NOT' => ['olas_id_tto' => null]],
129+
['NOT' => ['olas_id_ttr' => null]],
130+
]]);
131+
132+
foreach ($tickets_with_ola as $ticket) {
133+
if ($ticket['olas_id_tto'] !== 0) {
134+
$io = new Item_Ola();
135+
136+
$_data = [
137+
'itemtype' => Ticket::class,
138+
'items_id' => $ticket['id'],
139+
'olas_id' => $ticket['olas_id_tto'],
140+
'start_time' => $ticket['ola_tto_begin_date'],
141+
'due_time' => $ticket['internal_time_to_own'],
142+
'waiting_time' => $ticket['ola_waiting_duration'],
143+
'waiting_start' => null,
144+
];
145+
146+
if (!$io->add($_data)) {
147+
throw new Exception('Failed to migrate OLA TTO data: ' . json_encode($_data));
148+
}
149+
}
150+
151+
if ($ticket['olas_id_ttr'] !== 0) {
152+
$io = new Item_Ola();
153+
154+
$_data = [
155+
'itemtype' => Ticket::class,
156+
'items_id' => $ticket['id'],
157+
'olas_id' => $ticket['olas_id_ttr'],
158+
'start_time' => $ticket['ola_ttr_begin_date'],
159+
'due_time' => $ticket['internal_time_to_resolve'],
160+
'waiting_time' => $ticket['ola_waiting_duration'],
161+
];
162+
163+
if (!$io->add($_data)) {
164+
throw new Exception('Failed to migrato OLA TTO data: ' . json_encode($_data));
165+
}
166+
}
167+
}
168+
}
169+
170+
function update_crontask(Migration $migration, DBmysql $DB): void
171+
{
172+
// find if cron task already exists to choose against adding it or updating it
173+
$crontask = $DB->request([
174+
'SELECT' => ['id'],
175+
'FROM' => CronTask::getTable(),
176+
'WHERE' => [
177+
'name' => 'olaticket',
178+
],
179+
]);
180+
$id = $crontask->current() ? $crontask->current()['id'] : null;
181+
182+
if (is_null($id)) {
183+
// add new crontask
184+
$migration->insertInTable(
185+
'glpi_crontasks',
186+
[
187+
'itemtype' => 'Item_Ola',
188+
'name' => 'olaticket',
189+
'frequency' => 5 * MINUTE_TIMESTAMP,
190+
'param' => null,
191+
'state' => CronTask::STATE_WAITING,
192+
'mode' => CronTask::MODE_INTERNAL,
193+
'lastrun' => null,
194+
'logs_lifetime' => 30,
195+
'hourmin' => 0,
196+
'hourmax' => 24,
197+
]
198+
);
199+
} else {
200+
// update existing crontask
201+
if (false === $DB->doQuery($DB->buildUpdate('glpi_crontasks', ['itemtype' => 'Item_Ola'], ['id' => $id]))) {
202+
throw new Exception('Failed to update crontask itemtype');
203+
}
204+
}
205+
}

install/mysql/glpi-empty.sql

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6727,14 +6727,36 @@ CREATE TABLE `glpi_olas` (
67276727
`end_of_working_day` tinyint NOT NULL DEFAULT '0',
67286728
`date_creation` timestamp NULL DEFAULT NULL,
67296729
`slms_id` int unsigned NOT NULL DEFAULT '0',
6730+
`groups_id` int unsigned NOT NULL DEFAULT '0',
67306731
PRIMARY KEY (`id`),
67316732
KEY `name` (`name`),
67326733
KEY `entities_id` (`entities_id`),
67336734
KEY `is_recursive` (`is_recursive`),
67346735
KEY `date_mod` (`date_mod`),
67356736
KEY `date_creation` (`date_creation`),
67366737
KEY `calendars_id` (`calendars_id`),
6737-
KEY `slms_id` (`slms_id`)
6738+
KEY `slms_id` (`slms_id`),
6739+
KEY `groups_id` (`groups_id`)
6740+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;
6741+
6742+
### Dump table glpi_items_olas
6743+
6744+
DROP TABLE IF EXISTS `glpi_items_olas`;
6745+
CREATE TABLE `glpi_items_olas` (
6746+
`id` int unsigned NOT NULL AUTO_INCREMENT,
6747+
`due_time` timestamp,
6748+
`end_time` timestamp,
6749+
`items_id` int unsigned NOT NULL,
6750+
`itemtype` varchar(255) NOT NULL,
6751+
`olas_id` int unsigned NOT NULL,
6752+
`ola_type` tinyint NOT NULL,
6753+
`start_time` timestamp,
6754+
`waiting_time` int NOT NULL DEFAULT 0,
6755+
`waiting_start` timestamp,
6756+
`is_late` tinyint NOT NULL DEFAULT 0,
6757+
PRIMARY KEY (`id`),
6758+
KEY `item` (`itemtype`,`items_id`),
6759+
KEY `olas_id` (`olas_id`)
67386760
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;
67396761

67406762
### Dump table glpi_softwarecategories
@@ -7306,14 +7328,6 @@ CREATE TABLE `glpi_tickets` (
73067328
`time_to_own` timestamp NULL DEFAULT NULL,
73077329
`begin_waiting_date` timestamp NULL DEFAULT NULL,
73087330
`sla_waiting_duration` int NOT NULL DEFAULT '0',
7309-
`ola_waiting_duration` int NOT NULL DEFAULT '0',
7310-
`olas_id_tto` int unsigned NOT NULL DEFAULT '0',
7311-
`olas_id_ttr` int unsigned NOT NULL DEFAULT '0',
7312-
`olalevels_id_ttr` int unsigned NOT NULL DEFAULT '0',
7313-
`ola_tto_begin_date` timestamp NULL DEFAULT NULL,
7314-
`ola_ttr_begin_date` timestamp NULL DEFAULT NULL,
7315-
`internal_time_to_resolve` timestamp NULL DEFAULT NULL,
7316-
`internal_time_to_own` timestamp NULL DEFAULT NULL,
73177331
`waiting_duration` int NOT NULL DEFAULT '0',
73187332
`close_delay_stat` int NOT NULL DEFAULT '0',
73197333
`solve_delay_stat` int NOT NULL DEFAULT '0',
@@ -7342,20 +7356,14 @@ CREATE TABLE `glpi_tickets` (
73427356
KEY `slas_id_ttr` (`slas_id_ttr`),
73437357
KEY `time_to_resolve` (`time_to_resolve`),
73447358
KEY `time_to_own` (`time_to_own`),
7345-
KEY `olas_id_tto` (`olas_id_tto`),
7346-
KEY `olas_id_ttr` (`olas_id_ttr`),
73477359
KEY `slalevels_id_ttr` (`slalevels_id_ttr`),
7348-
KEY `internal_time_to_resolve` (`internal_time_to_resolve`),
7349-
KEY `internal_time_to_own` (`internal_time_to_own`),
73507360
KEY `users_id_lastupdater` (`users_id_lastupdater`),
73517361
KEY `type` (`type`),
73527362
KEY `itilcategories_id` (`itilcategories_id`),
73537363
KEY `is_deleted` (`is_deleted`),
73547364
KEY `name` (`name`),
73557365
KEY `locations_id` (`locations_id`),
73567366
KEY `date_creation` (`date_creation`),
7357-
KEY `ola_waiting_duration` (`ola_waiting_duration`),
7358-
KEY `olalevels_id_ttr` (`olalevels_id_ttr`),
73597367
KEY `tickettemplates_id` (`tickettemplates_id`)
73607368
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;
73617369

0 commit comments

Comments
 (0)