/**
  * Add a new vote
  * @param integer $id
  * @param string $type
  * @param integer $vote
  * @return mixed
  */
 public function actionVote($id, $type, $vote = 1)
 {
     if (!Yii::$app->user->isGuest) {
         $voteModel = Vote::find()->where(['voteable_type' => $type, 'voteable_id' => $id, 'user_id' => Yii::$app->user->id])->one();
         if (!isset($voteModel->id)) {
             $voteModel = new Vote();
             $voteModel->voteable_type = $type;
             $voteModel->voteable_id = $id;
             $voteModel->user_id = Yii::$app->user->id;
         } elseif ($voteModel->vote == $vote) {
             $out = ['success' => false, 'error' => 'User has already voted'];
             echo Json::encode($out);
             return;
         }
         $voteModel->vote = $vote;
         if ($voteModel->save()) {
             $rating = Vote::getRating($id, $type);
             $out = ['success' => true, 'rating' => $rating];
         } else {
             $out = ['success' => false, 'error' => $voteModel->getErrors()];
         }
     } else {
         $out = ['success' => false, 'error' => 'User is guest'];
     }
     echo Json::encode($out);
 }
示例#2
0
 /**
  * Get rating 
  * 
  * @return integer 
  */
 public function getRating()
 {
     return Vote::getRating($this->id, Vote::VOTEABLE_COMMENT);
 }
示例#3
0
 /**
  * Get block with 3 blog posts, which are best by rating during last 3 months
  * @return array Data
  */
 public static function getBlogPostsByRating($cache = true)
 {
     if ($cache) {
         $cacheBlock = CacheBlock::find()->where(['machine_name' => 'blogPostsByRating'])->one();
         if (isset($cacheBlock)) {
             return ['view' => '@frontend/views/blocks/cache_block', 'data' => ['content' => $cacheBlock->content]];
         }
     }
     $connection = Yii::$app->db;
     $blogsIDQuery = 'SELECT id
                 FROM posts
                 WHERE created_at > DATE_SUB(NOW(), INTERVAL 90 day) AND content_category_id = ' . Post::CATEGORY_BLOG;
     //INNER JOIN votes ON post.id = votes.voteable_id AND votes.voteable_type = '.Vote::VOTEABLE_POST.'
     $cmd = $connection->createCommand($blogsIDQuery);
     $bestBlogs = $cmd->queryAll();
     $ratingArray = [];
     foreach ($bestBlogs as &$blog) {
         $blog['rating'] = Vote::getRating($blog['id'], Vote::VOTEABLE_POST);
     }
     for ($i = 0; $i < count($bestBlogs) - 1; $i++) {
         for ($j = $i + 1; $j < count($bestBlogs); $j++) {
             if ($bestBlogs[$j]['rating'] > $bestBlogs[$i]['rating']) {
                 $temp = $bestBlogs[$j];
                 $bestBlogs[$j] = $bestBlogs[$i];
                 $bestBlogs[$i] = $temp;
             }
         }
     }
     $count = 3;
     if (count($bestBlogs) < 3) {
         $count = count($bestBlogs);
     }
     $best3Blogs = [];
     for ($i = 0; $i < $count; $i++) {
         $best3Blogs[] = $bestBlogs[$i]['id'];
     }
     $blogs = Post::findAll($best3Blogs);
     $block = ['view' => '@frontend/views/blocks/blog_block_rating', 'data' => compact('blogs')];
     if (!$cache) {
         $view = new \yii\base\View();
         return $view->renderFile($block['view'] . '.php', $block['data']);
     }
     return $block;
 }