Exemplo n.º 1
2
 public function run()
 {
     $modelName = $this->modelName;
     $ratingArray = AggregateRating::find()->select('target_id, rating')->where('model_id = :modelId', ['modelId' => Rating::getModelIdByName($modelName)])->orderBy('rating DESC')->limit($this->limit)->asArray()->all();
     $ids = ArrayHelper::getColumn($ratingArray, 'target_id');
     $models = $modelName::find()->joinWith('aggregate')->where(['in', $modelName::tableName() . '.' . $modelName::primaryKey()[0], $ids])->orderBy('rating DESC')->all();
     return $this->render('topRated', ['models' => $models, 'title' => $this->title, 'titleField' => $this->titleField, 'path' => $this->path]);
 }
Exemplo n.º 2
1
 public function afterFind($event)
 {
     if ($received_rating = Rating::getRating($this->model_name, $this->owner->{$this->owner->primaryKey()[0]})) {
         $rating = $received_rating['likes'] - $received_rating['dislikes'];
         $aggregate_rating = $received_rating['aggregate_rating'];
         if ($this->owner->{$this->rating_field} != $rating or $this->owner->{$this->aggregate_rating_field} != $aggregate_rating) {
             \Yii::$app->db->createCommand()->update($this->owner->tableName(), [$this->rating_field => $rating, $this->aggregate_rating_field => $aggregate_rating], [$this->owner->primaryKey()[0] => $this->owner->{$this->owner->primaryKey()[0]}])->execute();
         }
     }
 }
Exemplo n.º 3
1
 /**
  * Bootstrap method to be called during application bootstrap stage.
  * @param Application $app the application currently running
  */
 public function bootstrap($app)
 {
     $models = Yii::$app->getModule('vote')->models;
     foreach ($models as $value) {
         $modelId = Rating::getModelIdByName($value);
         $modelName = Rating::getModelNameById($modelId);
         Event::on($modelName::className(), $modelName::EVENT_INIT, function ($event) {
             if (null === $event->sender->getBehavior('rating')) {
                 $event->sender->attachBehavior('rating', ['class' => RatingBehavior::className()]);
             }
         });
     }
 }
Exemplo n.º 4
1
 public function testBehaivorIsWorks()
 {
     $model = new \tests\unit\mocks\FakeModel();
     $this->assertEquals($model::className(), 'tests\\unit\\mocks\\FakeModel');
     $this->assertEquals(Rating::getModelIdByName($model->className()), 255);
     $this->assertEquals($model->aggregate, null);
     $model->id = 1;
     $model->save();
     Rating::updateRating(Rating::getModelIdByName($model->className()), $model->id);
     $newModel = $model::findOne($model->id);
     $this->assertEquals($newModel->aggregate->likes, 0);
     $this->assertEquals($newModel->aggregate->dislikes, 0);
     $this->assertEquals($newModel->aggregate->rating, 0.0);
 }
Exemplo n.º 5
0
 public function testGetIsAllowChangeVote()
 {
     $firstIsAllow = Rating::getIsAllowChangeVote($this->firstModelId);
     $this->assertEquals($firstIsAllow, true);
     $secondIsAllow = Rating::getIsAllowChangeVote($this->secondModelId);
     $this->assertEquals($secondIsAllow, true);
     $thirdIsAllow = Rating::getIsAllowChangeVote($this->thirdModelId);
     $this->assertEquals($thirdIsAllow, false);
 }
Exemplo n.º 6
0
 public function run()
 {
     $target_id = $this->target_id;
     $model_name = $this->model_name;
     $rating = Rating::getRating($this->model_name, $this->target_id);
     $id = $model_name . $target_id;
     $content = Html::beginTag('div', $this->mainDivOptions);
     $content .= Html::tag('span', $rating['likes'], ['id' => "vote-up-{$id}", 'class' => $this->classLike, 'onclick' => "vote('{$model_name}',{$target_id},'like');return false;", 'style' => 'cursor:pointer;']);
     $content .= $this->separator;
     $content .= Html::tag('span', $rating['dislikes'], ['id' => "vote-down-{$id}", 'class' => $this->classDislike, 'onclick' => "vote('{$model_name}',{$target_id},'dislike');return false;", 'style' => 'cursor:pointer;']);
     $content .= Html::tag('div', $this->view_aggregate_rating ? Yii::t('vote', 'Aggregate rating') . ': ' . $rating['aggregate_rating'] : '', ['id' => "vote-response-{$id}"]);
     $content .= Html::endTag('div');
     return $content;
 }
