mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-08-01 08:16:55 -04:00
187 lines
5.5 KiB
PHP
187 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\JobPortal\Entities;
|
|
|
|
use Modules\JobPortal\Entities\BaseModel;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Review extends BaseModel
|
|
{
|
|
use SoftDeletes;
|
|
protected $table = 'jp_review';
|
|
protected $fillable = [
|
|
'object_id',
|
|
'object_model',
|
|
'title',
|
|
'content',
|
|
'rate_number',
|
|
'author_ip',
|
|
'status',
|
|
'vendor_id'
|
|
];
|
|
|
|
/**
|
|
* Get Category
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function getUserInfo()
|
|
{
|
|
return $this->hasOne("App\Models\User", "id", 'create_user')->withTrashed();
|
|
}
|
|
|
|
public static function getDisplayTextScoreByLever($lever)
|
|
{
|
|
switch ($lever) {
|
|
case 5:
|
|
return __("Excellent");
|
|
break;
|
|
case 4:
|
|
return __("Very Good");
|
|
break;
|
|
case 3:
|
|
return __("Average");
|
|
break;
|
|
case 2:
|
|
return __("Poor");
|
|
break;
|
|
case 1:
|
|
case 0:
|
|
return __("Terrible");
|
|
break;
|
|
default:
|
|
return __("Not rated");
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function getService()
|
|
{
|
|
$allServices = get_bookable_services();
|
|
$module = $allServices[$this->object_model];
|
|
return $this->hasOne($module, "id", 'object_id');
|
|
}
|
|
|
|
public function getReviewMeta()
|
|
{
|
|
return ReviewMeta::where("review_id", $this->id)->get();
|
|
}
|
|
|
|
public function getReviewMetaPicture()
|
|
{
|
|
return ReviewMeta::where("review_id", $this->id)->where('name', 'upload_picture')->first();
|
|
}
|
|
|
|
public static function countReviewByStatus($status = false)
|
|
{
|
|
$count = parent::query();
|
|
if (!empty($status)) {
|
|
$count->where("status", $status);
|
|
}
|
|
return $count->count("id");
|
|
}
|
|
|
|
public static function countReviewByServiceID($service_id = false, $user_id = false, $status = false,$service_type = '')
|
|
{
|
|
if (empty($service_id))
|
|
return false;
|
|
$count = parent::where("object_id", $service_id);
|
|
if (!empty($status)) {
|
|
$count->where("status", $status);
|
|
}
|
|
|
|
if($service_type){
|
|
$count->where('object_model',$service_type);
|
|
}
|
|
if (!empty($user_id)) {
|
|
$count->where("create_user", $user_id);
|
|
}
|
|
return $count->count("id");
|
|
}
|
|
|
|
public function save(array $options = [])
|
|
{
|
|
$check = parent::save($options); // TODO: Change the autogenerated stub
|
|
if ($check) {
|
|
Cache::forget("review_" . $this->object_model . "_" . $this->object_id);
|
|
}
|
|
return $check;
|
|
}
|
|
|
|
public function addMeta($key, $val, $multiple = false)
|
|
{
|
|
if (is_object($val) or is_array($val))
|
|
$val = json_encode($val);
|
|
if ($multiple) {
|
|
$reviewMeta = new ReviewMeta([
|
|
"object_id" => $this->object_id,
|
|
"object_model" => $this->object_model,
|
|
"name" => $key,
|
|
"val" => $val,
|
|
'review_id'=>$this->id
|
|
]);
|
|
return $reviewMeta->save();
|
|
|
|
} else {
|
|
$old = ReviewMeta::query()->where([
|
|
'review_id' => $this->id,
|
|
'name' => $key
|
|
])->first();
|
|
|
|
if ($old) {
|
|
$old->val = $val;
|
|
return $old->save();
|
|
|
|
} else {
|
|
$reviewMeta = new ReviewMeta([
|
|
"object_id" => $this->object_id,
|
|
"object_model" => $this->object_model,
|
|
"name" => $key,
|
|
"val" => $val,
|
|
'review_id'=>$this->id
|
|
]);
|
|
return $reviewMeta->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
public function author()
|
|
{
|
|
return $this->belongsTo("App\Models\User", "create_user", "id")->select(['id','name','first_name','last_name','avatar_id'])->withDefault();
|
|
}
|
|
|
|
public static function getTotalViewAndRateAvg($objectId, $objectModel)
|
|
{
|
|
$list_score = [
|
|
'sbc_total' => 0,
|
|
'total_review' => 0,
|
|
];
|
|
if (empty($objectId) || empty($objectModel)) {
|
|
return $list_score;
|
|
}
|
|
|
|
$list_score = Cache::rememberForever('review_'.$objectModel.'_' . $objectId, function () use ($objectId, $objectModel) {
|
|
$dataReview = self::selectRaw(" AVG(rate_number) as sbc_total , COUNT(id) as total_review ")->where('object_id', $objectId)->where('object_model', $objectModel)->where("status", "approved")->first();
|
|
$sbc_total = !empty($dataReview->sbc_total) ? number_format($dataReview->sbc_total, 1) : 0;
|
|
return [
|
|
'sbc_total' => $sbc_total,
|
|
'total_review' => !empty($dataReview->total_review) ? $dataReview->total_review : 0,
|
|
];
|
|
});
|
|
return $list_score;
|
|
}
|
|
|
|
public static function getReviewMetaAvg($objectId, $objectModel){
|
|
$review_meta = DB::table('jp_review_meta')
|
|
->selectRaw("AVG(val) as sbc_total, name")
|
|
->where('object_id', $objectId)
|
|
->where('object_model', $objectModel)
|
|
->groupBy('name')
|
|
->get();
|
|
return $review_meta;
|
|
}
|
|
}
|