<?php

class clsItemRating extends clsItemDB
{
    
    function clsItemRating($RatingId=NULL)
    {
        $this->clsItemDB();
        $this->tablename = GetTablePrefix()."ItemRating";
        $this->id_field = "RatingId";
        $this->type=-10;
        if($RatingId!=NULL)
            $this->LoadFromDatabase($RatingId);
    }

    function Validate()
    {
        global $Errors;
        
        $dataValid = true;
        if(!isset($this->m_IPAddress))
        {
            $Errors->AddError("error.fieldIsRequired",'IPAddress',"","",get_class($this),"Validate");
            $dataValid = false;
        }
        
        if(!isset($this->m_CreatedOn))
        {
            $Errors->AddError("error.fieldIsRequired",'CreatedOn',"","",get_class($this),"Validate");
            $dataValid = false;
        }


        if(!isset($this->m_VoteRating))
        {
            $Errors->AddError("error.fieldIsRequired",'VoteRating',"","",get_class($this),"Validate");
            $dataValid = false;
        }

        if($this->m_VoteRating > 5 || $this->m_VoteRating < 1 || !is_int($this->m_VoteRating))
        {
            $Errors->AddError("error.invalidVoteRaiting","","","",get_class($this),"Validate");
            $dataValid = false;
        }

        return $dataValid;
    }
    
    function Create()
    {
        global $REMOTE_ADDR;
        

        $this->Set("CreatedOn",adodb_date("U"));
        $this->Set("IPAddress",$REMOTE_ADDR);
        if(ip_exists($this->Get("IPAddress"),$this->Get("ItemId"),$this->SourceTable))
        {
           //$Errors->AddError("error.already_rated","","","","clsItemRating","Create");
           return false;
        }

        parent::Create();

        return true;
    }
    
    function LoadFromDatabase($Id)
    {
        global $Errors;

        if(!isset($Id))
        {
            $Errors->AddError("error.AppError",NULL,'Internal error: LoadFromDatabase id',"",get_class($this),"LoadFromDatabase");
            return false;
        }
        
        $sql = sprintf("SELECT * FROM ItemRating WHERE RatingId = '%s'",$Id);
        $result = $this->adodbConnection->Execute($sql);
        if ($result === false)
        {
            $Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"LoadFromDatabase");
            return false;
        }
        $data = $result->fields;
        $this->SetFromArray($data);
        $this->Clean();
        return true;
    }
    
} /*clsItemRating*/

class clsItemRatingList extends clsItemCollection 
{
    var $ItemResourceId = 0;

    function clsItemRatingList($ItemId=NULL)
    {
        $this->clsItemCollection;
        $this->SourceTable = GetTablePrefix()."ItemRating";
        if($ItemId)
        {
            $this->ItemResourceId = $ItemId;
            $this->LoadItemVotes($ItemId);
        }
    }

    function LoadItemVotes($ItemId=NULL)
    {
        if(!$ItemId)
            $ItemId = $this->ItemResourceId;
        if($ItemId)
        {
            $sql = "SELECT * FROM ".$this->SourceTable." WHERE ItemId=$ItemId";
            return $this->Query_Item($sql);
        }
        else
            return NULL;
    }
}
/*
function RatingImage($n)
{
    if($n<0.25 || $n>5)
		return "0";
	if($n>=0.25 && $n<0.75)
		return "0_half";
	if($n>=0.75 && $n<1.25)
		return "1";
	if($n>=1.25 && $n<1.75)
		return "1_half";
	if($n>=1.75 && $n<2.25)
		return "2";
	if($n>=2.25 && $n<2.75)
		return "2_half";
	if($n>=2.75 && $n<3.25)
		return "3";
	if($n>=3.25 && $n<3.75)
		return "3_half";
	if($n>=3.75 && $n<4.25)
		return "4";
	if($n>=4.25 && $n<4.75)
		return "4_half";
	if($n>=4.75 && $n<=5)
		return "5";
}
*/
function RatingTickImage($n, $OnImage, $OffImage)
{
    $a = array();
    for($x=1;$x<6;$x++)
    {
      if($n >= $x)
      {
          $a[$x] = $OnImage;
      }
      else
        $a[$x] = $OffImage;
    }
    return $a;
}

function RatingText($n)
{
  $text = array(language("lu_rating_0"),language("lu_rating_1"),language("lu_rating_2"),language("lu_rating_3"),language("lu_rating_4"),language("lu_rating_5"));

  $n = round($n);
  return $text[$n];
}
?>