<?PHP
//
// -----------------------------------------------------
// copyright 2000 by voxeo corporation. (see LGPL.txt)
//
// -----------------------------------------------------
//
// v1.0 Coded in PHP
// v1.0 Coded by David Grobleski
//
// This application contains both a Web Browser and a phone 
// interface to a consumer poll application similar to those 
// found on CNN.com or other news portals.
// The user is presented with a poll question, and a series 
// of possible responses.  When the user chooses a response, 
// he/she is given the complete poll results.
// Written in PHP as sample code on how to use CallXML and 
// backend scripting languages.  
//
// This zip file contains the following files:				 
//     consumerpoll.mdb		<---- The actual database file itself
//     dbInclude.inc		<---- abstracted ODBC database constants (ASP)
//     LGPL.txt				<---- opensource license agreement
//     readme.txt			<---- Readme file
//     consumerpoll.css		<---- Client side style sheet for web interface
//     default.asp			<---- ASP version for introductory web interface
//     voxeo.asp			<---- ASP version for introductory phone interface
//     gatherpoll.asp		<---- ASP version that gathers consumer inputs for
//								  both web and phone versions
//     default.php			<---- PHP version for introductory web interface
//     voxeo.php			<---- PHP version for introductory phone interface
//     gatherpoll.php		<---- This file.  PHP version that gathers consumer inputs for
//								  both web and phone versions
//     consumerPoll.sql		<---- SQL Scripts to create SQL Version of Database
//								  if desired
//
//     Please ensure that IUSR_Webserver has Read/Write permissions 
//     on the Access Database (if you are using access).  If not, you will 
//     get an error similar to "The operation must be an updatable query".
//
//
// There is one image file that is used to build the graph for the results 
// screen on the web based interface.  It should be placed in a subfolder 
// called "images":
//
// Several audio files are associated with this application.  They 
// should be placed in a subfolder called "voice":
//
//     ad1.wav
//     herestheresults.wav
//     percent.wav
//     q2_options.wav
//     q2_question.wav
//     q2option1.wav
//     q2option2.wav
//     q2option3.wav
//     q2option4.wav
//     q2option5.wav
//     questionheader.wav
//     received.wav
//     represents.wav
//     resultsfooter.wav
//     resultsheader.wav
//     staytuned.wav
//     votes.wav
//
// All audio files are PCM format, 8-bit, mono, unsigned.
//
//------------------------------------------------------------------------


//---------------------------------------------------------------------------
// In a deployed environment a stored procedure would be helpful so when a 
// question's "score" is incremented, concurrency issues are not encountered.
// For example, a situation may occur whereby two users were selecting the 
// same answer at the same time.  If a regular sql statement is used, they 
// could possibly overwrite each other's responses.
//
// For our purposes, we will simply implement a SQL Query to an Access 
// Database for the incrementation and other db calls
//---------------------------------------------------------------------------
      
//------------------------------------------------------------------------
// Include file for DataBase constants 
//------------------------------------------------------------------------

include('database-win32.inc');

global $HTTP_GET_VARS;

$sqlUpdate = "SELECT AnswerCount FROM Answer WHERE answerID = " . $HTTP_GET_VARS["Answer"];

$connPoll = OpenDatabase("ConsumerPoll");

$rsUpdate = ReadDatabase($connPoll, $sqlUpdate);

while ( $rsRow = FetchRowFromDatabase( $rsUpdate ) ) {
	  $iAnswer = GetResultFromRow( "AnswerCount",   $rsRow, $rsUpdate ) + 1;
}  // while statement

$sqlUpdate = "UPDATE Answer SET AnswerCount = " . $iAnswer . " WHERE answerID = " . $HTTP_GET_VARS["Answer"];

$rsUpdate = ReadDatabase($connPoll, $sqlUpdate);

//---------------------------------------------------------------------------
// open the database to retreive the correct answers for the poll question,
// as well as the total "score" for each question and present them to the 
// user
//---------------------------------------------------------------------------

