<?php
include('../../config/config.php');
include('../classes/class.Vote.php');

$objVote=new Vote();

if(isset($_REQUEST['questionId']))
{
	$objVote->question=$_REQUEST['questionId'];
	$objVote->answer=$_REQUEST['answer'];
	
	$objVote->saveQuestionAnswer();
	 
	$lastId=$objVote->getLastInsertId();
	array_push($_SESSION['QuestionAttempt'],$lastId);
	
	$inserted=$objVote->wasInserted();
	
	$questionArr=array();
	$questionArr=$_SESSION['QuestionLeft'];
	unset($_SESSION['QuestionLeft']);
	
	$remainingQuestion=array();
	
	if($inserted)
	{
		foreach($questionArr as $arr)
		{
			if($arr!=$objVote->question)
			{
				array_push($remainingQuestion,$arr);
			}
		}
		
		$i=0;
		if($remainingQuestion)
		{
		  foreach($remainingQuestion as $quest)
		  {
			 $_SESSION['QuestionLeft'][$i]=$quest; 
			 $i++; 
		  }
	?>
	   <h2 id="introduction">Question # <?php echo $_SESSION['TotalQuestion']-sizeof($_SESSION['QuestionLeft'])+1;?>/<?php echo $_SESSION['TotalQuestion'];?></h2>
		<?php
              $questionId=$_SESSION['QuestionLeft'][0];
              $objVote->question=$questionId;
              $questionData=$objVote->getQuestionData();
		?>
		<p><?php echo $questionData[0]->Heading;?></p>
		<div id="question">
		<?php 
		  echo $questionData[0]->Question;
		 ?>
		</div>
		<input type="hidden" id="questionId" value="<?php echo $_SESSION['QuestionLeft'][0];?>"/>
	<?php
		}
		else
		{
			echo "finish";
		}
		
	}
}

if(isset($_REQUEST['startVoting']))
{
	$questions=$objVote->loadQuestions();
	
	unset($_SESSION['QuestionAttempt']);
	unset($_SESSION['QuestionLeft']);
	unset($_SESSION['TotalQuestion']);
	$_SESSION['QuestionAttempt']=array();
	
	if($questions)
	{
		$i=0;
		foreach($questions as $question)
		{
	      $_SESSION['QuestionLeft'][$i]=$question->Id;
		  $i++;		
		}
		$_SESSION['TotalQuestion']=sizeof($_SESSION['QuestionLeft']);
	
		echo "1";
	}
	else
	{
		echo "0";
	}
	
}

?>