<?php
/**
 * Rating for the article - Dao
 *
 * @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
 */

/**
 * Dao class for the rating for the article based on the Zend_Db_Table
 *
 * @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_Dao_ArticleRating extends HomeWebApp_Model_Dao
{
    /** table name */
    const TABLE_NAME = "articlerating";

    /** table name
     * @var string
     */
    protected $_name = self::TABLE_NAME;

    /** primary key column
     * @var string
     */
    protected $_primary = "id";

    /** reference map for the relationship
     * @var array
     */
    protected $_referenceMap = array(
        'Article' => array(
            'columns' => 'article_id',
            'refTableClass' => 'Blog_Model_Dao_Article',
            'refColumns' => 'id'
        )
    );

    /**
     * Returns average rating for the article.
     * @param int $articleId 
     * @return int 
     */ 
    public function getAverageRatingForArticle($articleId)
    {
        $sql="select avg(rating) from $this->_name where "
            . "article_id=$articleId "
            . "group by article_id";

        return $this->getAdapter()->fetchOne($sql); 
    }

    /**
     * Returns voting count for the article.
     * @param int $articleId 
     * @return int 
     */ 
    public function getArticleVotingCount($articleId)
    {
        $sql="select count(*) from $this->_name where "
            . "article_id=$articleId";

        return $this->getAdapter()->fetchOne($sql); 
    }

    /**
     * Returns rating info for the article
     * @param int $articleId 
     * @return array array woth indexes
     *  - average : for average rating
     *  - votingCount : count of voting   
     */
    public function getRatingInfo($articleId)
    {
        $sql="select avg(rating) as average, count(*) as votingCount "
            . "from $this->_name where "
            . "article_id=$articleId";

        return $this->getAdapter()->fetchRow($sql);  
    }

    /**
     * Returns a voting count by IP addres and the time interval. The time interval
     * is defined by count of hours before now.    
     * @param string $ipaddr ip addres
     * @param int $articleId article id
     * @param int $interval count of hours before now
     * @return int
     */
    public function getVoteCountByIp($articleId,$ipAddr,$interval)
    {
        $select=$this->getAdapter()->select()
            ->from($this->_name, array("count(*)"))
            ->where($this->getAdapter()->quoteInto("ipaddr = ?",$ipAddr))
            ->where($this->getAdapter()->quoteInto("article_id = ?",$articleId))
            ->where("votingDateTime + INTERVAL $interval HOUR > NOW()");

        return $select->query()->fetchColumn(0);
    }
}
