Changed stylesheets to use relative scaling for horizontal bars.
[onis.git] / lib / Onis / Plugins / Userdetails.pm
index 66aa39e..30faca4 100644 (file)
@@ -6,52 +6,40 @@ use warnings;
 use Onis::Config (qw(get_config));
 use Onis::Html (qw(html_escape get_filehandle));
 use Onis::Language (qw(translate));
-use Onis::Data::Core (qw(get_main_nick register_plugin nick_to_name));
-use Onis::Users (qw(ident_to_name get_link get_image));
+use Onis::Data::Core (qw(get_main_nick register_plugin nick_to_name get_most_recent_time));
+use Onis::Users (qw(get_link get_image));
 
 use Onis::Plugins::Core (qw(get_core_nick_counters get_sorted_nicklist));
+use Onis::Plugins::Weekdays (qw(get_weekdays));
+use Onis::Plugins::Longterm (qw(get_longterm));
 use Onis::Plugins::Conversations (qw(get_conversations));
 use Onis::Plugins::Bignumbers (qw(get_bignumbers));
 use Onis::Plugins::Interestingnumbers (qw(get_interestingnumbers));
 
-our $DISPLAY_IMAGES = 0;
-our $DEFAULT_IMAGE = '';
+our $DisplayImages = 0;
+our $DefaultImage = '';
 
 register_plugin ('OUTPUT', \&output);
 
-our $SORT_BY = 'lines';
-if (get_config ('sort_by'))
+our $NumUserdetails = 10;
+if (get_config ('userdetails_number'))
 {
-       my $tmp = get_config ('sort_by');
-       $tmp = lc ($tmp);
-
-       if (($tmp eq 'lines') or ($tmp eq 'words') or ($tmp eq 'chars'))
-       {
-               $SORT_BY = $tmp;
-       }
-       else
-       {
-               # The Core plugin already complained about this..
-       }
-}
-our $PLUGIN_MAX = 10;
-if (get_config ('plugin_max'))
-{
-       my $tmp = get_config ('plugin_max');
+       my $tmp = get_config ('userdetails_number');
        $tmp =~ s/\D//g;
-       $PLUGIN_MAX = $tmp if ($tmp);
+       $NumUserdetails = $tmp if ($tmp);
 }
+
 if (get_config ('display_images'))
 {
        my $tmp = get_config ('display_images');
 
        if ($tmp =~ m/true|on|yes/i)
        {
-               $DISPLAY_IMAGES = 1;
+               $DisplayImages = 1;
        }
        elsif ($tmp =~ m/false|off|no/i)
        {
-               $DISPLAY_IMAGES = 0;
+               $DisplayImages = 0;
        }
        else
        {
@@ -61,10 +49,10 @@ if (get_config ('display_images'))
 }
 if (get_config ('default_image'))
 {
-       $DEFAULT_IMAGE = get_config ('default_image');
+       $DefaultImage = get_config ('default_image');
 }
 
-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 @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'))
 {
        my @tmp = get_config ('horizontal_images');
@@ -83,11 +71,11 @@ if (get_config ('horizontal_images'))
                        next;
                }
 
-               $H_IMAGES[$i] = $tmp[$i];
+               $HorizontalImages[$i] = $tmp[$i];
        }
 }
 
-our @V_IMAGES = qw#images/ver0n.png images/ver1n.png images/ver2n.png images/ver3n.png#;
+our @VerticalImages = qw#images/ver0n.png images/ver1n.png images/ver2n.png images/ver3n.png#;
 if (get_config ('vertical_images'))
 {
        my @tmp = get_config ('vertical_images');
@@ -106,25 +94,24 @@ if (get_config ('vertical_images'))
                        next;
                }
 
-               $V_IMAGES[$i] = $tmp[$i];
+               $VerticalImages[$i] = $tmp[$i];
        }
 }
 
-our $BAR_HEIGHT = 130;
-if (get_config ('bar_height'))
+our $ConversationsNumber = 10;
+if (get_config ('userdetails_conversations_number'))
 {
-       my $tmp = get_config ('bar_height');
+       my $tmp = get_config ('userdetails_conversations_number');
        $tmp =~ s/\D//g;
-       $BAR_HEIGHT = $tmp if ($tmp >= 10);
+       $ConversationsNumber = $tmp if ($tmp);
 }
-#$BAR_HEIGHT = int ($BAR_HEIGHT / 2);
 
