Upgrade to 4.2.2
[usit-rt.git] / lib / RT / Action / LinearEscalate.pm
CommitLineData
84fb5b46
MKG
1# BEGIN BPS TAGGED BLOCK {{{
2#
3# COPYRIGHT:
4#
320f0092 5# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
84fb5b46
MKG
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
51RT::Action::LinearEscalate - will move a ticket's priority toward its final priority.
52
53=head1 This vs. RT::Action::EscalatePriority
54
55This action doesn't change priority if due date is not set.
56
57This action honor the Starts date.
58
59This action can apply changes silently.
60
61This action can replace EscalatePriority completly. If you want to tickets
62that have been created without Due date then you can add scrip that sets
63default due date. For example a week then priorities of your tickets will
64escalate linearly during the week from intial value towards final.
65
66=head1 This vs. LinearEscalate from the CPAN
67
68This action is an integration of the module from the CPAN into RT's core
69that's happened in RT 3.8. If you're upgrading from 3.6 and have been using
70module from the CPAN with old version of RT then you should uninstall it
71and use this one.
72
73However, this action doesn't support control over config. Read </CONFIGURATION>
74to find out ways to deal with it.
75
76=head1 DESCRIPTION
77
78LinearEscalate is a ScripAction that will move a ticket's priority
79from its initial priority to its final priority linearly as
80the ticket approaches its due date.
81
82It's intended to be called by an RT escalation tool. One such tool is called
83rt-crontool and is located in $RTHOME/bin (see C<rt-crontool -h> for more details).
84
85=head1 USAGE
86
87Once the ScripAction is installed, the following script in "cron"
88will get tickets to where they need to be:
89
90 rt-crontool --search RT::Search::FromSQL --search-arg \
91 "(Status='new' OR Status='open' OR Status = 'stalled')" \
92 --action RT::Action::LinearEscalate
93
94The Starts date is associated with intial ticket's priority or
95the Created field if the former is not set. End of interval is
96the Due date. Tickets without due date B<are not updated>.
97
98=head1 CONFIGURATION
99
100Initial and Final priorities are controlled by queue's options
af59614d 101and can be defined using the web UI via Admin tab. This
84fb5b46
MKG
102action should handle correctly situations when initial priority
103is greater than final.
104
105LinearEscalate's behavior can be controlled by two options:
106
107=over 4
108
109=item RecordTransaction - defaults to false and if option is true then
110causes the tool to create a transaction on the ticket when it is escalated.
111
112=item UpdateLastUpdated - which defaults to true and updates the LastUpdated
113field when the ticket is escalated, otherwise don't touch anything.
114
115=back
116
117You cannot set "UpdateLastUpdated" to false unless "RecordTransaction"
118is also false. Well, you can, but we'll just ignore you.
119
120You can set this options using either in F<RT_SiteConfig.pm>, as action
121argument in call to the rt-crontool or in DB if you want to use the action
122in scrips.
123
124From a shell you can use the following command:
125
126 rt-crontool --search RT::Search::FromSQL --search-arg \
127 "(Status='new' OR Status='open' OR Status = 'stalled')" \
128 --action RT::Action::LinearEscalate \
129 --action-arg "RecordTransaction: 1"
130
131This ScripAction uses RT's internal _Set or __Set calls to set ticket
132priority without running scrips or recording a transaction on each
133update, if it's been said to.
134
135=cut
136
137package RT::Action::LinearEscalate;
138
139use strict;
140use warnings;
141use base qw(RT::Action);
142
84fb5b46
MKG
143#Do what we need to do and send it out.
144
145#What does this type of Action does
146
147sub Describe {
148 my $self = shift;
149 my $class = ref($self) || $self;
150 return "$class will move a ticket's priority toward its final priority.";
151}
152
153sub Prepare {
154 my $self = shift;
155
156 my $ticket = $self->TicketObj;
157
158 my $due = $ticket->DueObj->Unix;
159 unless ( $due > 0 ) {
160 $RT::Logger->debug('Due is not set. Not escalating.');
161 return 1;
162 }
163
164 my $priority_range = ($ticket->FinalPriority ||0) - ($ticket->InitialPriority ||0);
165 unless ( $priority_range ) {
166 $RT::Logger->debug('Final and Initial priorities are equal. Not escalating.');
167 return 1;
168 }
169
170 if ( $ticket->Priority >= $ticket->FinalPriority && $priority_range > 0 ) {
171 $RT::Logger->debug('Current priority is greater than final. Not escalating.');
172 return 1;
173 }
174 elsif ( $ticket->Priority <= $ticket->FinalPriority && $priority_range < 0 ) {
175 $RT::Logger->debug('Current priority is lower than final. Not escalating.');
176 return 1;
177 }
178
179 # TODO: compute the number of business days until the ticket is due
180
181 # now we know we have a due date. for every day that passes,
182 # increment priority according to the formula
183
184 my $starts = $ticket->StartsObj->Unix;
185 $starts = $ticket->CreatedObj->Unix unless $starts > 0;
186 my $now = time;
187
188 # do nothing if we didn't reach starts or created date
189 if ( $starts > $now ) {
190 $RT::Logger->debug('Starts(Created) is in future. Not escalating.');
191 return 1;
192 }
193
194 $due = $starts + 1 if $due <= $starts; # +1 to avoid div by zero
195
196 my $percent_complete = ($now-$starts)/($due - $starts);
197
198 my $new_priority = int($percent_complete * $priority_range) + ($ticket->InitialPriority || 0);
af59614d 199 $new_priority = $ticket->FinalPriority if $new_priority > $ticket->FinalPriority;
84fb5b46
MKG
200 $self->{'new_priority'} = $new_priority;
201
202 return 1;
203}
204
205sub Commit {
206 my $self = shift;
207
208 my $new_value = $self->{'new_priority'};
209 return 1 unless defined $new_value;
210
211 my $ticket = $self->TicketObj;
212 # if the priority hasn't changed do nothing
213 return 1 if $ticket->Priority == $new_value;
214
215 # override defaults from argument
216 my ($record, $update) = (0, 1);
217 {
218 my $arg = $self->Argument || '';
219 if ( $arg =~ /RecordTransaction:\s*(\d+)/i ) {
220 $record = $1;
221 $RT::Logger->debug("Overrode RecordTransaction: $record");
222 }
223 if ( $arg =~ /UpdateLastUpdated:\s*(\d+)/i ) {
224 $update = $1;
225 $RT::Logger->debug("Overrode UpdateLastUpdated: $update");
226 }
227 $update = 1 if $record;
228 }
229
230 $RT::Logger->debug(
231 'Linearly escalating priority of ticket #'. $ticket->Id
232 .' from '. $ticket->Priority .' to '. $new_value
233 .' and'. ($record? '': ' do not') .' record a transaction'
234 .' and'. ($update? '': ' do not') .' touch last updated field'
235 );
236
237 my ( $val, $msg );
238 unless ( $record ) {
239 unless ( $update ) {
240 ( $val, $msg ) = $ticket->__Set(
241 Field => 'Priority',
242 Value => $new_value,
243 );
244 }
245 else {
246 ( $val, $msg ) = $ticket->_Set(
247 Field => 'Priority',
248 Value => $new_value,
249 RecordTransaction => 0,
250 );
251 }
252 }
253 else {
254 ( $val, $msg ) = $ticket->SetPriority( $new_value );
255 }
256
257 unless ($val) {
258 $RT::Logger->error( "Couldn't set new priority value: $msg" );
259 return (0, $msg);
260 }
261 return 1;
262}
263
264RT::Base->_ImportOverlays();
265
2661;
267
268=head1 AUTHORS
269
270Kevin Riggle E<lt>kevinr@bestpractical.comE<gt>
271
272Ruslan Zakirov E<lt>ruz@bestpractical.comE<gt>
273
274=cut