BxDolVoteQuery.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDolDb');
  9. /**
  10. * @see BxDolVote
  11. */
  12. class BxDolVoteQuery extends BxDolDb
  13. {
  14. protected $_oModule;
  15. protected $_sTable;
  16. protected $_sTableTrack;
  17. protected $_sTriggerTable;
  18. protected $_sTriggerFieldId;
  19. protected $_sTriggerFieldRate;
  20. protected $_sTriggerFieldCount;
  21. protected $_iPostTimeout;
  22. public function __construct(&$oModule)
  23. {
  24. parent::BxDolDb();
  25. $this->_oModule = $oModule;
  26. $aSystem = $this->_oModule->getSystemInfo();
  27. $this->_sTable = $aSystem['table_main'];
  28. $this->_sTableTrack = $aSystem['table_track'];
  29. $this->_sTriggerTable = $aSystem['trigger_table'];
  30. $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
  31. $this->_sTriggerFieldRate = $aSystem['trigger_field_rate'];
  32. $this->_sTriggerFieldCount = $aSystem['trigger_field_count'];
  33. $this->_iPostTimeout = (int)$aSystem['post_timeout'];
  34. }
  35. public function isPostTimeoutEnded($iObjectId, $sAuthorIp)
  36. {
  37. if($this->_iPostTimeout == 0)
  38. return true;
  39. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_nip` = ? AND `date` > (UNIX_TIMESTAMP() - ?)", $iObjectId, ip2long($sAuthorIp), $this->_iPostTimeout);
  40. return (int)$this->getOne($sQuery) == 0;
  41. }
  42. public function getVote($iObjectId)
  43. {
  44. $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);
  45. $aResult = $this->getRow($sQuery);
  46. if(empty($aResult) || !is_array($aResult))
  47. $aResult = array('count' => 0, 'sum' => 0, 'rate' => 0);
  48. return $aResult;
  49. }
  50. public function isVoted($iObjectId, $iAuthorId, $sAuthorIp)
  51. {
  52. $iAuthorNip = ip2long($sAuthorIp);
  53. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_nip` = ? LIMIT 1", $iObjectId, $iAuthorNip);
  54. return (int)$this->getOne($sQuery) != 0;
  55. }
  56. public function putVote($iObjectId, $iAuthorId, $sAuthorIp, $iValue, $bUndo = false)
  57. {
  58. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  59. $bExists = (int)$this->getOne($sQuery) != 0;
  60. if(!$bExists && $bUndo)
  61. return false;
  62. if(!$bExists)
  63. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET `object_id` = ?, `count` = '1', `sum` = ?", $iObjectId, $iValue);
  64. else
  65. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` " . ($bUndo ? "-" : "+") . " 1, `sum` = `sum` " . ($bUndo ? "-" : "+") . " ? WHERE `object_id` = ?", $iValue, $iObjectId);
  66. if((int)$this->query($sQuery) == 0)
  67. return false;
  68. $iAuthorNip = ip2long($sAuthorIp);
  69. if($bUndo)
  70. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_nip` = ? LIMIT 1", $iObjectId, $iAuthorNip);
  71. else
  72. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `author_nip` = ?, `value` = ?, `date` = UNIX_TIMESTAMP()", $iObjectId, $iAuthorId, $iAuthorNip, $iValue);
  73. return (int)$this->query($sQuery) > 0;
  74. }
  75. public function updateTriggerTable($iObjectId) {
  76. $aVote = $this->getVote($iObjectId);
  77. if(empty($aVote) || !is_array($aVote))
  78. return false;
  79. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ?, `{$this->_sTriggerFieldRate}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aVote['count'], $aVote['rate'], $iObjectId);
  80. return (int)$this->query($sQuery) > 0;
  81. }
  82. function getSqlParts($sMainTable, $sMainField)
  83. {
  84. if(empty($sMainTable) || empty($sMainField))
  85. return array();
  86. $sPre = $this->_aSystem['row_prefix'];
  87. $sTable = $this->_aSystem['table_rating'];
  88. return array (
  89. 'fields' => ",`{$this->_sTable}`.`count` as `vote_count`, (`{$this->_sTable}`.`sum` / `{$this->_sTable}`.`count`) AS `vote_rate` ",
  90. 'join' => $this->prepare(" LEFT JOIN `{$this->_sTable}` ON (`{$this->_sTable}`.`object_id` = ?) ", "`{$sMainTable}`.`{$sMainField}`"),
  91. );
  92. }
  93. function getVotedBy($iObjectId)
  94. {
  95. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=?", $iObjectId);
  96. return $this->getColumn($sQuery);
  97. }
  98. }