예제 #1
0
 public function actionDisableExpiredVotes()
 {
     if (Yii::$app->request->getUserIP() == '127.0.0.1') {
         $updatedCnt = Vote::updateAll(['status' => Vote::STATUS_DISABLED], "date_end > 0 AND date_end < " . time());
         if ($updatedCnt > 0) {
             CacheHelper::delete(CacheHelper::getTag(Vote::className()));
         }
     }
 }
예제 #2
0
 public function run()
 {
     $activeVote = $isVoted = false;
     $allVotes = Vote::getActiveVotes();
     if (!empty($allVotes)) {
         $validVotes = Vote::getValidVotes($allVotes);
         if (!empty($validVotes)) {
             $firstVote = reset($validVotes);
             foreach ($allVotes as $vote) {
                 if ($vote->id == $firstVote) {
                     $activeVote = $vote;
                 }
             }
             $isVoted = false;
         } else {
             $activeVote = end($allVotes);
             $isVoted = true;
         }
     }
     if ($activeVote) {
         $options = [];
         $voteOptions = CacheHelper::get(Vote::VOTE_OPTIONS_CACHE_KEY . $activeVote->id);
         if (false === $voteOptions) {
             $voteOptions = $activeVote->voteOptions;
             CacheHelper::set(Vote::VOTE_OPTIONS_CACHE_KEY . $activeVote->id, $voteOptions, CacheHelper::getTag(Vote::className()));
         }
         $maxPercent = 0;
         if (!empty($voteOptions)) {
             if ($isVoted) {
                 $options = $voteOptions;
                 $maxPercent = $activeVote->getMaxPercent();
             } else {
                 foreach ($voteOptions as $option) {
                     $options[$option->id] = $option->title;
                 }
             }
         }
         return $this->render('Vote', ['vote' => $activeVote, 'options' => $options, 'isVoted' => $isVoted, 'maxPercent' => $maxPercent]);
     } else {
         return '';
     }
 }
예제 #3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getParent()
 {
     return $this->hasOne(Vote::className(), ['id' => 'parent_id']);
 }
예제 #4
0
 public function statistics()
 {
     $statistics = [];
     $totalAnswers = 0;
     $rows = (new \yii\db\Query())->select('option_id, count(*) as cnt')->from(VoteLog::tableName())->where(['vote_id' => $this->id])->groupBy('option_id')->all();
     foreach ($rows as $row) {
         $statistics[$row['option_id']] = $row['cnt'];
         $totalAnswers += $row['cnt'];
     }
     if ($this->type == self::TYPE_SINGLE) {
         $totalCnt = $totalAnswers;
     } else {
         $totalCnt = (new \yii\db\Query())->select('count(distinct(ip)) as cnt')->from(VoteLog::tableName())->where(['vote_id' => $this->id])->one();
     }
     $this->setAttribute('total_votes', $totalCnt);
     $this->setAttribute('total_answers', $totalAnswers);
     if ($this->save() && !empty($this->voteOptions) > 0) {
         foreach ($this->voteOptions as $k => $voteOptions) {
             if ($totalAnswers > 0 && isset($statistics[$voteOptions->id])) {
                 $this->voteOptions[$k]->setAttributes(['total_votes' => $statistics[$voteOptions->id], 'percent' => round($statistics[$voteOptions->id] / $totalAnswers * 100, 2)]);
             } else {
                 $this->voteOptions[$k]->setAttributes(['total_votes' => 0, 'percent' => 0]);
             }
             $this->voteOptions[$k]->save();
         }
         CacheHelper::set(Vote::VOTE_OPTIONS_CACHE_KEY . $this->id, $this->voteOptions, CacheHelper::getTag(Vote::className()));
     }
 }