<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: 			*/

// +----------------------------------------------------------------------+

// | PHP version 4/5                                                      |

// +----------------------------------------------------------------------+

// | Copyright (c) 2004-2010 ARMIA INC                                    |

// +----------------------------------------------------------------------+

// | This source file is a part of iScripts VistaCart                     |

// +----------------------------------------------------------------------+

// | Authors: Jipson<jipson.thomas@armiasystems.com>             		  |

// +----------------------------------------------------------------------+ 

class Rating extends AppModel

{

   var $name 	= 'Rating';

   var $uses  	= array('Product','User','Productimages'); 

  

  ###### Function toget average rating f a product.............

  function avgrate($pid){

  	$Db = ConnectionManager::getDataSource($this->useDbConfig);

	$rttbl = $Db->fullTableName('ratings');

	$cnt			=	$this->findCount('Rating.product_id = '.$pid);

	if($cnt > 0){

		$rat		=	$this->query("SELECT SUM(rate ) AS trate FROM $rttbl WHERE product_id = ".$pid);

		$totrate	=	$rat[0][0]["trate"];

		$avg		=	number_format($totrate/$cnt,1);

	}else{

		$avg		=	0;

	}

	return $avg;

  }

  

  ######## Function to get 5 Toprated Products....................

  function toprated(){

		$Db = ConnectionManager::getDataSource($this->useDbConfig);

		$pdtbl = $Db->fullTableName('products');

		$pdimgtbl = $Db->fullTableName('productimages');

		//echo ($fullTableName);exit;

		$pdarray		=	$this->query("SELECT id from ".$pdtbl. " WHERE status = 'A' AND stock > 0");

		$i	=	0;

		$rtarr	=	array();

		foreach($pdarray as $pdct){

			$pdtbl		=	str_replace('`','',$pdtbl);

			//print_r($pdct[$pdtbl]);exit;

			$rtarr[$i]["id"]	=	$pdct[$pdtbl]["id"];

			$rtarr[$i]["rat"]	=	$this->avgrate($pdct[$pdtbl]["id"]);

			$i++;

		}

		$i	=	0;

		foreach ($rtarr as $key => $row) {

			$id[$key]  = $row['id'];

			$rat[$key] = $row['rat'];

		}

		if(!empty($pdarray) ==true){

			array_multisort($rat, SORT_DESC, $id, SORT_ASC, $rtarr);

		}

		$st	= '';

		if(sizeof($rtarr) > 5){

			$mx	=	5;

		}else{

			$mx	=	sizeof($rtarr);

		}

		for($i=0;$i < $mx; $i++){

			if($i <> $mx-1){

				$st	.=	$rtarr[$i]["id"].",";

			}else{

				$st	.=	$rtarr[$i]["id"];

			}

		}

		//echo "SELECT id,pname from $pdtbl WHERE id IN (".$st.")";exit;

		if(isset($st) && $st <> ''){

			$tparr	=	$this->query("SELECT id,category_id,pname,price,wholesale_price,discount,discount_type,descr,combination from $pdtbl WHERE id IN (".$st.") ORDER BY id DESC limit 0,6");

		}else{

			$tparr	=	false;

		}	

			

		if(isset($tparr) && !empty($tparr))

		{

			for($i=0;$i < sizeof($tparr); $i++){

				$tparr[$i]["Product"]	=	$tparr[$i][$pdtbl];

				unset($tparr[$i][$pdtbl]);

			}		

			$j=0;

                        

			foreach($tparr as $prodarr)

			{
                          
			  $img	=	$this->query("SELECT imagename from $pdimgtbl img WHERE product_id='".$prodarr["Product"]["id"]."' and (default_img='Y' OR default_img='N')"); //... select either default image or any other image
                          
			   if(isset($img[0]["img"]["imagename"])){
                          
			  $tparr[$j]["Productimages"] = $img[0]["img"]["imagename"];
                           }
			  $j++;		

			}

		}			

			return $tparr;			

	}

}

?>