oxrating.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxRating extends oxBase
00008 {
00014     protected $_blDisableShopCheck = true;
00015 
00021     protected $_sClassName = 'oxrating';
00022 
00026     public function __construct()
00027     {
00028         parent::__construct();
00029         $this->init( 'oxratings' );
00030     }
00031 
00037     protected function _insert()
00038     {
00039         // set oxcreate
00040         $this->oxratings__oxtimestamp= new oxField(date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() ));
00041 
00042         return parent::_insert();
00043     }
00044 
00054     public function allowRating( $sUserId, $sType, $sObjectId)
00055     {
00056         $oDb = oxDb::getDb();
00057         $myConfig = $this->getConfig();
00058 
00059         if ( $iRatingLogsTimeout = $myConfig->getConfigParam( 'iRatingLogsTimeout' ) ) {
00060             $sExpDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() - $iRatingLogsTimeout*24*60*60);
00061             $oDb->execute( "delete from oxratings where oxtimestamp < '$sExpDate'" );
00062         }
00063         $sSelect = "select oxid from oxratings where oxuserid = ".$oDb->quote( $sUserId )." and oxtype=".$oDb->quote( $sType )." and oxobjectid = ".$oDb->quote( $sObjectId );
00064         if ( $oDb->getOne( $sSelect ) ) {
00065             return false;
00066         }
00067 
00068         return true;
00069     }
00070 
00071 
00081     public function getRatingAverage( $sObjectId, $sType, $aIncludedObjectsIds = null )
00082     {
00083         $oDb = oxDb::getDb();
00084 
00085         $sQuerySnipet = '';
00086         if ( is_array( $aIncludedObjectsIds ) && count( $aIncludedObjectsIds ) > 0 ) {
00087             $sQuerySnipet = " AND ( `oxobjectid` = ".$oDb->quote( $sObjectId ) . " OR `oxobjectid` in ('" .implode("', '", $aIncludedObjectsIds). "') )";
00088         } else {
00089             $sQuerySnipet = " AND `oxobjectid` = ".$oDb->quote( $sObjectId );
00090         }
00091 
00092         $sSelect = "
00093             SELECT
00094                 AVG(`oxrating`)
00095             FROM `oxreviews`
00096             WHERE `oxrating` > 0
00097                  AND `oxtype` = " . $oDb->quote( $sType )
00098                . $sQuerySnipet . "
00099             LIMIT 1";
00100 
00101         $fRating = 0;
00102         if ( $fRating = $oDb->getOne( $sSelect, false, false ) ) {
00103             $fRating = round( $fRating, 1 );
00104         }
00105 
00106         return $fRating;
00107     }
00108 
00118     public function getRatingCount( $sObjectId, $sType, $aIncludedObjectsIds = null )
00119     {
00120         $oDb = oxDb::getDb();
00121 
00122         $sQuerySnipet = '';
00123         if ( is_array( $aIncludedObjectsIds ) && count( $aIncludedObjectsIds ) > 0 ) {
00124             $sQuerySnipet = " AND ( `oxobjectid` = ".$oDb->quote( $sObjectId ) . " OR `oxobjectid` in ('" .implode("', '", $aIncludedObjectsIds). "') )";
00125         } else {
00126             $sQuerySnipet = " AND `oxobjectid` = ".$oDb->quote( $sObjectId );
00127         }
00128 
00129         $sSelect = "
00130             SELECT
00131                 COUNT(*)
00132             FROM `oxreviews`
00133             WHERE `oxrating` > 0
00134                 AND `oxtype` = " . $oDb->quote( $sType )
00135                . $sQuerySnipet . "
00136             LIMIT 1";
00137 
00138         $iCount = 0;
00139         $iCount = $oDb->getOne( $sSelect, false, false );
00140 
00141         return $iCount;
00142     }
00143 
00144 }