Пример #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 static function getLanguages()
 {
     $languages = CacheHelper::get('languages');
     if (!$languages) {
         $languages = self::find()->orderBy(['position' => SORT_ASC])->all();
         CacheHelper::set('languages', $languages, CacheHelper::getTag(self::className()));
     }
     return $languages;
 }
Пример #3
0
 public static function processRedirect()
 {
     $redirects = CacheHelper::get('redirects');
     if (empty($redirects)) {
         $redirects = (new Query())->select(['old_url', 'new_url'])->from(Redirect::tableName())->all();
         $redirects = ArrayHelper::map($redirects, 'old_url', 'new_url');
         CacheHelper::set('redirects', $redirects, CacheHelper::getTag(self::className()));
     }
     $url = $_SERVER['REQUEST_URI'];
     if (isset($redirects[$url])) {
         Yii::$app->response->redirect($redirects[$url], 301);
     }
 }
Пример #4
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 '';
     }
 }
Пример #5
0
 static function saveBlockToCache($cacheKey, $data)
 {
     CacheHelper::set($cacheKey, $data, CacheHelper::getTag(self::className()));
 }
Пример #6
0
 public function getStructure()
 {
     $owner = $this->owner;
     $cacheKey = CacheHelper::getTag($owner::className()) . '_items_tree' . Yii::$app->language;
     $structure = CacheHelper::get($cacheKey);
     if (!$structure) {
         $structure = $this->makeStructure();
         CacheHelper::set($cacheKey, $structure, CacheHelper::getTag($owner::className()));
     }
     return $structure;
 }
Пример #7
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()));
     }
 }