File: /home/safarimaris/home/safarimaris/common/models/Review.php
<?php
namespace common\models;
use common\components\ReviewStatus;
use common\components\UserRole;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
/**
* This is the model class for table "review".
*
* @property integer $id
* @property integer $entityId
* @property string $user
* @property string $title
* @property string $text
* @property string $date
* @property double $rating
* @property integer $ratingAirport
* @property integer $ratingInfrastructure
* @property integer $ratingGuiding
* @property integer $ratingClean
* @property integer $ratingEat
* @property integer $ratingService
* @property integer $ratingZodiak
* @property integer $ratingEquipment
* @property integer $ratingPrice
* @property integer $ratingHost
* @property integer $status
* @property string $hash
*
* @property Entity $entity
*/
class Review extends \yii\db\ActiveRecord
{
public $updated_at;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'review';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['entityId', 'ratingAirport', 'ratingInfrastructure', 'ratingGuiding', 'ratingClean', 'ratingEat', 'ratingService', 'ratingZodiak', 'ratingEquipment', 'ratingPrice', 'ratingHost'], 'required'],
[['entityId', 'ratingAirport', 'ratingInfrastructure', 'ratingGuiding', 'ratingClean', 'ratingEat', 'ratingService', 'ratingZodiak', 'ratingEquipment', 'ratingPrice', 'ratingHost', 'status'], 'integer'],
[['ratingAirport', 'ratingInfrastructure', 'ratingGuiding', 'ratingClean', 'ratingEat', 'ratingService', 'ratingZodiak', 'ratingEquipment', 'ratingPrice', 'ratingHost'], 'compare', 'compareValue' => 5, 'operator' => '<=', 'type' => 'number'],
[['text', 'hash'], 'string'],
[['date'], 'safe'],
[['rating'], 'number'],
[['user', 'title', 'hash'], 'string', 'max' => 255],
[['entityId'], 'exist', 'skipOnError' => true, 'targetClass' => Entity::className(), 'targetAttribute' => ['entityId' => 'id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('model', 'ID'),
'entityId' => Yii::t('model', 'Entity ID'),
'user' => in_array(Yii::$app->getUser()->identity->role,[UserRole::ADMIN, UserRole::MANAGER])?'Имя клиента':Yii::t('model', 'User'),
'title' => Yii::t('model', 'Title'),
'text' => Yii::t('model', 'Text'),
'date' => Yii::t('model', 'Date'),
'rating' => Yii::t('model', 'Rating'),
'ratingAirport' => Yii::t('model', 'Rating Airport'),
'ratingInfrastructure' => Yii::t('model', 'Rating Infrastructure'),
'ratingGuiding' => Yii::t('model', 'Rating Guiding'),
'ratingClean' => Yii::t('model', 'Rating Clean'),
'ratingEat' => Yii::t('model', 'Rating Eat'),
'ratingService' => Yii::t('model', 'Rating Service'),
'ratingZodiak' => Yii::t('model', 'Rating Zodiak'),
'ratingEquipment' => Yii::t('model', 'Rating Equipment'),
'ratingPrice' => Yii::t('model', 'Rating Price'),
'ratingHost' => Yii::t('model', 'Rating Host'),
'status' => Yii::t('model', 'Status'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEntity()
{
return $this->hasOne(Entity::className(), ['id' => 'entityId']);
}
public function setRating()
{
$this->rating = $this->getMainRating();
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$this->rating = $this->getMainRating();
if ($this->isNewRecord) {
$this->date = new Expression('NOW()');
if ($this->status != ReviewStatus::STATUS_SENDED) {
$this->status = ReviewStatus::STATUS_NEW;
}
}
return true;
}
return false;
}
public static function getText($i)
{
if(is_null($i)) return false;
if($i >= 9) return Yii::t('app', 'Excellent');
if($i >= 7) return Yii::t('app', 'Good');
if($i >= 5) return Yii::t('app', 'Not bad');
if($i >= 3) return Yii::t('app', 'Bad');
if($i >= 1) return Yii::t('app', 'Very bad');
}
public function getMainRating()
{
return ($this->getValueIn10PointScale($this->ratingAirport) + $this->getValueIn10PointScale($this->ratingInfrastructure) +
$this->getValueIn10PointScale($this->ratingGuiding) + + $this->getValueIn10PointScale($this->ratingClean) +
$this->getValueIn10PointScale($this->ratingEat) + $this->getValueIn10PointScale($this->ratingService) +
$this->getValueIn10PointScale($this->ratingZodiak) + $this->getValueIn10PointScale($this->ratingEquipment) +
$this->getValueIn10PointScale($this->ratingPrice) + $this->getValueIn10PointScale($this->ratingHost)) / 10;
}
public function getValueIn10PointScale($value)
{
switch ($value){
case 5:
return 10;
case 4:
return 9;
case 3:
return 7;
case 2:
return 5;
case 1:
return 3;
default:
return 0;
}
}
}