1 package Onis::Plugins::Weekdays;
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 ();
16 Onis::Plugins::Weekdays - Activity based on weekdays
20 @Onis::Plugins::Weekdays::EXPORT_OK = (qw(get_weekdays));
21 @Onis::Plugins::Weekdays::ISA = ('Exporter');
23 register_plugin ('TEXT', \&add);
24 register_plugin ('ACTION', \&add);
25 register_plugin ('OUTPUT', \&output);
27 our $WeekdayCache = Onis::Data::Persistent->new ('WeekdayCache', 'nick',
37 our $WeekdayData = {};
38 our @Weekdays = (qw(sun mon tue wed thu fri sat));
40 =head1 CONFIGURATION OPTIONS
44 =item B<vertical_images>: I<image0>, I<image1>, I<image2>, I<image3>;
46 Sets the images used for vertical bars.
50 our @VImages = get_config ('vertical_images');
51 if (scalar (@VImages) != 4)
53 @VImages = qw#images/ver0n.png images/ver1n.png images/ver2n.png images/ver3n.png#;
61 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
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;
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);
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);
88 for ($WeekdayCache->keys ())
91 my $main = $nick eq '<TOTAL>' ? '<TOTAL>' : get_main_nick ($nick);
92 my @data = $WeekdayCache->get ($nick);
94 if (!defined ($WeekdayData->{$main}))
96 $WeekdayData->{$main} =
108 for (my $i = 0; $i < 7; $i++)
110 my $day = $Weekdays[$i];
111 for (my $j = 0; $j < 4; $j++)
113 my $idx = ($i * 4) + $j;
114 $WeekdayData->{$main}{$day}[$j] += $data[$idx];
123 return (undef) unless (%$WeekdayData);
127 [1, 'mon', 'Monday'],
128 [2, 'tue', 'Tuesday'],
129 [3, 'wed', 'Wednesday'],
130 [4, 'thu', 'Thursday'],
131 [5, 'fri', 'Friday'],
132 [6, 'sat', 'Saturday'],
136 my $data = $WeekdayData->{'<TOTAL>'};
138 my $fh = get_filehandle ();
145 my ($num, $abbr, $name) = @$_;
147 for (my $i = 0; $i < 4; $i++)
149 $max = $data->{$abbr}[$i] if ($max < $data->{$abbr}[$i]);
150 $total += $data->{$abbr}[$i];
154 print $fh qq#<table class="plugin weekdays">\n <tr class="bars">\n#;
157 my ($num, $abbr, $name) = @$_;
158 for (my $i = 0; $i < 4; $i++)
160 my $num = $data->{$abbr}[$i];
161 my $height = sprintf ("%.2f", (95 * $num / $max));
162 my $img = $VImages[$i];
164 print $fh qq# <td class="bar vertical $abbr">#,
165 qq(<img src="$img" alt="" class="first last" style="height: ${height}%;" /></td>\n);
168 print $fh qq( </tr>\n <tr class="counter">\n);
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);
176 print $fh qq( </tr>\n <tr class="numeration">\n);
179 my ($num, $abbr, $name) = @$_;
180 print $fh qq( <td colspan="4" class="numeration $abbr">$name</td>\n);
182 print $fh " </tr>\n</table>\n\n";
185 =head1 EXPORTED FUNCTIONS
189 =item B<get_weekdays> (I<$nick>)
191 Returns a hashref with the weekday information for I<$nick>. Numbers are
192 character counters. The returned data has the following format:
210 if (!defined ($WeekdayData->{$nick}))
215 return ($WeekdayData->{$nick});
222 Florian octo Forster E<lt>octo at verplant.orgE<gt>