Star rating plugin for CakePHP

Download

Latest version for CakePHP 1.2: http://www.wuala.com/mystic11/public/rating2.3.zip

Latest version for CakePHP 1.3: http://www.wuala.com/mystic11/public/rating2.4.zip

 

Installation and Use

1) Make sure you meet the requirements above.
For the download and integration of a javascript framework, please visit http://www.prototypejs.org/ for Prototype and http://jquery.com/ for jQuery.

2) Extract the plugin, including the subfolder rating, to your app plugins folder app/plugins.

3) Copy the rating/config/plugin_rating.php to your app configs folder app/config and change the settings to your desire.
It is recommended to let Rating.showHelp set to true until everything works.

4) Apply the install.sql to your database to create the ratings table

CREATE TABLE `ratings` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `user_id` char(36) NOT NULL default '',
  `model_id` char(36) NOT NULL default '',  
  `model` varchar(100) NOT NULL default '',
  `rating` tinyint(2) unsigned NOT NULL default '0',
  `name` varchar(100) default '',
  `created` datetime default NULL,
  `modified` datetime default NULL,
  PRIMARY KEY (`id`),
  KEY `rating` (`model_id`,`model`,`rating`,`name`)
); 

 
5) Load the plugin javascript and css files into your layout. Replace [your_framework] with prototype_min or jquery_min depending on the framework you use.



View Template:


<?php
  
echo $javascript->link('/rating/js/rating_[your_framework]');
  echo 
$html->css('/rating/css/rating'); ?>

6) For full model integration in your app, apply the following relation to your models. (replace [name_of_your_model])



Model Class:

<?php  var $hasMany = array('Rating' => 
                     array(
'className'   => 'Rating',
                           
'foreignKey'  => 'model_id',
                           
'conditions' => array('Rating.model' => '[name_of_your_model]'),
                           
'dependent'   => true,
                           
'exclusive'   => true
               
)
);
?>


 
7) If you set Rating.saveToModel to true, then add the defined Rating.modelAverageField and Rating.modelVotesField to all models you want to rate.
To do that you can use the following SQL statements (replace [your_table], [Rating.modelAverageField], [Rating.modelVotesField]).

ALTER TABLE [your_table] ADD (`[Rating.modelAverageField]` decimal(3,1) unsigned default '0.0');
ALTER TABLE [your_table] ADD (`[Rating.modelVotesField]` int(11) unsigned default '0');

If the plugin shows the fields are still missing, try to clear the model cache of your app at app/tmp/cache/models.

8) You can change the styles of the rating element in the css file rating/vendors/css/rating.css.

9) Finally you can place the rating element in your views as follows. (replace [name_of_your_model] and [id_of_your_model])



Default rating element for one model id


View Template:


<?php echo $this->element('rating', array('plugin' => 'rating',
                                    
'model' => '[name_of_your_model]',
                                    
'id' => [id_of_your_model])); ?>


Default rating element for one model id
If you want to have different ratings for one model id like sound and picture of a movie, you can use the additional name parameter.



View Template:


<?php
  
echo $this->element('rating', array('plugin' => 'rating',
                                      
'model' => '[name_of_your_model]',
                                      
'id' => [id_of_your_model],
                                      
'name' => 'sound'));

  echo 
$this->element('rating', array('plugin' => 'rating',
                                      
'model' => '[name_of_your_model]',
                                      
'id' => [id_of_your_model],
                                      
'name' => 'picture')); ?>


 
 
Individual configuration of a rating element
Sometimes you want to use more than one style of rating elements in your app. That can be reached with the 'config' parameter and different config files in 'app/config'. Just clone the default 'plugin_rating.php' and give it a different name, which you then pass to the element.



View Template:


  // uses 'plugin_rating.php' in 'app/config'
  echo $this->element('rating', array('plugin' => 'rating', 
                                      'model' => '[name_of_your_model]',
                                      'id' => [id_of_your_model]));
  
  // uses 'plugin_rating_style1.php' in 'app/config'
  echo $this->element('rating', array('plugin' => 'rating',
                                      'model' => '[name_of_your_model]',
                                      'id' => [id_of_your_model],
                                      'config' => 'plugin_rating_style1'));

Comments