Exemplo n.º 7
0
 public function run()
 {
     return $this->render('vote', ['modelId' => Rating::getModelIdByName($this->model->className()), 'targetId' => $this->model->{$this->model->primaryKey()[0]}, 'likes' => isset($this->model->aggregate->likes) ? $this->model->aggregate->likes : 0, 'dislikes' => isset($this->model->aggregate->dislikes) ? $this->model->aggregate->dislikes : 0, 'rating' => isset($this->model->aggregate->rating) ? $this->model->aggregate->rating : 0.0, 'showAggregateRating' => $this->showAggregateRating]);
 }
Exemplo n.º 8
0
 /**
  * @inheritdoc
  */
 public function getAggregate()
 {
     return $this->owner->hasOne(AggregateRating::className(), ['target_id' => $this->owner->primaryKey()[0]])->onCondition(['model_id' => Rating::getModelIdByName($this->owner->className())]);
 }
Exemplo n.º 9
0
 public function run()
 {
     if (Yii::$app->request->getIsAjax()) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         if (null === ($modelId = (int) Yii::$app->request->post('modelId'))) {
             return ['content' => Yii::t('vote', 'modelId has not been sent')];
         }
         if (null === ($targetId = (int) Yii::$app->request->post('targetId'))) {
             return ['content' => Yii::t('vote', 'The purpose is not defined')];
         }
         $act = Yii::$app->request->post('act');
         if (!in_array($act, ['like', 'dislike'], true)) {
             return ['content' => Yii::t('vote', 'Wrong action')];
         }
         $value = $act === 'like' ? Rating::VOTE_LIKE : Rating::VOTE_DISLIKE;
         $userId = Yii::$app->user->getId();
         if ($userId === null && !Rating::getIsAllowGuests($modelId)) {
             return ['content' => Yii::t('vote', 'Guests are not allowed to vote')];
         }
         if (!($userIp = Rating::compressIp(Yii::$app->request->getUserIP()))) {
             return ['content' => Yii::t('vote', 'The user is not recognized')];
         }
         if (!is_int($modelId)) {
             return ['content' => Yii::t('vote', 'The model is not registered')];
         }
         if (Rating::getIsAllowGuests($modelId)) {
             $isVoted = Rating::findOne(['model_id' => $modelId, 'target_id' => $targetId, 'user_ip' => $userIp]);
         } else {
             $isVoted = Rating::findOne(['model_id' => $modelId, 'target_id' => $targetId, 'user_id' => $userId]);
         }
         if (is_null($isVoted)) {
             $newVote = new Rating();
             $newVote->model_id = $modelId;
             $newVote->target_id = $targetId;
             $newVote->user_id = $userId;
             $newVote->user_ip = $userIp;
             $newVote->value = $value;
             if ($newVote->save()) {
                 if ($value === Rating::VOTE_LIKE) {
                     return ['content' => Yii::t('vote', 'Your vote is accepted. Thanks!'), 'success' => true];
                 } else {
                     return ['content' => Yii::t('vote', 'Thanks for your opinion'), 'success' => true];
                 }
             } else {
                 return ['content' => Yii::t('vote', 'Validation error')];
             }
         } else {
             if ($isVoted->value !== $value && Rating::getIsAllowChangeVote($modelId)) {
                 $isVoted->value = $value;
                 if ($isVoted->save()) {
                     return ['content' => Yii::t('vote', 'Your vote has been changed. Thanks!'), 'success' => true, 'changed' => true];
                 } else {
                     return ['content' => Yii::t('vote', 'Validation error')];
                 }
             }
             return ['content' => Yii::t('vote', 'You have already voted!')];
         }
     } else {
         throw new MethodNotAllowedHttpException(Yii::t('vote', 'Forbidden method'), 405);
     }
 }