<?php if (!isset(Request::$params->score)) { $vote = PostVotes::find_by_user_id_and_post_id(User::$current->id, Request::$params->id); $score = $vote ? $vote->score : 0; respond_to_success("", array(), array('vote' => $score)); return; } $p = Post::find(Request::$params->id); $score = (int) Request::$params->score; if (!User::is('>=40') && ($score < 0 || $score > 3)) { respond_to_error("Invalid score", "#show", array('id' => Request::$params->id, 'tag_title' => $p->tag_title(), 'status' => 424)); return; } $vote_successful = $p->vote($score, User::$current); $api_data = Post::batch_api_data(array($p)); $api_data['voted_by'] = $p->voted_by(); if ($vote_successful) { respond_to_success("Vote saved", array("#show", 'id' => Request::$params->id, 'tag_title' => $p->tag_title()), array('api' => $api_data)); } else { respond_to_error("Already voted", array("#show", array('id' => Request::$params->id, 'tag_title' => $p->tag_title())), array('api' => $api_data, 'status' => 423)); }
<tr> <th>Votes</th> <td> <span class="stars"> <?php foreach (range(1, 3) as $i) { ?> <a class="star star-<?php echo $i; ?> " href="<?php echo url_for('post#index', array('tags' => "vote:>={$i}:{$user->name} order:vote")); ?> "> <?php echo PostVotes::count(array('conditions' => array("user_id = {$user->id} AND score = {$i}"))); ?> <span class="score-on score-voted score-visible">★</span> </a> <?php } ?> </span> </td> </tr> <tr> <td><strong>Comments</strong></td> <td><?php echo link_to(Comment::count(array('conditions' => "user_id = {$user->id}")), array('comment#search', 'query' => "user:{$user->name}")); ?> </td>
function vote($score, $user, $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 = PostVotes::find_by_user_id_and_post_id(array($user->id, $this->id)); if (!$vote) { $vote = PostVotes::create(array('post_id' => $this->id, 'user_id' => $user->id, 'score' => $score)); } $vote->update_attributes(array('score' => $score)); $this->recalculate_score(); return true; }