From 0d33dc90a445742d7f023ffba4f9e60eadc0db01 Mon Sep 17 00:00:00 2001
From: Jared Hancock <gravydish@gmail.com>
Date: Mon, 23 Apr 2012 16:16:55 -0500
Subject: [PATCH] Add annulment support for ticket events

Add the ability of revoking previous ticket state tracking events when new
events are logged for the same ticket. This will allow, for instance, the
ability to revert the 'closed' state of a ticket when the ticket is
reopened.

For statistics tracking, a user could configure whether or not the events
should be counted for each event tracked or just the non-annulled events.
For instance, if a ticket is closed and reopened several times, only the
very last closed event should count toward the statistics for the ticket.
Therefore, when a ticket is reopened, previous closed events should be
marked as annulled.
---
 include/class.ticket.php                  | 12 ++++++++++--
 setup/inc/sql/abe9c0cb-3c2e0f54.patch.sql | 14 ++++++++++++++
 setup/inc/sql/osticket-v1.7-mysql.sql     |  1 +
 setup/inc/sql/osticket-v1.7-mysql.sql.md5 |  2 +-
 4 files changed, 26 insertions(+), 3 deletions(-)
 create mode 100644 setup/inc/sql/abe9c0cb-3c2e0f54.patch.sql

diff --git a/include/class.ticket.php b/include/class.ticket.php
index 275ef78c2..aa20928f5 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -802,7 +802,7 @@ class Ticket{
 
         //TODO: log reopen event here 
 
-        $this->logEvent('reopened');
+        $this->logEvent('reopened', 'closed');
         return (db_query($sql) && db_affected_rows());
     }
 
@@ -1463,13 +1463,21 @@ class Ticket{
     }
 
     // History log -- used for statistics generation (pretty reports)
-    function logEvent($state, $staff=null) {
+    function logEvent($state, $annul=null, $staff=null) {
         global $thisstaff;
 
         if ($staff === null) {
             if ($thisstaff) $staff=$thisstaff->getUserName();
             else $staff='SYSTEM';               # XXX: Security Violation ?
         }
+        # Annul previous entries if requested (for instance, reopening a
+        # ticket will annul an 'closed' entry). This will be useful to
+        # easily prevent repeated statistics.
+        if ($annul) {
+            db_query('UPDATE '.TICKET_EVENT_TABLE.' SET annulled=1'
+                .' WHERE ticket_id='.db_input($this->getId())
+                  .' AND state='.db_input($annul));
+        }
 
         return db_query('INSERT INTO '.TICKET_EVENT_TABLE
             .' SET ticket_id='.db_input($this->getId())
diff --git a/setup/inc/sql/abe9c0cb-3c2e0f54.patch.sql b/setup/inc/sql/abe9c0cb-3c2e0f54.patch.sql
new file mode 100644
index 000000000..8594a47ac
--- /dev/null
+++ b/setup/inc/sql/abe9c0cb-3c2e0f54.patch.sql
@@ -0,0 +1,14 @@
+/**
+ * Add an 'annulled' column to the %ticket_event table to assist in tracking
+ * real statistics for reopened and closed tickets -- the events should not
+ * count more than one time.
+ *
+ * @version 1.7-dpr3 ticket-event-annul
+ */
+
+ALTER TABLE `%TABLE_PREFIX%ticket_event`
+    ADD `annulled` tinyint(1) NOT NULL DEFAULT '0' AFTER `timestamp`;
+
+-- Finished with patch
+UPDATE `%TABLE_PREFIX%config`
+    SET `schema_signature`='3c2e0f54d9e08bad38028cd42767c7c9';
diff --git a/setup/inc/sql/osticket-v1.7-mysql.sql b/setup/inc/sql/osticket-v1.7-mysql.sql
index fbd2059ba..625342f65 100644
--- a/setup/inc/sql/osticket-v1.7-mysql.sql
+++ b/setup/inc/sql/osticket-v1.7-mysql.sql
@@ -616,6 +616,7 @@ CREATE TABLE `%TABLE_PREFIX%ticket_event` (
   `state` enum('created','closed','reopened','assigned','transferred','overdue') NOT NULL,
   `staff` varchar(255) NOT NULL default 'SYSTEM',
   `timestamp` datetime NOT NULL,
+  `annulled` tinyint(1) unsigned NOT NULL defalt '0',
   KEY `ticket_state` (`ticket_id`, `state`, `timestamp`),
   KEY `ticket_stats` (`timestamp`, `state`)
 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
diff --git a/setup/inc/sql/osticket-v1.7-mysql.sql.md5 b/setup/inc/sql/osticket-v1.7-mysql.sql.md5
index 23011d006..fb57be3ed 100644
--- a/setup/inc/sql/osticket-v1.7-mysql.sql.md5
+++ b/setup/inc/sql/osticket-v1.7-mysql.sql.md5
@@ -1 +1 @@
-abe9c0cb845be52c10fcd7b3e626a589
+3c2e0f54d9e08bad38028cd42767c7c9
-- 
GitLab