CStarRating

Package system.web.widgets
Inheritance class CStarRating » CInputWidget » CWidget » CBaseController » CComponent
Since 1.0
Source Code framework/web/widgets/CStarRating.php
CStarRating displays a star rating control that can collect user rating input.

CStarRating is based on jQuery Star Rating Plugin. It displays a list of stars indicating the rating values. Users can toggle these stars to indicate their rating input. On the server side, when the rating input is submitted, the value can be retrieved in the same way as working with a normal HTML input. For example, using
$this->widget('CStarRating',array('name'=>'rating'));
we can retrieve the rating value via $_POST['rating'].

CStarRating allows customization of its appearance. It also supports empty rating as well as read-only rating.

Public Properties

Property Type Description Defined By
actionPrefix string the prefix to the IDs of the actions. CWidget
allowEmpty boolean whether the rating value can be empty (not set). CStarRating
attribute string the attribute associated with this widget. CInputWidget
blur string Callback when the stars are not focused. CStarRating
callback string Callback when the stars are clicked. CStarRating
controller CController Returns the controller that this widget belongs to. CWidget
cssFile mixed the CSS file used for the widget. CStarRating
focus string Callback when the stars are focused. CStarRating
htmlOptions array additional HTML options to be rendered in the input tag CInputWidget
id string Returns the ID of the widget or generates a new one if requested. CWidget
maxRating mixed the maximum rating allowed. CStarRating
minRating mixed the minimum rating allowed. CStarRating
model CModel the data model associated with this widget. CInputWidget
name string the input name. CInputWidget
owner CBaseController Returns the owner/creator of this widget. CWidget
ratingStepSize mixed the step size of rating. CStarRating
readOnly boolean whether the rating value is read-only or not. CStarRating
resetText string the hint text for the reset button. CStarRating
resetValue string the value taken when the rating is cleared. CStarRating
skin mixed the name of the skin to be used by this widget. CWidget
starCount integer the number of stars. CStarRating
starWidth integer the width of star image. CStarRating
titles array the titles associated with the rating options. CStarRating
value string the input value CInputWidget
viewPath string Returns the directory containing the view files for this widget. CWidget

Protected Properties

Property Type Description Defined By
clientOptions array the javascript options for the star rating CStarRating

Public Methods

Method Description Defined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CWidget
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
actions() Returns a list of actions that are used by this widget. CWidget
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
beginCache() Begins fragment caching. CBaseController
beginClip() Begins recording a clip. CBaseController
beginContent() Begins the rendering of content that is to be decorated by the specified view. CBaseController
beginWidget() Creates a widget and executes it. CBaseController
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
createWidget() Creates a widget and initializes it. CBaseController
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
endCache() Ends fragment caching. CBaseController
endClip() Ends recording a clip. CBaseController
endContent() Ends the rendering of content. CBaseController
endWidget() Ends the execution of the named widget. CBaseController
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getController() Returns the controller that this widget belongs to. CWidget
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getId() Returns the ID of the widget or generates a new one if requested. CWidget
getOwner() Returns the owner/creator of this widget. CWidget
getViewFile() Looks for the view script file according to the view name. CWidget
getViewPath() Returns the directory containing the view files for this widget. CWidget
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the widget. CWidget
raiseEvent() Raises an event. CComponent
registerClientScript() Registers the necessary javascript and css scripts. CStarRating
registerCssFile() Registers the needed CSS file. CStarRating
render() Renders a view. CWidget
renderFile() Renders a view file. CBaseController
renderInternal() Renders a view file. CBaseController
run() Executes the widget. CStarRating
setId() Sets the ID of the widget. CWidget
widget() Creates a widget and executes it. CBaseController

Protected Methods

Method Description Defined By
getClientOptions() Returns the javascript options for the star rating CStarRating
hasModel() Determines whether this widget is associated with a data model. CInputWidget
renderStars() Renders the stars. CStarRating
resolveNameID() CInputWidget

Property Details

allowEmpty property

public boolean $allowEmpty;

whether the rating value can be empty (not set). Defaults to true. When this is true, a reset button will be displayed in front of stars.

blur property

public string $blur;

Callback when the stars are not focused.

callback property

public string $callback;

Callback when the stars are clicked.

clientOptions property read-only

protected array getClientOptions()

the javascript options for the star rating

cssFile property

public mixed $cssFile;

the CSS file used for the widget. Defaults to null, meaning using the default CSS file included together with the widget. If false, no CSS file will be used. Otherwise, the specified CSS file will be included when using this widget.

focus property

public string $focus;

Callback when the stars are focused.

maxRating property

public mixed $maxRating;

the maximum rating allowed. This can be either an integer or a float value. Defaults to 10.

minRating property

public mixed $minRating;

the minimum rating allowed. This can be either an integer or a float value. Defaults to 1.

ratingStepSize property

public mixed $ratingStepSize;

