<?
// survey.php3
//
// Original Author Unknown
// Converted from php to php3
// by Jeff Schmitt
// Towson University
// March, 1998
?>

<html><head><title>Example Survey</title></head>
<body bgcolor="#cccccc">
<center><h2>Example Survey</h2></center>

<?
function printresults() {
global $r,$total;
echo "<TABLE BORDER>\n";
echo "<TR><TH>Survey<BR>Choice<TH>Frequency\n";
echo "<TR><TH>A<TD ALIGN=CENTER>$r[0]\n";
echo "<TR><TH>B<TD ALIGN=CENTER>$r[1]\n";
echo "<TR><TH>C<TD ALIGN=CENTER>$r[2]\n";
echo "<TR><TH>D<TD ALIGN=CENTER>$r[3]\n";
echo "<TR><TH>TOTAL<TD ALIGN=CENTER>$total\n";
echo "</TABLE>\n";
}

// Set this to a writable dir accessed by HTML
$survey_www_dir = "~schmitt/tmp";

// Set this to the Unix equivalent of the above
$survey_real_dir = "/usr/faculty/schmitt/WWW/tmp";

// Set this to the name of the survey
$survey_name = "example";

// database name
$dbm = $survey_real_dir."/".$survey_name;

// The following is to clean up old gif files that are lying around.
// Unfortunately we cannot delete the temporary gif file right after
// creating it and sending the img tag because there is no way to know
// when httpd is done sending it to the client. So, every time someone
// brings up this survey page we'll scan for old files older than 10 minutes
// and delete them.
$t = time() - 600;
opendir($survey_real_dir);
$file = readdir();
while ($file) {
if (ereg("simg.*\.gif$",$file)) {
$full = $survey_real_dir."/".$file;
if (filemtime($full) < $t) {
unlink($full);
}
}
$file = readdir();
}
closedir();

if ($submit) {
/* If the form was submitted, record result in a dbm file */
if ($survey) {
if (!fileinode("$dbm.pag")) {
$dbmid=dbmopen($dbm,"n"); // file does not exist
} else {
$dbmid=dbmopen($dbm,"w");
}
dbminsert($dbmid,date("U"),$survey." ".$REMOTE_HOST);
dbmclose($dbmid);
// debug code:
// echo "DEBUG: dbminsert($dbmid,",date("U"),
// ",$survey$REMOTE_HOST<BR>";
}
?>
<center><h2>Survey Response</h2></center>
Thank you for submitting the survey
<p><center> <a href="<?echo $PHP_SELF;?>">[Survey Question]</a></center>
<p><center>
<a href="<?echo $PHP_SELF;?>?results=1">[See Results]</a>
</center>

<?
} else if (!$results) {
?>

This is an example survey which can be used as a template for
a more complete survey script. It is completely written in the PHP
Script language using PHP's GD support to draw the charts on the
results page.<p>

What do you think of the language PHP for programming interactive web
applications?

<form action="<?echo $PHP_SELF;?>" method="POST">
<input type="radio" name="survey" value="a"> <b>A.</b> It is poor.<br>
<input type="radio" name="survey" value="b"> <b>B.</b> It is good but
not great.<br>
<input type="radio" name="survey" value="c" CHECKED> <b>C.</b> It is
a great language -- one of the best.<br>
<input type="radio" name="survey" value="d"> <b>D.</b> What is it?<br>
<input type="submit"name="submit" value=" Submit ">
</form>
<center>
<a href="<?echo $PHP_SELF;?>?results=1">[See Results]</a>
</center>
<?
}

