BxDolVoteQuery.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 $_iPostTimeout;
  15. public function __construct(&$oModule)
  16. {
  17. parent::__construct($oModule);
  18. $aSystem = $this->_oModule->getSystemInfo();
  19. $this->_sTriggerFieldRate = $aSystem['trigger_field_rate'];
  20. $this->_iPostTimeout = (int)$aSystem['post_timeout'];
  21. $this->_sMethodGetEntry = 'getVote';
  22. }
  23. public function isPostTimeoutEnded($iObjectId, $sAuthorIp)
  24. {
  25. if($this->_iPostTimeout == 0)
  26. return true;
  27. $iDate = time() - $this->_iPostTimeout;
  28. $iAuthorNip = ip2long($sAuthorIp);
  29. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_nip` = ? AND `date` > ?", $iObjectId, $iAuthorNip, $iDate);
  30. return (int)$this->getOne($sQuery) == 0;
  31. }
  32. public function getVote($iObjectId)
  33. {
  34. $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);
  35. $aResult = $this->getRow($sQuery);
  36. if(empty($aResult) || !is_array($aResult))
  37. $aResult = array('count' => 0, 'sum' => 0, 'rate' => 0);
  38. return $aResult;
  39. }
  40. public function putVote($iObjectId, $iAuthorId, $sAuthorIp, $iValue, $bUndo = false)
  41. {
  42. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  43. $bExists = (int)$this->getOne($sQuery) != 0;
  44. if(!$bExists && $bUndo)
  45. return false;
  46. if(!$bExists)
  47. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET `object_id` = ?, `count` = '1', `sum` = ?", $iObjectId, $iValue);
  48. else
  49. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` " . ($bUndo ? "-" : "+") . " 1, `sum` = `sum` " . ($bUndo ? "-" : "+") . " ? WHERE `object_id` = ?", $iValue, $iObjectId);
  50. if((int)$this->query($sQuery) == 0)
  51. return false;
  52. if($bUndo) {
  53. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  54. $iId = (int)$this->getOne($sQuery);
  55. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `id` = ? LIMIT 1", $iId);
  56. if((int)$this->query($sQuery) > 0)
  57. return $iId;
  58. }
  59. else {
  60. $iNow = time();
  61. $iAuthorNip = ip2long($sAuthorIp);
  62. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `author_nip` = ?, `value` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $iValue, $iNow);
  63. if((int)$this->query($sQuery) > 0)
  64. return $this->lastId();
  65. }
  66. return false;
  67. }
  68. public function getLegend($iObjectId)
  69. {
  70. $sQuery = $this->prepare("SELECT `value` AS `value`, COUNT(`value`) AS `count` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? GROUP BY `value`", $iObjectId);
  71. return $this->getAllWithKey($sQuery, 'value');
  72. }
  73. public function getSqlParts($sMainTable, $sMainField)
  74. {
  75. $aResult = parent::getSqlParts($sMainTable, $sMainField);
  76. if(empty($aResult))
  77. return $aResult;
  78. $aResult['fields'] = ",`{$this->_sTable}`.`count` as `vote_count`, (`{$this->_sTable}`.`sum` / `{$this->_sTable}`.`count`) AS `vote_rate` ";
  79. return $aResult;
  80. }
  81. }
  82. /** @} */