[Rt-commit] rt branch, 4.4/project-time-worked, created. rt-4.4.1-310-g06d81a1

Dave Goehrig dave at bestpractical.com
Thu Feb 23 12:30:45 EST 2017


The branch, 4.4/project-time-worked has been created
        at  06d81a174ad6decdab839269d95ba0680bc04749 (commit)

- Log -----------------------------------------------------------------
commit 06d81a174ad6decdab839269d95ba0680bc04749
Author: Dave Goehrig <dave at bestpractical.com>
Date:   Thu Feb 23 11:51:47 2017 -0500

    adding project time worked
    
    This adds a new recursive project time worked function to Ticket.pm
    and adds a COLUMN_MAP entry so that it can be displayed in reports.
    
    The code is optional and requires setting a $DisplayProjectTimeWorked
    in RT_SiteConfig.pm.  Also before creating tickets using this one needs
    to disable the  'On TimeWorked Change Update Parent TimeWorked' scrip
    which mutates the value of time worked on the parent tickets.
    
    It should be noted running this on an existing database will report
    invalid results for all tickets where that scrip was active as it will
    double count all of the child time worked in the parent tickets.
    
    Fixes: I#181638

diff --git a/lib/RT/Ticket.pm b/lib/RT/Ticket.pm
index f2581de..fe4c038 100644
--- a/lib/RT/Ticket.pm
+++ b/lib/RT/Ticket.pm
@@ -1411,8 +1411,34 @@ sub TimeEstimatedAsString {
     return $self->_DurationAsString( $self->TimeEstimated );
 }
 
+=head2 ProjectTimeWorked
 
+Returns the amount of time worked on this ticket and all child tickets
 
+=cut
+
+sub ProjectTimeWorked {
+    my $self = shift;
+    my $time = $self->TimeWorked;
+    my $links = $self->Members;
+    LINK: while (my $link = $links->Next) {
+        my $obj = $link->BaseObj;
+        next LINK unless $obj->isa('RT::Ticket') || $link->Type ne 'MemberOf' || $obj->id == $self->id;
+        $time += $obj->ProjectTimeWorked;
+    }
+    return $time;
+}
+
+=head2 ProjectTimeWorkedAsString
+
+Returns the amount of time worked on this ticket and all it's children as a formatted duration string
+
+=cut
+
+sub ProjectTimeWorkedAsString {
+    my $self = shift;
+    return $self->_DurationAsString( $self->ProjectTimeWorked );
+}
 
 =head2 Comment
 
diff --git a/share/html/Elements/RT__Ticket/ColumnMap b/share/html/Elements/RT__Ticket/ColumnMap
index 2db1149..8001d5f 100644
--- a/share/html/Elements/RT__Ticket/ColumnMap
+++ b/share/html/Elements/RT__Ticket/ColumnMap
@@ -332,7 +332,9 @@ $COLUMN_MAP = {
             return \($m->scomp("/Ticket/Elements/PopupTimerLink", id => $_[0]->id ) );
         },
     },
+
 };
+
 </%ONCE>
 <%init>
 # if no encryption support, then KeyOwnerName and KeyRequestors fall back to the regular
@@ -342,6 +344,16 @@ unless (RT->Config->Get('Crypt')->{'Enable'}) {
     $COLUMN_MAP->{KeyRequestors} = $GenericMap->{Requestors};
 }
 
+if(RT->Config->Get('DisplayProjectTimeWorked')) {
+  $COLUMN_MAP->{ProjectTimeWorked} = {
+        attribute => 'ProjectTimeWorked',
+        title => 'Project Time Worked',
+        value => sub {
+            return $_[0]->ProjectTimeWorkedAsString;
+        },
+    }
+}
+
 $m->callback( GenericMap => $GenericMap, COLUMN_MAP => $COLUMN_MAP, CallbackName => 'Once', CallbackOnce => 1 );
 return GetColumnMapEntry( Map => $COLUMN_MAP, Name => $Name, Attribute => $Attr );
 </%init>
diff --git a/share/html/Search/Elements/BuildFormatString b/share/html/Search/Elements/BuildFormatString
index 1bf71b1..bc86080 100644
--- a/share/html/Search/Elements/BuildFormatString
+++ b/share/html/Search/Elements/BuildFormatString
@@ -101,6 +101,9 @@ my @fields = qw(
     NBSP
 ); # loc_qw
 
