Added Weekdays-Plugin. Not tested yet..
[onis.git] / lib / Onis / Plugins / Weekdays.pm
1 package Onis::Plugins::Weekdays;
2
3 use strict;
4 use warnings;
5
6 use Onis::Config (qw(get_config));
7 use Onis::Html (qw(get_filehandle));
8 use Onis::Language (qw(translate));
9 use Onis::Data::Core (qw(register_plugin get_main_nick nick_to_ident nick_to_name));
10 use Onis::Data::Persistent ();
11
12 register_plugin ('TEXT', \&add);
13 register_plugin ('ACTION', \&add);
14 register_plugin ('OUTPUT', \&output);
15
16 our $WeekdayCache = Onis::Data::Persistent->new ('WeekdayCache', 'nick',
17 qw(
18         sun0 sun1 sun2 sun3
19         mon0 mon1 mon2 mon3
20         tue0 tue1 tue2 tue3
21         wed0 wed1 wed2 wed3
22         thu0 thu1 thu2 thu3
23         fri0 fri1 fri2 fri3
24         sat0 sat1 sat2 sat3
25 ));
26 our $WeekdayData = {};
27 our @Weekdays = (qw(sun mon tue wed thu fri sat));
28
29 our $BarHeight = 130;
30 if (get_config ('bar_height'))
31 {
32         my $tmp = get_config ('bar_height');
33         $tmp =~ s/\D//g;
34         $BarHeight = $tmp if ($tmp >= 10);
35 }
36
37 our @VImages = get_config ('vertical_images');
38 if (scalar (@VImages) != 4)
39 {
40         @VImages = qw#images/ver0n.png images/ver1n.png images/ver2n.png images/ver3n.png#;
41 }
42
43 my $VERSION = '$Id$';
44 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
45
46 return (1);
47
48 sub add
49 {
50         my $data = shift;
51         my $nick = $data->{'nick'};
52         my $time = $data->{'epoch'};
53         my $hour = int ($data->{'hour'} / 6);
54         my $chars = length ($data->{'text'});
55         my $day   = (localtime ($time))[6];
56         my $index = ($day * 4) + $hour;
57
58         my @data = $WeekdayCache->get ($nick) || (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));
59         $data[$index] += $chars;
60         $WeekdayCache->put ($nick, @data);
61         
62         @data = $WeekdayCache->get ('<TOTAL>') || (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));
63         $data[$index] += $chars;
64         $WeekdayCache->put ('<TOTAL>', @data);
65 }
66
67 sub calculate
68 {
69         for ($WeekdayCache->keys ())
70         {
71                 my $nick = $_;
72                 my $main = $nick eq '<TOTAL>' ? '<TOTAL>' : get_main_nick ($nick);
73                 my @data = $WeekdayCache->get ($nick);
74
75                 if (!defined ($WeekdayData->{$main}))
76                 {
77                         $WeekdayData->{$main} =
78                         {
79                                 sun => [0, 0, 0, 0],
80                                 mon => [0, 0, 0, 0],
81                                 tue => [0, 0, 0, 0],
82                                 wed => [0, 0, 0, 0],
83                                 thu => [0, 0, 0, 0],
84                                 fri => [0, 0, 0, 0],
85                                 sat => [0, 0, 0, 0]
86                         };
87                 }
88
89                 for (my $i = 0; $i < 7; $i++)
90                 {
91                         my $day = $Weekdays[$i];
92                         for (my $j = 0; $j < 4; $j++)
93                         {
94                                 my $idx = ($i * 4) + $j;
95                                 $WeekdayData->{$main}{$day}[$j] += $data[$idx];
96                         }
97                 }
98         }
99 }
100
101 sub output
102 {
103         calculate ();
104         return (undef) unless (%$WeekdayData);
105
106         my @order =
107         (
108                 [1, 'mon', 'Monday'],
109                 [2, 'tue', 'Tuesday'],
110                 [3, 'wed', 'Wednesday'],
111                 [4, 'thu', 'Thursday'],
112                 [5, 'fri', 'Friday'],
113                 [6, 'sat', 'Saturday'],
114                 [0, 'sun', 'Sunday']
115         );
116
117         my $data = $WeekdayData->{'<TOTAL>'};
118
119         my $fh = get_filehandle ();
120         
121         my $max = 0;
122         my $bar_factor = 0;
123
124         for (@order)
125         {
126                 my ($num, $abbr, $name) = @$_;
127                 my $sum = $data->{$abbr}[0] + $data->{$abbr}[1] + $data->{$abbr}[2] + $data->{$abbr}[3];
128
129                 $max = $sum if ($max < $sum);
130         }
131         
132         $bar_factor = $BarHeight / $max;
133         
134         print $fh qq#<table class="plugin weekdays">\n  <tr class="bars">\n#;
135         for (@order)
136         {
137                 my ($num, $abbr, $name) = @$_;
138                 my $sum = $data->{$abbr}[0] + $data->{$abbr}[1] + $data->{$abbr}[2] + $data->{$abbr}[3];
139
140                 print $fh qq#    <td class="bar $abbr">$sum<br />\n      #;
141                 for (my $i = 0; $i < 4; $i++)
142                 {
143                         my $num = $data->{$abbr}[$i];
144                         my $height = int (0.5 + $num * $bar_factor) || 1;
145                         my $img = $VImages[$i];
146                         
147                         print $fh qq(<img src="$img" alt="" style="height: ${height}px;" />);
148                 }
149                 print $fh "\n    </td>\n";
150         }
151         print $fh qq(  </tr>\n  <tr class="numeration">\n);
152         for (@order)
153         {
154                 my ($num, $abbr, $name) = @$_;
155                 print $fh qq(    <td class="numeration $abbr">$name</td>\n);
156         }
157         print $fh "  </tr>\n</table>\n\n";
158 }