2004-12-12 03:51:51 +00:00
< ? php
2006-06-17 06:10:10 +00:00
require_once ( BASE . " include/util.php " );
2004-12-28 00:01:21 +00:00
/* max votes per user */
define ( 'MAX_VOTES' , 3 );
2004-03-15 16:22:00 +00:00
2007-10-09 21:10:29 +02:00
class vote
{
public $iUserId ;
public $iVoteId ;
public $iSlotIndex ;
public $iVersionId ;
public function vote ( $iVoteId = null , $oRow = null )
{
if ( ! $iVoteId && ! $oRow ) /* Nothing to do */
return ;
if ( ! $oRow )
{
$hResult = query_parameters ( " SELECT * FROM appVotes WHERE id = '?' " , $iVoteId );
$oRow = mysql_fetch_object ( $hResult );
}
if ( $oRow )
{
$this -> iUserId = $oRow -> userId ;
$this -> iVoteId = $oRow -> id ;
$this -> iSlotIndex = $oRow -> slot ;
$this -> iVersionId = $oRow -> versionId ;
}
}
public function update ()
{
/* Check for valid vote slot index */
if ( $this -> iSlotIndex < 1 || $this -> iSlotIndex > MAX_VOTES )
return ;
/* Avoid pointless votes */
if ( ! $this -> iVersionId )
return ;
if ( ! $this -> iVoteId )
{
$hResult = query_parameters ( " INSERT INTO appVotes (versionId,userId,slot) VALUES('?','?','?') " ,
$this -> iVersionId , $_SESSION [ 'current' ] -> iUserId , $this -> iSlotIndex );
} else
{
$hResult = query_parameters ( " UPDATE appVotes SET versionId = '?' WHERE id = '?' " , $this -> iVersionId , $this -> iVoteId );
}
if ( ! $hResult )
return FALSE ;
return TRUE ;
}
2007-10-09 21:15:10 +02:00
public function delete ()
{
/* A vote needs to have a versionId , so if it doesn ' t that means it is not in the
database or it was not selected in the vote editor */
if ( ! $this -> iVersionId )
return TRUE ;
$hResult = query_parameters ( " DELETE FROM appVotes WHERE id = '?' " , $this -> iVoteId );
if ( ! $hResult )
return FALSE ;
return TRUE ;
}
2007-10-09 21:10:29 +02:00
}
class voteManager
{
private $iUserId ;
private $aVotes ;
public function voteManager ( $iUserId = null , $oRow = null )
{
$this -> iUserId = $iUserId ;
}
public function objectGetCustomVars ( $sAction )
{
switch ( $sAction )
{
case " edit " :
return array ( " iVersionId " );
break ;
default :
return null ;
}
}
public function outputEditor ( $aClean = null )
{
2007-10-09 21:15:10 +02:00
echo " The following shows your current votes. Check the boxes next to the apps you wish to replace with a vote for " . version :: fullNameLink ( $aClean [ 'iVersionId' ]) . " or delete. " ;
2007-10-09 21:10:29 +02:00
$oTable = new table ();
$this -> aVotes = $this -> getVotes ();
for ( $i = 0 ; $i < MAX_VOTES ; $i ++ )
{
$sVersionText = $this -> aVotes [ $i ] -> iVersionId ? version :: fullNameLink ( $this -> aVotes [ $i ] -> iVersionId ) : " No app selected " ;
$oTableRow = new tableRow ();
$oTableRow -> addTextCell ( '<input type="checkbox" name="iSlot' . $i . '" value="' . $aClean [ 'iVersionId' ] . '" />' );
$oTableRow -> addTextCell ( $sVersionText );
$oTable -> addRow ( $oTableRow );
}
echo $oTable -> getString ();
}
public function canEdit ()
{
if ( $_SESSION [ 'current' ] -> iUserId == $this -> iUserId )
return TRUE ;
return FALSE ;
}
2007-10-09 21:15:10 +02:00
function objectGetMail ( $sAction , $bMailSubmitter , $bParentAction )
{
return array ( null , null , null ); /* No mail */
}
2007-10-09 21:10:29 +02:00
public function mustBeQueued ()
{
return FALSE ;
}
public function objectGetEntries ( $bQueued , $bRejected )
{
return query_parameters ( " SELECT * FROM appVotes " );
}
public function objectGetId ()
{
return $this -> iUserId ;
}
public function create ()
{
return TRUE ;
}
2007-10-09 21:15:10 +02:00
public function delete ()
{
$bSuccess = TRUE ;
if ( ! is_array ( $this -> aVotes ))
$this -> aVotes = $this -> getVotes ();
foreach ( $this -> aVotes as $oVote )
{
if ( ! $oVote -> delete ())
$bSuccess = FALSE ;
}
return $bSuccess ;
}
2007-10-09 21:10:29 +02:00
public function update ()
{
foreach ( $this -> aVotes as $oVote )
$oVote -> update ();
}
public function getOutputEditorValues ( $aClean )
{
$this -> aVotes = $this -> getVotes ();
for ( $i = 0 ; $i < MAX_VOTES ; $i ++ )
$this -> aVotes [ $i ] -> iVersionId = $aClean [ " iSlot $i " ];
}
public function objectGetEntriesCount ()
{
$hResult = query_parameters ( " SELECT COUNT(id) as count FROM appVotes " );
if ( ! $hResult )
return FALSE ;
if ( ! ( $oRow = mysql_fetch_object ( $hResult )))
return FALSE ;
return $oRow -> count ;
}
public function objectGetSubmitterId ()
{
return $this -> iUserId ;
}
public function getVotes ()
{
$aVotes = array ();
$hResult = query_parameters ( " SELECT * FROM appVotes WHERE userId = '?' ORDER BY slot " , $this -> iUserId );
if ( ! $hResult )
return $aVotes ;
for ( $i = 0 ; $i < MAX_VOTES ; $i ++ )
$aVotes [ $i ] = null ;
while ( $oRow = mysql_fetch_object ( $hResult ))
$aVotes [ $oRow -> slot - 1 ] = new vote ( null , $oRow );
for ( $i = 0 ; $i < MAX_VOTES ; $i ++ )
{
if ( ! $aVotes [ $i ])
{
$aVotes [ $i ] = new vote ();
$aVotes [ $i ] -> iSlotIndex = $i + 1 ;
}
}
return $aVotes ;
}
}
2004-12-12 03:51:51 +00:00
/**
2004-03-15 16:22:00 +00:00
* count the number of votes for appId by userId
*/
2007-01-21 18:06:53 +00:00
function vote_count ( $iVersionId , $iUserId = null )
2004-03-15 16:22:00 +00:00
{
2006-07-19 00:11:40 +00:00
if ( ! $iUserId )
2004-12-12 03:51:51 +00:00
{
2005-01-30 23:12:48 +00:00
if ( $_SESSION [ 'current' ] -> isLoggedIn ())
2006-07-19 00:11:40 +00:00
$iUserId = $_SESSION [ 'current' ] -> iUserId ;
2004-12-12 03:51:51 +00:00
else
return 0 ;
2006-07-19 00:11:40 +00:00
}
2007-01-21 18:06:53 +00:00
$hResult = query_parameters ( " SELECT * FROM appVotes WHERE versionId = '?' AND userId = '?' " ,
$iVersionId , $iUserId );
2007-08-03 23:27:25 +00:00
return query_num_rows ( $hResult );
2004-03-15 16:22:00 +00:00
}
2004-12-12 03:51:51 +00:00
/**
2004-03-15 16:22:00 +00:00
* total votes by userId
*/
2006-07-19 00:11:40 +00:00
function vote_count_user_total ( $iUserId = null )
2004-03-15 16:22:00 +00:00
{
2006-07-19 00:11:40 +00:00
if ( ! $iUserId )
2004-12-12 03:51:51 +00:00
{
2005-01-30 23:12:48 +00:00
if ( $_SESSION [ 'current' ] -> isLoggedIn ())
2006-07-19 00:11:40 +00:00
$iUserId = $_SESSION [ 'current' ] -> iUserId ;
2004-12-12 03:51:51 +00:00
else
return 0 ;
}
2006-07-19 00:11:40 +00:00
$hResult = query_parameters ( " SELECT * FROM appVotes WHERE userId = '?' " , $iUserId );
2007-08-03 23:27:25 +00:00
return query_num_rows ( $hResult );
2004-03-15 16:22:00 +00:00
}
2004-12-12 03:51:51 +00:00
2004-03-15 16:22:00 +00:00
/*
2007-01-21 18:06:53 +00:00
* total votes for versionId
2004-03-15 16:22:00 +00:00
*/
2007-01-21 18:06:53 +00:00
function vote_count_version_total ( $iVersionId )
2004-03-15 16:22:00 +00:00
{
2007-01-21 18:06:53 +00:00
$hResult = query_parameters ( " SELECT * FROM appVotes WHERE versionId = '?' " ,
$iVersionId );
2007-08-03 23:27:25 +00:00
return query_num_rows ( $hResult );
2004-03-15 16:22:00 +00:00
}
2004-12-12 03:51:51 +00:00
/**
2004-03-15 16:22:00 +00:00
* add a vote for appId
*/
2007-01-21 18:06:53 +00:00
function vote_add ( $iVersionId , $iSlot , $iUserId = null )
2004-03-15 16:22:00 +00:00
{
2006-07-19 00:11:40 +00:00
if ( ! $iUserId )
2005-05-11 03:08:07 +00:00
{
2005-01-30 23:12:48 +00:00
if ( $_SESSION [ 'current' ] -> isLoggedIn ())
2006-07-19 00:11:40 +00:00
$iUserId = $_SESSION [ 'current' ] -> iUserId ;
2005-05-11 03:08:07 +00:00
else
return ;
}
2004-03-15 16:22:00 +00:00
2006-07-19 00:11:40 +00:00
if ( $iSlot > MAX_VOTES )
2004-12-28 00:01:21 +00:00
return ;
2006-07-19 00:11:40 +00:00
vote_remove ( $iSlot , $iUserId );
2006-06-24 04:20:32 +00:00
2007-01-21 18:06:53 +00:00
query_parameters ( " INSERT INTO appVotes (id, time, versionId, userId, slot)
2007-07-31 23:48:22 +00:00
VALUES ( ? , ? , '?' , '?' , '?' ) " , " null " , " NOW () " ,
2007-01-21 18:06:53 +00:00
$iVersionId , $iUserId , $iSlot );
2004-03-15 16:22:00 +00:00
}
2004-12-12 03:51:51 +00:00
/**
2004-12-28 00:01:21 +00:00
* remove vote for a slot
2004-03-15 16:22:00 +00:00
*/
2006-07-19 00:11:40 +00:00
function vote_remove ( $iSlot , $iUserId = null )
2004-03-15 16:22:00 +00:00
{
2006-07-19 00:11:40 +00:00
if ( ! $iUserId )
2005-05-11 03:08:07 +00:00
{
if ( $_SESSION [ 'current' ] -> isLoggedIn ())
2006-07-19 00:11:40 +00:00
$iUserId = $_SESSION [ 'current' ] -> iUserId ;
2005-05-11 03:08:07 +00:00
else
return ;
}
2006-06-27 19:16:27 +00:00
$sQuery = " DELETE FROM appVotes WHERE userId = '?' AND slot = '?' " ;
2006-07-19 00:11:40 +00:00
query_parameters ( $sQuery , $iUserId , $iSlot );
2004-03-15 16:22:00 +00:00
}
2004-12-12 03:51:51 +00:00
2006-07-19 00:11:40 +00:00
function vote_get_user_votes ( $iUserId = null )
2004-03-15 16:22:00 +00:00
{
2006-07-19 00:11:40 +00:00
if ( ! $iUserId )
2004-12-12 03:51:51 +00:00
{
2005-01-30 23:12:48 +00:00
if ( $_SESSION [ 'current' ] -> isLoggedIn ())
2006-07-19 00:11:40 +00:00
$iUserId = $_SESSION [ 'current' ] -> iUserId ;
if ( ! $iUserId )
2004-12-12 03:51:51 +00:00
return array ();
}
2006-07-19 00:11:40 +00:00
$hResult = query_parameters ( " SELECT * FROM appVotes WHERE userId = '?' " , $iUserId );
2006-06-21 01:04:12 +00:00
if ( ! $hResult )
2004-12-12 03:51:51 +00:00
return array ();
2004-03-15 16:22:00 +00:00
$obs = array ();
2007-08-03 23:27:25 +00:00
while ( $oRow = query_fetch_object ( $hResult ))
2006-06-21 01:04:12 +00:00
$obs [ $oRow -> slot ] = $oRow ;
2004-03-15 16:22:00 +00:00
return $obs ;
}
2004-12-12 03:51:51 +00:00
2004-03-15 16:22:00 +00:00
function vote_menu ()
{
2007-01-04 02:35:01 +00:00
global $aClean ;
2006-06-17 06:10:10 +00:00
2004-03-15 16:22:00 +00:00
$m = new htmlmenu ( " Votes " , " updatevote.php " );
2007-01-21 18:06:53 +00:00
2007-09-08 22:52:00 +00:00
$oVersion = new version ( $aClean [ 'iVersionId' ]);
2004-03-15 16:22:00 +00:00
2007-09-08 22:52:00 +00:00
if ( $oVersion -> iObsoleteBy )
2004-12-12 03:51:51 +00:00
{
2007-09-08 22:52:00 +00:00
$m -> add ( " This version is marked as obsolete, so you cannot vote for it. " );
} else
{
$votes = vote_get_user_votes ();
for ( $i = 1 ; $i <= MAX_VOTES ; $i ++ )
{
if ( isset ( $votes [ $i ]))
$str = Version :: fullNameLink ( $votes [ $i ] -> versionId );
else
$str = " No App Selected " ;
$m -> add ( " <input type=radio name=iSlot value=' $i '> $str " );
}
$m -> addmisc ( " " );
2007-01-21 18:06:53 +00:00
2007-09-08 22:52:00 +00:00
$m -> add ( " <input type=submit name=sClear value=' Clear Vote ' class=votebutton> " );
$m -> add ( " <input type=submit name=sVote value='Vote for App' class=votebutton> " );
$m -> addmisc ( " <input type=hidden name=iVersionId value= { $aClean [ 'iVersionId' ] } > " );
$m -> add ( " View Results " , BASE . " votestats.php " );
$m -> add ( " Voting Help " , BASE . " help/?sTopic=voting " );
2004-12-12 03:51:51 +00:00
}
2004-03-15 16:22:00 +00:00
2007-09-08 22:52:00 +00:00
$m -> done ( 1 );
2004-03-15 16:22:00 +00:00
}
function vote_update ( $vars )
{
2005-01-30 23:12:48 +00:00
if ( ! $_SESSION [ 'current' ] -> isLoggedIn ())
2006-07-06 18:44:56 +00:00
util_show_error_page_and_exit ( " You must be logged in to vote " );
2004-03-15 16:22:00 +00:00
2007-04-08 23:04:31 +00:00
$oVersion = new version ( $vars [ 'iVersionId' ]);
2007-01-21 18:06:53 +00:00
if ( ! is_numeric ( $vars [ 'iVersionId' ]) OR ! is_numeric ( $vars [ 'iSlot' ]))
2004-12-27 05:16:33 +00:00
{
2007-01-21 18:06:53 +00:00
if ( is_numeric ( $vars [ 'iVersionId' ]))
2007-04-24 00:22:36 +00:00
{
addmsg ( " You need to select a voting slot " , " red " );
2007-04-08 23:04:31 +00:00
util_redirect_and_exit ( $oVersion -> objectMakeUrl ());
2007-04-24 00:22:36 +00:00
} else
{
2006-07-06 18:44:56 +00:00
util_redirect_and_exit ( apidb_fullurl ( " index.php " ));
2007-04-24 00:22:36 +00:00
}
2005-05-11 03:08:07 +00:00
2004-12-27 05:16:33 +00:00
return ;
}
2007-04-08 23:04:31 +00:00
2006-07-06 17:27:54 +00:00
if ( $vars [ " sVote " ])
2005-05-11 03:08:07 +00:00
{
2007-01-21 18:06:53 +00:00
addmsg ( " Registered vote for App # " . $vars [ 'iVersionId' ], " green " );
vote_add ( $vars [ 'iVersionId' ], $vars [ 'iSlot' ]);
2006-07-19 00:11:40 +00:00
} else if ( $vars [ 'sClear' ])
2005-05-11 03:08:07 +00:00
{
/* see if we have a vote in this slot, if we don't there is */
/* little reason to remove it or even mention that we did anything */
2006-07-19 00:11:40 +00:00
if ( is_vote_in_slot ( $vars [ 'iSlot' ]))
2005-05-11 03:08:07 +00:00
{
2006-07-19 00:11:40 +00:00
vote_remove ( $vars [ 'iSlot' ]);
2007-01-21 18:06:53 +00:00
addmsg ( " Removed vote for App # " . $vars [ 'iVersionId' ], " green " );
2005-05-11 03:08:07 +00:00
}
}
2007-04-08 23:04:31 +00:00
util_redirect_and_exit ( $oVersion -> objectMakeUrl ());
2004-03-15 16:22:00 +00:00
}
2005-05-11 03:08:07 +00:00
// tell us if there is a vote in a given slot so we don't
// display incorrect information to the user or go
// through the trouble of trying to remove a vote that doesn't exist
2006-07-19 00:11:40 +00:00
function is_vote_in_slot ( $iSlot , $iUserId = null )
2005-05-11 03:08:07 +00:00
{
2006-07-19 00:11:40 +00:00
if ( ! $iUserId )
2005-05-11 03:08:07 +00:00
{
if ( $_SESSION [ 'current' ] -> isLoggedIn ())
2006-07-19 00:11:40 +00:00
$iUserId = $_SESSION [ 'current' ] -> iUserId ;
2005-05-11 03:08:07 +00:00
else
return ;
}
2006-06-27 19:16:27 +00:00
$sQuery = " SELECT COUNT(*) as count from appVotes WHERE userId = '?' AND slot = '?' " ;
2006-07-19 00:11:40 +00:00
if ( $hResult = query_parameters ( $sQuery , $iUserId , $iSlot ))
2005-05-11 03:08:07 +00:00
{
2007-08-03 23:27:25 +00:00
$oRow = query_fetch_object ( $hResult );
2005-05-11 03:08:07 +00:00
if ( $oRow -> count != 0 )
return true ;
else
return false ;
}
return false ;
}
2004-03-15 16:22:00 +00:00
?>