commiting rt 3.8.9 to HEAD
[freeside.git] / rt / lib / RT / Action / EscalatePriority.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 =head1 NAME
50
51   RT::Action::EscalatePriority
52
53 =head1 DESCRIPTION
54
55 EscalatePriority is a ScripAction which is NOT intended to be called
56 per transaction. It's intended to be called by an RT escalation tool.
57 One such tool is called rt-crontool and is located in $RTHOME/bin (see
58 C<rt-crontool -h> for more details)
59
60 EsclatePriority uses the following formula to change a ticket's priority:
61
62     Priority = Priority +  (( FinalPriority - Priority ) / ( DueDate-Today))
63
64 Unless the duedate is past, in which case priority gets bumped straight
65 to final priority.
66
67 In this way, priority is either increased or decreased toward the final priority
68 as the ticket heads toward its due date.
69
70
71 =cut
72
73
74 package RT::Action::EscalatePriority;
75 use base 'RT::Action';
76
77 use strict;
78
79 #Do what we need to do and send it out.
80
81 #What does this type of Action does
82
83 # {{{ sub Describe 
84 sub Describe  {
85   my $self = shift;
86   return (ref $self . " will move a ticket's priority toward its final priority.");
87 }
88 # }}}
89         
90
91 # {{{ sub Prepare 
92 sub Prepare  {
93     my $self = shift;
94     
95     if ($self->TicketObj->Priority() == $self->TicketObj->FinalPriority()) {
96         # no update necessary.
97         return 0;
98     }
99    
100     #compute the number of days until the ticket is due
101     my $due = $self->TicketObj->DueObj();
102     
103
104     # If we don't have a due date, adjust the priority by one
105     # until we hit the final priority
106     if ($due->Unix() < 1) {
107         if ( $self->TicketObj->Priority > $self->TicketObj->FinalPriority ){
108             $self->{'prio'} = ($self->TicketObj->Priority - 1);
109             return 1;
110         }
111         elsif ( $self->TicketObj->Priority < $self->TicketObj->FinalPriority ){
112             $self->{'prio'} = ($self->TicketObj->Priority + 1);
113             return 1;
114         }
115         # otherwise the priority is at the final priority. we don't need to
116         # Continue
117         else {
118             return 0;
119         }
120     }
121
122     # we've got a due date. now there are other things we should do
123     else { 
124         my $arg = $self->Argument || '';
125         my $now = time();
126         if ( $arg =~ /CurrentTime:\s*(\d+)/i ) {
127             $now = $1;
128         } 
129         my $diff_in_seconds = $due->Diff($now);    
130         my $diff_in_days = int( $diff_in_seconds / 86400);    
131         
132         #if we haven't hit the due date yet
133         if ($diff_in_days > 0 ) {       
134             
135             # compute the difference between the current priority and the
136             # final priority
137             
138             my $prio_delta = 
139               $self->TicketObj->FinalPriority() - $self->TicketObj->Priority;
140             
141             my $inc_priority_by = int( $prio_delta / $diff_in_days );
142             
143             #set the ticket's priority to that amount
144             $self->{'prio'} = $self->TicketObj->Priority + $inc_priority_by;
145             
146         }
147         #if $days is less than 1, set priority to final_priority
148         else {  
149             $self->{'prio'} = $self->TicketObj->FinalPriority();
150         }
151
152     }
153     return 1;
154 }
155 # }}}
156
157 sub Commit {
158     my $self = shift;
159    my ($val, $msg) = $self->TicketObj->SetPriority($self->{'prio'});
160
161    unless ($val) {
162         $RT::Logger->debug($self . " $msg"); 
163    }
164 }
165
166 eval "require RT::Action::EscalatePriority_Vendor";
167 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Action/EscalatePriority_Vendor.pm});
168 eval "require RT::Action::EscalatePriority_Local";
169 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Action/EscalatePriority_Local.pm});
170
171 1;