#!/usr/bin/perl -w
use strict;
use DBI;
sub save_response
{
my $dbh = shift(@_);
my $survey_id = shift(@_);
my $taker_id = shift(@_);
my $all_qs = shift(@_);
my $params = shift(@_);
# if the take has already entered survey data for this survey, deleted and
# add it back in. No need for dups.
my $sr_id = dbi_exec_for_scalar($dbh, "SELECT id FROM survey_responses WHERE taker_id = ? AND survey_id = ?", $taker_id, $survey_id);
if($sr_id)
{
$dbh->do("DELETE FROM survey_data WHERE sr_id = ?", {}, $sr_id);
$dbh->do("DELETE FROM survey_responses WHERE taker_id = ? AND survey_id = ?", {}, $taker_id, $survey_id);
}
$dbh->do("INSERT INTO survey_responses (taker_id, survey_id) VALUES(?, ?)", {}, $taker_id, $survey_id);
$sr_id = dbi_exec_for_scalar($dbh, "SELECT id FROM survey_responses WHERE taker_id = ? AND survey_id = ?", $taker_id, $survey_id);
foreach (@{$all_qs})
{
$dbh->do("INSERT INTO survey_data (sr_id, question_id, text) VALUES(?, ?, ?)", {}, $sr_id, $_, $params->{$_});
}
}
sub add_taker
{
my $dbh = shift(@_);
my $email = shift;
my $taker_id = dbi_exec_for_scalar($dbh, "select id from survey_takers where email = ?", $email);
return $taker_id if($taker_id);
$dbh->do("insert into survey_takers (email) values(?)", {}, $email);
return(dbi_exec_for_scalar($dbh, "select id from survey_takers where email = ?", $email));
}
sub highlight_errors
{
# will not highlight unless you've defined a message in the dfv profile
my ($content, $errors) = @_;
my $error_start = '';
my $error_end = '';
$$content =~ s#(.*?)#
my $name = $1;
if($name)
{
if(exists($errors->{$name}))
{
print STDERR "$name\n";
qq{$error_start$2$error_end}
}
else { $2 }
} #seig;
return($$content);
}
sub db_connect
{
return DBI->connect( 'dbi:Pg:dbname=falcon;host=db1.netdojo.com', 'falcon', 'dbp@ss', { RaiseError => 1, AutoCommit => 1 } ) || die "$DBI::errstr";
}
sub DBI_CACHE_STMT { 1 }
sub prepare_and_exec
{
my $dbh = shift;
my $flags = 0;
$flags = shift if ($_[0] =~ /^\d+$/);
my ($query, @binds) = @_;
my $sth = (($flags & DBI_CACHE_STMT) ? $dbh->prepare_cached($query) : $dbh->prepare($query))
or croak "error preparing query: ".$dbh->errstr." ($query)";
$sth->execute(@binds) or croak "error executing query: ".$dbh->errstr." ($query)";
return $sth;
}
sub dbi_exec_for_column_list { dbi_fetch_column_list (prepare_and_exec(@_)) }
sub dbi_exec_for_scalar { dbi_fetch_scalar (prepare_and_exec(@_)) }
sub dbi_exec_for_loh { dbi_fetch_loh (prepare_and_exec(@_)) }
sub dbi_fetch_column_list {
my ($sth) = shift;
my $lref = $sth->fetchall_arrayref([0]);
return map($_->[0],@$lref);
}
sub dbi_exists
{
my $sth = prepare_and_exec(@_);
return defined($sth->fetchrow_arrayref);
}
sub dbi_fetch_scalar
{
my $sth = shift;
die "query asks for ".$sth->{NUM_OF_FIELDS}." fields, not 1" if $sth->{NUM_OF_FIELDS} != 1;
my $lref = $sth->fetchrow_arrayref;
return undef if !defined($lref);
die "query returned more than one value" if (defined($sth->fetchrow_arrayref));
return $lref->[0];
}
sub dbi_fetch_loh
{
my $sth = shift;
my $lref = $sth->fetchall_arrayref({});
return @$lref;
}
sub params_hash
{
my $cgi = shift(@_);
my @args = $cgi->param;
my %params;
foreach (@args)
{
$params{$_} = $cgi->param($_);
}
return(\%params);
}
sub cleanup
{
my $dbh = shift(@_);
$dbh->disconnect();
}
1;