Exemplo n.º 1
0
 /**
  * @return array
  * @throws MethodNotAllowedHttpException
  */
 public function run()
 {
     if (!Yii::$app->request->getIsAjax() || !Yii::$app->request->getIsPost()) {
         throw new MethodNotAllowedHttpException(Yii::t('vote', 'Forbidden method'), 405);
     }
     Yii::$app->response->format = Response::FORMAT_JSON;
     $module = $this->getModule();
     $form = new VoteForm();
     $form->load(Yii::$app->request->post());
     $this->trigger(self::EVENT_BEFORE_VOTE, $event = $this->createEvent($form, $response = []));
     if ($form->validate()) {
         $settings = $module->getSettingsForEntity($form->entity);
         if ($settings['type'] == Module::TYPE_VOTING) {
             $response = $this->processVote($form);
         } else {
             $response = $this->processToggle($form);
         }
         $response = array_merge($event->responseData, $response);
         $response['aggregate'] = VoteAggregate::findOne(['entity' => $module->encodeEntity($form->entity), 'target_id' => $form->targetId]);
     } else {
         $response = ['success' => false, 'errors' => $form->errors];
     }
     $this->trigger(self::EVENT_AFTER_VOTE, $event = $this->createEvent($form, $response));
     return $event->responseData;
 }
Exemplo n.º 2
0
 /**
  * Include vote aggregate model/values.
  *
  * @param $entity
  * @return \yii\base\Component
  * @throws \yii\base\InvalidConfigException
  */
 public function withVoteAggregate($entity)
 {
     $entityEncoded = $this->getModule()->encodeEntity($entity);
     $voteAggregateTable = VoteAggregate::tableName();
     $model = new $this->owner->modelClass();
     $this->initSelect($model);
     $this->owner->leftJoin("{$voteAggregateTable} {$entity}Aggregate", ["{$entity}Aggregate.target_id" => new Expression("`{$model->tableSchema->name}`.`{$model->primaryKey()[0]}`"), "{$entity}Aggregate.entity" => $entityEncoded])->addSelect([new Expression("`{$entity}Aggregate`.`positive` as `{$entity}Positive`"), new Expression("`{$entity}Aggregate`.`negative` as `{$entity}Negative`"), new Expression("`{$entity}Aggregate`.`rating` as `{$entity}Rating`")]);
     return $this->owner;
 }
Exemplo n.º 3
0
 /**
  * Initialize widget with default options.
  *
  * @throws \yii\base\InvalidConfigException
  */
 public function initDefaults()
 {
     $this->voteUrl = isset($this->voteUrl) ?: Yii::$app->getUrlManager()->createUrl(['vote/default/vote']);
     $this->targetId = isset($this->targetId) ?: $this->model->getPrimaryKey();
     if (!isset($this->aggregateModel)) {
         $this->aggregateModel = $this->isBehaviorIncluded() ? $this->model->getVoteAggregate($this->entity) : VoteAggregate::findOne(['entity' => $this->getModule()->encodeEntity($this->entity), 'target_id' => $this->targetId]);
     }
     if (!isset($this->userValue)) {
         $this->userValue = $this->isBehaviorIncluded() ? $this->model->getUserValue($this->entity) : null;
     }
 }
Exemplo n.º 4
0
 /**
  * @param $entity
  * @param $targetId
  */
 public static function updateRating($entity, $targetId)
 {
     $positive = static::find()->where(['entity' => $entity, 'target_id' => $targetId, 'value' => self::VOTE_POSITIVE])->count();
     $negative = static::find()->where(['entity' => $entity, 'target_id' => $targetId, 'value' => self::VOTE_NEGATIVE])->count();
     if ($positive + $negative !== 0) {
         $rating = (($positive + 1.9208) / ($positive + $negative) - 1.96 * SQRT($positive * $negative / ($positive + $negative) + 0.9604) / ($positive + $negative)) / (1 + 3.8416 / ($positive + $negative));
     } else {
         $rating = 0;
     }
     $rating = round($rating * 10, 2);
     $aggregateModel = VoteAggregate::findOne(['entity' => $entity, 'target_id' => $targetId]);
     if ($aggregateModel == null) {
         $aggregateModel = new VoteAggregate();
         $aggregateModel->entity = $entity;
         $aggregateModel->target_id = $targetId;
     }
     $aggregateModel->positive = $positive;
     $aggregateModel->negative = $negative;
     $aggregateModel->rating = $rating;
     $aggregateModel->save();
 }