From 49e1d225aa9fc70af41acb4916fad97cc8de101c Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Thu, 28 Jun 2018 06:09:46 +0000
Subject: [PATCH] Ticket Reopen Count

Add ticket reopen count as a column annotation
---
 include/class.export.php |  4 ++++
 include/class.queue.php  | 31 +++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/include/class.export.php b/include/class.export.php
index 1cf4d9bf4..d62d44607 100644
--- a/include/class.export.php
+++ b/include/class.export.php
@@ -74,6 +74,10 @@ class Export {
                     ->filter(array('ticket__ticket_id' => new SqlField('ticket_id', 1)))
                     ->filter(array('entries__attachments__inline' => 0))
                     ->aggregate(array('count' => SqlAggregate::COUNT('entries__attachments__id'))),
+                'reopen_count' => TicketThread::objects()
+                    ->filter(array('ticket__ticket_id' => new SqlField('ticket_id', 1)))
+                    ->filter(array('events__annulled' => 0, 'events__state' => 'reopened'))
+                    ->aggregate(array('count' => SqlAggregate::COUNT('events__id'))),
                 'thread_count' => TicketThread::objects()
                     ->filter(array('ticket__ticket_id' => new SqlField('ticket_id', 1)))
                     ->exclude(array('entries__flags__hasbit' => ThreadEntry::FLAG_HIDDEN))
diff --git a/include/class.queue.php b/include/class.queue.php
index a37accc7a..338b582bb 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -1437,6 +1437,37 @@ extends QueueColumnAnnotation {
     }
 }
 
+class TicketReopenCount
+extends QueueColumnAnnotation {
+    static $icon = 'folder-open-alt';
+    static $qname = '_reopen_count';
+    static $desc = /* @trans */ 'Reopen Count';
+
+    function annotate($query) {
+        return $query->annotate(array(
+        static::$qname => TicketThread::objects()
+            ->filter(array('ticket__ticket_id' => new SqlField('ticket_id', 1)))
+            ->filter(array('events__annulled' => 0, 'events__state' => 'reopened'))
+            ->aggregate(array('count' => SqlAggregate::COUNT('events__id')))
+        ));
+    }
+
+    function getDecoration($row, $text) {
+        $reopencount = $row[static::$qname];
+        if ($reopencount) {
+            return sprintf(
+                '&nbsp;<small class="faded-more"><i class="icon-%s"></i> %s</small>',
+                static::$icon,
+                $reopencount > 1 ? $reopencount : ''
+            );
+        }
+    }
+
+    function isVisible($row) {
+        return $row[static::$qname];
+    }
+}
+
 class ThreadAttachmentCount
 extends QueueColumnAnnotation {
     static $icon = 'paperclip';
-- 
GitLab