Cheetah
ChWsbVoting.php
Go to the documentation of this file.
1 <?php
2 
8 require_once( CH_DIRECTORY_PATH_CLASSES . 'ChWsbVotingQuery.php' );
9 
10 define( 'CH_PERIOD_PER_VOTE', 7 * 86400 );
11 define( 'CH_OLD_VOTES', 365*86400 ); // votes older than this number of seconds will be deleted automatically
12 
90 {
91  var $_iId = 0; // item id to be rated
92  var $_iCount = 0; // number of votes
93  var $_fRate = 0; // average rate
94  var $_sSystem = 'profile'; // current rating system name
95 
96  var $_aSystem = array (); // current rating system array
97 
98  var $_oQuery = null;
99 
103  function __construct( $sSystem, $iId, $iInit = 1)
104  {
105  $this->_aSystems =& $this->getSystems();
106 
107  $this->_sSystem = $sSystem;
108  if (isset($this->_aSystems[$sSystem]))
109  $this->_aSystem = $this->_aSystems[$sSystem];
110  else
111  return;
112 
113  $this->_oQuery = new ChWsbVotingQuery($this->_aSystem);
114 
115  if ($iInit)
116  $this->init($iId);
117  }
118 
126  static function getObjectInstance($sSys, $iId, $iInit = true)
127  {
129  if (!isset($aSystems[$sSys]))
130  return null;
131 
132  ch_import('ChTemplVotingView');
133  $sClassName = 'ChTemplVotingView';
134  if ($aSystems[$sSys]['override_class_name']) {
135  require_once (CH_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['override_class_file']);
136  $sClassName = $aSystems[$sSys]['override_class_name'];
137  }
138 
139  return new $sClassName($sSys, $iId, $iInit);
140  }
141 
142  public static function & getSystems ()
143  {
144  if (isset($GLOBALS['ch_dol_voting_systems'])) {
145  return $GLOBALS['ch_dol_voting_systems'];
146  }
147 
148  $oCache = $GLOBALS['MySQL']->getDbCacheObject();
149 
150  $GLOBALS['ch_dol_voting_systems'] = $oCache->getData($GLOBALS['MySQL']->genDbCacheKey('sys_objects_vote'));
151 
152  if (null === $GLOBALS['ch_dol_voting_systems']) {
153 
154  // cache is empty | load data from DB
155 
156  $sQuery = "SELECT * FROM `sys_objects_vote`";
157  $rResult = db_res($sQuery);
158 
159  $GLOBALS['ch_dol_voting_systems'] = array();
160  while( $aRow = $rResult ->fetch() ) {
161  $GLOBALS['ch_dol_voting_systems'][$aRow['ObjectName']] = array
162  (
163  'table_rating' => $aRow['TableRating'],
164  'table_track' => $aRow['TableTrack'],
165  'row_prefix' => $aRow['RowPrefix'],
166  'max_votes' => $aRow['MaxVotes'],
167  'post_name' => $aRow['PostName'],
168  'is_duplicate' => is_int($aRow['IsDuplicate']) ? $aRow['IsDuplicate'] : constant($aRow['IsDuplicate']),
169  'is_on' => $aRow['IsOn'],
170 
171  'className' => $aRow['className'],
172  'classFile' => $aRow['classFile'],
173 
174  'trigger_table' => $aRow['TriggerTable'], // table with field to update on every rating change
175  'trigger_field_rate' => $aRow['TriggerFieldRate'], // table field name with rating
176  'trigger_field_rate_count' => $aRow['TriggerFieldRateCount'], // table field name with rating count
177  'trigger_field_id' => $aRow['TriggerFieldId'], // table field name with object id
178 
179  'override_class_name' => $aRow['OverrideClassName'], // new class to override
180  'override_class_file' => $aRow['OverrideClassFile'], // class file path
181  );
182  }
183 
184  // write data into cache file
185 
186  $oCache = $GLOBALS['MySQL']->getDbCacheObject();
187  $oCache->setData ($GLOBALS['MySQL']->genDbCacheKey('sys_objects_vote'), $GLOBALS['ch_dol_voting_systems']);
188  }
189 
190  return $GLOBALS['ch_dol_voting_systems'];
191  }
192 
193  function init ($iId)
194  {
195  if(!$iId)
196  $iId = $this->_iId;
197 
198  if(!$this->isEnabled())
199  return;
200 
201  if (!$this->_iId && $iId)
202  $this->setId($iId);
203  }
204 
205  function initVotes ()
206  {
207  if(!$this->isEnabled() || !$this->_oQuery)
208  return;
209 
210  $aVote = $this->_oQuery->getVote ($this->getId());
211  if(empty($aVote) || !is_array($aVote))
212  return;
213 
214  $this->_iCount = $aVote['count'];
215  $this->_fRate = $aVote['rate'];
216  }
217 
218  function makeVote ($iVote)
219  {
220  if(!$this->isEnabled() || $this->isDublicateVote() || !$this->checkAction())
221  return false;
222 
223  if($this->_sSystem == 'profile' && $this->getId() == getLoggedId())
224  return false;
225 
226  $sVoterIdentification = isLogged() ? getLoggedId() : getVisitorIP();
227  if(!$this->_oQuery->putVote ($this->getId(), $sVoterIdentification, $iVote))
228  return false;
229 
230  $this->checkAction(true);
231  $this->_triggerVote();
232 
233  $oZ = new ChWsbAlerts($this->_sSystem, 'rate', $this->getId(), getLoggedId(), array ('rate' => $iVote));
234  $oZ->alert();
235 
236  return true;
237  }
238 
239  function checkAction ($bPerformAction = false)
240  {
241  if (isset($this->_checkActionResult))
242  return $this->_checkActionResult;
243 
244  $iId = getLoggedId();
245  $aResult = checkAction($iId, ACTION_ID_VOTE, $bPerformAction);
246  return ($this->_checkActionResult = ($aResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED));
247  }
248 
249  function isDublicateVote ()
250  {
251  if (!$this->isEnabled()) return false;
252 
253  $sVoterIdentification = isLogged() ? getLoggedId() : getVisitorIP();
254  return $this->_oQuery->isDublicateVote ($this->getId(), $sVoterIdentification);
255  }
256 
257  function getId ()
258  {
259  return $this->_iId;
260  }
261 
262  function isEnabled ()
263  {
264  return $this->_aSystem['is_on'];
265  }
266 
267  function getMaxVote()
268  {
269  return $this->_aSystem['max_votes'];
270  }
271 
272  function getVoteCount()
273  {
274  return $this->_iCount;
275  }
276 
277  function getVoteRate()
278  {
279  return $this->_fRate;
280  }
281 
282  function getSystemName()
283  {
284  return $this->_sSystem;
285  }
286 
290  function setId ($iId)
291  {
292  if ($iId == $this->getId()) return;
293  $this->_iId = $iId;
294  $this->initVotes();
295  }
296 
297  function getSqlParts ($sMailTable, $sMailField)
298  {
299  if ($this->isEnabled())
300  return $this->_oQuery->getSqlParts ($sMailTable, $sMailField);
301  else
302  return array();
303  }
304 
305  function isValidSystem ($sSystem)
306  {
307  return isset($this->_aSystems[$sSystem]);
308  }
309 
310  function deleteVotings ($iId)
311  {
312  if (!(int)$iId) return false;
313  $this->_oQuery->deleteVotings ($iId);
314  return true;
315  }
316 
317  function getTopVotedItem ($iDays, $sJoinTable = '', $sJoinField = '', $sWhere = '')
318  {
319  return $this->_oQuery->getTopVotedItem ($iDays, $sJoinTable, $sJoinField, $sWhere);
320  }
321 
322  function getVotedItems ($sIp)
323  {
324  return $this->_oQuery->getVotedItems ($sIp);
325  }
326 
330  function maintenance ()
331  {
332  $iDeletedRecords = 0;
333  foreach ($this->_aSystems as $aSystem) {
334  if (!$aSystem['is_on'])
335  continue;
336  $sPre = $aSystem['row_prefix'];
337  $iDeletedRecords += $GLOBALS['MySQL']->query("DELETE FROM `{$aSystem['table_track']}` WHERE `{$sPre}date` < DATE_SUB(NOW(), INTERVAL " . CH_OLD_VOTES . " SECOND)");
338  $GLOBALS['MySQL']->query("OPTIMIZE TABLE `{$aSystem['table_track']}`");
339  }
340  return $iDeletedRecords;
341  }
342 
343  public function actionVote()
344  {
345  if(!$this->isEnabled())
346  return '{}';
347 
348  $iResult = $this->_getVoteResult();
349  if($iResult === false)
350  return '{}';
351 
352  if(!$this->makeVote($iResult))
353  return '{}';
354 
355  $this->initVotes();
356  echo json_encode(array('rate' => $this->getVoteRate(), 'count' => $this->getVoteCount()));
357  }
358 
359  protected function _getVoteResult ()
360  {
361  if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') !== 0 || ch_get($this->_aSystem['post_name']) === false)
362  return false;
363 
364  $iVote = (int)ch_get($this->_aSystem['post_name']);
365  if(!$iVote)
366  return false;
367 
368  if($iVote > $this->getMaxVote())
369  $iVote = $this->getMaxVote();
370 
371  if($iVote < 1)
372  $iVote = 1;
373 
374  return $iVote;
375  }
376 
377  protected function _triggerVote()
378  {
379  if (!$this->_aSystem['trigger_table'])
380  return false;
381  $iId = $this->getId();
382  if (!$iId)
383  return false;
384  $this->initVotes ();
385  $iCount = $this->getVoteCount();
386  $fRate = $this->getVoteRate();
387  return $this->_oQuery->updateTriggerTable($iId, $fRate, $iCount);
388  }
389 }
ChWsbVoting\getVoteRate
getVoteRate()
Definition: ChWsbVoting.php:277
getVisitorIP
getVisitorIP($isProxyCheck=true)
Definition: utils.inc.php:643
ChWsbVoting\__construct
__construct( $sSystem, $iId, $iInit=1)
Definition: ChWsbVoting.php:103
ChWsbVoting\$_iCount
$_iCount
Definition: ChWsbVoting.php:92
$aSystems
$aSystems
Definition: cmts.php:20
CHECK_ACTION_RESULT_ALLOWED
const CHECK_ACTION_RESULT_ALLOWED
Definition: membership_levels.inc.php:60
$sSys
$sSys
Definition: cmts.php:15
ChWsbVoting\getObjectInstance
static getObjectInstance($sSys, $iId, $iInit=true)
Definition: ChWsbVoting.php:126
ChWsbVoting\getSystemName
getSystemName()
Definition: ChWsbVoting.php:282
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
$aResult
$aResult
Definition: index.php:19
ch_get
ch_get($sName)
Definition: utils.inc.php:1664
CHECK_ACTION_RESULT
const CHECK_ACTION_RESULT
Definition: membership_levels.inc.php:54
php
ChWsbVoting\actionVote
actionVote()
Definition: ChWsbVoting.php:343
$oZ
$oZ
Definition: db.php:20
ChWsbVoting
Definition: ChWsbVoting.php:90
$iId
$iId
Definition: license.php:15
isLogged
isLogged()
Definition: profiles.inc.php:24
ChWsbVoting\getTopVotedItem
getTopVotedItem($iDays, $sJoinTable='', $sJoinField='', $sWhere='')
Definition: ChWsbVoting.php:317
$oCache
$oCache
Definition: prof.inc.php:10
ChWsbVoting\getSystems
static & getSystems()
Definition: ChWsbVoting.php:142
ChWsbAlerts
Definition: ChWsbAlerts.php:39
getLoggedId
getLoggedId()
Definition: profiles.inc.php:32
ChWsbVoting\getSqlParts
getSqlParts($sMailTable, $sMailField)
Definition: ChWsbVoting.php:297
CH_OLD_VOTES
const CH_OLD_VOTES
Definition: ChWsbVoting.php:11
ChWsbVoting\checkAction
checkAction($bPerformAction=false)
Definition: ChWsbVoting.php:239
ChWsbVoting\getVotedItems
getVotedItems($sIp)
Definition: ChWsbVoting.php:322
ChWsbVoting\getId
getId()
Definition: ChWsbVoting.php:257
ChWsbVoting\initVotes
initVotes()
Definition: ChWsbVoting.php:205
ChWsbVoting\$_iId
$_iId
Definition: ChWsbVoting.php:91
ACTION_ID_VOTE
const ACTION_ID_VOTE
Definition: membership_levels.inc.php:37
ChWsbVoting\getMaxVote
getMaxVote()
Definition: ChWsbVoting.php:267
ChWsbVoting\deleteVotings
deleteVotings($iId)
Definition: ChWsbVoting.php:310
ChWsbVoting\$_sSystem
$_sSystem
Definition: ChWsbVoting.php:94
ChWsbVoting\init
init($iId)
Definition: ChWsbVoting.php:193
ChWsbVoting\$_oQuery
$_oQuery
Definition: ChWsbVoting.php:98
ChWsbVoting\getVoteCount
getVoteCount()
Definition: ChWsbVoting.php:272
ChWsbVoting\$_aSystem
$_aSystem
Definition: ChWsbVoting.php:96
ChWsbVoting\setId
setId($iId)
Definition: ChWsbVoting.php:290
ChWsbVoting\_getVoteResult
_getVoteResult()
Definition: ChWsbVoting.php:359
ChWsbVoting\$_fRate
$_fRate
Definition: ChWsbVoting.php:93
db_res
db_res($query, $bindings=[])
Definition: db.inc.php:39
ChWsbVotingQuery
Definition: ChWsbVotingQuery.php:14
ChWsbVoting\isDublicateVote
isDublicateVote()
Definition: ChWsbVoting.php:249
ChWsbVoting\isEnabled
isEnabled()
Definition: ChWsbVoting.php:262
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
ChWsbVoting\_triggerVote
_triggerVote()
Definition: ChWsbVoting.php:377
ChWsbVoting\makeVote
makeVote($iVote)
Definition: ChWsbVoting.php:218
ChWsbVoting\isValidSystem
isValidSystem($sSystem)
Definition: ChWsbVoting.php:305
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbVoting\maintenance
maintenance()
Definition: ChWsbVoting.php:330
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10