Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from burzum/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Florian Krämer committed Apr 25, 2014
2 parents 7eac003 + b4044f0 commit 9d726d5
Show file tree
Hide file tree
Showing 26 changed files with 582 additions and 413 deletions.
30 changes: 12 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,22 @@ language: php
php:
- 5.3
- 5.4
- 5.5

env:
- CAKE_VERSION=master
- CAKE_VERSION=2.4
global:
- PLUGIN_NAME=Ratings
- DB=mysql

before_script:
- git clone --depth 1 --branch $CAKE_VERSION git://github.com/cakephp/cakephp ../cakephp && cd ../cakephp
- cp -R ../ratings plugins/Ratings
- sh -c "mysql -e 'CREATE DATABASE cakephp_test;'"
- echo "<?php
class DATABASE_CONFIG {
public \$test = array(
'datasource' => 'Database/Mysql',
'database' => 'cakephp_test',
'host' => '0.0.0.0',
'login' => 'travis',
'persistent' => false,
);
}" > app/Config/database.php
- cd app
- git clone https://github.com/burzum/travis.git --depth 1 ../travis
- ../travis/before_script.sh

script:
- ../lib/Cake/Console/cake test Ratings AllRatingsPlugin --stderr
- ../travis/script.sh

after_success:
- ../travis/after_success.sh

