#!/usr/bin/perl

# $Id: generate_schedule,v 1.8 2004/02/04 18:25:34 mig Exp $
######################################
# Comas - Conference Management System
######################################
# Copyright 2003 CONSOL
# Congreso Nacional de Software Libre (http://www.consol.org.mx/)
#   Gunnar Wolf <gwolf@gwolf.cx>
#   Manuel Rabade <mig@mig-29.net>
#
# 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
######################################

######################################
# Script: generate_schedule
# Generates the html schedule
######################################
# Depends on:
#

=head1 NAME

generate_schedule - Generates the html schedule

=head1 SYNOPSIS

./generate_schedule

=head1 DESCRIPTION

This script is hardcoded and it only should be used by somebody who knows what
he does.

=head1 REQUIRES

A lot :)

=head1 EXPORTS

Nothing

=head1 SEE ALSO

Nothing.

=head1 AUTHOR

Gunnar Wolf, gwolf@gwolf.cx
Manuel Rabade, mig@mig-29.net

Comas has been developed for CONSOL, Congreso Nacional de Software Libre,
http://www.consol.org.mx/

=head1 COPYRIGHT

Copyright 2003 Gunnar Wolf and Manuel Rabade

This library is free software, you can redistribute it and/or modify it
under the terms of the GPL version 2 or later.
    
=cut


use lib qw(../);

use strict;
use warnings;

use Comas::Person;
use Comas::Schedule;
use Comas::Proposal;
use Comas::DB;
use Comas::HTML::common qw(:all);
use Date::Parse;
use Data::Dumper;
use Date::Calc qw(Add_Delta_DHMS);

my $special_events = { 
    '2004-02-10' => [ [ 1, '10:00:00', '11:00:00', 'Inaguracion', 0 ],
                      [ 0, '11:00:00', '12:00:00', 'Registro' ],
                      [ 0, '14:00:00', '16:00:00', 'Comida' ],
                      [ 0, '20:00:00', '21:00:00', 'Fin Actividades' ] ],

    '2004-02-11' => [ [ 0, '14:00:00', '16:00:00', 'Comida' ],
                      [ 0, '20:00:00', '21:00:00', 'Fin Actividades' ] ],

    '2004-02-12' => [ [ 0, '14:00:00', '16:00:00', 'Comida' ],
                      [ 0, '20:00:00', '21:00:00', 'Fin Actividades' ] ],

    '2004-02-13' => [ [ 0, '14:00:00', '16:00:00', 'Comida' ],
                      [ 0, '18:00:00', '21:00:00', 'Fin Actividades' ] ]
};

my $day_strings = { '2004-02-10' => 'Martes 10 de Febrero 2004',
                    '2004-02-11' => 'Miercoles 11 de Febrero 2004',
                    '2004-02-12' => 'Jueves 12 de Febrero 2004',
                    '2004-02-13' => 'Viernes 13 de Febrero 2004' };

my $header = <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
	<title>CONSOL 2004</title>
	<meta HTTP-EQUIV="Pragma" content="no-cache">
	<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">
	<meta HTTP-EQUIV="Content-Language" CONTENT="ES">
	<meta HTTP-EQUIV="Content-Type" content="text/html; charset=ISO-8859-1">
	<meta NAME="description" CONTENT="Congreso Nacional de Software Libre 2004">
	<meta NAME="keywords" CONTENT="congreso,nacional,software,libre,software libre,congreso nacional software libre,2004,CONSOL,linux,gnu,gpl,openbsd,freebsd,netbsd,gnu/linux">

	<meta NAME="author" CONTENT="Ta^3, gwolf, mig, ion, jcgalici">
	<meta NAME="copyright" CONTENT="Copyleft ?2002,2004 CONSOL 2004">
	<meta NAME="audience" CONTENT="All">
	<meta NAME="distribution" content="Global">
	<meta NAME="rating" content="General">
	<meta HTTP-EQUIV="Reply-to" CONTENT="webmaster\@consol.org.mx">
	<meta NAME="revisit-after" CONTENT="1 days">
	<link rel="SHORTCUT ICON" href="/2004/favicon.png" type="image/png">
	<link rel="icon" href="/2004/favicon.png" type="image/png">

	<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
	<td id="sch_btopleft">&nbsp;</td>
	<td id="sch_btop">&nbsp;</td>
	<td id="sch_btopright">&nbsp;</td>
