Changed stylesheets to use relative scaling for horizontal bars.
[onis.git] / lib / Onis / Plugins / Conversations.pm
index 5b28192..b20d3b7 100644 (file)
@@ -9,8 +9,18 @@ use Onis::Config qw(get_config);
 use Onis::Html qw(get_filehandle);
 use Onis::Language qw(translate);
 use Onis::Data::Core qw(register_plugin get_main_nick nick_to_ident nick_to_name);
-use Onis::Users (qw(ident_to_name));
-use Onis::Data::Persistent;
+use Onis::Data::Persistent ();
+
+=head1 NAME
+
+Onis::Plugins::Conversations - Who talks with who
+
+=head1 DESCRIPTION
+
+This plugins tries to recignise conversations and counts the amount that people
+talk to each other.
+
+=cut
 
 @Onis::Plugins::Conversations::EXPORT_OK = (qw(get_conversations));
 @Onis::Plugins::Conversations::ISA = ('Exporter');
@@ -18,8 +28,7 @@ use Onis::Data::Persistent;
 our $ConversationCache = Onis::Data::Persistent->new ('ConversationCache', 'partners', qw(time0 time1 time2 time3));
 our $ConversationData = {};
 
-our @H_IMAGES = qw#dark-theme/h-red.png dark-theme/h-blue.png dark-theme/h-yellow.png dark-theme/h-green.png#;
-our $BAR_WIDTH  = 100;
+our @HorizontalImages = qw#dark-theme/h-red.png dark-theme/h-blue.png dark-theme/h-yellow.png dark-theme/h-green.png#;
 
 if (get_config ('horizontal_images'))
 {
@@ -34,14 +43,16 @@ if (get_config ('horizontal_images'))
        for ($i = 0; $i < 4; $i++)
        {
                next unless (defined ($tmp[$i]));
-               $H_IMAGES[$i] = $tmp[$i];
+               $HorizontalImages[$i] = $tmp[$i];
        }
 }
-if (get_config ('bar_width'))
+
+our $NumConversations = 10;
+if (get_config ('conversations_number'))
 {
-       my $tmp = get_config ('bar_width');
+       my $tmp = get_config ('conversations_number');
        $tmp =~ s/\D//g;
-       $BAR_WIDTH = 2 * $tmp if ($tmp >= 10);
+       $NumConversations = $tmp if ($tmp);
 }
 
 register_plugin ('TEXT', \&add);
@@ -160,12 +171,8 @@ sub output
        my $title = translate ('Conversation partners');
 
        my $max_num = 0;
-       my $factor = 0;
 
-       my @img = get_config ('horizontal_images');
-
-       # FIXME
-       my @data = get_top (10);
+       my @data = get_top ($NumConversations);
        return (undef) unless (@data);
 
        for (@data)
@@ -187,8 +194,6 @@ sub output
                $max_num = $sum1 if ($max_num < $sum1);
        }
        
-       $factor = $BAR_WIDTH / $max_num;
-
        print $fh <<EOF;
 <table class="plugin conversations">
   <tr>
@@ -211,37 +216,29 @@ EOF
   <tr>
 EOF
 
-               print $fh '    <td class="bar left">';
-               for (3, 2, 1, 0)
+               print $fh '    <td class="bar horizontal left">';
+               for (my $i = 3; $i >= 0; $i--)
                {
-                       my $i = $img[$_];
-                       my $w = int (0.5 + ($rec->{'nicks'}{$nick0}[$_] * $factor));
-                       my $c = '';
-                       $w ||= 1;
-
-                       $w = $w . 'px';
+                       my $width = sprintf ("%.2f", 95 * $rec->{'nicks'}{$nick0}[$i] / $max_num);
+                       my $image = $HorizontalImages[$i];
+                       my $class = '';
 
-                       if    ($_ == 3) { $c = qq# class="first"#; }
-                       elsif ($_ == 0) { $c = qq# class="last"#;  }
+                       if    ($i == 3) { $class = qq# class="first"#; }
+                       elsif ($i == 0) { $class = qq# class="last"#;  }
 
-                       print $fh qq#<img src="$i" style="width: $w;"$c alt="" />#;
+                       print $fh qq#<img src="$image" style="width: $width%;"$class alt="" />#;
                }
-
-               print $fh qq#</td>\n    <td class="bar right">#;
-
-               for (0, 1, 2, 3)
+               print $fh qq#</td>\n    <td class="bar horizontal right">#;
+               for (my $i = 0; $i < 4; $i++)
                {
-                       my $i = $img[$_];
-                       my $w = int (0.5 + ($rec->{'nicks'}{$nick1}[$_] * $factor));
-                       my $c = '';
-                       $w ||= 1;
+                       my $width = sprintf ("%.2f", 95 * $rec->{'nicks'}{$nick1}[$i] / $max_num);
+                       my $image = $HorizontalImages[$i];
+                       my $class = '';
 
-                       $w = $w . 'px';
+                       if    ($i == 0) { $class = qq# class="first"#; }
+                       elsif ($i == 3) { $class = qq# class="last"#;  }
 
-                       if    ($_ == 0) { $c = qq# class="first"#; }
-                       elsif ($_ == 3) { $c = qq# class="last"#;  }
-
-                       print $fh qq#<img src="$i" style="width: $w;"$c alt=""/>#;
+                       print $fh qq#<img src="$image" style="width: $width%;"$class alt="" />#;
                }
                print $fh "</td>\n  </tr>\n";
        }
@@ -249,6 +246,33 @@ EOF
        print $fh "</table>\n\n";
 }
 
+=head1 EXPORTED FUNCTIONS
+
+=over 4
+
+=item B<get_conversations> (I<$nick>)
+
+Returns a hashref to the conversations this nick was involved with. The layout
+is the following. I<$other> is the nick of the person I<$nick> chattet with.
+The arrays hold the number of characters written by the nick used as the key.
+The first field contains the characters for the hours 0-5, the second field
+holds 6-11 and so on.
+
+  {
+    $other =>
+    {
+      total => 0,
+      nicks =>
+      {
+        $nick  => [0, 0, 0, 0],
+        $other => [0, 0, 0, 0]
+      }
+    },
+    ...
+  }
+
+=cut
+
 sub get_conversations
 {
        my $nick = shift;
@@ -262,3 +286,11 @@ sub get_conversations
                return ($ConversationData->{$nick});
        }
 }
+
+=back
+
+=head1 AUTHOR
+
+Florian octo Forster, E<lt>octo at verplant.orgE<gt>
+
+=cut