Yii2 Newton Cool Ranking Behavior
This behavior provides the algorithm of rank hotness with Newton's law of cooling
explained by Evan Miller., (*1)
You can use it to rate comments or blog posts. Listing active discussion threads in an online forum., (*2)
Read this article Rank Hotness With Newton's Law of Cooling for more details., (*3)
Installation
Package is available on Packagist,
you can install it using Composer., (*4)
composer require sizeg/yii2-newton-cool-ranking-behavior
Dependencies
- Yii2 (testing with 2.8, but should work with lower versions)
Basic usage
Create migration,, (*5)
public function up()
{
// [[NewtonCoolRankingBehavior::$rankAttribute]]
$this->addColumn('{{%tableName}}', 'rank', $this->float());
// [[NewtonCoolRankingBehavior::$rankTimeAttribute]]
// By default time update with result of php time() function
// For example we will use DateTime instead of UnixTimestamp
$this->addColumn('{{%tableName}}', 'rankTime', $this->datetime());
// [[NewtonCoolRankingBehavior::$rankBoostAttribute]]
// This field is optional
$this->addField('{{%tableName}}', 'rankBoost', $this->float());
}
Add behavior to your ActiveRecord model,, (*6)
class Item extends \yii\base\ActiveRecord
{
public function behaviors()
{
return \yii\helpers\ArrayHelper::merge(parent::behaviors(), [
[
'class' => 'sizeg\newtoncoolranking\NewtonCoolRankingBehavior',
// optional params
'initial' => 1000,
'coolingRate' => 150,
'timeValue' => date('Y-m-d H:i:s'), // can be a callback function
]
]);
}
}
By default the new model would have [[NewtonCoolRankingBehavior::$initial]] value
and will cooling with [[NewtonCoolRankingBehavior::$coolingRate]]., (*7)
When there is new activity on an model, you need update rank,, (*8)
/** @var ActiveRecord $model */
$model->heat(20);
Sometimes you need one or more models to show in top for a few days, then you need to boost it., (*9)
Boost value will be received from model [[NewtonCoolRankingBehavior::$rankBoostAttribute]] field.
If field doesn't exist, the value will be received from optional [[NewtonCoolRankingBehavior::$boost]] attribute., (*10)
/** @var ActiveRecord $model */
$model->boost();