Mage_Rating_Model_Mysql4_Rating_Option Class Reference

List of all members.

Public Member Functions

 __construct ()
 save ($object)
 delete ($object)
 addVote ($option)
 aggregate ($option)
 aggregateEntityByRatingId ($ratingId, $entityPkValue)
 load ($optionId)

Protected Attributes

 $_reviewTable
 $_ratingOptionTable
 $_ratingVoteTable
 $_aggregateTable
 $_reviewStoreTable
 $_ratingStoreTable
 $_read
 $_write
 $_optionData
 $_optionId


Detailed Description

Definition at line 34 of file Option.php.


Constructor & Destructor Documentation

__construct (  ) 

Definition at line 49 of file Option.php.

00050     {
00051         $this->_reviewTable     = Mage::getSingleton('core/resource')->getTableName('review/review');
00052         $this->_ratingOptionTable  = Mage::getSingleton('core/resource')->getTableName('rating/rating_option');
00053         $this->_ratingVoteTable    = Mage::getSingleton('core/resource')->getTableName('rating/rating_vote');
00054         $this->_aggregateTable    = Mage::getSingleton('core/resource')->getTableName('rating/rating_vote_aggregated');
00055         $this->_reviewStoreTable  = Mage::getSingleton('core/resource')->getTableName('review/review_store');
00056         $this->_ratingStoreTable  = Mage::getSingleton('core/resource')->getTableName('rating/rating_store');
00057 
00058         $this->_read  = Mage::getSingleton('core/resource')->getConnection('rating_read');
00059         $this->_write = Mage::getSingleton('core/resource')->getConnection('rating_write');
00060     }


Member Function Documentation

addVote ( option  ) 

Definition at line 80 of file Option.php.

00081     {
00082         $action = Mage::app()->getFrontController()->getAction();
00083 
00084         if ($action instanceof Mage_Core_Controller_Front_Action || $action instanceof Mage_Adminhtml_Controller_Action) {
00085             $optionData = $this->load($option->getId());
00086             $data = array(
00087                 'option_id'     => $option->getId(),
00088                 'review_id'     => $option->getReviewId(),
00089                 'percent'       => (($optionData['value'] / 5) * 100),
00090                 'value'          => $optionData['value']
00091             );
00092 
00093             if( !$option->getDoUpdate() ) {
00094                 $data['remote_ip'] = $action->getRequest()->getServer('REMOTE_ADDR');
00095                 $data['remote_ip_long'] = ip2long($action->getRequest()->getServer('REMOTE_ADDR'));
00096                 $data['customer_id'] = Mage::getSingleton('customer/session')->getCustomerId();
00097                 $data['entity_pk_value'] = $option->getEntityPkValue();
00098                 $data['rating_id'] = $option->getRatingId();
00099             }
00100 
00101             $this->_write->beginTransaction();
00102             try {
00103                 if( $option->getDoUpdate() ) {
00104                     $condition = "vote_id = '{$option->getVoteId()}' AND review_id = '{$option->getReviewId()}'";
00105                     $this->_write->update($this->_ratingVoteTable, $data, $condition);
00106                     $this->aggregate($option);
00107                 } else {
00108                     $this->_write->insert($this->_ratingVoteTable, $data);
00109                     $option->setVoteId($this->_write->lastInsertId());
00110                     $this->aggregate($option);
00111                 }
00112                 $this->_write->commit();
00113             }
00114             catch (Exception $e){
00115                 $this->_write->rollback();
00116                 throw new Exception($e->getMessage());
00117             }
00118         }
00119         return $this;
00120     }

aggregate ( option  ) 

Definition at line 122 of file Option.php.

00123     {
00124         $optionData = $this->load($option->getId());
00125         $vote = Mage::getModel('rating/rating_option_vote')->load($option->getVoteId());
00126         $this->aggregateEntityByRatingId($vote->getRatingId(), $vote->getEntityPkValue());
00127     }

aggregateEntityByRatingId ( ratingId,
entityPkValue 
)

Definition at line 129 of file Option.php.

