#!/usr/local/bin/perl -w
###############################################################################
# hof.pl - this code displays the pda hall of fame page
#
# Copyright (C) 2001 Norbert "Momo_102" Kuemin
# momo_102@bluemail.ch
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
###############################################################################
# pre stories cache update
use strict;
use lib '/home/slash';
use vars '%I';
use Slash;

##### Configuration #############################################
my $AVG = new AvantGo("/home/slash/avantgo.user");
my $HOF_LIMIT = 10;
my $param = "";

#################################################################
sub main {
   pda_panalyze();

   *I = getSlashConf();
   getSlash();
   my $SECT = getSection($I{F}{section});
   $SECT->{mainsize} = int($SECT->{artcount} / 3);
   my $title = $SECT->{title};
   $title = "$title" unless $SECT->{isolate};
   my ($story, $comment, $poll, $hof, $uid) = $AVG->user_get("-");

   pda_header($I{sitename});
   pda_hof_nav();
   print "<HR>\n";
   pda_hof_story1("Bestbesuchte Beitr&auml;ge",$HOF_LIMIT);
   print "<P>\n";
   pda_hof_story2("Aktivste Beitr&auml;ge",$HOF_LIMIT);
   print "<P>\n";
   pda_hof_author("Die aktivsten Autoren",$HOF_LIMIT);
   print "<P>\n";
   pda_hof_karma("Die Personen mit dem meisten Karma",$HOF_LIMIT);
   print "<P>\n";
   pda_hof_poll("Die beliebtesten Umfragethemen",$HOF_LIMIT);
   print "<HR>\n";
   pda_hof_nav();
   pda_footer();
}

#################################################################
sub pda_panalyze {
   my $lnames = "&sid&nr&";
   my @names = CGI::param;
   foreach (@names) {
      if (index($lnames, "&".$_."&") == -1) {
         $param = $param."&".$_."=".CGI::param($_);
      }
   }
}

#################################################################
sub pda_header {
   my $title = shift;
   print CGI::header();
   print <<EOT if $title;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML><HEAD><TITLE>$title</TITLE></HEAD><BODY>
EOT
}  
   
#################################################################
sub pda_footer {
   print <<EOT; 
</BODY></HTML>  
EOT
}  

#################################################################
sub pda_hof_nav {
   print "<A HREF=\"index.pl";
   if ($param ne "") { print "?".substr($param,1); }
   print "\">Home</A>\n";
}

#################################################################
sub pda_hof_stats {
   my ($title, $cursor) = @_;
   print "<B>$title</B>\n";
   print "<OL>\n";
   while (my ($nr,$text) = $cursor->fetchrow) {
      print "<LI> $text ($nr)\n";
   }
   print "</OL>\n";
   $cursor->finish;
}

#################################################################
sub pda_hof_story1 {
   my ($title, $limit) = @_;
   pda_hof_stats($title,
      sqlSelectMany("storiestuff.hits as hits,title",
                    "stories,storiestuff", "stories.sid=storiestuff.sid",
                    "ORDER BY hits DESC LIMIT $limit")
   );
}
 
#################################################################
sub pda_hof_story2 {
   my ($title, $limit) = @_;
   pda_hof_stats($title,
      sqlSelectMany("commentcount,title",
                    "stories","", "ORDER BY commentcount DESC LIMIT $limit")
   );
}
 
#################################################################
sub pda_hof_author {
   my ($title, $limit) = @_;
   pda_hof_stats($title,
      sqlSelectMany("count(*) as c, stories.aid", "stories, authors",
                    "authors.aid=stories.aid",
                    "GROUP BY aid ORDER BY c DESC LIMIT $limit")
   );
}

#################################################################
sub pda_hof_karma {
   my ($title, $limit) = @_;
   pda_hof_stats($title,
      sqlSelectMany("karma,nickname",
                    "users, users_info",
                    "users.uid=users_info.uid AND users.uid>=0",
                    "ORDER BY karma DESC LIMIT $limit")
   );
}

#################################################################
sub pda_hof_poll {
   my ($title, $limit) = @_;
   pda_hof_stats($title,
      sqlSelectMany("voters,question","pollquestions",
                    "","ORDER by voters DESC LIMIT $limit")
   );
}

#################################################################
main();
#################################################################
