##
--- #YAML:1.0
identifier: IT_Staff
description: Please vote about ...
public: Yes
--- #YAML:1.0
- question: How do you think...
id: q01
- question: Please rate ...
id: q02
####
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use YAML qw ( LoadFile );
my $poll_file = shift @ARGV or die "no file no poll!\n";
my ($poll, $questions) = LoadFile($poll_file) or die "Sorry, no question-file!\n";
my $config = LoadFile('conf/conf.yaml');
my $sql_create = qq{
CREATE TABLE $poll->{identifier} (
ID bigint(20) unsigned NOT NULL auto_increment,
Zeitstempel timestamp(14) NOT NULL,
};
$sql_create .= join("\n", map { " $_->{id} smallint(5) unsigned default '3'," } @$questions);
$sql_create .= "\n primary key (ID))\n TYPE=MyISAM;";
my $dbh = DBI->connect ("DBI:mysql:host=$config->{db_host};database=$config->{db_database}",
$config->{db_user}, $config->{db_pass}, {PrintError => 0, RaiseError => 1})
or die "Failed to connect via DBI:$!\n";
$dbh->do($sql_create)
or die "$sql_create: $dbh->errstr\n";
exit;
####
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw ( warningsToBrowser fatalsToBrowser );
use DBI;
use HTML::Template;
use YAML qw ( LoadFile );
my $q = new CGI;
my $action = $q->param('action') || '';
my $what_poll = $q->param('what_poll') || '';
$q->delete('action', 'what_poll');
my $results = $q->Vars;
my ($poll, $questions) = LoadFile("$what_poll.yaml") or die "Sorry, no question-file!\n";
print $q->header();
if ($action eq 'Abstimmen') {
my $dbh = &connect_to_db();
&vote($dbh);
&show_results($dbh);
$dbh->disconnect();
} else {
&fill_form();
}
exit;
# subs
sub fill_form {
my $template = HTML::Template->new(filename => 'templates/poll.tmpl',
die_on_bad_params => 0);
$template->param( 'questions' => $questions,
'description' => ${poll}->{description},
'what_poll' => $what_poll );
print $template->output;
}
sub vote {
my $dbh = shift;
my $sql_insert = "INSERT INTO $what_poll (";
$sql_insert .= join(", ", keys %$results);
$sql_insert .= ") VALUES (";
$sql_insert .= join(", ", values %$results);
$sql_insert .= ");";
my $sth = $dbh->prepare($sql_insert)
or die "$sql_insert: $dbh->errstr\n";
$sth->execute();
$sth->finish();
}
sub show_results {
my $dbh = shift;
my $sql_query = "SELECT ";
$sql_query .= join(", ", map { 'AVG('.$_->{id}.')' } @$questions);
$sql_query .= " FROM $what_poll;";
my $sth = $dbh->prepare($sql_query)
or die "$sql_query: $dbh->errstr\n";
$sth->execute();
my @row = $sth->fetchrow_array();
$sth->finish();
my $sth = $dbh->prepare("SELECT COUNT(*) AS Anzahl FROM $what_poll;")
or die "$sql_query: $dbh->errstr\n";
$sth->execute();
my @count = $sth->fetchrow_array();
$sth->finish();
my @bar = map { sprintf("%u", ($_/5.0)*100) } @row;
map { $_ = sprintf("%.2f", $_) } @row;
$_->{result} = shift @row for @$questions;
$_->{bar} = shift @bar for @$questions;
my $template = HTML::Template->new(filename => 'templates/result.tmpl',
die_on_bad_params => 0);
$template->param( 'public' => ( ${poll}->{public} =~ /yes/i) ? 1 : undef,
'questions' => $questions,
'number_of_votes' => $count[0], );
print $template->output;
}
sub connect_to_db {
my $config = LoadFile('conf/conf.yaml');
my $dbh = DBI->connect ("DBI:mysql:host=$config->{db_host};database=$config->{db_database}",
$config->{db_user}, $config->{db_pass}, {PrintError => 0, RaiseError => 1})
or die "Failed to connect via DBI:$!\n";
return $dbh;
}
####
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw ( warningsToBrowser fatalsToBrowser );
use HTML::Template;
my $q = new CGI;
my @polls = <*.yaml>;
my $polls = [];
map {$_ =~ s/(.*)\.yaml$/$1/} @polls;
foreach my $poll (@polls) {
my %row;
$row{poll} = $poll;
push @$polls, \%row;
}
#die join(", ", @polls);
print $q->header();
my $template = HTML::Template->new(filename => 'templates/what_poll.tmpl',
die_on_bad_params => 1);
$template->param( 'polls' => $polls );
print $template->output;
####
####
Bitte bewerten Sie die einzelnen Fragen von 1 (schlecht) bis 5 (sehr gut).
## ##
Danke für die Teilnahme an dieser Umfrage
px;
height: 14px;
border-width:1px;
border-style:solid;"
src="images/bar.png"
alt="" />
Anzahl abgegebener Stimmen: