BxDolVotingQuery.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 BxDolVoting
  11. */
  12. class BxDolVotingQuery extends BxDolDb
  13. {
  14. var $_aSystem; ///< current voting system
  15. function BxDolVotingQuery(&$aSystem)
  16. {
  17. $this->_aSystem = &$aSystem;
  18. parent::BxDolDb();
  19. }
  20. function getVote ($iId)
  21. {
  22. $sPre = $this->_aSystem['row_prefix'];
  23. $sTable = $this->_aSystem['table_rating'];
  24. $sQuery = $this->prepare("SELECT `{$sPre}rating_count` as `count`, (`{$sPre}rating_sum` / `{$sPre}rating_count`) AS `rate` FROM {$sTable} WHERE `{$sPre}id` = ? LIMIT 1", $iId);
  25. return $this->getRow($sQuery);
  26. }
  27. function putVote ($iId, $sIp, $iRate)
  28. {
  29. $sPre = $this->_aSystem['row_prefix'];
  30. $sTable = $this->_aSystem['table_rating'];
  31. $sQuery = $this->prepare("SELECT `{$sPre}id` FROM $sTable WHERE `{$sPre}id` = ? LIMIT 1", $iId);
  32. if ($this->getOne($sQuery))
  33. {
  34. $sQuery = $this->prepare("UPDATE {$sTable} SET `{$sPre}rating_count` = `{$sPre}rating_count` + 1, `{$sPre}rating_sum` = `{$sPre}rating_sum` + ? WHERE `{$sPre}id` = ?", $iRate, $iId);
  35. $ret = $this->query ($sQuery);
  36. }
  37. else
  38. {
  39. $sQuery = $this->prepare("INSERT INTO {$sTable} SET `{$sPre}id` = ?, `{$sPre}rating_count` = '1', `{$sPre}rating_sum` = ?", $iId, $iRate);
  40. $ret = $this->query ($sQuery);
  41. }
  42. if (!$ret) return $ret;
  43. $sTable = $this->_aSystem['table_track'];
  44. $sQuery = $this->prepare("INSERT INTO {$sTable} SET `{$sPre}id` = ?, `{$sPre}ip` = ?, `{$sPre}date` = NOW()", $iId, $sIp);
  45. return $this->query ($sQuery);
  46. }
  47. function isDublicateVote ($iId, $sIp)
  48. {
  49. $sPre = $this->_aSystem['row_prefix'];
  50. $sTable = $this->_aSystem['table_track'];
  51. $iSec = $this->_aSystem['is_duplicate'];
  52. $sQuery = $this->prepare("SELECT `{$sPre}id` FROM {$sTable} WHERE `{$sPre}ip` = ? AND `{$sPre}id` = ? AND `{$sPre}date` > FROM_UNIXTIME(UNIX_TIMESTAMP() - ?)", $sIp, $iId, $iSec);
  53. return $this->getOne ($sQuery);
  54. }
  55. function getSqlParts ($sMailTable, $sMailField)
  56. {
  57. if ($sMailTable)
  58. $sMailTable .= '.';
  59. if ($sMailField)
  60. $sMailField = $sMailTable.$sMailField;
  61. $sPre = $this->_aSystem['row_prefix'];
  62. $sTable = $this->_aSystem['table_rating'];
  63. return array (
  64. 'fields' => ",$sTable.`{$sPre}rating_count` as `voting_count`, ($sTable.`{$sPre}rating_sum` / $sTable.`{$sPre}rating_count`) AS `voting_rate` ",
  65. 'join' => $this->prepare(" LEFT JOIN $sTable ON ({$sTable}.`{$sPre}id` = ?) ", $sMailField),
  66. );
  67. }
  68. function deleteVotings ($iId)
  69. {
  70. $sPre = $this->_aSystem['row_prefix'];
  71. $sTable = $this->_aSystem['table_track'];
  72. $sQuery = $this->prepare("DELETE FROM {$sTable} WHERE `{$sPre}id` = ?", $iId);
  73. $this->query ($sQuery);
  74. $sTable = $this->_aSystem['table_rating'];
  75. $sQuery = $this->prepare("DELETE FROM {$sTable} WHERE `{$sPre}id` = ?", $iId);
  76. return $this->query ($sQuery);
  77. }
  78. function getTopVotedItem ($iDays, $sJoinTable = '', $sJoinField = '', $sWhere = '')
  79. {
  80. $sPre = $this->_aSystem['row_prefix'];
  81. $sTable = $this->_aSystem['table_track'];
  82. $sJoin = $sJoinTable && $sJoinField ? " INNER JOIN $sJoinTable ON ({$sJoinTable}.{$sJoinField} = $sTable.`{$sPre}id`) " : '';
  83. $sQuery = $this->prepare("SELECT $sTable.`{$sPre}id`, COUNT($sTable.`{$sPre}id`) AS `voting_count` FROM {$sTable} $sJoin WHERE $sTable.`{$sPre}date` >= DATE_SUB(NOW(), INTERVAL ? DAY) $sWhere GROUP BY $sTable.`{$sPre}id` HAVING `voting_count` > 2 ORDER BY `voting_count` DESC LIMIT 1", $iDays);
  84. return $this->getOne ($sQuery);
  85. }
  86. function getVotedItems ($sIp)
  87. {
  88. $sPre = $this->_aSystem['row_prefix'];
  89. $sTable = $this->_aSystem['table_track'];
  90. $iSec = $this->_aSystem['is_duplicate'];
  91. $sQuery = $this->prepare("SELECT `{$sPre}id` FROM {$sTable} WHERE `{$sPre}ip` = ? AND `{$sPre}date` > FROM_UNIXTIME(UNIX_TIMESTAMP() - ?) ORDER BY `{$sPre}date` DESC", $sIp, $iSec);
  92. return $this->getAll ($sQuery);
  93. }
  94. function updateTriggerTable($iId, $fRate, $iCount) {
  95. $sQuery = $this->prepare("UPDATE `{$this->_aSystem['trigger_table']}` SET `{$this->_aSystem['trigger_field_rate']}` = ?, `{$this->_aSystem['trigger_field_rate_count']}` = ? WHERE `{$this->_aSystem['trigger_field_id']}` = ?", $fRate, $iCount, $iId);
  96. return $this->query($sQuery);
  97. }
  98. }