Exemplo n.º 1
0
<script>
//jQuery('span#rating<?php 
echo $id;
?>
 input ').rating();
</script>
<div  id="rating_info_<?php 
echo $id;
?>
">
<?php 
if ($rating = FeedbackRating::model()->findByAttributes(array('feedback_id' => $id))) {
    echo "Rating: <strong>" . $rating->vote_average . "</strong>";
    echo " " . $rating->vote_count . " votes";
    $current_rating = $rating->vote_average;
} else {
    $current_rating = 0;
}
$feedback = Feedback::model()->findByPk($id);
?>
        </div>

<?php 
if (isset($update)) {
    $cs = Yii::app()->clientScript;
    $cs->scriptMap = array('jquery.js' => false, 'jquery.rating.js' => false, 'jquery.metadata.js' => false);
} elseif ($loop > 0) {
    $cs = Yii::app()->clientScript;
    $cs->scriptMap = array('jquery.js' => false, 'jquery.rating.js' => false, 'jquery.metadata.js' => false);
}
// rating
 public function actionRatefeedback()
 {
     if (Yii::app()->request->isAjaxRequest) {
         $rating = FeedbackRating::model()->findByAttributes(array('feedback_id' => $_GET['id']));
         if (count($rating) == 0) {
             $rating = new FeedbackRating();
             $rating->vote_count = 1;
             $rating->feedback_id = $_GET['id'];
             $rating->vote_sum = $_GET['val'];
             $rating->vote_average = round($rating->vote_sum / $rating->vote_count, 2);
         } else {
             $rating->vote_count = $rating->vote_count + 1;
             $rating->vote_sum = $rating->vote_sum + $_GET['val'];
             $rating->vote_average = round($rating->vote_sum / $rating->vote_count, 2);
         }
         if ($rating->save()) {
             echo CJSON::encode(array('status' => 'success', 'div' => 'Thank you for voting!', 'info' => "Rating: " . $rating->vote_average . " " . $rating->vote_count . " votes"));
         }
     }
 }