apps / action / synthesis /
01bbdf0 6 years ago
1 contributor
82 lines | 3.12kb
#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;
use POSIX qw(strftime);
use Data::Dumper;

my $scriptconf = $ENV{PWD} . "/action.conf";
if (-r $scriptconf) {
    package cfg;
    unless (my $return = do $scriptconf) {
        warn "couldn't parse $scriptconf: $@" if $@;
        warn "couldn't do $scriptconf: $!"    unless defined $return;
        warn "couldn't run $scriptconf"       unless $return;
    }
}
else {
    print "pas de config\n";
    exit;
}

my $dbh = DBI->connect($cfg::config{db}->{driver}, $cfg::config{db}->{user}, $cfg::config{db}->{password}, {'RaiseError' => 1, 'AutoCommit' => 1});

my $request;
my $sth;
my $polls;
my $slots;
my $votes;
my %tables = ();
$tables{polls} = $cfg::config{prefix} . "poll";
$tables{votes} = $cfg::config{prefix} . "vote";
$tables{slots} = $cfg::config{prefix} . "slot";

$request = "select id as poll_id, admin_name as category, admin_id, title, recurrent, description from $tables{polls}";
$sth = $dbh->prepare($request);
$polls = $dbh->selectall_hashref($sth, [ qw(category poll_id) ]);

# admin_name = catégorie
my $dt = strftime "%s", localtime;
foreach my $category (keys(%{$polls})) {
    foreach my $poll_id (keys(%{$polls->{$category}})) {
        $request = "select id as slot_id, title, poll_id from $tables{slots} where poll_id = '$poll_id'";
        $sth = $dbh->prepare($request);
        $slots =  $dbh->selectall_hashref($sth, [ qw(poll_id slot_id) ]);

        $request = "select id as vote_id, uniqId, poll_id, choices from $tables{votes} where poll_id = '$poll_id'";
        $sth = $dbh->prepare($request);
        $votes =  $dbh->selectall_hashref($sth, [ qw(poll_id uniqId) ]);

        my $last_date = 0;
        my @deleted = ();
        $polls->{$category}->{$poll_id}->{next} = strftime "%s", 0, 0, 0, 1, 12, 2099;
        $polls->{$category}->{$poll_id}->{compteur} = 0;
        foreach my $slot_id (keys(%{$slots->{$poll_id}})) {
            my $title = $slots->{$poll_id}->{$slot_id}->{title};
            if ($title lt $polls->{$category}->{$poll_id}->{next}) {
                $polls->{$category}->{$poll_id}->{next} = $title;
            }
            foreach my $uniqId (keys(%{$votes->{$poll_id}})) {
                $polls->{$category}->{$poll_id}->{compteur}++;
            }
        }
    }
}
$dbh->disconnect();

my $data = '<!DOCTYPE html><html lang="fr"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body>';

foreach my $category (keys(%{$polls})) {
    next unless (keys%{$polls->{$category}});
    $data .= "<center><h1>$category</h1></center>";
    foreach my $poll_id (keys(%{$polls->{$category}})) {
        my $title    = $polls->{$category}->{$poll_id}->{title};
        $data .= "<b>" . strftime("[%a %e %b] ", localtime($polls->{$category}->{$poll_id}->{next})). $title . "</b><br>";
        $data .= "($polls->{$category}->{$poll_id}->{compteur} inscrits)<br>" if ($polls->{$category}->{$poll_id}->{compteur} ge 0);
        $data .= $polls->{$category}->{$poll_id}->{description} . "<br><br>";
    }
}

$data .= strftime("%c", localtime()) . '</body></html>';

print $data;