+# Project time worked is an optional ColumnMap enabled for rolling up child timeworked
+push @fields, 'ProjectTimeWorked' if (RT->Config->Get('DisplayProjectTimeWorked'));
+
 my $CustomFields = RT::CustomFields->new( $session{'CurrentUser'});
 foreach my $id (keys %queues) {
     # Gotta load up the $queue object, since queues get stored by name now.
diff --git a/share/html/Ticket/Elements/ShowBasics b/share/html/Ticket/Elements/ShowBasics
index 0a06f02..71db661 100644
--- a/share/html/Ticket/Elements/ShowBasics
+++ b/share/html/Ticket/Elements/ShowBasics
@@ -74,6 +74,12 @@
     <td class="value"><& ShowTime, minutes => $Ticket->TimeWorked &></td>
   </tr>
 % }
+% if (RT->Config->Get('DisplayProjectTimeWorked') && $Ticket->ProjectTimeWorked) {
+  <tr class="project time worked sum">
+    <td class="label"><&|/l&>Project Time Worked</&>:</td>
+    <td class="value"><& ShowTime, minutes => $Ticket->ProjectTimeWorked &></td>
+  </tr>
+% }
 % if ( keys %$time_worked ) {
 <tr class="time worked by-user">
   <td class="label"><&|/l&>Users</&>:</td>
diff --git a/t/api/ticket.t b/t/api/ticket.t
index c5f1e24..b7aebd0 100644
--- a/t/api/ticket.t
+++ b/t/api/ticket.t
@@ -330,4 +330,70 @@ diag("Test ticket types with different cases");
     is($t->Type, "approval", "Approvals, the third and final internal type, are also lc'd during Create");
 }
 
+diag("Test tickets project time worked");
+{
+    my $scrip = RT::Scrip->new(RT->SystemUser);
+    $scrip->LoadByCols(Description => 'On TimeWorked Change Update Parent TimeWorked');
+    $scrip->SetDisabled(1);
+
+    my $t1 = RT::Ticket->new(RT->SystemUser);
+    my ($ok1) = $t1->Create(
+        Queue => 'general',
+        Subject => 'project time worked test parent',
+    );
+
+    my $t2 = RT::Ticket->new(RT->SystemUser);
+    my ($ok2) = $t2->Create(
+        Queue => 'general',
+        Subject => 'project time worked test child',
+    );
+
+    my $t3 = RT::Ticket->new(RT->SystemUser);
+    my ($ok3) = $t3->Create(
+        Queue => 'general',
+        Subject => 'project time worked test child child',
+    );
+
+    my ($status1,$msg1) = $t1->AddLink(
+        Type => 'MemberOf',
+        Base => $t2->id,
+    );
+
+    my ($status2,$msg2) = $t2->AddLink(
+        Type => 'MemberOf',
+        Base => $t3->id,
+    );
+
+    my $t4 = RT::Ticket->new(RT->SystemUser);
+    my ($ok4) = $t4->Create(
+        Queue => 'general',
+        Subject => 'project time worked test other child child',
+    );
+
+    my ($status4,$msg4) = $t2->AddLink(
+        Type => 'MemberOf',
+        Base => $t4->id,
+    );
+
+    $t1->SetTimeWorked(10);
+    $t2->SetTimeWorked(20);
+    $t3->SetTimeWorked(40);
+    $t4->SetTimeWorked(50);
+
+    is $t4->TimeWorked, 50, 'set other child child time worked';
+    is $t4->TimeWorked, 50, 'check other child child time worked';
+    is $t3->TimeWorked, 40, 'check child child time worked';
+    is $t2->TimeWorked, 20, 'check child time worked';
+    is $t1->TimeWorked, 10, 'check parent time worked';
+
+    is $t1->ProjectTimeWorked, 120, 'check parent project time worked';
+    is $t2->ProjectTimeWorked, 110, 'check child project time worked';
+    is $t3->ProjectTimeWorked, 40, 'check child child project time worked';
+    is $t4->ProjectTimeWorked, 50, 'check other child child project time worked';
+
+    is $t1->ProjectTimeWorkedAsString, '2 hours (120 minutes)', 'check parent project time worked as string';
+    is $t3->ProjectTimeWorkedAsString, '40 minutes', 'check child child project time workd as string';
+}
+
+
 done_testing;

-----------------------------------------------------------------------


More information about the rt-commit mailing list