Software Freedom Law Center

root/trunk/antimatter/tim/Modules/SFLC/TimeTracker/Question/Category.pm

Revision 53, 4.2 kB (checked in by bkuhn, 10 months ago)
  • Added SFLC's internally developed tim bot released under AGPLv3
Line 
1 # Copyright (C) 2005   Software Freedom Law Center, Inc.
2 #  Author: Bradley M. Kuhn <bkuhn@softwarefreedom.org>
3 #
4 #  This software gives you freedom; it is licensed to you under version
5 #  3 of the GNU Affero General Public License.
6 #
7 #  This software is distributed WITHOUT ANY WARRANTY, without even the
8 #  implied warranties of MERCHANTABILITY and FITNESS FOR A PARTICULAR
9 #  PURPOSE.  See the GNU Affero General Public License for further
10 #  details.
11 #
12 # You should have received a copy of the GNU Affero General Public
13 # License, version 3 along with this software.  If not, see
14 # <http://www.gnu.org/licenses/>.
15 package SFLC::TimeTracker::Question::Category;
16
17 use strict;
18 use warnings;
19
20
21 require Exporter;
22 #use AutoLoader qw(AUTOLOAD);
23
24 use base qw(SFLC::TimeTracker::Question Exporter);
25
26
27 our @EXPORT_OK = ();
28
29 our @EXPORT = qw( );
30
31 our $VERSION = '0.01';
32
33 use Carp;
34 our $FREE_FORM_STRING = "__FREEFORM__";
35 our $REJECT_STRING = "__REJECT__";
36 our $EXISTING_STRING = "__EXISTING__";
37
38 sub new {
39   my $class = shift;
40   my $self = SFLC::TimeTracker::Question::new($class, @_);
41   my %args = @_;
42   $self->{originalCategory} = $args{category} if defined $args{category};
43   $self->{resolutionData} = {};
44   $self->{resolutionData}{answerType} = 'loopback';
45   return $self;
46 }
47 sub setResolutionData {
48   my $self = shift;
49
50   $self->{resolutionData} = shift;
51 }
52 sub getResolutionData {
53   my $self = shift;
54   return %{$self->{resolutionData}};
55 }
56 ###############################################################################
57 sub resolve ($$) {
58   my($self, $answer) = @_;
59
60   my $retVal = undef;
61
62   my $number = $self->validateAnswer($answer);
63   return "INVALID" unless defined $number;
64   my $categoryID;
65   my $rejected = 0;
66   if ($self->{answers}{$number} eq $FREE_FORM_STRING) {
67     $answer =~ s/^\s*$number\s+(.*)\s*$/$1/;
68     $categoryID = $1;
69     return "INVALID" unless $categoryID =~ /^\/([^\/\s]+\/)*[^\/\s]+$/;
70     my $category = new SFLC::TimeTracker::Category(name => $categoryID);
71     $categoryID = $category->get('id');
72     $self->getDatabase()->setCategoryAlias($self->{userHandle},
73              $self->{originalCategory}, $category)
74       if defined $self->{originalCategory};
75     print "Aliased \"$self->{originalCategory}\" to \"$categoryID\" if defined\n"
76   } elsif ($self->{answers}{$number} eq $EXISTING_STRING) {
77     $answer =~ /^\s*$number\s+(.*)\s*$/;
78     my $categoryCandidate = $1;
79     print "doing Existing! $answer$categoryCandidate\n";
80     return "INVALID" unless $categoryCandidate =~ /^\/([^\/\s]+\/)*[^\/\s]+$/;
81     $categoryID = $self->getDatabase()->categoryAliasLookup($self->{userHandle},
82                                                             $categoryCandidate);
83     if (not defined $categoryID) {
84       return "UNKNOWN";
85     }
86   } elsif ($self->{answers}{$number} eq $REJECT_STRING) {
87     $rejected = 1;
88   } else {
89     $categoryID = $self->{answers}{$number};
90   }
91   if ($rejected) {
92     $self->setResolutionData({ answerType => 'abort'});
93     return "REJECTED";
94   } else {
95     $self->getDatabase()->setCategoryAlias($self->{userHandle},
96              $self->{originalCategory}, $categoryID)
97       if defined $self->{originalCategory};
98     print "Aliased \"$self->{originalCategory}\" to \"$categoryID\" if defined\n"
99   }
100   $self->setResolutionData({ answerType => 'category',
101                              category => $categoryID});
102   return "RESOLVED";
103 }
104 ###############################################################################
105 sub freeFormReplacement {
106   my($self, $ans) = @_;
107
108   return "Enter your own new category by typing \"$ans /NEW/CAT\"";
109
110 }
111
112 sub rejectReplacement {
113   my($self, $ans) = @_;
114
115   return "Abort this entry";
116
117 }
118 sub existingReplacement {
119   my($self, $ans) = @_;
120
121   return "Enter an known, existing category not listed here";
122
123 }
124 sub formatResolveString ($$) {
125   my($self, $string) = @_;
126
127   my $superAnswer = $self->SUPER::formatResolveString($string);
128   if (not defined $superAnswer) {
129       return "Aborted." if $string eq "REJECTED";
130       return "There is no known, existing category by that name." if $string eq "UNKNOWN";
131       return "Ok, I am truly confused.  I got the answer, $string.";
132     } else {
133       return $superAnswer;
134     }
135 }
136
137 1;
138 __END__
139
140 #
141 # Local variables:
142 # compile-command: "perl -I ../../../../Modules -c Category.pm"
143 # End:
Note: See TracBrowser for help on using the browser.

SFLC Main Page

[frdm] Support SFLC