</tr>
<tr>
<td id="sch_bleft">&nbsp;</td>
<td id="sch_main" valign="top">

<br />
<br />
<h1>Programa de Ponencias</h1>
<br />

<!-- MAIN -->

EOF

my $tracks = <<EOF;

<table>
<tr>
<td width=70><b>Tracks:</b></td>
<td id="nom0">Conferencia Magistral</td>
<td id="nom1">Desarrollo de Software</td>
<td id="nom2">Seguridad y Redes</td>
<td id="nom3">Aplicaciones</td>
<td id="nom4">Novatos y Rollos filos&oacute;ficos</td>
</tr>
</table>
<br/>

EOF


my $footer = <<EOF;
<!-- /MAIN -->
<div align="center">
<form><input type="button" value="Regresar" onClick="history.go(-1)"></form>
</div>
</td>
<td id="sch_bright">&nbsp;</td>

</tr>
<tr>
	<td id="sch_bbotleft">&nbsp;</td>
	<td id="sch_bbot">&nbsp;</td>
	<td id="sch_bbotright">&nbsp;</td>
</td>
</tr>
</table>
<div id="sch_footer">
&copy; 2002-2004 Congreso Nacional de Software Libre<br/>
ESIME Zacatenco, <a href="http://www.ipn.mx">IPN</a> - Mxico

</div>
</body>
</html>
EOF

my (%rooms, @days, @rooms_in_order);

my $db = Comas::DB->new;

my $schedule = Comas::Schedule->new($db);
my $data = $schedule->get_schedule;

my $table = { };

foreach my $day (keys %{$data->{'by_day'}}) {
    $table->{$day} = { };    
    foreach my $prop (@{$data->{'by_day'}->{$day}}) {
        my (undef,undef,$hour1) = strptime($prop->[2]);
        my (undef,undef,$hour2) = strptime($prop->[3]);
        my $height = $hour2 - $hour1;
        $table->{$day}->{$prop->[2]}->{$prop->[0]} = [ $prop->[4], $height ];
        $rooms{$prop->[0]} = _id2descr($db, "room", $prop->[0]);
    }
}

foreach my $day (keys %{$special_events}) {
    foreach my $event (@{$special_events->{$day}}) {
        my (undef,undef,$hour1) = strptime($event->[1]);
        my (undef,undef,$hour2) = strptime($event->[2]);
        my $height = $hour2 - $hour1;
        $table->{$day}->{$event->[1]}->{$event->[0]} = [ $event->[3],
                                                         $height, 1 ];
        if (defined $event->[4]) {
            push (@{$table->{$day}->{$event->[1]}->{$event->[0]}}, $event->[4]);
        }
        if ($event->[0] != 0) {
            $rooms{$event->[0]} = _id2descr($db, "room", $event->[0]);
        }
    }
}

#print Data::Dumper->Dump([$table]);

foreach my $room (keys %{$data->{'by_room'}}) {
    $rooms{$room} = _id2descr($db, "room", $room);
}

#@rooms_in_order = sort {$rooms{$a} cmp $rooms{$b}} keys %rooms;
@rooms_in_order = (1 .. 8);

print $header;

