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(); } } }
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; }