File Coverage

blib/lib/Workflow/History.pm
Criterion Covered Total %
statement 34 34 100.0
branch 7 8 87.5
condition n/a
subroutine 10 10 100.0
pod 5 5 100.0
total 56 57 98.2


line stmt bran cond sub pod time code
1             package Workflow::History;
2              
3 21     21   2848766 use warnings;
  21         64  
  21         1566  
4 21     21   140 use strict;
  21         42  
  21         838  
5 21     21   328 use v5.14.0;
  21         77  
6 21     21   143 use parent qw( Class::Accessor );
  21         46  
  21         181  
7 21     21   13465 use DateTime;
  21         703282  
  21         12479  
8              
9             $Workflow::History::VERSION = '2.09';
10              
11             my @FIELDS
12             = qw( id workflow_id action description date user state time_zone );
13             __PACKAGE__->mk_accessors(@FIELDS);
14              
15             sub new {
16 46     46 1 4917 my ( $class, $params ) = @_;
17 46         218 my $self = bless { _saved => 0 }, $class;
18 46         152 for (@FIELDS) {
19 368 100       4710 $self->$_( $params->{$_} ) if ( $params->{$_} );
20             }
21              
22             my $time_zone
23 46 100       591 = exists $params->{time_zone} ? $params->{time_zone} : 'floating';
24 46         223 $self->time_zone($time_zone);
25              
26 46 100       694 unless ( $self->date ) {
27 12         362 $self->date( DateTime->now( time_zone => $self->time_zone() ) );
28             }
29 46         9089 return $self;
30             }
31              
32             sub set_new_state {
33 10     10 1 150 my ( $self, $new_state ) = @_;
34 10 50       41 unless ( $self->state ) {
35 10         136 $self->state($new_state);
36             }
37             }
38              
39             sub is_saved {
40 111     111 1 6749 my ($self) = @_;
41 111         627 return $self->{_saved};
42             }
43              
44             sub set_saved {
45 43     43 1 541 my ($self) = @_;
46 43         110 $self->{_saved} = 1;
47              
48 43         125 return 1;
49             }
50              
51             sub clear_saved {
52 1     1 1 3 my ($self) = @_;
53 1         98 $self->{_saved} = 0;
54              
55 1         11 return 0;
56             }
57              
58             1;
59              
60             __END__
61              
62             =pod
63              
64             =head1 NAME
65              
66             Workflow::History - Recorded work on a workflow action or workflow itself
67              
68             =head1 VERSION
69              
70             This documentation describes version 2.09 of this package
71              
72             =head1 SYNOPSIS
73              
74             # in your action
75             sub execute {
76             my ( $self, $wf ) = @_;
77             my $current_user = $wf->context->param( 'current_user' );
78             # ... do your work with $ticket
79             $wf->add_history(
80             {
81             action => 'create ticket',
82             user => $current_user->full_name,
83             description => "Ticket $ticket->{subject} successfully created"
84             });
85             }
86              
87             # in your view (using TT2)
88             [% FOREACH history = workflow.get_history %]
89             On: [% OI.format_date( history.date, '%Y-%m-%d %H:%M' ) %]<br>
90             Action: [% history.action %] (ID: [% history.id %])<br>
91             by: [% history.user %]<br>
92             [% history.description %]
93             [% END %]
94              
95             =head1 DESCRIPTION
96              
97             Every workflow can record its history. More appropriately, every
98             action the workflow executes can deposit history entries in the
99             workflow to be saved later. Neither the action nor the workflow knows
100             about how the history is saved, just that the history is available.
101              
102             =head1 METHODS
103              
104             =head2 Public Methods
105              
106             =head3 new( \%params )
107              
108             Create a new history object, filling it with properties from
109             C<\%params>.
110              
111             =head3 set_new_state( $new_state )
112              
113             Assigns the new state C<$new_state> to the history if the state is not
114             already assigned. This is used when you
115             L<generate a history request|Workflow/add_history> in
116             a L<Workflow::Action> since the workflow state will change once the
117             action has successfully completed. So in the action you create
118             history without the state:
119              
120             $wf->add_history(
121             {
122             action => "Cocoa Puffs",
123             description => "They're magically delicious",
124             user => "Count Chocula",
125             }
126             );
127              
128             And then after the new state has been set but before the history
129             objects are stored the workflow sets the new state in all unsaved
130             history objects.
131              
132             =head3 is_saved()
133              
134             Returns true (1) if this history object has been saved, false (0) if not.
135              
136             =head2 Properties
137              
138             =over 4
139              
140             =item *
141              
142             B<id> - ID of history entry
143              
144             =item *
145              
146             B<workflow_id> - ID of workflow to which history is attached
147              
148             =item *
149              
150             B<action> - Brief description of action taken
151              
152             =item *
153              
154             B<description> - Lengthy description of action taken
155              
156             =item *
157              
158             B<date> - Date history noted, set to a L<DateTime> object.
159              
160             =item *
161              
162             B<time_zone> - Time zone to pass to the L<DateTime> object.
163              
164             =item *
165              
166             B<user> - User name (ID, login, or full name, up to you) taking action
167             (may be blank)
168              
169             =item *
170              
171             B<state> - State of workflow as history was recorded.
172              
173             =back
174              
175             =head3 clear_saved
176              
177             Sets saved state to false and returns 0
178              
179             =head3 set_saved
180              
181             Sets saved state to true and returns 1
182              
183             =head1 SEE ALSO
184              
185             =over
186              
187             =item * L<Workflow>
188              
189             =back
190              
191             =head1 COPYRIGHT
192              
193             Copyright (c) 2003-2021 Chris Winters. All rights reserved.
194              
195             This library is free software; you can redistribute it and/or modify
196             it under the same terms as Perl itself.
197              
198             Please see the F<LICENSE>
199              
200             =head1 AUTHORS
201              
202             Please see L<Workflow>
203              
204             =cut