diff --git a/common/models/Portfolio.php b/common/models/Portfolio.php index 1a04ee7..bc6520f 100644 --- a/common/models/Portfolio.php +++ b/common/models/Portfolio.php @@ -2,9 +2,12 @@ namespace common\models; + use common\modules\comment\models\Comment; + use common\modules\comment\models\Rating; use Yii; use yii\behaviors\BlameableBehavior; use yii\behaviors\TimestampBehavior; + use yii\db\ActiveQuery; use yii\db\Expression; /** @@ -26,6 +29,9 @@ * @property PortfolioSpecialization[] $portfolioSpecializations * @property Specialization[] $specializations * @property User $user + * @property Comment[] $comment + * @property Rating[] $rating + * @property string $ratingValue */ class Portfolio extends \yii\db\ActiveRecord { @@ -199,4 +205,37 @@ { return $this->hasOne(User::className(), [ 'id' => 'user_id' ]); } + + /** + * @return ActiveQuery + */ + public function getComments() + { + return $this->hasMany(Comment::className(), [ 'model_id' => 'portfolio_id' ]) + ->andWhere([ 'comment.model' => $this->className(), 'comment.status' => Comment::STATUS_ACTIVE ]); + } + + /** + * @return ActiveQuery + */ + public function getRating() + { + return $this->hasMany(Rating::className(), [ 'model_id' => 'comment_id' ]) + ->via('comments') + ->andWhere([ + 'not', + [ 'rating.value' => NULL ], + ]); + } + + /** + * @return string + */ + public function getRatingValue() + { + return $this->getRating() + ->asArray() + ->select([ 'sum' => 'ROUND(SUM(rating.value)/COUNT(rating.value)::numeric, 1)' ]) + ->scalar(); + } } diff --git a/common/modules/comment/widgets/views/form-comment.php b/common/modules/comment/widgets/views/form-comment.php index 9262ebc..c14055e 100644 --- a/common/modules/comment/widgets/views/form-comment.php +++ b/common/modules/comment/widgets/views/form-comment.php @@ -5,6 +5,7 @@ * @var \yii\data\ActiveDataProvider $dataProvider * @var null|\common\modules\comment\models\Rating $rating */ + use kartik\rating\StarRating; use yii\widgets\ActiveForm; use yii\helpers\Html; @@ -15,16 +16,16 @@ field($rating, 'value') - ->label(false) - ->radioList([ - 1 => 1, - 2 => 2, - 3 => 3, - 4 => 4, - 5 => 5, - ]); + if($rating) { + echo $form->field(( !empty( $model->rating ) ? $model->rating : $rating ), 'value') + ->label(false) + ->widget(StarRating::className(), [ 'pluginOptions' => [ 'size' => 'xxs', + 'step' => 1, + 'value' => 2, + 'showCaption' => false, + ], + ]); + } if($model->scenario == $model::SCENARIO_GUEST) { echo $form->field($model, 'user_name', [ @@ -44,7 +45,8 @@ 'class' => 'custom-input-4', ], ]) - ->textInput(); + ->textInput() + ->hint('Ваш email не будет доступен к просмотру.'); } ?> diff --git a/common/modules/comment/widgets/views/project_comment_view.php b/common/modules/comment/widgets/views/project_comment_view.php index f17588c..c876f4d 100644 --- a/common/modules/comment/widgets/views/project_comment_view.php +++ b/common/modules/comment/widgets/views/project_comment_view.php @@ -1,5 +1,6 @@ with('userInfo') ->one(); } + $model->buildButtons(['delete']); ?> -