<?php


// this defines some of your basic setup
include_once 'config.php';
// the facebook client library
include_once $BIB.'facebook.php';
include_once $BIB.'bib_generic.php3';
include_once $BIB.'bib_quiz_facebook.php3';
include_once $BIB.'bib_facebook_user.php3';


include 'header.php';


//indique si on est dans un cas de resultat ou non
$in_result = 0;

if ($_POST['QuizFoot']==1) 
{
	$in_result = 1;
	//on contruit la chaine de reponses
	$responsesQuiz = "";
	$i = 0;
	
	while($_POST["q".$i] > 0)
	{	
		$responsesQuiz .= "_".$_POST["r".$i];		
		$i++;
	}	
	
}



if($in_result)
{
?>
	<div id="fiche_resultat">

		<h2>VOS RESULTATS</h2>
		<table border=0 width=100%>
		<tr>
			<td width=50%> 
				<table>
				<tr>
					<td>
						<fb:profile-pic uid="<?=$user?>" size="thumb" linked="true" />						
					</td>
					<td>
						<fb:name firstnameonly="true" uid="<?=$user?>" useyou="false"/> :
						<?php				
						//on récupere les résultats du quiz			
						list($points,$nb_bonnes_reponses,$time_sec) = getResult($user,$responsesQuiz);			
						
							
						echo "<br />votre note : ".$nb_bonnes_reponses." / ".$NB_QUESTIONS."<br />";
						echo "temps de réponse : ".formatTime($time_sec);
						?>
					</td>
				</tr>
				</table>
			</td>				
			<td width=50%>				
				<?php
				//si on a la moyen
				if($nb_bonnes_reponses >= ($NB_QUESTIONS/2))
					$color = "<span class='resultat_win'>";
				else
					$color = "<span class='resultat_loose'>";
				
				echo "<div class='resultat_noir'>Votre Score : ".$color.$points." %</span></div>";
				echo "<i>Joué ".getNbTimesTheme($Id)." fois | ";
				echo "Score moyen ".getMoyenneTheme($Id)." %</i><br />\n";
				
				
				//on ecrit les scores dans la boite du profile du joueur
				$QuizName = getThemeName($Id);
				$QuizLink = getThemeLink($Id);
				$QuizImg = $IMAGES_DIR."Themes/".getImageTheme($Id);
				$profileFBML .= "\t\t<table><tr><td><a href='".getThemeLink($Id)."'><img src='".$QuizImg."' border=0></a></td>\n";
				$profileFBML .= "\t\t<td valign=top>";
				$profileFBML .= "<div style='font-family:tahoma,;font-size:11px;font-weight:bold'><a href='".$QuizLink."'>".$QuizName."</a></div>";
				$profileFBML .= "<div style='font-family:tahoma,;font-size:11px;font-weight:normal'>Mon score : ".$points." %</div>";
				$profileFBML .= "<div style='font-family:tahoma,;font-size:11px;font-weight:bold'><a href='".$QuizLink."'>Battez moi !</a></div>";
				$profileFBML .= "</td></tr></table>\n";					
			
				$profileFBML .= "<div style:'text-align:right'><a style='font-family:tahoma,;font-size:10px;font-weight:normal;' href='".$HISTOQUIZ_PAGE."?friend_user=".$user."'>voir mes stats</a>";
				$profileFBML .= " | <a style='font-family:tahoma,;font-size:10px;font-weight:normal;' href='".$CANVAS_URL."'>plus de QuizFoot</a></div>";
				
				$facebook->api_client->profile_setFBML($api_key, $user, $profileFBML); 
				
				
			    $Pict = array(array('src' => $QuizImg,
			              'href' => $QuizLink
						  ));

				
				
				$title_template ="{actor} a r&eacute;pondu au QuizFoot {quiz}";
				$title_data = '{"quiz":"<a href=\"'.$QuizLink.'\">'.$QuizName.'</a>"}';
				$body_template = "{actor} a fait un score de {score}";
				$body_data= '{"score":"'.$points.'%"}';
				$body_general = '<a href="'.$QuizLink.'">Essayez de le battre !</a>';
				$facebook->api_client->feed_publishTemplatizedAction($title_template,$title_data,$body_template,$body_data,$body_general);
				?>
				
			</td>
		</tr>
		</table>
	</div>
<?php
}
?>




	<div class="quiz" id="quiz">
	
		<h2><?php echo getThemeName($Id);?> </h2>
		<?php
		echo "\t\t<table><tr><td><img src='".$IMAGES_DIR."Themes/".getImageTheme($Id)."'></td>\n";
		echo "\t\t<td valign=top>".getDescriptionTheme($Id)."</td></tr></table><br />\n";	
		?>
		<div id="questions">
		<?php	
			if($in_result)
			{
				echo "<form method='post' action='".$CAVAS_URL.$LISTQUIZ_PAGE."'>";
			}
			else
			{
				echo "<form method='post' >";
			}
			
			
				$questions_quiz = getQuestions($user,$Id,$in_result);
				$i = 0;
				echo "<ul>\n";
				foreach($questions_quiz as $une_question)
				{
					echo "<div class='question clearfix'>";
						echo "<li><h3>Question ".($i+1)."</h3>\n";
						echo "<div class='noir'>".$une_question["Texte"]."</div>\n";
						echo " <input type='hidden' name='q".$i."' value='". $une_question["Id"] ."'/>\n";
						$j = 1;
						
						echo "<ul class='choices'>\n";
						
						$bonne_Reponse_Id = getAnswerId($une_question["Id"]);
						
						if($in_result && !$_POST["r".$i])
						{
							echo "<div class='loose'>Aucune réponse donnée</div><br/>\n";
						}
						
						foreach($une_question["Reponses"] as $une_reponse)
						{
							
							//si on affichage de résultat
							if($in_result)
							{
								//par defaut reponse en noir
								$selected = "";
								$color = "<div class='noir'>";
								
								//si la reponse est la bonne on la met en vert
								if( $bonne_Reponse_Id == $une_reponse["Id"] )
								{
									$color = "<div class='win'>";
								}
								//si la reponse donnée n'est pas la bonne on la met en rouge
								elseif($_POST["r".$i] == $une_reponse["Id"])
								{
									if( $bonne_Reponse_Id  != $une_reponse["Id"] )
									{
										$color = "<div class='loose'>";

									}
									$selected = "checked";
								}
								
								echo $color.$j.". ".$une_reponse["Texte"]."</div>\n";
							}
							//si on affichage de quiz
							else
							{
															
								echo "\t<li>\n";
								echo "\t\t\t<input type='radio' id='reponse".$une_reponse["Id"]."' name='r".$i."' value='" . $une_reponse["Id"] . "' ".$selected."/>\n";
								echo "\t\t\t".$une_reponse["Texte"]."\n";
								echo "\t</li>\n";
							}
							$j++;
							
							
						}						
						echo "</ul>\n";						
						echo "</li>\n";
					echo "</div>\n";
					
					$i++;
				}
				echo "</ul>\n";
				if($in_result)
				{
					
					echo "<input name='retour' value='1' type='hidden'/>";
					echo "<br /><input value='Retour à la liste >>' type='submit' class='inputBouton'/>";
					
				}
				else
				{
					echo "<input type='hidden' name='QuizFoot' value='1' />";
					echo "<br /><input value='Répondre >>' type='submit' class='inputBouton'/>";
				}
				
				
			?>
		   
			</form>
		</div>
	</div>
	<?php
	if($in_result)
	{
	?>
		<div id="autre_quiz">
			<h2>AUTRES QUIZFOOT :</h2>
			<div id="categories">
				<ul>
				<?php
				//on va lister les themes
				$query = "SELECT * FROM Qcm_Themes";
				$rows = main_db_query($query);
				while($row = db_fetch_assoc($rows))
				{
					if($_POST["Id"] != $row["Id"])
					{
						echo "\t<li class='categorie'>\n";
							echo "\t\t<a href='".$QUIZ_PAGE."?Id=".$row["Id"]."'>".$row["Nom"]."</a>\n";
						echo "\t</li>\n";
					}
				}
				?>
				</ul>
			</div>
		</div>
		<br />
		<br />
		<br />
	<?php
	}
	?>
	<div id="promo_right">
		<a href="http://www.kestufoot.com/trck_bannieres.php3?code=FACEBOOK_243" target="_blank"><img src="<?php echo $IMAGES_DIR?>sky_promo.gif" border=0></a>
	</div>

<?php
include 'footer.php';
?>
