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]); }
/** * 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()]); } }); } }
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); }
public function testGetModelIdByName() { $firstModelId = Rating::getModelIdByName($this->firstModelName); $this->assertEquals($firstModelId, $this->firstModelId); $secondModelId = Rating::getModelIdByName($this->secondModelName); $this->assertEquals($secondModelId, $this->secondModelId); $thirdModelId = Rating::getModelIdByName($this->thirdModelName); $this->assertEquals($thirdModelId, $this->thirdModelId); }
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]); }
/** * @inheritdoc */ public function getAggregate() { return $this->owner->hasOne(AggregateRating::className(), ['target_id' => $this->owner->primaryKey()[0]])->onCondition(['model_id' => Rating::getModelIdByName($this->owner->className())]); }
public function run() { if (Yii::$app->request->getIsAjax()) { Yii::$app->response->format = Response::FORMAT_JSON; if (null === ($model_name = Yii::$app->request->post('model_name'))) { return ['content' => Yii::t('vote', 'Model name has not been sent')]; } if (null === ($target_id = Yii::$app->request->post('target_id'))) { 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' ? 1 : 0; $user_id = Yii::$app->user->getId(); if ($user_id === null && !Rating::getIsAllowGuests($model_name)) { return ['content' => Yii::t('vote', 'Guests are not allowed to vote')]; } if (!($user_ip = Rating::compressIp(Yii::$app->request->getUserIP()))) { return ['content' => Yii::t('vote', 'The user is not recognized')]; } $model_id = Rating::getModelIdByName($model_name); if (!is_int($model_id)) { return ['content' => Yii::t('vote', 'The model is not registered')]; } if (Rating::getIsAllowGuests($model_name)) { $isVoted = Rating::findOne(['model_id' => $model_id, 'target_id' => $target_id, 'user_ip' => $user_ip]); } else { $isVoted = Rating::findOne(['model_id' => $model_id, 'target_id' => $target_id, 'user_id' => $user_id]); } if (is_null($isVoted)) { $newVote = new Rating(); $newVote->model_id = $model_id; $newVote->target_id = $target_id; $newVote->user_id = $user_id; $newVote->user_ip = $user_ip; $newVote->value = $value; if ($newVote->save()) { Yii::$app->cache->delete('rating' . $model_name . $target_id); if ($value === 1) { 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($model_name)) { $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); } }