public function give_favorites_to_parent() { if (!$this->parent_id) { return; } $parent = Post::find($this->parent_id); foreach (PostVote::where('post_id = ?', $this->id)->take() as $vote) { $parent->vote($vote->score, $vote->user); $this->vote(0, $vote->user); } }
public function vote($score, $user, array $options = array()) { $score < CONFIG()->vote_record_min && ($score = CONFIG()->vote_record_min); $score > CONFIG()->vote_record_max && ($score = CONFIG()->vote_record_max); if ($user->is_anonymous()) { return false; } $vote = PostVote::where(['user_id' => $user->id, 'post_id' => $this->id])->first(); if (!$vote) { $vote = PostVote::create(array('post_id' => $this->id, 'user_id' => $user->id, 'score' => $score)); } $vote->updateAttributes(array('score' => $score)); $this->recalculate_score(); return true; }
public function addVote($user, $vote) { $postVote = new PostVote(); $postVote->setPost($this); $postVote->setUser($user); $postVote->setPower($vote == 'plus' ? 1 : -1); $postVote->save(); }
public function vote() { if ($this->params()->score === null) { $vote = PostVote::where(['user_id' => current_user()->id, 'post_id' => $this->params()->id])->first(); $score = $vote ? $vote->score : 0; $this->respond_to_success("", array(), array('vote' => $score)); return; } $p = Post::find($this->params()->id); $score = (int) $this->params()->score; if (!current_user()->is_mod_or_higher() && ($score < 0 || $score > 3)) { $this->respond_to_error("Invalid score", array("#show", 'id' => $this->params()->id, 'tag_title' => $p->tag_title(), 'status' => 424)); return; } $vote_successful = $p->vote($score, current_user()); $api_data = Post::batch_api_data(array($p)); $api_data['voted_by'] = $p->voted_by(); if ($vote_successful) { $this->respond_to_success("Vote saved", array("#show", 'id' => $this->params()->id, 'tag_title' => $p->tag_title()), array('api' => $api_data)); } else { $this->respond_to_error("Already voted", array("#show", 'id' => $this->params()->id, 'tag_title' => $p->tag_title()), array('api' => $api_data, 'status' => 423)); } }
/** * MI: Accept "fake_sample_url" option, passed only in * post#index. It will set a flag that will be read by PostFileMethods::sample_url() */ public static function batch_api_data(array $posts, $options = array()) { self::$create_fake_sample_url = !empty($options['fake_sample_url']); foreach ($posts as $post) { $result['posts'][] = $post->api_attributes(); } if (empty($options['exclude_pools'])) { $result['pools'] = $result['pool_posts'] = []; $pool_posts = Pool::get_pool_posts_from_posts($posts); $pools = Pool::get_pools_from_pool_posts($pool_posts); foreach ($pools as $p) { $result['pools'][] = $p->api_attributes(); } foreach ($pool_posts as $pp) { $result['pool_posts'][] = $pp->api_attributes(); } } if (empty($options['exclude_tags'])) { $result['tags'] = Tag::batch_get_tag_types_for_posts($posts); } if (!empty($options['user'])) { $user = $options['user']; } else { $user = current_user(); } # Allow loading votes along with the posts. # # The post data is cachable and vote data isn't, so keep this data separate from the # main post data to make it easier to cache API output later. if (empty($options['exclude_votes'])) { $vote_map = array(); if ($posts) { $post_ids = array(); foreach ($posts as $p) { $post_ids[] = $p->id; } if ($post_ids) { $post_ids = implode(',', $post_ids); $sql = sprintf("SELECT v.* FROM post_votes v WHERE v.user_id = %d AND v.post_id IN (%s)", $user->id, $post_ids); $votes = PostVote::findBySql($sql); foreach ($votes as $v) { $vote_map[$v->post_id] = $v->score; } } } $result['votes'] = $vote_map; } self::$create_fake_sample_url = false; return $result; }
/** * 为帖子投票 * Enter description here ... * @param unknown_type $postid * @param unknown_type $value */ public function actionPost($postid = 0, $value = 0) { if ($postid) { //$vote = new PostVote; $vote = PostVote::model()->findByAttributes(array('userId' => Yii::app()->user->id, 'postid' => $postid)); if ($vote && $vote->value == $value) { $result = $vote->delete(); } else { $vote or $vote = new PostVote(); $vote->postid = $postid; $vote->value = $value; $vote->userId = Yii::app()->user->id; $vote->addTime = time(); $result = $vote->save(); } if ($result) { $post = Post::model()->with('voteupCount', 'votedownCount')->findByPk($vote->postid); $post->updateVoteCount(); $score = $post->voteupCount - $post->votedownCount; $this->renderPartial('result', array('score' => $score, 'voteupers' => $post->voteupers)); } } }