<?php

require_once 'model/om/BaseSnippetVote.php';


/**
 * Skeleton subclass for representing a row from the 'sn_vote' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package model
 */	
class SnippetVote extends BaseSnippetVote
{
  public function save($con = null)
  {

    $con = Propel::getConnection();
    try
    {
      $con->begin();

      $ret = parent::save($con);

      // update average_vote in snippet table
      $c = new Criteria();
      $c->add(SnippetVotePeer::SNIPPET_ID, $this->getSnippetId());
      $votes = SnippetVotePeer::doSelect($c);

      $sum_of_votes = 0;
      foreach($votes as $vote)
      {
        $sum_of_votes += $vote->getVote();
      }

      $this->getSnippetSnippet()->setAverageVote($sum_of_votes / count($votes));
      $this->getSnippetSnippet()->save($con);

      $con->commit();

      return $ret;
    }
    catch (Exception $e)
    {
      $con->rollback();
      throw $e;
    }
  }
}

