OXID eShop CE  4.9.10
 All Classes Files Functions Variables Pages
oxrating.php
Go to the documentation of this file.
1 <?php
2 
8 class oxRating extends oxBase
9 {
10 
16  protected $_blDisableShopCheck = true;
17 
23  protected $_sClassName = 'oxrating';
24 
28  public function __construct()
29  {
31  $this->init('oxratings');
32  }
33 
43  public function allowRating($sUserId, $sType, $sObjectId)
44  {
45  $oDb = oxDb::getDb();
46  $myConfig = $this->getConfig();
47 
48  if ($iRatingLogsTimeout = $myConfig->getConfigParam('iRatingLogsTimeout')) {
49  $sExpDate = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() - $iRatingLogsTimeout * 24 * 60 * 60);
50  $oDb->execute("delete from oxratings where oxtimestamp < '$sExpDate'");
51  }
52  $sSelect = "select oxid from oxratings where oxuserid = " . $oDb->quote($sUserId) . " and oxtype=" . $oDb->quote($sType) . " and oxobjectid = " . $oDb->quote($sObjectId);
53  if ($oDb->getOne($sSelect)) {
54  return false;
55  }
56 
57  return true;
58  }
59 
60 
70  public function getRatingAverage($sObjectId, $sType, $aIncludedObjectsIds = null)
71  {
72  $oDb = oxDb::getDb();
73 
74  $sQuerySnipet = '';
75  if (is_array($aIncludedObjectsIds) && count($aIncludedObjectsIds) > 0) {
76  $sQuerySnipet = " AND ( `oxobjectid` = " . $oDb->quote($sObjectId) . " OR `oxobjectid` in ('" . implode("', '", $aIncludedObjectsIds) . "') )";
77  } else {
78  $sQuerySnipet = " AND `oxobjectid` = " . $oDb->quote($sObjectId);
79  }
80 
81  $sSelect = "
82  SELECT
83  AVG(`oxrating`)
84  FROM `oxreviews`
85  WHERE `oxrating` > 0
86  AND `oxtype` = " . $oDb->quote($sType)
87  . $sQuerySnipet . "
88  LIMIT 1";
89 
90  $fRating = 0;
91  if ($fRating = $oDb->getOne($sSelect, false, false)) {
92  $fRating = round($fRating, 1);
93  }
94 
95  return $fRating;
96  }
97 
107  public function getRatingCount($sObjectId, $sType, $aIncludedObjectsIds = null)
108  {
109  $oDb = oxDb::getDb();
110 
111  $sQuerySnipet = '';
112  if (is_array($aIncludedObjectsIds) && count($aIncludedObjectsIds) > 0) {
113  $sQuerySnipet = " AND ( `oxobjectid` = " . $oDb->quote($sObjectId) . " OR `oxobjectid` in ('" . implode("', '", $aIncludedObjectsIds) . "') )";
114  } else {
115  $sQuerySnipet = " AND `oxobjectid` = " . $oDb->quote($sObjectId);
116  }
117 
118  $sSelect = "
119  SELECT
120  COUNT(*)
121  FROM `oxreviews`
122  WHERE `oxrating` > 0
123  AND `oxtype` = " . $oDb->quote($sType)
124  . $sQuerySnipet . "
125  LIMIT 1";
126 
127  $iCount = 0;
128  $iCount = $oDb->getOne($sSelect, false, false);
129 
130  return $iCount;
131  }
132 
138  public function getObjectType()
139  {
140  return $this->oxratings__oxtype->value;
141  }
142 
148  public function getObjectId()
149  {
150  return $this->oxratings__oxobjectid->value;
151  }
152 
153 }