1
0

BxDolVoteQuery.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * @see BxDolVote
  11. */
  12. class BxDolVoteQuery extends BxDolObjectQuery
  13. {
  14. protected $_sTriggerFieldRate;
  15. protected $_iPostTimeout;
  16. public function __construct(&$oModule)
  17. {
  18. parent::__construct($oModule);
  19. $aSystem = $this->_oModule->getSystemInfo();
  20. $this->_sTriggerFieldRate = $aSystem['trigger_field_rate'];
  21. $this->_iPostTimeout = (int)$aSystem['post_timeout'];
  22. $this->_sMethodGetEntry = 'getVote';
  23. }
  24. public function isPostTimeoutEnded($iObjectId, $sAuthorIp)
  25. {
  26. if($this->_iPostTimeout == 0)
  27. return true;
  28. $iDate = time() - $this->_iPostTimeout;
  29. $iAuthorNip = ip2long($sAuthorIp);
  30. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_nip` = ? AND `date` > ?", $iObjectId, $iAuthorNip, $iDate);
  31. return (int)$this->getOne($sQuery) == 0;
  32. }
  33. public function getVote($iObjectId)
  34. {
  35. $sQuery = $this->prepare("SELECT `count` as `count`, `sum` as `sum`, ROUND(`sum` / `count`, 2) AS `rate` FROM {$this->_sTable} WHERE `object_id` = ? LIMIT 1", $iObjectId);
  36. $aResult = $this->getRow($sQuery);
  37. if(empty($aResult) || !is_array($aResult))
  38. $aResult = array('count' => 0, 'sum' => 0, 'rate' => 0);
  39. return $aResult;
  40. }
  41. public function putVote($iObjectId, $iAuthorId, $sAuthorIp, $iValue, $bUndo = false)
  42. {
  43. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  44. $bExists = (int)$this->getOne($sQuery) != 0;
  45. if(!$bExists && $bUndo)
  46. return false;
  47. if(!$bExists)
  48. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET `object_id` = ?, `count` = '1', `sum` = ?", $iObjectId, $iValue);
  49. else
  50. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` " . ($bUndo ? "-" : "+") . " 1, `sum` = `sum` " . ($bUndo ? "-" : "+") . " ? WHERE `object_id` = ?", $iValue, $iObjectId);
  51. if((int)$this->query($sQuery) == 0)
  52. return false;
  53. if($bUndo) {
  54. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  55. $iId = (int)$this->getOne($sQuery);
  56. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `id` = ? LIMIT 1", $iId);
  57. if((int)$this->query($sQuery) > 0)
  58. return $iId;
  59. }
  60. else {
  61. $iNow = time();
  62. $iAuthorNip = ip2long($sAuthorIp);
  63. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `author_nip` = ?, `value` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $iValue, $iNow);
  64. if((int)$this->query($sQuery) > 0)
  65. return $this->lastId();
  66. }
  67. return false;
  68. }
  69. public function getLegend($iObjectId)
  70. {
  71. $sQuery = $this->prepare("SELECT `value` AS `value`, COUNT(`value`) AS `count` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? GROUP BY `value`", $iObjectId);
  72. return $this->getAllWithKey($sQuery, 'value');
  73. }
  74. public function getSqlParts($sMainTable, $sMainField)
  75. {
  76. $aResult = parent::getSqlParts($sMainTable, $sMainField);
  77. if(empty($aResult))
  78. return $aResult;
  79. $aResult['fields'] = ",`{$this->_sTable}`.`count` as `vote_count`, (`{$this->_sTable}`.`sum` / `{$this->_sTable}`.`count`) AS `vote_rate` ";
  80. return $aResult;
  81. }
  82. protected function _updateTriggerTable($iObjectId, $aEntry)
  83. {
  84. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ?, `{$this->_sTriggerFieldRate}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $aEntry['rate'], $iObjectId);
  85. return (int)$this->query($sQuery) > 0;
  86. }
  87. }
  88. /** @} */