Example #1
0
 protected function _afterSave(Mage_Core_Model_Abstract $object)
 {
     parent::_afterSave($object);
     if ($object->hasRatingCodes()) {
         try {
             $this->_getWriteAdapter()->beginTransaction();
             $condition = $this->_getWriteAdapter()->quoteInto('rating_id = ?', $object->getId());
             $this->_getWriteAdapter()->delete($this->getTable('rating_title'), $condition);
             if ($ratingCodes = $object->getRatingCodes()) {
                 foreach ($ratingCodes as $storeId => $value) {
                     if (trim($value) == '') {
                         continue;
                     }
                     $data = new Varien_Object();
                     $data->setRatingId($object->getId())->setStoreId($storeId)->setValue($value);
                     $this->_getWriteAdapter()->insert($this->getTable('rating_title'), $data->getData());
                 }
             }
             $this->_getWriteAdapter()->commit();
         } catch (Exception $e) {
             $this->_getWriteAdapter()->rollBack();
         }
     }
     if ($object->hasStores()) {
         try {
             $condition = $this->_getWriteAdapter()->quoteInto('rating_id = ?', $object->getId());
             $this->_getWriteAdapter()->delete($this->getTable('rating_store'), $condition);
             foreach ($object->getStores() as $storeId) {
                 $storeInsert = new Varien_Object();
                 $storeInsert->setStoreId($storeId);
                 $storeInsert->setRatingId($object->getId());
                 $this->_getWriteAdapter()->insert($this->getTable('rating_store'), $storeInsert->getData());
             }
         } catch (Exception $e) {
             $this->_getWriteAdapter()->rollBack();
         }
     }
     return $this;
 }
Example #2
0
 public function aggregate($option)
 {
     $select = $this->_read->select();
     $optionData = $this->load($option->getId());
     $vote = Mage::getModel('rating/rating_option_vote')->load($option->getVoteId());
     $select->from($this->_aggregateTable)->where('rating_id = ?', $vote->getRatingId())->where('entity_pk_value = ?', $vote->getEntityPkValue());
     $data = $this->_read->fetchAll($select);
     $oldData = array();
     foreach ($data as $row) {
         $oldData[$row['store_id']] = $row['primary_id'];
     }
     $select = $this->_read->select()->from(array('vote' => $this->_ratingVoteTable), array('COUNT(vote.vote_id) AS vote_count', 'SUM(vote.value) AS vote_value_sum'))->join(array('review' => $this->_reviewTable), 'vote.review_id=review.review_id', array())->joinLeft(array('store' => $this->_reviewStoreTable), 'vote.review_id=store.review_id', 'store_id')->join(array('rstore' => $this->_ratingStoreTable), 'vote.rating_id=rstore.rating_id AND rstore.store_id=store.store_id', array())->where('vote.rating_id = ?', $vote->getRatingId())->where('vote.entity_pk_value = ?', $vote->getEntityPkValue())->group('vote.rating_id')->group('vote.entity_pk_value')->group('store.store_id');
     $perStoreInfo = $this->_read->fetchAll($select);
     $usedStores = array();
     foreach ($perStoreInfo as $row) {
         $saveData = new Varien_Object();
         $saveData->setRatingId($vote->getRatingId())->setEntityPkValue($vote->getEntityPkValue())->setVoteCount($row['vote_count'])->setVoteValueSum($row['vote_value_sum'])->setPercent($row['vote_value_sum'] / $row['vote_count'] / 5 * 100)->setStoreId($row['store_id']);
         if (isset($oldData[$row['store_id']])) {
             $condition = $this->_write->quoteInto("primary_id = ?", $oldData[$row['store_id']]);
             $this->_write->update($this->_aggregateTable, $saveData->getData(), $condition);
         } else {
             $this->_write->insert($this->_aggregateTable, $saveData->getData());
         }
         $usedStores[] = $row['store_id'];
     }
     $toDelete = array_diff(array_keys($oldData), $usedStores);
     foreach ($toDelete as $storeId) {
         $condition = $this->_write->quoteInto("primary_id = ?", $oldData[$storeId]);
         $this->_write->delete($this->_aggregateTable, $condition);
     }
     /* Wrong
             if( $oldData['primary_id'] > 0 ) {
                 $option->setVoteCount(new Zend_Db_Expr('vote_count + 1'))
                     ->setVoteValueSum( new Zend_Db_Expr('vote_value_sum + ' . $optionData['value']) )
                     ->setPercent( ((($oldData['vote_value_sum'] / 100) / (($oldData['vote_count'] * 5) / 100)) * 100) )
                     ->unsetData('option_id')
                     ->unsetData('review_id');
     
                 $condition = $this->_write->quoteInto("{$this->_aggregateTable}.primary_id = ?", $oldData['primary_id']);
                 $this->_write->update($this->_aggregateTable, $option->getData(), $condition);
             } else {
                 $option->setVoteCount('1')
                     ->setVoteValueSum( $optionData['value'] )
                     ->setPercent( (($optionData['value'] / 5) * 100) )
                     ->unsetData('option_id')
                     ->unsetData('review_id');
     
                 $this->_write->insert($this->_aggregateTable, $option->getData());
             }*/
 }