<?php
class Solution
{
  private $idsolution;
  private $hprc;
  private $lprc;
  private $pcc;
  private $mark;
  private $rank;
  private $normalScore;
  private $candidate;
  
  public function __construct($id,$hp,$lp,$pc,$m,$r,$n,$c)
  {
    $this->idsolution = $id;
    $this->hprc = $hp;
    $this->lprc = $lp;
    $this->pcc = $pc;
    $this->mark = $m;
    $this->rank = $r;
    $this->normalScore  = $n;
    $this->candidate = $c;
  }

  public function getIdSolution()
  {
    return $this->idsolution;
  }
  public function setIdSolution($p_id)
  {
    $this->idsolution = $p_id;
  }
  public function getHPRC()
  {
    return $this->hprc;
  }
  public function setHPRC($p_h)
  {
    $this->hprc = $p_h;
  }
  public function getLPRC()
  {
    return $this->lprc;
  }
  public function setLPRC($p_l)
  {
    $this->lprc = $p_l;
  }
  public function getPCC()
  {
    return $this->pcc;
  }
  public function setPCC($p_c)
  {
    $this->pcc = $p_c;
  }
  public function getMark()
  {
    return $this->mark;
  }
  public function setMark($p_m)
  {
    $this->mark = $p_m;
  }
  public function getRank()
  {
    return $this->rank;
  }
  public function setRank($p_s)
  {
    $this->rank = $p_s;
  }
  public function getNormalizedScore()
  {
    return $this->normalScore;
  }
  public function setNormalizedScore($n)
  {
    $this->normalScore = $n;
  }
  public function getCandidateName()
  {
    $bdd = BDD::getBDD();
    $req = $bdd->prepare('SELECT candidate_name from Candidates2005 where id=:i');
    $req->execute(array('i'=>$this->candidate));
    $result = $req->fetch();
    return $result['candidate_name'];
  }
  public function setCandidate($can)
  {
    $this->candidate = $can;
  }

  public function computeNormalizedMark($id_instance)
  {
    	$bdd = BDD::getBDD();
    if($this->mark!=-1)
      {
	$req = $bdd->prepare('select distinct averageScore from InstancesX i, Solutions2005 s where s.numInstance=i.id_instance and id_instance=:i');
	$req->execute(array('i'=>$id_instance));
	$result = $req->fetch();
	$average = $result['averageScore'];
	$req = $bdd->prepare('select mark from Solutions2005 where numInstance=:i and mark>0 order by mark limit 1');
	$req->execute(array('i'=>$id_instance));
	$result = $req->fetch();
	$bestScore=  $result['mark'];
	$req = $bdd->prepare('select mark from Solutions2005 where numInstance=:i and mark>0 order by mark DESC limit 1');
	$req->execute(array('i'=>$id_instance));
	$result = $req->fetch();
	$worstScore = $result['mark'];
	$normalizedScore = ($this->mark - $average)/($bestScore - $average);
	$finalNormalized = pow(2,$normalizedScore) - 1;
	//echo '<b>Mark : </b>' . $this->mark . ' <b>Normalized : </b> ' . $normalizedScore . ' <b>Final : </b>' . $finalNormalized . '<br />';

	//$normalizedScore = ($bestScore - $this->mark)/($bestScore - $worstScore);
	$this->normalScore = $finalNormalized;
      }
    else
      {
	$this->normalScore=-1;
      }
    $req = $bdd->prepare('UPDATE Solutions2005 SET normalized_mark=:n where id_Solution=:i');
    $req->execute(array('n'=>$this->normalScore,
			'i'=>$this->idsolution));

  }
}
?>