foreach my $day (sort keys %{$table}) {

    print "<h2>$day_strings->{$day}</h2>";
    print $tracks;

    print "<table id='sch_table'>\n";
#    print "<tr><th COLSPAN=\"". (scalar (keys %rooms) + 1) . "\">$day</th></tr>\n";
    
    print "<tr>\n";
    print "<th>Hora</th>\n";
    foreach my $room (@rooms_in_order) {
        print "<th>$rooms{$room}</th>\n";
    }
    print "</tr>\n";
    

#     my ($order, %start_hr_order) = 0;
#     foreach my $start_hr (sort keys %{$table->{$day}}) {
#         $order++;
#         $start_hr_order{$order} = $start_hr;
#     }

    my %start_hr_order;
    my $start_hr = 10;
    foreach my $order (0 .. 10) {
        my $new_hr = $start_hr + $order;
        $start_hr_order{$order} = "$new_hr:00:00";
    }
    
    
    foreach my $start_hr (sort {$a <=> $b} keys %start_hr_order) {
        my (undef, $this_min, $this_hr) = strptime($start_hr_order{$start_hr});
        print "<tr>\n";
        print "<td id='sch_hour'>$this_hr:$this_min</td>\n";
        
        if (my $event = $table->{$day}->{$start_hr_order{$start_hr}}->{'0'}) {
            print "<td COLSPAN='".scalar (keys %rooms)."' ALIGN='center'";
            print " ROWSPAN='".$event->[1]."' id='trackother'>";
            print "$event->[0]</td>";
        } else {
            foreach my $room (@rooms_in_order) {
                if (my $event =
                    $table->{$day}->{$start_hr_order{$start_hr}}->{$room}) {
                    if (defined $event->[2] and $event->[2] == 1) {
                        print "<td ROWSPAN='$event->[1]' ";
                        if (defined $event->[3]) {
                            print "id='track$event->[3]' ALIGN='center'>";
                        } else {
                            print "id='trackother' ALIGN='center'>";
                        }
                        print "$event->[0]</td>\n";
                    } else {
                        my $prop = Comas::Proposal->new(-db => $db, -id =>
                                                        $event->[0]);
                        my ($auth_id) = $prop->get_authors;
                        my $auth = Comas::Person->new(-db=>$db, -id=>$auth_id);
                        my $auth_name = $auth->get_firstname.' '.
                            $auth->get_famname;
                        my %prop_data = $prop->get_data;
                        my $type =  _id2descr($db, "prop_type",
                                              $prop_data{-prop_type_id});
                        print "<td ROWSPAN='$event->[1]' ALIGN='center' ";
                        if ($prop_data{-prop_type_id} == 6) {
                            print "id='track0'>";
                        } else {
                            print "id='track$prop_data{-track_id}'>";
                        }
                        print "<a href='/2004/comas/general/proposals/$prop_data{-id}'>";
                        if ($prop_data{-prop_type_id} == 6) {
                            print "$prop_data{-title}</a><br><b>$auth_name</b></td>\n";
                        } else {
                            print "$prop_data{-title}</a><br><b>$auth_name</b><br>$type</td>\n";
                        }
                    }
                } else {
                    my $used = 0;
                    foreach my $hr (sort {$b <=> $a} keys %start_hr_order) {
                        next if $hr >= $start_hr;
                        if (defined
                            $table->{$day}->{$start_hr_order{$hr}}->{'0'}) {
                            if (($hr + 
                                 $table->{$day}->{$start_hr_order{$hr}}->{'0'}->[1]
                                 - $start_hr) > 0) {
                                $used = 1;
                            }
                        }
                        if (defined
                            $table->{$day}->{$start_hr_order{$hr}}->{$room}) {
                            if (($hr +
                                 $table->{$day}->{$start_hr_order{$hr}}->{$room}->[1]
                                 - $start_hr) > 0) {
                                $used = 1;
                            }
                        }
                    }
                    if ($used == 0) {
                        print "<td id='trackempty'></td>";
                    }
                }
            }
        }
        print "</tr>\n";
    }
    print "</table>\n<br /><br />\n\n";
}

print $footer;

# $Log: generate_schedule,v $
# Revision 1.8  2004/02/04 18:25:34  mig
# - Los eventos especiales se pueden ahora colorear como un track especifico
#
# Revision 1.7  2004/02/04 18:13:23  mig
# - Faltaba el boton de regresar
#
# Revision 1.6  2004/02/04 17:57:50  mig
# - Quitamos el tipo de ponencia de las magistrales
#
# Revision 1.5  2004/02/04 17:46:09  mig
# - Las magistrales se colorean amarillo :)
#
# Revision 1.4  2004/02/03 19:48:56  mig
# - Cambio el titulo principal a 'Programa de Ponencias'
#
# Revision 1.3  2004/02/03 17:31:30  mig
# - Los tracks ya aparecen arriba de cada dia
#
# Revision 1.2  2004/02/03 03:25:56  mig
# - Eliminio la bronca de que los tracks se ponian frente al logo
#
# Revision 1.1  2004/01/31 06:21:19  mig
# - El generador de horarios :), hardcodeado y feo, pero funciona al pedo :)
#