00130     {
00131         $select = $this->_read->select()
00132             ->from($this->_aggregateTable)
00133             ->where('rating_id = ?', $ratingId)
00134             ->where('entity_pk_value = ?', $entityPkValue);
00135 
00136         $data = $this->_read->fetchAll($select);
00137 
00138         $oldData = array();
00139         foreach($data as $row) {
00140             $oldData[$row['store_id']] = $row['primary_id'];
00141         }
00142 
00143         $select = $this->_read->select()
00144             ->from(array('vote'=>$this->_ratingVoteTable),
00145                 array('COUNT(vote.vote_id) AS vote_count',
00146                     'SUM(vote.value) AS vote_value_sum',
00147                     'COUNT(CASE WHEN review.status_id=1 THEN vote.vote_id ELSE NULL END) AS app_vote_count',
00148                     'SUM(CASE WHEN review.status_id=1 THEN vote.value ELSE 0 END) AS app_vote_value_sum',
00149             ))
00150             ->join(array('review'=>$this->_reviewTable), 'vote.review_id=review.review_id', array())
00151             ->joinLeft(array('store'=>$this->_reviewStoreTable), 'vote.review_id=store.review_id', 'store_id')
00152             ->join(array('rstore'=>$this->_ratingStoreTable), 'vote.rating_id=rstore.rating_id AND rstore.store_id=store.store_id', array())
00153             ->where('vote.rating_id = ?', $ratingId)
00154             ->where('vote.entity_pk_value = ?', $entityPkValue)
00155             ->group('vote.rating_id')
00156             ->group('vote.entity_pk_value')
00157             ->group('store.store_id');
00158 
00159          $perStoreInfo = $this->_read->fetchAll($select);
00160 
00161          $usedStores = array();
00162          foreach($perStoreInfo as $row) {
00163              $saveData = new Varien_Object(array(
00164                 'rating_id'        => $ratingId,
00165                 'entity_pk_value'  => $entityPkValue,
00166                 'vote_count'       => $row['vote_count'],
00167                 'vote_value_sum'   => $row['vote_value_sum'],
00168                 'percent'          => (($row['vote_value_sum']/$row['vote_count'])/5) * 100,
00169                 'percent_approved' => ($row['app_vote_count'] ? ((($row['app_vote_value_sum']/$row['app_vote_count'])/5) * 100) : 0),
00170                 'store_id'         => $row['store_id'],
00171              ));
00172 
00173              if(isset($oldData[$row['store_id']])) {
00174                  $condition = $this->_write->quoteInto("primary_id = ?", $oldData[$row['store_id']]);
00175                  $this->_write->update($this->_aggregateTable, $saveData->getData(), $condition);
00176              } else {
00177                  $this->_write->insert($this->_aggregateTable, $saveData->getData());
00178              }
00179 
00180              $usedStores[] = $row['store_id'];
00181          }
00182 
00183          $toDelete = array_diff(array_keys($oldData), $usedStores);
00184 
00185          foreach ($toDelete as $storeId) {
00186              $condition = $this->_write->quoteInto("primary_id = ?", $oldData[$storeId]);
00187              $this->_write->delete($this->_aggregateTable, $condition);
00188          }
00189     }

delete ( object  ) 

Definition at line 74 of file Option.php.

00075     {
00076         $condition = $this->_write->quoteInto('option_id = ?', $object->getId());
00077         $this->_write->delete($this->_ratingOptionTable, $condition);
00078     }

load ( optionId  ) 

Definition at line 191 of file Option.php.

00192     {
00193         if( !$this->_optionData || $this->_optionId != $optionId ) {
00194             $select = $this->_read->select();
00195             $select->from($this->_ratingOptionTable)
00196                 ->where('option_id = ?', $optionId);
00197 
00198             $data = $this->_read->fetchRow($select);
00199 
00200             $this->_optionData = $data;
00201             $this->_optionId = $optionId;
00202             return $data;
00203         }
00204 
00205         return $this->_optionData;
00206     }

save ( object  ) 

Definition at line 62 of file Option.php.

00063     {
00064         if( $object->getId() ) {
00065             $condition = $this->_write->quoteInto('option_id = ?', $object->getId());
00066             $object->unsetData('option_id');
00067             $this->_write->update($this->_ratingOptionTable, $object->getData(), $condition);
00068         } else {
00069             $this->_write->insert($this->_ratingOptionTable, $object->getData());
00070         }
00071         return $object;
00072     }


Member Data Documentation

$_aggregateTable [protected]

Definition at line 39 of file Option.php.

$_optionData [protected]

Definition at line 46 of file Option.php.

$_optionId [protected]

Definition at line 47 of file Option.php.

$_ratingOptionTable [protected]

Definition at line 37 of file Option.php.

$_ratingStoreTable [protected]

Definition at line 41 of file Option.php.

$_ratingVoteTable [protected]

Definition at line 38 of file Option.php.

$_read [protected]

Definition at line 43 of file Option.php.

$_reviewStoreTable [protected]

Definition at line 40 of file Option.php.

$_reviewTable [protected]

Definition at line 36 of file Option.php.

$_write [protected]

Definition at line 44 of file Option.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:35 2009 for Magento by  doxygen 1.5.8