notifications:
email: false
email: false
17 changes: 8 additions & 9 deletions Config/Migration/001_initialize_ratings_schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class InitializeRatingsSchema extends CakeMigration {
'up' => array(
'create_table' => array(
'ratings' => array(
'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary'),
'user_id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36),
'foreign_key' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36),
'model' => array('type' => 'string', 'null' => false, 'default' => NULL, 'key' => 'index'),
'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary'),
'user_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36),
'foreign_key' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36),
'model' => array('type' => 'string', 'null' => false, 'default' => null, 'key' => 'index'),
'value' => array('type' => 'float', 'null' => true, 'default' => '0', 'length' => '8,4'),
'created' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'UNIQUE_RATING' => array('column' => array('user_id', 'foreign_key', 'model'), 'unique' => 1)
Expand All @@ -68,7 +68,7 @@ class InitializeRatingsSchema extends CakeMigration {
* @return boolean Should process continue
* @access public
*/
function before($direction) {
public function before($direction) {
return true;
}

Expand All @@ -79,9 +79,8 @@ function before($direction) {
* @return boolean Should process continue
* @access public
*/
function after($direction) {
public function after($direction) {
return true;
}

}
?>
1 change: 0 additions & 1 deletion Config/Migration/map.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@
$map = array(
1 => array('001_initialize_ratings_schema' => 'InitializeRatingsSchema')
);
?>
20 changes: 6 additions & 14 deletions Config/Schema/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@

class RatingsSchema extends CakeSchema {

/**
* Schema name
*
* @var string
* @access public
*/
public $name = 'Ratings';

/**
* Before callback
Expand Down Expand Up @@ -58,17 +51,16 @@ public function after($event = array()) {
* @access public
*/
public $ratings = array(
'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary'),
'user_id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36),
'foreign_key' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36),
'model' => array('type' => 'string', 'null' => false, 'default' => NULL, 'key' => 'index'),
'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary'),
'user_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36),
'foreign_key' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36),
'model' => array('type' => 'string', 'null' => false, 'default' => null, 'key' => 'index'),
'value' => array('type' => 'float', 'null' => true, 'default' => '0', 'length' => '8,4'),
'created' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'UNIQUE_RATING' => array('column' => array('user_id', 'foreign_key', 'model'), 'unique' => 1)
)
);
}
?>
77 changes: 58 additions & 19 deletions Controller/Component/RatingsComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ class RatingsComponent extends Component {
*
* @var array $components
*/
public $components = array('Cookie', 'Session', 'Auth', 'RequestHandler');
public $components = array(
'Cookie',
'Session',
'Auth',
'RequestHandler'
);

/**
* Enabled / disable the component
Expand All @@ -42,7 +47,9 @@ class RatingsComponent extends Component {
*
* @var array $actionNames
*/
public $actionNames = array('view');
public $actionNames = array(
'view'
);

/**
* Name of 'rateable' model
Expand All @@ -63,11 +70,22 @@ class RatingsComponent extends Component {
public $assocName = 'Rating';

/**
* List of named args used in component
* List of query args used in component
*
* @var array $parameters
*/
public $parameters = array('rate' => true, 'rating'=> true, 'redirect' => true);
public $parameters = array(
'rate' => true,
'rating' => true,
'redirect' => true
);

/**
* Used named parameters or query string
*
* @return array
*/
public $named = true;

/**
* Constructor.
Expand All @@ -83,16 +101,17 @@ public function __construct(ComponentCollection $collection, $settings = array()
}
}
}
}
}

/**
* Callback
*
* @param object Controller object
* @param Controller $Controller
* @return void
*/
public function initialize(Controller $Controller) {
//public function initialize(&$Controller, $settings = array()) {
$this->Controller = $Controller;
if ($this->enabled == true) {
if ($this->enabled === true) {
$this->Controller->request->params['isJson'] = (isset($this->Controller->request->params['url']['ext']) && $this->Controller->request->params['url']['ext'] === 'json');
if ($this->Controller->request->params['isJson']) {
Configure::write('debug', 0);
Expand All @@ -110,12 +129,16 @@ public function initialize(Controller $Controller) {
/**
* Callback
*
* @param object Controller object
* @param Controller $Controller
*/
public function startup(Controller $Controller) {
$message = '';
$rating = null;
$params = $Controller->request->params['named'];
if ($this->named === true) {
$params = $Controller->request->params['named'];
} else {
$params = $Controller->request->query;
}
if (empty($params['rating']) && !empty($Controller->request->data[$Controller->modelClass]['rating'])) {
$params['rating'] = $Controller->request->data[$Controller->modelClass]['rating'];
}
Expand Down Expand Up @@ -153,7 +176,7 @@ public function rate($rate, $rating, $user, $redirect = false) {
$this->Controller->set($result);
if (!empty($redirect)) {
if (is_bool($redirect)) {
$this->redirect($this->buildUrl());
$this->redirect($this->removeRatingParamsFromUrl());
} else {
$this->redirect($redirect);
}
Expand All @@ -167,15 +190,26 @@ public function rate($rate, $rating, $user, $redirect = false) {
*
* @return array
*/
public function buildUrl() {
$params = array('plugin' => $this->Controller->request->params['plugin'], 'controller' => $this->Controller->request->params['controller'], 'action' => $this->Controller->request->params['action']);
$params = array_merge($params, $this->Controller->request->params['pass']);
foreach ($this->Controller->request->params['named'] as $name => $value) {
if (!isset($this->parameters[$name])) {
$params[$name] = $value;
public function removeRatingParamsFromUrl() {
if ($this->named === true) {
$queryParams = $this->Controller->request->params['named'];
} else {
$queryParams = $this->Controller->request->query;
}

foreach ($queryParams as $name => $value) {
if (isset($this->parameters[$name])) {
unset($queryParams[$name]);
}
}
return $params;

if ($this->named === true) {
$this->Controller->request->params['named'] = $queryParams;
} else {
$this->Controller->request->query = $queryParams;
}

return Router::reverse($this->Controller->request);
}

/**
Expand All @@ -198,7 +232,12 @@ public function redirect($url, $code = null, $exit = true) {
$this->Session->setFlash($this->viewVars['authMessage']);
}
if (!empty($this->Controller->request->params['isAjax']) || !empty($this->Controller->request->params['isJson'])) {
$this->Controller->setAction('rated', $this->Controller->request->params['named']['rate']);
if ($this->named === true) {
$rate = $this->Controller->request->params['named']['rate'];
} else {
$rate = $this->Controller->request->query['rate'];
}
$this->Controller->setAction('rated', $rate);
return $this->Controller->render('rated');
} else if (isset($this->Controller->viewVars['status']) && isset($this->Controller->viewVars['message'])) {
$this->Controller->Session->setFlash($this->Controller->viewVars['message'], 'default', array(), $this->Controller->viewVars['status']);
Expand Down
1 change: 1 addition & 0 deletions Controller/RatingsAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @copyright Copyright 2010 - 1013 - 1013, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('AppController', 'Controller');

/**
* CakePHP Ratings Plugin
Expand Down
18 changes: 18 additions & 0 deletions Docs/Documentation/Installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Installation
============

To create tables you can use migrations plugin or schema shell. To create the tables via schema shell execute:

```cake schema create --plugin Ratings --name ratings```

To create the tables via the migrations plugin run:

```cake migrations.migration run all -p ratings```

Make sure the plugin is loaded in the application.

```php
CakePlugin::load('Ratings');
// or
CakePlugin::loadAll();
```
50 changes: 50 additions & 0 deletions Docs/Documentation/The-Ratable-Behavior.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
The Ratable Behavior
====================

Behavior configuration
----------------------

* **modelClass:** Must be set in the case of a plugin model to make the behavior work with plugin models like `Plugin.Model`. Required to define for plugin's models, and for model of app which name does not equal to class name,
* **saveToField:** Boolean, true if the calculated result should be saved in the rated model
* **calculation:** 'average' or 'sum', default is average.
* **update:** Boolean flag, that define permission to rerate(change previous rating)
* **modelValidate:** Validate the model before save, default is false
* **modelCallbacks:** Run model callbacks when the rating is saved to the model, default is false

The following options provide common defaults that, in most cases, need not be redefined:

* **rateClass:** Name of the rate class model, by default is 'Ratings.Rating'.
* **foreignKey:** Foreign key field, contains rated model id.
* **field:** Name of the field that is updated with the calculated rating,
* **fieldSummary:** Optional cache field that will store summary of all ratings that allow to implement quick rating calculation,
* **fieldCounter:** Optional cache field that will store count of all ratings that allow to implement quick rating calculation.

Behavior callbacks
------------------

The `beforeRate` and `afterRate` callbacks are currently supported, and are defined in the rated model. These are called before and after the rate operation respectively.

BeforeRate callback get one $data parameter that is a array containing:

* **foreignKey:** Rated object id
* **userId:** Rated user id
* **value:** Rating value
* **type:** Rating mode: saveRating or removeRating

AfterRate callback get one $data parameter that is a array containing:

* **foreignKey:** Rated object id
* **userId:** Rated user id
* **value:** Rating value
* **type:** Rating mode: saveRating or removeRating
* **result:** New rating value
* **update:** Update mode value based on configuration
* **oldRating:** Previous rating state

Provided API is:

* **saveRating($foreignKey, $userId, $value):** Allow to add new rating.
* **removeRating($foreignKey, $userId):** Allow to remove undesired rating.
* **rate($foreignKey, $userId, $rating, $options):** Aallow to rate agains not numeric values like 'up'/'down' that defined in $options array.
* **isRatedBy($foreignKey, $userId):** Acheck method that user already rate defined model object
* **cacheRatingStatistics($data):** Caches the sum of the different ratings for each of them if fields with database structure contain fields rating_{$value}.
Loading

0 comments on commit 9d726d5

Please sign in to comment.