00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Reports_Model_Mysql4_Review_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
00036 {
00037 public function joinReview()
00038 {
00039 $this->addAttributeToSelect('name');
00040 $this->getSelect()->join(
00041 array('r' => $this->getTable('review/review')),
00042 'e.entity_id=r.entity_pk_value',
00043 array(
00044 'review_cnt' => 'COUNT(DISTINCT r.review_id)',
00045 'last_created' => 'MAX(r.created_at)',
00046 )
00047 );
00048
00049 $this->getSelect()->joinLeft(
00050 array('table_rating' => $this->getTable('rating_option_vote_aggregated')),
00051 'e.entity_id=table_rating.entity_pk_value AND table_rating.store_id>0',
00052 array(
00053 'avg_rating' => 'SUM(table_rating.percent)/COUNT(table_rating.rating_id)',
00054 'avg_rating_approved' => 'SUM(table_rating.percent_approved)/COUNT(table_rating.rating_id)'
00055 )
00056 );
00057 $this->getSelect()->group('e.entity_id');
00058
00059 return $this;
00060 }
00061
00062 public function addAttributeToSort($attribute, $dir='asc')
00063 {
00064 if (in_array($attribute, array('review_cnt', 'last_created', 'avg_rating', 'avg_rating_approved'))) {
00065 $this->getSelect()->order($attribute.' '.$dir);
00066 return $this;
00067 }
00068
00069 return parent::addAttributeToSort($attribute, $dir);
00070 }
00071 }