Commits

matteosister committed 18ecac4

refactoring for new propel

Comments (0)

Files changed (1)

lib/behavior/RatableBehavior.php

 {
     protected $parameters = array(
         'average_column'  => 'ratable_average',
-        'average_column_phpname' => 'Average',
-        'nb_votes_column' => 'ratable_nb_votes',
-        'nb_votes_column_phpname' => 'NbVotes',
-        'precision'       => 1,
-        'max_vote'        => 5
+        'vote_number_column' => 'ratable_nb_votes',
+        'max_vote'        => 10
     );
 
     /**
     */
 	public function modifyTable()
 	{
-		if(!$this->getTable()->containsColumn($this->getParameter('average_column'))) {
+        // add the average column
+		if(!$this->getTable()->hasColumn($this->getParameter('average_column'))) {
 			$this->getTable()->addColumn(array(
-				'name' => $this->getParameter('average_column'),
-                'phpName' => $this->getParameter('average_column_phpname'),
-				'type' => PropelTypes::FLOAT
+				'name'         => $this->getParameter('average_column'),
+				'type'         => 'FLOAT',
+                'defaultValue' => NULL
 			));
 		}
 
-        if(!$this->getTable()->containsColumn($this->getParameter('nb_votes_column'))) {
+        // add the vote number column
+        if(!$this->getTable()->hasColumn($this->getParameter('vote_number_column'))) {
 			$this->getTable()->addColumn(array(
-				'name' => $this->getParameter('nb_votes_column'),
-                'phpName' => $this->getParameter('nb_votes_column_phpname'),
-				'type' => PropelTypes::INTEGER
+				'name'         => $this->getParameter('vote_number_column'),
+				'type'         => 'INTEGER',
+                'defaultValue' => 0
 			));
 		}
 	}
 	 */
 	protected function getNbVotesColumnGetter()
 	{
-		return 'get' . $this->getColumnForParameter('nb_votes_column')->getPhpName();
+		return 'get' . $this->getColumnForParameter('vote_number_column')->getPhpName();
 	}
 
 	/**
 	 */
 	protected function getNbVotesColumnSetter()
 	{
-		return 'set' . $this->getColumnForParameter('nb_votes_column')->getPhpName();
+		return 'set' . $this->getColumnForParameter('vote_number_column')->getPhpName();
 	}
 
 	/**
 
     protected function generateSetRatingMethod()
     {
-        $precision = $this->parameters['precision'];
         return "
 /**
-* Rate a propel object with ratable behavior
+* Rate the {$this->getTable()->getName()}
+*
+* @param \$rating the vote from 1 to 10
+* @return float the new rating
 */
 public function setRating(\$rating) {
     if (!is_int(\$rating)) {
         throw new InvalidArgumentException('You must provide an integer to rate an object');
     }
-    if (!in_array(\$rating, array(1, 2, 3, 4, 5))) {
-        throw new InvalidArgumentException('You must provide a vote from 1 to 5, no float number allowed');
+    if (!in_array(\$rating, range(1, 10, 1))) {
+        throw new InvalidArgumentException('You must provide a vote from 1 to 10, no float number allowed');
     }
 
     if (null === \$this->{$this->getAverageColumnGetter()}()) {
         \$newSum = (\$this->{$this->getAverageColumnGetter()}() * \$this->{$this->getNbVotesColumnGetter()}()) + \$rating;
     }
     \$this->{$this->getNbVotesColumnSetter()}(\$this->{$this->getNbVotesColumnGetter()}() + 1);
-    \$this->{$this->getAverageColumnSetter()}(round(\$newSum / \$this->{$this->getNbVotesColumnGetter()}(), {$precision}));
+    \$this->{$this->getAverageColumnSetter()}(\$newSum / \$this->{$this->getNbVotesColumnGetter()}());
 }
 ";
     }
 
     protected function generateGetRatingMethod()
     {
-        $precision = $this->parameters['precision'];
         $maxVote = $this->parameters['max_vote'];
         return "
 /**
- * Get the average rate for a Propel object with ratable behavior
- * @int \$max          The maximum vote
- * @bool \$precision   Precision value for the 'round' php function
+ * Get the average rate for this {$this->getTable()->getName()}
  *
- * @return \$vote or null
+ * @int \$max          The maximum vote
+ * @return float|null the rating or null if the object has never been rated
  */
-public function getRating(\$max = {$maxVote}, \$precision = {$precision}) {
-    return null === \$this->{$this->getAverageColumnGetter()}() ? null : round((\$this->{$this->getAverageColumnGetter()}() * \$max) / 5, \$precision);
+public function getRating(\$max = {$maxVote}) {
+    return null === \$this->{$this->getAverageColumnGetter()}() ? null : (\$this->{$this->getAverageColumnGetter()}() / 10) * \$max;
 }
 ";
     }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.