the step size of rating. This is the minimum difference between two rating values. Defaults to 1.

readOnly property

public boolean $readOnly;

whether the rating value is read-only or not. Defaults to false. When this is true, the rating cannot be changed.

resetText property

public string $resetText;

the hint text for the reset button. Defaults to null, meaning using the system-defined text (which is 'Cancel Rating').

resetValue property

public string $resetValue;

the value taken when the rating is cleared. Defaults to null, meaning using the system-defined value (which is '').

starCount property

public integer $starCount;

the number of stars. Defaults to 5.

starWidth property

public integer $starWidth;

the width of star image. Defaults to null, meaning using the system-defined value (which is 16).

titles property

public array $titles;

the titles associated with the rating options. The keys are ratings and the values are the corresponding titles. Defaults to null, meaning using the rating value as the title.

Method Details

getClientOptions() method

protected array getClientOptions()
{return} array the javascript options for the star rating
Source Code: framework/web/widgets/CStarRating.php#180 (show)
protected function getClientOptions()
{
    
$options=array();
    if(
$this->resetText!==null)
        
$options['cancel']=$this->resetText;
    if(
$this->resetValue!==null)
        
$options['cancelValue']=$this->resetValue;
    if(
$this->allowEmpty===false)
        
$options['required']=true;
    if(
$this->starWidth!==null)
        
$options['starWidth']=$this->starWidth;
    if(
$this->readOnly===true)
        
$options['readOnly']=true;
    foreach(array(
'focus''blur''callback') as $event)
    {
        if(
$this->$event!==null)
        {
            if(
$this->$event instanceof CJavaScriptExpression)
                
$options[$event]=$this->$event;
            else
                
$options[$event]=new CJavaScriptExpression($this->$event);
        }
    }
    return 
$options;
}

registerClientScript() method

public void registerClientScript(string $id)
$id string the ID of the container
Source Code: framework/web/widgets/CStarRating.php#122 (show)
public function registerClientScript($id)
{
    
$jsOptions=$this->getClientOptions();
    
$jsOptions=empty($jsOptions) ? '' CJavaScript::encode($jsOptions);
    
$js="jQuery('#{$id} > input').rating({$jsOptions});";
    
$cs=Yii::app()->getClientScript();
    
$cs->registerCoreScript('rating');
    
$cs->registerScript('Yii.CStarRating#'.$id,$js);

    if(
$this->cssFile!==false)
        
self::registerCssFile($this->cssFile);
}

Registers the necessary javascript and css scripts.

registerCssFile() method

public static void registerCssFile(string $url=NULL)
$url string the CSS URL. If null, a default CSS URL will be used.
Source Code: framework/web/widgets/CStarRating.php#139 (show)
public static function registerCssFile($url=null)
{
    
$cs=Yii::app()->getClientScript();
    if(
$url===null)
        
$url=$cs->getCoreScriptUrl().'/rating/jquery.rating.css';
    
$cs->registerCssFile($url);
}

Registers the needed CSS file.

renderStars() method

protected void renderStars(string $id, string $name)
$id string the ID of the container
$name string the name of the input
Source Code: framework/web/widgets/CStarRating.php#152 (show)
protected function renderStars($id,$name)
{
    
$inputCount=(int)(($this->maxRating-$this->minRating)/$this->ratingStepSize+1);
    
$starSplit=(int)($inputCount/$this->starCount);
    if(
$this->hasModel())
    {
        
$attr=$this->attribute;
        
CHtml::resolveName($this->model,$attr);
        
$selection=$this->model->$attr;
    }
    else
        
$selection=$this->value;
    
$options=$starSplit>? array('class'=>"{split:{$starSplit}}") : array();
    for(
$value=$this->minRating$i=0;$i<$inputCount; ++$i$value+=$this->ratingStepSize)
    {
        
$options['id']=$id.'_'.$i;
        
$options['value']=$value;
        if(isset(
$this->titles[$value]))
            
$options['title']=$this->titles[$value];
        else
            unset(
$options['title']);
        echo 
CHtml::radioButton($name,!strcmp($value,$selection),$options) . "\n";
    }
}

Renders the stars.

run() method

public void run()
Source Code: framework/web/widgets/CStarRating.php#101 (show)
public function run()
{
    list(
$name,$id)=$this->resolveNameID();
    if(isset(
$this->htmlOptions['id']))
        
$id=$this->htmlOptions['id'];
    else
        
$this->htmlOptions['id']=$id;
    if(isset(
$this->htmlOptions['name']))
        
$name=$this->htmlOptions['name'];

    
$this->registerClientScript($id);

    echo 
CHtml::openTag('span',$this->htmlOptions)."\n";
    
$this->renderStars($id,$name);
    echo 
"</span>";
}

Executes the widget. This method registers all needed client scripts and renders the text field.

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc/api/1.1/CStarRating