Yii Star Rating

Is Rating or Voting concept available in yii?

Yes It Is

available in yii framework.
CStarRating class show the star rating widget in yii framework. It is used to get the rating details of user. It is based on jQuery Star Rating Plugin. We can show this star and assign the value to this stars. We can get the value of rated star value in controller.

CStarRating

<?php
$this->widget('CStarRating',array('name'=>'rating'));
?>

We can get this value in controller.

$rating=$_POST['rating'];
[OR]
$rating=$_GET['rating'];

Star Rating: Properties

We can assign the minimum and maximum value for star list. “minRating”, “maxRating” properties are used for this. ‘value’ property used to set default value of selected star value. ‘starCount’ used to show the number of star.

<?php
$this->widget('CStarRating',array(
            'name'=>'rating_1',
            'value'=>'3',
            'minRating'=>1,
            'maxRating'=>10,
            'starCount'=>10,
            ));
?>

Star Rating: Read Only

‘readOnly’ is a boolean property. The default value is false. So User can give the rating. But if you set ‘true’ We can only see the rating cannot give rating.

<?php
$this->widget('CStarRating',array(
            'name'=>'rating_2',
            'value'=>'3',
            'readOnly'=>true,
            ));
?>

Star Rating With Model

We can assing the model and model attribute to CStarRating widget using ‘attribute’,’model’ parameter.

<?php
$this->widget('CStarRating',array(
            'attribute'=>'rememberMe',
            'model'=>$model,
            ));
?>

Star Rating: Ajax

Using callback property we can call function using ajax and pass the value of rating via ajax.

<?php
$this->widget('CStarRating',array(
    'name'=>'star_rating_ajax',
    'callback'=>'
        function(){
                $.ajax({
                    type: "POST",
                    url: "'.Yii::app()->createUrl('cstarrating/ajax').'",
                    data: "star_rating=" + $(this).val(),
                    success: function(data){
                                $("#mystar_voting").html(data);
                        }})}'
  ));
echo "<br/>";
echo "<div id='mystar_voting'></div>";
?>

CStarRating: Titles

The default value of stars are value of star like 1, 2, 3, 4 etc. we can change the title of star using ‘titles’ property. It contains array of values.Ex 1->Normal, 2->Average

<?php
$this->widget('CStarRating',array(
            'name'=>'rating_4',
            'value'=>'3',
            'minRating'=>1,
            'maxRating'=>5,
            'titles'=>array(
                '1'=>'Normal',
                '2'=>'Average',
                '3'=>'OK',
                '4'=>'Good',
                '5'=>'Excellent'
            ),
            ));
?>