-our $BAR_WIDTH  = 100;
-if (get_config ('bar_width'))
+our $LongtermDays = 7;
+if (get_config ('userdetails_longterm_days'))
 {
-       my $tmp = get_config ('bar_width');
+       my $tmp = get_config ('userdetails_longterm_days');
        $tmp =~ s/\D//g;
-       $BAR_WIDTH = $tmp if ($tmp >= 10);
+       $LongtermDays = $tmp if ($tmp);
 }
 
 my $VERSION = '$Id: Userdetails.pm,v 1.5 2005/03/14 18:40:25 octo Exp $';
@@ -136,8 +123,6 @@ sub output
 {
        my $nicks_ref = get_sorted_nicklist ();
        
-       my $max = $PLUGIN_MAX;
-       
        my $fh = get_filehandle ();
 
        my $trans = translate ('Detailed nick stats');
@@ -145,17 +130,21 @@ sub output
 
        my $max_time = 0;
        my $max_conv = 0;
+       my $max_weekdays = 0;
+       my $max_longterm = 0;
 
        my @nicks = @$nicks_ref;
        my $nick_data = {};
 
-       splice (@nicks, $max) if (scalar (@nicks) > $max);
+       splice (@nicks, $NumUserdetails) if (scalar (@nicks) > $NumUserdetails);
 
        for (@nicks)
        {
                my $nick = $_;
 
                $nick_data->{$nick} = get_core_nick_counters ($nick);
+               $nick_data->{$nick}{'weekdays'} = get_weekdays ($nick);
+               $nick_data->{$nick}{'longterm'} = get_longterm ($nick);
                $nick_data->{$nick}{'conversations'} = get_conversations ($nick);
                $nick_data->{$nick}{'bignumbers'} = get_bignumbers ($nick);
                $nick_data->{$nick}{'interestingnumbers'} = get_interestingnumbers ($nick);
@@ -173,24 +162,36 @@ sub output
                        $num = $ptr->[0] + $ptr->[1] + $ptr->[2] + $ptr->[3];
                        $max_conv = $num if ($max_conv < $num);
                }
-       }
 
-       my $time_factor = 0;
-       my $conv_factor = 0;
+               for (keys %{$nick_data->{$nick}{'weekdays'}})
+               {
+                       my $ptr = $nick_data->{$nick}{'weekdays'}{$_};
+                       for (my $i = 0; $i < 4; $i++)
+                       {
+                               $max_weekdays = $ptr->[$i] if ($max_weekdays < $ptr->[$i]);
+                       }
+               }
 
-       if ($max_time)
-       {
-               $time_factor = $BAR_HEIGHT / $max_time;
-       }
+               if (@{$nick_data->{$nick}{'longterm'}})
+               {
+                       my $num = scalar (@{$nick_data->{$nick}{'longterm'}});
+                       $LongtermDays = $num if ($LongtermDays > $num);
 
-       if ($max_conv)
-       {
-               $conv_factor = $BAR_WIDTH / $max_conv;
+                       for (my $i = $num - $LongtermDays; $i < $num; $i++)
+                       {
+                               my $ptr = $nick_data->{$nick}{'longterm'}[$i];
+
+                               for (my $j = 0; $j < 4; $j++)
+                               {
+                                       $max_longterm = $ptr->[$j] if ($max_longterm < $ptr->[$j]);
+                               }
+                       }
+               }
        }
-       
+
        print $fh qq#<table class="plugin userdetails">\n#,
        qq#  <tr>\n#,
-       qq#    <th colspan="#, $DISPLAY_IMAGES ? 3 : 2, qq#">$trans</th>\n#,
+       qq#    <th colspan="#, $DisplayImages ? 4 : 3, qq#">$trans</th>\n#,
        qq#  </tr>\n#;
 
        for (@nicks)
@@ -201,18 +202,18 @@ sub output
                my $ptr = $nick_data->{$nick};
 
                print $fh qq#  <tr>\n#,
-               qq#    <th colspan="#, $DISPLAY_IMAGES ? 3 : 2, qq#" class="nick">$print</th>\n#,
+               qq#    <th colspan="#, $DisplayImages ? 4 : 3, qq#" class="nick">$print</th>\n#,
                qq#  </tr>\n#,
                qq#  <tr>\n#;
 
