Cheetah
ChWsbVotingQuery.php
Go to the documentation of this file.
1 <?php
2 
8 require_once( CH_DIRECTORY_PATH_CLASSES . 'ChWsbDb.php' );
9 
14 {
15  var $_aSystem; // current voting system
16 
17  function __construct(&$aSystem)
18  {
19  $this->_aSystem = &$aSystem;
20  parent::__construct();
21  }
22 
23  function getVote ($iId)
24  {
25  $sPre = $this->_aSystem['row_prefix'];
26  $sTable = $this->_aSystem['table_rating'];
27 
28  return $this->getRow("SELECT `{$sPre}rating_count` as `count`, (`{$sPre}rating_sum` / `{$sPre}rating_count`) AS `rate` FROM {$sTable} WHERE `{$sPre}id` = ? LIMIT 1", [$iId]);
29  }
30 
31  function putVote ($iId, $sIp, $iRate)
32  {
33  $sPre = $this->_aSystem['row_prefix'];
34 
35  $sTable = $this->_aSystem['table_rating'];
36 
37  if ($this->getOne("SELECT `{$sPre}id` FROM $sTable WHERE `{$sPre}id` = '$iId' LIMIT 1")) {
38  $ret = $this->query ("UPDATE {$sTable} SET `{$sPre}rating_count` = `{$sPre}rating_count` + 1, `{$sPre}rating_sum` = `{$sPre}rating_sum` + '$iRate' WHERE `{$sPre}id` = '$iId'");
39  } else {
40  $ret = $this->query ("INSERT INTO {$sTable} SET `{$sPre}id` = '$iId', `{$sPre}rating_count` = '1', `{$sPre}rating_sum` = '$iRate'");
41 
42  }
43  if (!$ret) return $ret;
44 
45  $sTable = $this->_aSystem['table_track'];
46  return $this->query ("INSERT INTO {$sTable} SET `{$sPre}id` = '$iId', `{$sPre}ip` = '$sIp', `{$sPre}date` = NOW()");
47  }
48 
49  function isDublicateVote ($iId, $sIp)
50  {
51  $sPre = $this->_aSystem['row_prefix'];
52  $sTable = $this->_aSystem['table_track'];
53  $iSec = $this->_aSystem['is_duplicate'];
54 
55  return $this->getOne ("SELECT `{$sPre}id` FROM {$sTable} WHERE `{$sPre}ip` = '$sIp' AND `{$sPre}id` = '$iId' AND UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`{$sPre}date`) < $iSec");
56 
57  }
58 
59  function getSqlParts ($sMailTable, $sMailField)
60  {
61  if ($sMailTable)
62  $sMailTable .= '.';
63 
64  if ($sMailField)
65  $sMailField = $sMailTable.$sMailField;
66 
67  $sPre = $this->_aSystem['row_prefix'];
68  $sTable = $this->_aSystem['table_rating'];
69 
70  return array (
71  'fields' => ",$sTable.`{$sPre}rating_count` as `voting_count`, ($sTable.`{$sPre}rating_sum` / $sTable.`{$sPre}rating_count`) AS `voting_rate` ",
72  //'fields' => ",34 as `voting_count`, 2.5 AS `voting_rate` ",
73  'join' => " LEFT JOIN $sTable ON ({$sTable}.`{$sPre}id` = $sMailField) "
74  );
75  }
76 
77  function deleteVotings ($iId)
78  {
79  $sPre = $this->_aSystem['row_prefix'];
80 
81  $sTable = $this->_aSystem['table_track'];
82  $this->query ("DELETE FROM {$sTable} WHERE `{$sPre}id` = '$iId'");
83 
84  $sTable = $this->_aSystem['table_rating'];
85  return $this->query ("DELETE FROM {$sTable} WHERE `{$sPre}id` = '$iId'");
86  }
87 
88  function getTopVotedItem ($iDays, $sJoinTable = '', $sJoinField = '', $sWhere = '')
89  {
90  $sPre = $this->_aSystem['row_prefix'];
91  $sTable = $this->_aSystem['table_track'];
92 
93  $sJoin = $sJoinTable && $sJoinField ? " INNER JOIN $sJoinTable ON ({$sJoinTable}.{$sJoinField} = $sTable.`{$sPre}id`) " : '';
94 
95  return $this->getOne ("SELECT $sTable.`{$sPre}id`, COUNT($sTable.`{$sPre}id`) AS `voting_count` FROM {$sTable} $sJoin WHERE TO_DAYS(NOW()) - TO_DAYS($sTable.`{$sPre}date`) <= $iDays $sWhere GROUP BY $sTable.`{$sPre}id` HAVING `voting_count` > 2 ORDER BY `voting_count` DESC LIMIT 1");
96  }
97 
98  function getVotedItems ($sIp)
99  {
100  $sPre = $this->_aSystem['row_prefix'];
101  $sTable = $this->_aSystem['table_track'];
102  $iSec = $this->_aSystem['is_duplicate'];
103  return $this->getAll ("SELECT `{$sPre}id` FROM {$sTable} WHERE `{$sPre}ip` = '$sIp' AND UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`{$sPre}date`) < $iSec ORDER BY `{$sPre}date` DESC");
104  }
105 
106  function updateTriggerTable($iId, $fRate, $iCount)
107  {
108  return $this->query("UPDATE `{$this->_aSystem['trigger_table']}` SET `{$this->_aSystem['trigger_field_rate']}` = '$fRate', `{$this->_aSystem['trigger_field_rate_count']}` = '$iCount' WHERE `{$this->_aSystem['trigger_field_id']}` = '$iId'");
109  }
110 }
ChWsbVotingQuery\getVotedItems
getVotedItems($sIp)
Definition: ChWsbVotingQuery.php:98
ChWsbVotingQuery\$_aSystem
$_aSystem
Definition: ChWsbVotingQuery.php:15
ChWsbVotingQuery\getTopVotedItem
getTopVotedItem($iDays, $sJoinTable='', $sJoinField='', $sWhere='')
Definition: ChWsbVotingQuery.php:88
$ret
$ret
Definition: index.php:39
php
$iId
$iId
Definition: license.php:15
ChWsbDb\getAll
getAll($sQuery, $aBindings=[], $iFetchType=PDO::FETCH_ASSOC)
Definition: ChWsbDb.php:206
ChWsbVotingQuery\putVote
putVote($iId, $sIp, $iRate)
Definition: ChWsbVotingQuery.php:31
ChWsbDb\getRow
getRow($sQuery, $aBindings=[], $iFetchStyle=PDO::FETCH_ASSOC)
Definition: ChWsbDb.php:225
ChWsbDb\query
query($sQuery, $aBindings=[])
Definition: ChWsbDb.php:386
ChWsbVotingQuery\isDublicateVote
isDublicateVote($iId, $sIp)
Definition: ChWsbVotingQuery.php:49
ChWsbDb\getOne
getOne($sQuery, $aBindings=[], $iIndex=0)
Definition: ChWsbDb.php:263
ChWsbVotingQuery\getSqlParts
getSqlParts($sMailTable, $sMailField)
Definition: ChWsbVotingQuery.php:59
ChWsbVotingQuery\updateTriggerTable
updateTriggerTable($iId, $fRate, $iCount)
Definition: ChWsbVotingQuery.php:106
ChWsbVotingQuery
Definition: ChWsbVotingQuery.php:14
ChWsbDb
Definition: ChWsbDb.php:13
ChWsbVotingQuery\getVote
getVote($iId)
Definition: ChWsbVotingQuery.php:23
ChWsbVotingQuery\deleteVotings
deleteVotings($iId)
Definition: ChWsbVotingQuery.php:77
ChWsbVotingQuery\__construct
__construct(&$aSystem)
Definition: ChWsbVotingQuery.php:17