<?php
namespace App\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Review;
use Pimcore\Model\DataObject\Solution;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Search;
use Pimcore\Model\DataObject\ClassDefinition;
use App\Services\SearchService;
use \Pimcore\Config;
class ReviewEventListener
{
public function postUpdate(DataObjectEvent $e)
{
$object = $e->getObject();
if($object instanceof Review){
$solution = Solution::getById($object->getSolution()->getId());
// Tính tổng điểm chung bình
$totalPoint=$this->totalPoint($solution->getId());
$solution->setTotalPoint($totalPoint);
// Đếm tổng số review
$totalReview=$this->countReview($solution->getId());
$solution->setTotalReview($totalReview);
// Đếm tổng review đã xác thực
$totalTrustedReview=$this->totalTrustedReview($solution->getId());
$solution->setTotalTrustedReview($totalTrustedReview);
$solution->save();
}
}
public function totalPoint($id){
$review = new Review\Listing();
$review->addConditionParam("solution__id = ?",$id);
$total = 0 ;
foreach($review as $value){
$total+=$value->getGeneralScore();
}
$total = $review->count() ? $total/$review->count() : 0;
$totalPoint=round($total,1);
return $totalPoint;
}
public function countReview($id){
$review = new Review\Listing();
$review->addConditionParam("solution__id =".$id);
$total = $review->count();
return $total;
}
public function totalTrustedReview($id){
$review = new Review\Listing();
$review->addConditionParam("solution__id =".$id);
$review->addConditionParam("accuracy = 1");
$total = $review->count();
return $total;
}
}