/** * Check if a user has voted for this entry * * @param integer $user_id Optinal user ID to set as voter * @param string $ip IP Address * @return integer */ public function ballot($user_id = 0, $ip = null) { if (User::isGuest()) { $vote = new Vote(); $vote->set('item_type', 'review'); $vote->set('item_id', $this->get('id')); $vote->set('created_by', $user_id); $vote->set('ip', $ip); return $vote; } $user = $user_id ? User::getInstance($user_id) : User::getInstance(); $ip = $ip ?: Request::ip(); // See if a person from this IP has already voted in the last week $votes = $this->votes(); if ($user->get('id')) { $votes->whereEquals('created_by', $user->get('id')); } elseif ($ip) { $votes->whereEquals('ip', $ip); } $vote = $votes->ordered()->limit(1)->row(); if (!$vote || !$vote->get('id')) { $vote = new Vote(); $vote->set('item_type', 'review'); $vote->set('item_id', $this->get('id')); $vote->set('created_by', $user_id); $vote->set('ip', $ip); } return $vote; }
/** * Vote this entry up or down * * @param integer $how How to vote (up or down) * @return boolean False if error, True on success */ public function vote($how) { $v = new Vote($this->_db); $v->created_by = \User::get('id'); $v->item_type = 'comment'; $v->vote = $how; $v->item_id = $this->get('id'); // Check content if (!$v->check()) { $this->setError($v->getError()); return false; } // Store new content if (!$v->store()) { $this->setError($v->getError()); return false; } return true; }