#!/opt/perl/bin/perl
#
#       This script will take a student's survey for political questions
#       and let them know where they lie on a political party graph.
#
#               James Judd juddjam@eng.auburn.edu 960729
#       for Auburn University's Political Science Department
#               under the MCI grant for U102 Online
#

print "Content-type: text/html\n\n";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);

    # Un-Webify plus signs and %-encoding
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    # Stop people from using subshells to execute commands
    $value =~ s/~!/ ~!/g; 

    #print "Setting $name to $value<BR>";

    $FORM{$name} = $value;
}

$filepath = "/home/duc1/juddjam/public_html/pquiz/data.txt";

print STDOUT <<EOT;
<HTML>
<HEAD>
<TITLE>Political Quiz scoring page</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#DD0000">
EOT


foreach $i (1 .. 20) {
  $indexer = "q$i";
  if ("$FORM{$indexer}" == "") {
print STDOUT <<EOT;
<CENTER><H2>Survey Incomplete</H2></CENTER>
<P>
Your response to question $i is not filled in.  Please click back and review your answers.  Your results cannot be calculated until all the questions have
been completed.
</BODY>
</HTML>
EOT
exit;
  }
  else
  {
  $cumulator1 = $cumulator1 + $FORM{$indexer};
  }
}

foreach $i (21 .. 40) {
  $indexer = "q$i";
  if ("$FORM{$indexer}" == "") {
print STDOUT <<EOT;
<CENTER><H2>Survey Incomplete</H2></CENTER>
<P>
Your response to question $i is not filled in.  Please click back and review your answers.  Your results cannot be calculated until all the questions have
been completed.
</BODY>
</HTML>
EOT
exit;
  }
  else
  {
  $cumulator2 = $cumulator2 + $FORM{$indexer};
  }
}

$result1 = $cumulator1/20;
$result2 = $cumulator2/20;
  
print STDOUT <<EOT;
<CENTER><H2>Survey Results</H2></CENTER>
<P>The goal of this questionnaire is to gauge...
<P><B>Economic Freedom Index score: $result1 </B>
<BR>
<B>Personal Freedom Index score: $result2 </B>
<P>
EOT
if ("$result1" > 3.5 && "$result1" < 4.5 && "$result2" > 3.5 && "$result2" < 4.5) {
  print "Your results indicate your basic ideological orientation is:<P>\n<FONT SIZE=6>D</FONT><FONT SIZE=5>ivided</FONT> or <FONT SIZE=6>A</FONT><FONT SIZE=5>pathetic</FONT>";
  }
elsif ("$result1" >= 4 && "$result2" >= 4) {
  print "Your results indicate your basic ideological orientation is:<P>
<FONT SIZE=6>L</FONT><FONT SIZE=5>ibertarian</FONT><P>";
}
elsif ("$result1" >= 4 && "$result2" <= 4) {
  print "Your results indicate your basic ideological orientation is:<P>
<FONT SIZE=6>C</FONT><FONT SIZE=5>onservative</FONT><P>";
}
elsif ("$result1" <= 4 && "$result2" => 4) {
  print "Your results indicate your basic ideological orientation is:<P>
<FONT SIZE=6>L</FONT><FONT SIZE=5>iberal</FONT><P>";
}
elsif ("$result1" <= 4 && "$result2" <= 4) {
  print "Your results indicate your basic ideological orientation is:<P>
<FONT SIZE=6>P</FONT><FONT SIZE=5>opulist</FONT><P>";
}

open(DATAFILE, ">> $filepath") || print "Cannot open file for data output!!: $!";
print DATAFILE "Race: \t\t$FORM{q41}\n";
print DATAFILE "Sex: \t\t$FORM{q42}\n";
print DATAFILE "State: \t\t$FORM{q43}\n";
print DATAFILE "Parent: \t$FORM{q44}\n";
print DATAFILE "GovtEmp: \t$FORM{q45}\n";
print DATAFILE "Union: \t\t$FORM{q46}\n";
print DATAFILE "Religion: \t$FORM{q47}\n";
print DATAFILE "Party: \t\t$FORM{q48}\n";
print DATAFILE "View: \t\t$FORM{q49}\n";
print DATAFILE "Registered: \t$FORM{q50}\n";
print DATAFILE "Election1: \t$FORM{q51}\n";
print DATAFILE "Election2: \t$FORM{q52}\n";
print DATAFILE "______________________________________________________\n";
close(DATAFILE);

print STDOUT <<EOT;
<applet codebase="http://www.auburn.edu/~juddjam/pquiz" code=pol.class width=450 height=470><param name="img1" value="ele.gif"><
param name="img2" value="lp-logo-t.gif"><param name="img3" value="populist.gif">
<param name="img4" value="leftdonkey.gif"><param name="data1" value="http://www.auburn.edu/~juddjam/pquiz/Winter1996"
><param name="data2" value="http://www.auburn.edu/~juddjam/pquiz/Spring1996"><param name="data3" value="http://www.auburn.edu/~juddjam/pquiz/Summer1996"><
param name="numSets" value="3"></applet>
<P>
Further information about the political parties links...
</BODY>
</HTML>
EOT