-               if ($DISPLAY_IMAGES)
+               if ($DisplayImages)
                {
                        my $link = get_link ($name);
                        my $image = get_image ($name);
                        
-                       if ($DEFAULT_IMAGE and !$image)
+                       if ($DefaultImage and !$image)
                        {
-                               $image = $DEFAULT_IMAGE;
+                               $image = $DefaultImage;
                        }
 
                        print $fh qq#    <td class="image" rowspan="2">#;
@@ -304,51 +305,7 @@ sub output
                        printf $fh ("      $trans<br />\n", $num);
                }
 
-               # actions # TODO
-               # exclamation ratio # TODO
-               # # of nicks
-               #
-               # chats with
-               # lines per day
-
-               print $fh qq#    </td>\n  </tr>\n  <tr>\n    <td class="houractivity">\n#;
-               
-               if (defined ($ptr->{'chars'}))
-               {
-                       print $fh qq#      <table class="hours">\n        <tr class="bars">\n#;
-                       
-                       for (my $i = 0; $i < 24; $i++)
-                       {
-                               $num = 0;
-
-                               my $img = $V_IMAGES[int ($i / 6)];
-                               my $height;
-
-                               $num  = $ptr->{'chars'}[$i];
-
-                               $height = sprintf ("%.2f", 95 * $num / $max_time);
-
-                               print $fh qq#          <td class="bar vertical"><img src="$img" alt="$num chars" #,
-                               qq#class="first last" style="height: $height\%;" /></td>\n#;
-                       }
-
-                       print $fh <<EOF;
-        </tr>
-       <tr class="numeration">
-         <td colspan="6" class="numeration">0-5</td>
-         <td colspan="6" class="numeration">6-11</td>
-         <td colspan="6" class="numeration">12-17</td>
-         <td colspan="6" class="numeration">18-23</td>
-       </tr>
-      </table>
-EOF
-               }
-               else
-               {
-                       print '&nbsp;';
-               }
-
-               print $fh qq#    </td>\n    <td class="convpartners">\n#;
+               print $fh qq#    </td>\n    <td>\n#;
                
                if (%{$ptr->{'conversations'}})
                {
@@ -370,26 +327,27 @@ EOF
                        $trans = translate ('Talks to');
 
                        print $fh <<EOF;
-      <table>
+      <table class="conversations">
         <tr>
          <td colspan="2">$trans:</td>
        </tr>
 EOF
 
-                       for (my $i = 0; $i < $PLUGIN_MAX and $i < scalar (@others); $i++)
+                       for (my $i = 0; $i < $ConversationsNumber and $i < scalar (@others); $i++)
                        {
                                my $other = $others[$i];
                                my $other_name = nick_to_name ($other) || $other;
                                my $total = 0;
 
                                print $fh "        <tr>\n",
-                               qq#          <td class="nick">$other_name</td>\n#,
-                               qq#          <td class="bar">#;
+                               qq#          <td class="nick right">$other_name</td>\n#,
+                               qq#          <td class="bar horizontal right">#;
 
                                for (my $k = 0; $k < 4; $k++)
                                {
-                                       my $img = $H_IMAGES[$k];
-                                       my $width = int (0.5 + ($conv_factor * $ptr->{'conversations'}{$other}{'nicks'}{$nick}[$k])) || 1;
+                                       my $img = $HorizontalImages[$k];
+                                       my $num = $ptr->{'conversations'}{$other}{'nicks'}{$nick}[$k];
+                                       my $width = sprintf ("%.2f", 95 * $num / $max_conv);
                                        
                                        print $fh qq#<img src="$img" alt="" #;
                                        if ($k == 0)
@@ -400,7 +358,7 @@ EOF
                                        {
                                                print $fh qq#class="last" #;
                                        }
-                                       print $fh qq#style="width: ${width}px;" />#;
+                                       print $fh qq#style="width: $width\%;" />#;
                                }
                                
                                print $fh "</td>\n        </tr>\n";
@@ -412,6 +370,142 @@ EOF
                {
                        print $fh '&nbsp;';
                }
+               print $fh qq#    </td>\n  </tr>\n#,
+               qq#  <tr>\n    <td>\n#;
+               
+               if (defined ($ptr->{'chars'}))
+               {
+                       print $fh qq#      <table class="hours">\n        <tr class="bars">\n#;
+                       
+                       for (my $i = 0; $i < 24; $i++)
+                       {
+                               $num = 0;
+
+                               my $img = $VerticalImages[int ($i / 6)];
+                               my $height;
+
+                               $num  = $ptr->{'chars'}[$i];
+
+                               $height = sprintf ("%.2f", 95 * $num / $max_time);
+
+                               print $fh qq#          <td class="bar vertical"><img src="$img" alt="$num chars" #,
+                               qq#class="first last" style="height: $height\%;" /></td>\n#;
+                       }
+
+                       print $fh <<EOF;
+        </tr>
+       <tr class="numeration">
+         <td colspan="6" class="numeration">0-5</td>
+         <td colspan="6" class="numeration">6-11</td>
+         <td colspan="6" class="numeration">12-17</td>
+         <td colspan="6" class="numeration">18-23</td>
+       </tr>
+      </table>
+EOF
+               }
+               else
+               {
+                       print $fh "      &nbsp;\n";
+               }
+
+               print $fh qq#    </td>\n    <td>\n#;
+
+               #weekly
+               if (%{$nick_data->{$nick}{'weekdays'}})
+               {
+                       my $data = $nick_data->{$nick}{'weekdays'};
+                       my @days = (qw(mon tue wed thu fri sat sun));
+
+                       print $fh qq#      <table class="weekdays">\n#,
+                       qq#        <tr class="bars">\n#;
+
+                       for (@days)
+                       {
+                               my $day = $_;
+                               for (my $i = 0; $i < 4; $i++)
+                               {
+                                       my $num = $nick_data->{$nick}{'weekdays'}{$day}[$i];
+                                       my $height = sprintf ("%.2f", 95 * $num / $max_weekdays);
+                                       my $img = $VerticalImages[$i];
+
+                                       print $fh qq#          <td class="bar vertical">#,
+                                       qq#<img src="$img" alt="" class="first last" style="height: $height\%;" />#,
+                                       qq#</td>\n#;
+                               }
+                       }
+
+                       print $fh qq#        </tr>\n#,
+                       qq#        <tr class="numeration">\n#;
+
+                       for (@days)
+                       {
+                               my $day = $_;
+                               my $trans = translate ($day);
+                               
+                               print $fh qq#          <td colspan="4" class="numeration $day">$trans</td>\n#;
+                       }
+
+                       print $fh qq#        </tr>\n#,
+                       qq#      </table>\n#;
+               }
+               else
+               {
+                       print $fh "      &nbsp;\n";
+               }
+
+               print $fh qq#    </td>\n    <td>\n#;
+
+               #longterm
+               if (@{$nick_data->{$nick}{'longterm'}})
+               {
+                       my $num_fields = scalar (@{$nick_data->{$nick}{'longterm'}});
+                       my $now_epoch = get_most_recent_time ();
+                       my $now_day = int ($now_epoch / 86400);
+                       my $last_day;
+
+                       my @weekdays = (qw(sun mon tue wed thu fri sat));
+
+                       $LongtermDays = $num_fields if ($LongtermDays > $num_fields);
+                       $last_day = 1 + $now_day - $LongtermDays;
+                       
+                       print $fh qq#      <table class="longterm">\n#,
+                       qq#        <tr class="bars">\n#;
+
+                       for (my $i = $num_fields - $LongtermDays; $i < $num_fields; $i++)
+                       {
+                               for (my $j = 0; $j < 4; $j++)
+                               {
+                                       my $num = $nick_data->{$nick}{'longterm'}[$i][$j];
+                                       my $height = sprintf ("%.2f", 95 * $num / $max_longterm);
+                                       my $img = $VerticalImages[$j];
+                                       
+                                       print $fh qq#          <td class="bar vertical">#,
+                                       qq#<img src="$img" alt="" class="first last" style="height: $height\%;" />#,
+                                       qq#</td>\n#;
+                               }
+                       }
+                       
+                       print $fh qq#        </tr>\n#,
+                       qq#        <tr class="numeration">\n#;
+
+                       for (my $i = 0; $i < $LongtermDays; $i++)
+                       {
+                               my $epoch = ($last_day + $i) * 86400;
+                               my ($day, $wd) = (localtime ($epoch))[3,6];
+                               $wd = $weekdays[$wd];
+
+                               print $fh qq#          <td colspan="4" class="numeration $wd">$day.</td>\n#;
+                       }
+
+                       print $fh qq#        </tr>\n#,
+                       qq#      </table>\n#;
+               }
+               else
+               {
+                       print $fh "      &nbsp;\n";
+               }
+
+               print $fh qq#    </td>\n  </tr>\n#;
        }
 
        print $fh "</table>\n\n";