Пример #1
0
 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);
     }
 }
Пример #2
0
 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;
 }
Пример #3
0
 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));
     }
 }