<?php
/* This page implements an opinion poll for a yes/no question
	One person one vote - attempt to enforce this with a cookie. 3 cases:
   1. New user, give a form (no cookie, no vote)
   2. Vote to be tallied    (no cookie, vote in Q.Str.) -- also give result
   3. Previously voted	    (cookie set)	just give current result
	Author: Lin Jensen, for csc103 class
	November 7, 2008
*/
$ftnfile = 'tally.php';  // or 'tally-db.php';
error_reporting(E_ALL ^ E_NOTICE);
if ($_COOKIE['voted']!='true')
{
    if (!isset($_GET['vote']))
    {  // give the form			?>
	<html><head>  
	  <title>yes-no vote form</title>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<style>
 form  {background-color:#c8f; padding-left:5em;} 
</style>
	</head><body onload="document.forms[0].reset()">
	<img src="http://cs-linux.ubishops.ca/%7Ejensen/coat-arm2.jpg
" align=left>
<p>Bishop's University has an official coat of arms, designed by the Canadian College of Heralds in 1993,
 for the Sesquicentennial.
</p><p>
Recently the logo on the BU website was changed to a design that is not acceptable to the college,
in that the Latin motto is removed, and the crest resembles more that of the Anglican Diocese of Ontario.
</p>
<p>Last spring, several petitions and representations were made by faculty and students, to the principal,
 calling for a return to the official crest, with the motto. The response so far has
been, don't expect us to throw away stationary already printed. 
However, to revert the website is a simple matter. 
So the question today is: </p>
<h1>Which logo do you prefer for the BU website?</h1>
	<p>Your opinion?</p>
<SCRIPT>
function checkform(f) {
if (f.name.value=='')
{ alert("Type your name")
  return false
}
if (!(f.vote[0].checked || f.vote[1].checked)) 
{	alert('Make up your mind!'); 
	return false;
}
return true
}
</script>
	<form action=yesnovote.php method=get onsubmit="return checkform(this)">
		<br><input type=radio name=vote value=yes> <img src=
"http://www.ubishops.ca/fileadmin/templates/new2011/images/BU-logo-purple.png"
align=middle style="background:white">
<br><br>
		<input type=radio name=vote value=no> 
<img src="http://cs.ubishops.ca/images1/BU-logo-purple2.png"
align=middle><br><br>
Your name: <input name=name><br><br>
	<input type=submit
		value="Register my opinion"> <input type=reset>
	</form>
	<hr>
					<?php
	die("You will see the results so far after you have voted");
    }
    else
    {  // tally the vote
	$vote = $_GET['vote'];
	$name= $_GET['name'];
	require_once($ftnfile);
	tally($vote, $name);
	setcookie('voted','true', time()+3600*24*30);	// to prevent extra votes
    }
}
// now give result
require_once($ftnfile);
gettally($yes, $no);
$both = $yes+$no;
?>
<html><head>  
  <title>yes-no result</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head><body>
<img src="http://cs-linux.ubishops.ca/%7Ejensen/coat-arm2.jpg
" align=left>
<?php if($vote) print "You just voted $vote<br>"; 
	if (!$both) die ("Nobody has voted yet... curious.");
	$yp=round($yes/$both*100);	// percentages
	$np=round($no/$both*100);
	print "<img src=\"bargraph.php?yes=$yp&no=$np&ylab=New&nlab=Old\" align=right>";
?>
<h1>Results</h1>
<h2>This week's question: Which logo do you prefer?</h2>
So far, here's what you are saying:
<table cellpadding=6 bgcolor=#aaff88 align=center>
<tr><td>New</td><td> <?php print $yes;?> </td><td> <?php print $yp=round($yes/$both*100).'%';?></td></tr>
<tr><td>Old</td><td> <?php print $no;?> </td><td> <?php print $np=round($no/$both*100).'%';?></td></tr>
</table>
<hr>
<a href=index.html>Home</a>

</body></html>