src/EventListener/ReviewEventListener.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Pimcore\Event\Model\ElementEventInterface;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Model\DataObject\Review;
  6. use Pimcore\Model\DataObject\Solution;
  7. use Pimcore\Model\DataObject;
  8. use Pimcore\Model\DataObject\Search;
  9. use Pimcore\Model\DataObject\ClassDefinition;
  10. use App\Services\SearchService;
  11. use \Pimcore\Config;
  12. class ReviewEventListener
  13. {
  14.     public function postUpdate(DataObjectEvent $e
  15.     {
  16.         $object $e->getObject();
  17.         if($object instanceof Review){
  18.             $solution Solution::getById($object->getSolution()->getId());
  19.             // Tính tổng điểm chung bình
  20.             $totalPoint=$this->totalPoint($solution->getId());
  21.             $solution->setTotalPoint($totalPoint);
  22.             // Đếm tổng số review
  23.             $totalReview=$this->countReview($solution->getId());
  24.             $solution->setTotalReview($totalReview);
  25.             // Đếm tổng review đã xác thực
  26.             $totalTrustedReview=$this->totalTrustedReview($solution->getId());
  27.             $solution->setTotalTrustedReview($totalTrustedReview);
  28.             $solution->save();
  29.         }
  30.     }
  31.     public function totalPoint($id){
  32.         
  33.         $review = new Review\Listing();
  34.         $review->addConditionParam("solution__id = ?",$id);
  35.        
  36.         $total ;
  37.     
  38.         foreach($review as $value){
  39.             $total+=$value->getGeneralScore();
  40.         }
  41.        
  42.         $total $review->count() ? $total/$review->count() : 0;
  43.         $totalPoint=round($total,1);
  44.         
  45.         return $totalPoint;
  46.     }
  47.     public function countReview($id){
  48.         $review = new Review\Listing();
  49.         $review->addConditionParam("solution__id =".$id);
  50.         $total $review->count();
  51.         return $total;
  52.     }
  53.     public function totalTrustedReview($id){
  54.         $review = new Review\Listing();
  55.         $review->addConditionParam("solution__id =".$id);
  56.         $review->addConditionParam("accuracy = 1");
  57.         $total $review->count();
  58.         return $total;
  59.     }
  60.     
  61. }