if ($results) {
if (!fileinode("$dbm.pag")) {
echo "No results yet. Why don't you start it off?<p>";
} else {
echo "<center><h2>Survey Results</h2></center>\n";
// If there are results in the dbm file, display them graphically
$dbmid=dbmopen($dbm,"r");
$key = dbmfirstkey($dbmid);
$r[0] = 0;
$r[1] = 0;
$r[2] = 0;
$r[3] = 0;
while ($key) {
$res = dbmfetch($dbmid,$key);
$a=strtok($res," ");
if ($a>="a" && $a<="d") {
$r[ord($a)-ord("a")]++;
}
$key=dbmnextkey($dbmid,$key);
}
dbmclose($dbmid);
$xm = 4 * 32;
// find maximum y
$total=$r[0];
$ym=$r[0];
for ($i=1;$i<4;$i++) {
$total+=$r[$i];
if ($r[$i]>$ym) {
$ym=$r[$i];
}
}
/// $ym = max($r); // this doesn't work
printresults();

$ym = $ym+12;
$im = ImageCreate($xm,$ym+12);
$black = ImageColorAllocate($im, 0, 0, 0);
$white = ImageColorAllocate($im,255,255,255);
$red = ImageColorAllocate($im,255, 10, 10);
$green = ImageColorAllocate($im, 10,255, 10);
$blue = ImageColorAllocate($im, 10, 10,255);
$yellow = ImageColorAllocate($im,255,255, 0);


// Now draw the simple bar chart
// start by clearing the background to white
// which will be the transparent color which will
// show through to the browser's background color
ImageFilledRectangle($im,0,0,$xm,$ym+12,$white);
ImageColorTransparent($im,$white);
ImageFilledRectangle($im,0,$ym-$r[0],$xm/4,$ym,$red);
ImageFilledRectangle($im,$xm/4+1,$ym-$r[1],$xm/2,$ym,$yellow);
ImageFilledRectangle($im,$xm/2+1,$ym-$r[2],3*$xm/4,$ym,$blue);
ImageFilledRectangle($im,3*$xm/4+1,$ym-$r[3],$xm,$ym,$green);
ImageString($im,3,$xm/8-6,$ym-$r[0]-12,$r[0],$black);
ImageString($im,3,$xm/4+$xm/8-6,$ym-$r[1]-12,$r[1],$black);
ImageString($im,3,$xm/2+$xm/8-6,$ym-$r[2]-12,$r[2],$black);
ImageString($im,3,3*$xm/4+$xm/8-6,$ym-$r[3]-12,$r[3],$black);
ImageChar($im,3,$xm/8-1,$ym+1,"A",$black);
ImageChar($im,3,$xm/4+$xm/8-1,$ym+1,"B",$black);
ImageChar($im,3,$xm/2+$xm/8-1,$ym+1,"C",$black);
ImageChar($im,3,3*$xm/4+$xm/8-1,$ym+1,"D",$black);
ImageInterlace($im,1);
$fn = tempnam($survey_real_dir,"simg");
$fn = $fn.".gif";
ImageGif($im,$fn);
chmod($fn,0644);
ImageDestroy($im);
$nfn = strrchr($fn,"/");
$nfn = $survey_www_dir.$nfn;
echo "<center><img src=\"http://triton.towson.edu/$nfn\"".
"width=$xm height="; echo $ym+12,">\n";
echo "<HR NOSHADE>\n";

// now do the pie chart
include "piechart.php3";

$vals = array(
array($r[0], "Answer A", 255, 0, 0),
array($r[1], "Answer B", 255, 255, 0),
array($r[2], "Answer C", 0, 0, 255),
array($r[3], "Answer D", 0, 255, 0)
);


$heads = array(
array("PHP Survey", 4, "c"), // centered
array("Towson University", 4, "l"), // left justfied
array("Jeff Schmitt", 4, "r") // right justified
);


$pie = new piechart;
$pie->init(400, 300, $vals);
$pie->draw_heading($heads);
$pie->set_legend_percent();

// compute a temporary file name
$fn = tempnam($survey_real_dir,"simg");
$fn = $fn.".gif";
$pie->display($fn);
chmod($fn,0644);

$nfn = strrchr($fn,"/");
$nfn = $survey_www_dir.$nfn;
echo "<center><img src=\"http://triton.towson.edu/$nfn\"></center>\n";




}
echo "<br><center> <a href=\"$PHP_SELF\">[Survey Question]</a></center>\n";
}

include "footer.inc";

?>
</body>
</html>