import rt 3.2.2
[freeside.git] / rt / lib / RT / Action / EscalatePriority.pm
1 # {{{ BEGIN BPS TAGGED BLOCK
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2004 Best Practical Solutions, LLC 
6 #                                          <jesse@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., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27
28 # CONTRIBUTION SUBMISSION POLICY:
29
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
35
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
44
45 # }}} END BPS TAGGED BLOCK
46 =head1 NAME
47
48   RT::Action::EscalatePriority
49
50 =head1 DESCRIPTION
51
52 EscalatePriority is a ScripAction which is NOT intended to be called
53 per transaction. It's intended to be called by an RT escalation tool.
54 One such tool is called rt-crontool and is located in $RTHOME/bin (see
55 C<rt-crontool -h> for more details)
56
57 EsclatePriority uses the following formula to change a ticket's priority:
58
59     Priority = Priority +  (( FinalPriority - Priority ) / ( DueDate-Today))
60
61 Unless the duedate is past, in which case priority gets bumped straight
62 to final priority.
63
64 In this way, priority is either increased or decreased toward the final priority
65 as the ticket heads toward its due date.
66
67
68 =cut
69
70
71 package RT::Action::EscalatePriority;
72 require RT::Action::Generic;
73
74 use strict;
75 use vars qw/@ISA/;
76 @ISA=qw(RT::Action::Generic);
77
78 #Do what we need to do and send it out.
79
80 #What does this type of Action does
81
82 # {{{ sub Describe 
83 sub Describe  {
84   my $self = shift;
85   return (ref $self . " will move a ticket's priority toward its final priority.");
86 }
87 # }}}
88         
89
90 # {{{ sub Prepare 
91 sub Prepare  {
92     my $self = shift;
93     
94     if ($self->TicketObj->Priority() == $self->TicketObj->FinalPriority()) {
95         # no update necessary.
96         return 0;
97     }
98    
99     #compute the number of days until the ticket is due
100     my $due = $self->TicketObj->DueObj();
101     
102
103     # If we don't have a due date, adjust the priority by one
104     # until we hit the final priority
105     if ($due->Unix() < 1) {
106         if ( $self->TicketObj->Priority > $self->TicketObj->FinalPriority ){
107             $self->{'prio'} = ($self->TicketObj->Priority - 1);
108             return 1;
109         }
110         elsif ( $self->TicketObj->Priority < $self->TicketObj->FinalPriority ){
111             $self->{'prio'} = ($self->TicketObj->Priority + 1);
112             return 1;
113         }
114         # otherwise the priority is at the final priority. we don't need to
115         # Continue
116         else {
117             return 0;
118         }
119     }
120
121     # we've got a due date. now there are other things we should do
122     else { 
123         my $diff_in_seconds = $due->Diff(time());    
124         my $diff_in_days = int( $diff_in_seconds / 86400);    
125         
126         #if we haven't hit the due date yet
127         if ($diff_in_days > 0 ) {       
128             
129             # compute the difference between the current priority and the
130             # final priority
131             
132             my $prio_delta = 
133               $self->TicketObj->FinalPriority() - $self->TicketObj->Priority;
134             
135             my $inc_priority_by = int( $prio_delta / $diff_in_days );
136             
137             #set the ticket's priority to that amount
138             $self->{'prio'} = $self->TicketObj->Priority + $inc_priority_by;
139             
140         }
141         #if $days is less than 1, set priority to final_priority
142         else {  
143             $self->{'prio'} = $self->TicketObj->FinalPriority();
144         }
145
146     }
147     return 1;
148 }
149 # }}}
150
151 sub Commit {
152     my $self = shift;
153    my ($val, $msg) = $self->TicketObj->SetPriority($self->{'prio'});
154
155    unless ($val) {
156         $RT::Logger->debug($self . " $msg\n"); 
157    }
158 }
159
160 eval "require RT::Action::EscalatePriority_Vendor";
161 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Action/EscalatePriority_Vendor.pm});
162 eval "require RT::Action::EscalatePriority_Local";
163 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Action/EscalatePriority_Local.pm});
164
165 1;