$sqlPoll = "SELECT * from question Q left join answer A on Q.ID=A.QuestionID order by A.answerID";

$rsPoll = ReadDatabase($connPoll, $sqlPoll);

//---------------------------------------------------------------------------
// A querystring parameter is passed to detirmine whether to pass HTML or 
// CallXML to the "browser".  The default is HTML view, but if the parameter 
// Presentation = callXML, the application will return CallXML
//---------------------------------------------------------------------------

if ($HTTP_GET_VARS["Presentation"] == "callXML") { 

//---------------------------------------------------------------------------
// callXML was passed.  Present CallXML to the "browser"
//---------------------------------------------------------------------------

			echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
			echo "<callxml>";
			echo "<assign var=\"audiourl\" value=\"http://orlweb1.voxeo.com/applications/consumerpoll/voice/\"/>";

//---------------------------------------------------------------------------
// Play "Stay Tuned" message and play an advertisement to the user
//---------------------------------------------------------------------------

			echo "<playAudio format=\"audio/wav\" " .
				"value=\"\$audiourl;staytuned.wav\" " .
				"termDigits=\"\" " .
				"clearDigits=\"TRUE\"/>";
			
			echo "<!-- play the Ad -->";

			$rsRow = FetchRowFromDatabase( $rsPoll );			

			echo "<playAudio format=\"audio/wav\" " .
				"value=\"\$audiourl;" . GetResultFromRow( "voiceAd",   $rsRow, $rsPoll ) . "\" " .
				"termDigits=\"\" " .
				"clearDigits=\"TRUE\"/>";

			echo "<playAudio format=\"audio/wav\" " .
				"value=\"\$audiourl;resultsheader.wav\" " .
				"termDigits=\"\" " .
				"clearDigits=\"TRUE\"/>";

//---------------------------------------------------------------------------
//  Play the question
//---------------------------------------------------------------------------

			echo "<playAudio format=\"audio/wav\" " .
				"value=\"\$audiourl;" . GetResultFromRow( "voicequestion",   $rsRow, $rsPoll ) . "\" " .
				"termDigits=\"\" " .
				"clearDigits=\"TRUE\"/>";
				
			echo "<playAudio format=\"audio/wav\" " .
				"value=\"\$audiourl;herestheresults.wav\" " .
				"termDigits=\"\" " .
				"clearDigits=\"TRUE\"/>";

			
			echo "<!-- Calculate and read the results!! -->";

//---------------------------------------------------------------------------
// Calculate the running total for each question, and for total responses
//---------------------------------------------------------------------------

			while ( $rsRow = FetchRowFromDatabase( $rsPoll ) ) {
				  $iRunningTotal = $iRunningTotal + GetResultFromRow( "answerCount",   $rsRow, $rsPoll );
			}  // while statement

		

//---------------------------------------------------------------------------
// Loop through the recordset to read each response and it's corresponding 
// poll performance.  Calculate percentages, and read them to the user 
//---------------------------------------------------------------------------
			$rsPoll = ReadDatabase($connPoll, $sqlPoll);
			while ( $rsRow = FetchRowFromDatabase( $rsPoll ) ) {
				echo "<!-- The answer at hand -->";

				echo "<playAudio format=\"audio/wav\" " .
					"value=\"\$audiourl;" . GetResultFromRow( "voiceanswer",   $rsRow, $rsPoll ) . "\" " .
					"termDigits=\"\" " .
					"clearDigits=\"TRUE\"/>";

				echo "<playAudio format=\"audio/wav\" " .
					"value=\"\$audiourl;received.wav\" " .
					"termDigits=\"\" " .
					"clearDigits=\"TRUE\"/>";

				echo "<playNumber format=\"number\" " .
					"value=\"" . GetResultFromRow( "answercount",   $rsRow, $rsPoll ) . "\" " .
					"termDigits=\"\" " .
					"clearDigits=\"TRUE\"/>";
				

				echo "<playAudio format=\"audio/wav\" " .
					"value=\"\$audiourl;votes.wav\" " .
					"termDigits=\"\" " .
					"clearDigits=\"TRUE\"/>";

				echo "<playAudio format=\"audio/wav\" " .
					"value=\"\$audiourl;represents.wav\" " .
					"termDigits=\"\" " .
					"clearDigits=\"TRUE\"/>";

				echo "<!-- percent of total votes -->";
				$iPercentage = 100 * (GetResultFromRow( "answercount",   $rsRow, $rsPoll ) / $iRunningTotal); 
				echo "<playNumber format=\"number\" " .  
					"value=\"" . number_format($iPercentage, 0) . "\" " .   
					"termDigits=\"\" " .  
					"clearDigits=\"TRUE\"/>";

			}  // while statement
			
//---------------------------------------------------------------------------
// Done looping through each question.  End the callxml code
//---------------------------------------------------------------------------

			echo "</callxml>";

//---------------------------------------------------------------------------
// Default response to the browser if no querystring is HTML.  So the same 
// funtionality above is duplicated, just with HTML tags instead of CallXML 
// tags.  WOW, the voxeo platform is easy.
//---------------------------------------------------------------------------
			
} else {
			echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">" .
			"<html>" ;

			echo "<head><title>Consumer Poll</title>" .
			"<link href=\"voxeo.ums.style.css\" type=\"text/css\" rel=\"stylesheet\">" .
			"</head><body>" .
				"<table class=\"pollTable\">" ;
				$rsRow = FetchRowFromDatabase( $rsPoll );
				echo "<tr class=\"question\"><td colspan=4>" . GetResultFromRow( "textquestion",   $rsRow, $rsPoll ) . "</td></tr>";

//---------------------------------------------------------------------------
// Calculate the running total for each question, and for total responses
//---------------------------------------------------------------------------

			while ( $rsRow = FetchRowFromDatabase( $rsPoll ) ) {
				  $iRunningTotal = $iRunningTotal + GetResultFromRow( "answerCount",   $rsRow, $rsPoll );
			}  // while statement

//---------------------------------------------------------------------------
// Loop through the recordset to present each response and it's corresponding 
// poll performance.  Calculate percentages, and present them to the user 
//---------------------------------------------------------------------------

			$rsPoll = ReadDatabase($connPoll, $sqlPoll);
			while ( $rsRow = FetchRowFromDatabase( $rsPoll ) ) {
						echo "<tr class=\"answer\">";
						echo "<td style=\"margin-right: 10px\" width=\"100\">" . GetResultFromRow( "textanswer",   $rsRow, $rsPoll ) . "</td>";

						$iPercentage = 100 * (GetResultFromRow( "answercount",   $rsRow, $rsPoll ) / $iRunningTotal);
		
						echo "	<td VALIGN=\"MIDDLE\" WIDTH=\"120\" NOWRAP><FONT SIZE=\"1\"><img border=1 style=\"border-color: #808080\" align=\"left\" src=images/red.gif height=20 width=" . 
						$iPercentage . 
						">&nbsp;&nbsp;" . 
						
						number_format($iPercentage, 2) . 
						
						"%</FONT></td>";
						echo "	<td align=\"right\">" . GetResultFromRow( "answercount",   $rsRow, $rsPoll ) . "</td>";
						echo "	<td align=\"left\">votes</td>";
						echo "</tr>";

			}  // while statement
//---------------------------------------------------------------------------
// Done looping through each question.  End the HTML
//---------------------------------------------------------------------------
					
					echo "<TR><TD colspan=4><HR width=\"90%\"></TD></TR>";
					echo "<TR class=\"Answer\"><TD colspan=\"3\" align=\"right\">total: <B>" . $iRunningTotal . "</B></TD><TD>votes</TD></TR>";
					echo "</table></body></html>";
					
}  // else statement - end Presentation QueryString check

?>
