<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of inspector
 *
 * @author TyKayn
 */
class inspector {
	public $rendu;
	public $debug;
	public $tabnbgens;
	public $vote_possible;
	
	
	public function barres($tab,$x,$y,$titre,$votes=''){
		if(!empty($tab)){
			if($votes=='')
			{
				foreach($tab AS $k=>$v){
				$votes += $v;
				}
				if($votes=='')
				{
					die("0 votes");
				}
			}
			
			$barres ='';
			foreach($tab AS $k=>$v){
				$taillemaxbarres = 400;
				$part= ceil(100*$v/$votes);
				$part= ceil($part*$taillemaxbarres/100);
				
				$barres .= '<div class="points">'.$k.'</div><div class="barre" style="width:'.$part.'px" >'.$v.'</div><br/>';
			}
			
			$this->rendu .='
				
				<table class="barres_table">
				
				<thead>
				<tr>
				<th> </th>
				<th><h2>'.$titre.'</h2></th>
				</tr>
				</thead>
				
				<tbody>
				<tr>
				<td>'.$x.'</td>
				<td>'.$barres.'</td>
				</tr>
				
				
				
				<tr>
				<td></td>
				<td>'.$y.'</td>
				</tr>
				</tbody>
				
				</table>


';
	}
	else{
		$this->rendu .='<h2> Le sondage n\'a pas encore de votes, soyez la première personne à participer! *0* </h2>';
		
	}
	
}
	
	//fait un graphique comparant les scores hommes femmes inconnu
	public function barreshfi($tabh,$tabf,$tabi,$x,$y,$titre,$votes=''){
		
			
			$tab = array($tabh,$tabf,$tabi);
			
			if($votes=='')
			{
				foreach($tab AS $k=>$v){
				$votes += $v;
				}
				if($votes=='')
				{
					die("0 votes");
				}
			}
			
			$barres ='';
			foreach($tab AS $k=>$v){
				$taillemaxbarres = 400;
				$part= ceil(100*$v/$votes);
				$part= ceil($part*$taillemaxbarres/100);
				$divh='<div class="barre" style="width:'.$part.'px" >'.$v.'</div>';
				$divf='<div class="barre" style="width:'.$part.'px" >'.$v.'</div>';
				$divi='<div class="barre" style="width:'.$part.'px" >'.$v.'</div>';
				
				$barres .= '<div class="points">'.$k.'</div>'.$divf.$divh.$divi.'<br/>';
			}
			
			$this->rendu .='
				
				<table class="barres_table">
				
				<thead>
				<tr>
				<th> </th>
				<th><h2>'.$titre.'</h2></th>
				</tr>
				</thead>
				
				<tbody>
				<tr>
				<td>'.$x.'</td>
				<td>'.$barres.'</td>
				</tr>
				
				
				
				<tr>
				<td></td>
				<td>'.$y.'</td>
				</tr>
				</tbody>
				
				</table>


';
	}
	
	
	
	
	//inspecte les votes et renvoie les scores.
	public function compt($idsondage){
			$comptvotes=0;
			$tabscore = '';
			$tabnbgens ='';
			$tabpseudos ='';
			//connexion
		try{
		// On se connecte à MySQL
				$this->debug.= '<br/><h1>connection...';
		$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
		$bdd = new PDO('mysql:host=localhost;dbname=tykayn', 'tykayn', 'plopplop01', $pdo_options);
		}
			catch(Exception $e){
		die('Erreur : '.$e->getMessage());
		}
		$this->debug.= ' ... connecté.</h1><br/>';

		
		
		//récup des questions	//////////////////////////////////////////////////////////////////////
		//	
		$reponse = $bdd->query('SELECT * FROM tkp_votes WHERE num_q="score" && id_sondage='.$idsondage.' ORDER BY datetime ASC');
			while ($donnees = $reponse->fetch())
			{
				if(!isset($tabnbgens[$donnees['reponse']])){
					$tabnbgens[$donnees['reponse']] ='';
				}
				if(!isset($tabpseudos[$donnees['reponse']])){
					$tabpseudos[$donnees['reponse']] ='';
				}
					$comptvotes ++;
					$tabscores[$comptvotes] =$donnees['reponse'] ;
					$tabnbgens[$donnees['reponse']]+=1 ;
					$tabpseudos[$donnees['reponse']] .= $donnees['votant'] . ',' ;
					$dernvote = $donnees['datetime'];
					$dernpseudo = $donnees['votant'];
					
			
			}
				//$tabdepoints [Q] = 'pointsoui,pointsnon'
			$reponse->closeCursor();
			//print_r($tabscores);
			//	print_r($tabnbgens);
			
			$this->tabnbgens = $tabnbgens;
			//print_r($tabpseudos);
			$this->rendu .='<h2>Pour ce sondage il y a eu '.$comptvotes.' votes </h2> Dernier Vote par: '.$dernpseudo.' , le '.$dernvote;
			$this->rendu .='<h2>scores </h2> ';
			//tri du tab des scores => nb de gens
			// sort($tabnbgens);
			//*	foreach($tabnbgens AS $k=>$v){
			/*		$this->rendu .= $k.' points = '.$v.'personnes <br/>';
			*
			*	}
			*/
			
	}
	
	public function stat($idsondage){
			$this->rendu .='';
			echo $this->rendu;
	}
	
	public function rendu(){
	
		echo $this->rendu;
	}
	
	
		public function verif_ip($sondage,$permissif='permissif')
	{	
			
			//connection
		try{	$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
		$bdd = new PDO('mysql:host=localhost;dbname=tykayn', 'tykayn', 'plopplop01', $pdo_options);
		}
			catch(Exception $e)  {  die('Erreur : '.$e->getMessage());
		}
			//voir si l'ip du client est déjà dans la table tkp_votes
			$ip_client = $_SERVER['REMOTE_ADDR'];
			try{
			$qpointsval = $bdd->query('SELECT * FROM tkp_votes WHERE ip ="'.$ip_client.'" && id_sondage="'.$sondage.'" GROUP BY votant');
			}
			catch(Exception $e)  {  die('Erreur : '.$e->getMessage());
		}
		$dejavoted =0;
		$pseudos_deja_voted='Sous le pseudo: ';
					while(	$data = $qpointsval->fetch() ){
						$dejavoted++;
						$pseudos_deja_voted.= $data['votant'].', ';
					
					}
						
				//	echo "salut  $ip_client , tu as $dejavoted[0] votes.  <br/>";		
							
		if($dejavoted!=0){
			echo $pseudos_deja_voted.'<br/> Vous avez déjà voté à ce sondage.<h2> Et ouais.</h2>';
			if ($permissif!='permissif'){
			$this->vote_possible = 0;
			}
			else{
				$this->vote_possible = 1;
			}
		
		}
		
	}
}

?>
