Added POD to Onis::Plugins::Weekdays
[onis.git] / lib / Onis / Plugins / Weekdays.pm
1 package Onis::Plugins::Weekdays;
2
3 use strict;
4 use warnings;
5
6 use Exporter;
7
8 use Onis::Config (qw(get_config));
9 use Onis::Html (qw(get_filehandle));
10 use Onis::Language (qw(translate));
11 use Onis::Data::Core (qw(register_plugin get_main_nick nick_to_ident nick_to_name));
12 use Onis::Data::Persistent ();
13
14 =head1 NAME
15
16 Onis::Plugins::Weekdays - Activity based on weekdays
17
18 =cut
19
20 @Onis::Plugins::Weekdays::EXPORT_OK = (qw(get_weekdays));
21 @Onis::Plugins::Weekdays::ISA = ('Exporter');
22
23 register_plugin ('TEXT', \&add);
24 register_plugin ('ACTION', \&add);
25 register_plugin ('OUTPUT', \&output);
26
27 our $WeekdayCache = Onis::Data::Persistent->new ('WeekdayCache', 'nick',
28 qw(
29         sun0 sun1 sun2 sun3
30         mon0 mon1 mon2 mon3
31         tue0 tue1 tue2 tue3
32         wed0 wed1 wed2 wed3
33         thu0 thu1 thu2 thu3
34         fri0 fri1 fri2 fri3
35         sat0 sat1 sat2 sat3
36 ));
37 our $WeekdayData = {};
38 our @Weekdays = (qw(sun mon tue wed thu fri sat));
39
40 =head1 CONFIGURATION OPTIONS
41
42 =over 4
43
44 =item B<vertical_images>: I<image0>, I<image1>, I<image2>, I<image3>;
45
46 Sets the images used for vertical bars.
47
48 =cut
49
50 our @VImages = get_config ('vertical_images');
51 if (scalar (@VImages) != 4)
52 {
53         @VImages = qw#images/ver0n.png images/ver1n.png images/ver2n.png images/ver3n.png#;
54 }
55
56 =back
57
58 =cut
59
60 my $VERSION = '$Id$';
61 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
62
63 return (1);
64
65 sub add
66 {
67         my $data = shift;
68         my $nick = $data->{'nick'};
69         my $time = $data->{'epoch'};
70         my $hour = int ($data->{'hour'} / 6);
71         my $chars = length ($data->{'text'});
72         my $day   = (localtime ($time))[6];
73         my $index = ($day * 4) + $hour;
74
75         my @data = $WeekdayCache->get ($nick);
76         @data = (qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)) unless (@data);
77         $data[$index] += $chars;
78         $WeekdayCache->put ($nick, @data);
79         
80         @data = $WeekdayCache->get ('<TOTAL>');
81         @data = (qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)) unless (@data);
82         $data[$index] += $chars;
83         $WeekdayCache->put ('<TOTAL>', @data);
84 }
85
86 sub calculate
87 {
88         for ($WeekdayCache->keys ())
89         {
90                 my $nick = $_;
91                 my $main = $nick eq '<TOTAL>' ? '<TOTAL>' : get_main_nick ($nick);
92                 my @data = $WeekdayCache->get ($nick);
93
94                 if (!defined ($WeekdayData->{$main}))
95                 {
96                         $WeekdayData->{$main} =
97                         {
98                                 sun => [0, 0, 0, 0],
99                                 mon => [0, 0, 0, 0],
100                                 tue => [0, 0, 0, 0],
101                                 wed => [0, 0, 0, 0],
102                                 thu => [0, 0, 0, 0],
103                                 fri => [0, 0, 0, 0],
104                                 sat => [0, 0, 0, 0]
105                         };
106                 }
107
108                 for (my $i = 0; $i < 7; $i++)
109                 {
110                         my $day = $Weekdays[$i];
111                         for (my $j = 0; $j < 4; $j++)
112                         {
113                                 my $idx = ($i * 4) + $j;
114                                 $WeekdayData->{$main}{$day}[$j] += $data[$idx];
115                         }
116                 }
117         }
118 }
119
120 sub output
121 {
122         calculate ();
123         return (undef) unless (%$WeekdayData);
124
125         my @order =
126         (
127                 [1, 'mon', 'Monday'],
128                 [2, 'tue', 'Tuesday'],
129                 [3, 'wed', 'Wednesday'],
130                 [4, 'thu', 'Thursday'],
131                 [5, 'fri', 'Friday'],
132                 [6, 'sat', 'Saturday'],
133                 [0, 'sun', 'Sunday']
134         );
135
136         my $data = $WeekdayData->{'<TOTAL>'};
137
138         my $fh = get_filehandle ();
139         
140         my $max = 0;
141         my $total = 0;
142
143         for (@order)
144         {
145                 my ($num, $abbr, $name) = @$_;
146
147                 for (my $i = 0; $i < 4; $i++)
148                 {
149                         $max = $data->{$abbr}[$i] if ($max < $data->{$abbr}[$i]);
150                         $total += $data->{$abbr}[$i];
151                 }
152         }
153         
154         print $fh qq#<table class="plugin weekdays">\n  <tr class="bars">\n#;
155         for (@order)
156         {
157                 my ($num, $abbr, $name) = @$_;
158                 for (my $i = 0; $i < 4; $i++)
159                 {
160                         my $num = $data->{$abbr}[$i];
161                         my $height = sprintf ("%.2f", (95 * $num / $max));
162                         my $img = $VImages[$i];
163
164                         print $fh qq#    <td class="bar vertical $abbr">#,
165                         qq(<img src="$img" alt="" class="first last" style="height: ${height}%;" /></td>\n);
166                 }
167         }
168         print $fh qq(  </tr>\n  <tr class="counter">\n);
169         for (@order)
170         {
171                 my ($num, $abbr, $name) = @$_;
172                 my $sum = $data->{$abbr}[0] + $data->{$abbr}[1] + $data->{$abbr}[2] + $data->{$abbr}[3];
173                 my $pct = sprintf ("%.1f", (100 * $sum / $total));
174                 print $fh qq(    <td colspan="4" class="counter $abbr">$pct%</td>\n);
175         }
176         print $fh qq(  </tr>\n  <tr class="numeration">\n);
177         for (@order)
178         {
179                 my ($num, $abbr, $name) = @$_;
180                 print $fh qq(    <td colspan="4" class="numeration $abbr">$name</td>\n);
181         }
182         print $fh "  </tr>\n</table>\n\n";
183 }
184
185 =head1 EXPORTED FUNCTIONS
186
187 =over 4
188
189 =item B<get_weekdays> (I<$nick>)
190
191 Returns a hashref with the weekday information for I<$nick>. Numbers are
192 character counters. The returned data has the following format:
193
194   {
195     sun => [0, 0, 0, 0],
196     mon => [0, 0, 0, 0],
197     tue => [0, 0, 0, 0],
198     wed => [0, 0, 0, 0],
199     thu => [0, 0, 0, 0],
200     fri => [0, 0, 0, 0],
201     sat => [0, 0, 0, 0]
202   }
203
204 =cut
205
206 sub get_weekdays
207 {
208         my $nick = shift;
209
210         if (!defined ($WeekdayData->{$nick}))
211         {
212                 return ({});
213         }
214
215         return ($WeekdayData->{$nick});
216 }
217
218 =back
219
220 =head1 AUTHOR
221
222 Florian octo Forster E<lt>octo at verplant.orgE<gt>
223
224 =cut