<?php
/**
 * Rating for articles as model.
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * Class, which represents a rating for the article.
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class Blog_Model_ArticleRating extends Blog_Model_Model
{
    /** identifier
     * @var int
     */  
    private $_id;

    /** id of the article
     * @var int
     */  
    private $_articleId;

    /** ip address
     * @var string
     */
    private $_ipAddr;

    /** the rating for an article
     * @var int
     */  
    private $_rating;

    /** a datime for a concrete voting
     * @var Zend_Date
     */
    private $_votingDateTime;

    protected function _setDefaultDao()
    {
        $this->setDao(new Blog_Model_Dao_ArticleRating());
    }

    public function setRating($rating)
    {
        $this->_rating=$rating;
    }

    public function setArticleId($articleId)
    {
        $this->_articleId=$articleId;
    }

    public function setIpAddr($ipAddr)
    {
        $this->_ipAddr=$ipAddr;
    }

    protected function _setPropFromArray(&$arr)
    {
        foreach($arr as $key=>$val) {
            $this->{"_".$key}=$val;
        }
    }

    public function getCookieName()
    {
        return "articleRating".$this->_articleId;
    }

    public function wasVoted()
    {
        return $this->getDao()->getVoteCountByIp($this->_articleId,$this->_ipAddr,1);
    }

    public function insert()
    {
        $this->_id=$this->getDao()->insert(array(
            "article_id"=>$this->_articleId,
            "ipAddr"=>$this->_ipAddr,
            "votingDatetime"=>new Zend_Db_Expr("NOW()"),
            "rating"=>$this->_rating
        ));
    }

    /**
     * Returns average rating for the article.
     * @return int 
     */ 
    public function getAverageRatingForArticle()
    {
        return $this->getDao()->getAverageRatingForArticle($this->_articleId);
    }

    /**
     * Returns voting count for the article.
     * @return int
     */ 
    public function getArticleVotingCount()
    {
        return $this->getDao()->getArticleVotingCount($this->_articleId);
    }

    /**
     * Returns voting info for the article
     * @return array 
     * @see Dao_ArticleRating::getRatingInfo()
     */ 
    public function getRatingInfo()
    {
        return $this->getDao()->getRatingInfo($this->_articleId);
    }

    /**
     * Gets database row object which corresponds our current properties.
     * It is used mostly for the operation with the relationship. Hence we need
     * to set only keys
     * @param bool $keyOnly true - sets only key properties (primary and foreign) it is
     * enough in most situations, false - sets all properties
     * @return Zend_Db_Table_Row_Abstract
     */
    public function getDaoRow($keyOnly=true)
    {
        $data = array(
            "id"=>$this->_id,
            "article_id"=>$this->_articleId
        );

        if (!$keyOnly) {
            $data["ipAddr"]=$this->_ipAddr;
            $data["votingDateTime"]=$this->_votingDateTime;
            $data["rating"]=$this->_rating;
        }

        $row=$this->getDao()->createRow($data);
        return $row;
    }

    protected function _inflectDaoRowIndexes(&$arr)
    {
        $notAllowedIndexes=array("dao");

        $keys=array_keys($arr);
        foreach($keys as $key) {
            //unset not allowed indexes
            if (in_array($key,$notAllowedIndexes))
                throw new Exception("you cannot use $key index as the column index");

            switch ($key) {
            case "article_id":
                MKLib_Utils::renameKey($arr,$key,"articleId");
                break;
            default:
                break;
            }
        }
    }
}
