/** * Adds a single up/down vote to the item and recalculates the vote results. * This method performs no checks to see whether the current user has already voted for the item. * * @param integer 0 = down, 1 = up * @param string IP number * @return void */ public function cast_vote($vote, $ip = NULL) { // Vote value must be either 0 or 1 $vote = min(1, max(0, (int) $vote)); $aux_vote = 0; if ($vote) { // Add an "up" vote $this->votes_up += 10; $sql = 'votes_up = votes_up + 10'; $aux_vote = 10; } else { $aux_vote = -10; // Add a "down" vote $this->votes_down += 10; $sql = 'votes_down = votes_down + 10'; } // Recalculate the vote results, no need to reload the item from database $this->calculate_votes(); // Update the item record $sth = ThumbsUp::db()->prepare('UPDATE ' . ThumbsUp::config('database_table_prefix') . 'items SET ' . $sql . ' WHERE id = ?'); $sth->execute(array($this->id)); // The current user has just cast a vote $this->user_voted = TRUE; $this->register_vote_in_owloo($this->name, $aux_vote); // Add the item id to a cookie if (ThumbsUp::config('cookie_check')) { ThumbsUp_Cookie::add_id($this->id); } // Combine the storage of the IP and user id into one query for optimization $ip = ThumbsUp::config('ip_check') ? ThumbsUp::get_ip() : NULL; $user_id = ThumbsUp::config('user_id_check') ? ThumbsUp::get_user_id() : NULL; if ($ip or $user_id) { $sth = ThumbsUp::db()->prepare('INSERT INTO ' . ThumbsUp::config('database_table_prefix') . 'votes (item_id, ip, user_id, value, date) VALUES (?, ?, ?, ?, ?)'); $sth->execute(array($this->id, $ip, $user_id, $vote, time())); } }
/** * Deletes the cookie completely. * * @return boolean was setcookie() successful or not? */ public static function delete() { // Delete cookie contents self::$cookie = ''; unset($_COOKIE[ThumbsUp::config('cookie_name')]); // If any output has been sent, setcookie() will fail. // If we're not in debug mode, we'll fail silently. if (headers_sent() and !ThumbsUp::config('debug')) { return FALSE; } // Setting a cookie with a value of FALSE will try to delete it return setcookie(ThumbsUp::config('cookie_name'), FALSE, time() - 86400, ThumbsUp::config('cookie_path'), ThumbsUp::config('cookie_domain')); }