import of rt 3.0.4
[freeside.git] / rt / lib / RT / Action / EscalatePriority.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4
5 # (Except where explictly superceded by other copyright notices)
6
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
10 # from www.gnu.org.
11
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
21
22
23 # END LICENSE BLOCK
24 =head1 NAME
25
26   RT::Action::EscalatePriority
27
28 =head1 DESCRIPTION
29
30 EscalatePriority is a ScripAction which is NOT intended to be called per 
31 transaction. It's intended to be called by an RT escalation daemon.
32 (The daemon is called escalator).
33
34 EsclatePriority uses the following formula to change a ticket's priority:
35
36     Priority = Priority +  (( FinalPriority - Priority ) / ( DueDate-Today))
37
38 Unless the duedate is past, in which case priority gets bumped straight
39 to final priority.
40
41 In this way, priority is either increased or decreased toward the final priority
42 as the ticket heads toward its due date.
43
44
45 =cut
46
47
48 package RT::Action::EscalatePriority;
49 require RT::Action::Generic;
50
51 use strict;
52 use vars qw/@ISA/;
53 @ISA=qw(RT::Action::Generic);
54
55 #Do what we need to do and send it out.
56
57 #What does this type of Action does
58
59 # {{{ sub Describe 
60 sub Describe  {
61   my $self = shift;
62   return (ref $self . " will move a ticket's priority toward its final priority.");
63 }
64 # }}}
65         
66
67 # {{{ sub Prepare 
68 sub Prepare  {
69     my $self = shift;
70     
71     if ($self->TicketObj->Priority() == $self->TicketObj->FinalPriority()) {
72         # no update necessary.
73         return 0;
74     }
75    
76     #compute the number of days until the ticket is due
77     my $due = $self->TicketObj->DueObj();
78     
79
80     # If we don't have a due date, adjust the priority by one
81     # until we hit the final priority
82     if ($due->Unix() < 1) {
83         if ( $self->TicketObj->Priority > $self->TicketObj->FinalPriority ){
84             $self->{'prio'} = ($self->TicketObj->Priority - 1);
85             return 1;
86         }
87         elsif ( $self->TicketObj->Priority < $self->TicketObj->FinalPriority ){
88             $self->{'prio'} = ($self->TicketObj->Priority + 1);
89             return 1;
90         }
91         # otherwise the priority is at the final priority. we don't need to
92         # Continue
93         else {
94             return 0;
95         }
96     }
97
98     # we've got a due date. now there are other things we should do
99     else { 
100         my $diff_in_seconds = $due->Diff(time());    
101         my $diff_in_days = int( $diff_in_seconds / 86400);    
102         
103         #if we haven't hit the due date yet
104         if ($diff_in_days > 0 ) {       
105             
106             # compute the difference between the current priority and the
107             # final priority
108             
109             my $prio_delta = 
110               $self->TicketObj->FinalPriority() - $self->TicketObj->Priority;
111             
112             my $inc_priority_by = int( $prio_delta / $diff_in_days );
113             
114             #set the ticket's priority to that amount
115             $self->{'prio'} = $self->TicketObj->Priority + $inc_priority_by;
116             
117         }
118         #if $days is less than 1, set priority to final_priority
119         else {  
120             $self->{'prio'} = $self->TicketObj->FinalPriority();
121         }
122
123     }
124     return 1;
125 }
126 # }}}
127
128 sub Commit {
129     my $self = shift;
130    my ($val, $msg) = $self->TicketObj->SetPriority($self->{'prio'});
131
132    unless ($val) {
133         $RT::Logger->debug($self . " $msg\n"); 
134    }
135 }
136
137 eval "require RT::Action::EscalatePriority_Vendor";
138 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Action/EscalatePriority_Vendor.pm});
139 eval "require RT::Action::EscalatePriority_Local";
140 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Action/EscalatePriority_Local.pm});
141
142 1;