This repository has been archived on 2025-05-24. You can view files and clone it, but cannot push or open issues or pull requests.
Files
qemudb/include/vote.php

212 lines
5.3 KiB
PHP
Raw Normal View History

<?php
require_once(BASE."include/util.php");
/* max votes per user */
define('MAX_VOTES',3);
2004-03-15 16:22:00 +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
{
if(!$iUserId)
{
if($_SESSION['current']->isLoggedIn())
$iUserId = $_SESSION['current']->iUserId;
else
return 0;
}
2007-01-21 18:06:53 +00:00
$hResult = query_parameters("SELECT * FROM appVotes WHERE versionId = '?' AND userId = '?'",
$iVersionId, $iUserId);
return mysql_num_rows($hResult);
2004-03-15 16:22:00 +00:00
}
/**
2004-03-15 16:22:00 +00:00
* total votes by userId
*/
function vote_count_user_total($iUserId = null)
2004-03-15 16:22:00 +00:00
{
if(!$iUserId)
{
if($_SESSION['current']->isLoggedIn())
$iUserId = $_SESSION['current']->iUserId;
else
return 0;
}
$hResult = query_parameters("SELECT * FROM appVotes WHERE userId = '?'", $iUserId);
return mysql_num_rows($hResult);
2004-03-15 16:22:00 +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);
return mysql_num_rows($hResult);
2004-03-15 16:22:00 +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
{
if(!$iUserId)
{
if($_SESSION['current']->isLoggedIn())
$iUserId = $_SESSION['current']->iUserId;
else
return;
}
2004-03-15 16:22:00 +00:00
if($iSlot > MAX_VOTES)
return;
vote_remove($iSlot, $iUserId);
2007-01-21 18:06:53 +00:00
query_parameters("INSERT INTO appVotes (id, time, versionId, userId, slot)
VALUES (?, ?, '?', '?', '?')", "null", "null",
$iVersionId, $iUserId, $iSlot);
2004-03-15 16:22:00 +00:00
}
/**
* remove vote for a slot
2004-03-15 16:22:00 +00:00
*/
function vote_remove($iSlot, $iUserId = null)
2004-03-15 16:22:00 +00:00
{
if(!$iUserId)
{
if($_SESSION['current']->isLoggedIn())
$iUserId = $_SESSION['current']->iUserId;
else
return;
}
$sQuery = "DELETE FROM appVotes WHERE userId = '?' AND slot = '?'";
query_parameters($sQuery, $iUserId, $iSlot);
2004-03-15 16:22:00 +00:00
}
function vote_get_user_votes($iUserId = null)
2004-03-15 16:22:00 +00:00
{
if(!$iUserId)
{
if($_SESSION['current']->isLoggedIn())
$iUserId = $_SESSION['current']->iUserId;
if(!$iUserId)
return array();
}
$hResult = query_parameters("SELECT * FROM appVotes WHERE userId = '?'", $iUserId);
if(!$hResult)
return array();
2004-03-15 16:22:00 +00:00
$obs = array();
while($oRow = mysql_fetch_object($hResult))
$obs[$oRow->slot] = $oRow;
2004-03-15 16:22:00 +00:00
return $obs;
}
2004-03-15 16:22:00 +00:00
function vote_menu()
{
global $aClean;
2004-03-15 16:22:00 +00:00
$m = new htmlmenu("Votes","updatevote.php");
2007-01-21 18:06:53 +00:00
2004-03-15 16:22:00 +00:00
$votes = vote_get_user_votes();
for($i = 1;$i <= MAX_VOTES; $i++)
{
if(isset($votes[$i]))
{
2007-01-21 18:06:53 +00:00
$sName = Version::fullName($votes[$i]->versionId);
$str = "<a href='appview.php?iVersionId=".$votes[$i]->versionId."'>".
"$sName</a>";
}
else
2007-01-21 18:06:53 +00:00
$str = "No App Selected";
$m->add("<input type=radio name=iSlot value='$i'> $str");
}
2004-03-15 16:22:00 +00:00
$m->addmisc("&nbsp;");
$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>");
2004-03-15 16:22:00 +00:00
2007-01-21 18:06:53 +00:00
$m->addmisc("<input type=hidden name=iVersionId value={$aClean['iVersionId']}>");
2004-03-15 16:22:00 +00:00
$m->add("View Results", BASE."votestats.php");
$m->add("Voting Help", BASE."help/?sTopic=voting");
2004-03-15 16:22:00 +00:00
$m->done(1);
}
function vote_update($vars)
{
if(!$_SESSION['current']->isLoggedIn())
util_show_error_page_and_exit("You must be logged in to vote");
2004-03-15 16:22:00 +00:00
2007-01-21 18:06:53 +00:00
if( !is_numeric($vars['iVersionId']) OR !is_numeric($vars['iSlot']))
{
2007-01-21 18:06:53 +00:00
if(is_numeric($vars['iVersionId']))
util_redirect_and_exit(apidb_fullurl(
"appview.php?iVersionId=".$vars['iVersionId']));
else
util_redirect_and_exit(apidb_fullurl("index.php"));
return;
}
if($vars["sVote"])
{
2007-01-21 18:06:53 +00:00
addmsg("Registered vote for App #".$vars['iVersionId'], "green");
vote_add($vars['iVersionId'], $vars['iSlot']);
} else if($vars['sClear'])
{
/* 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 */
if(is_vote_in_slot($vars['iSlot']))
{
vote_remove($vars['iSlot']);
2007-01-21 18:06:53 +00:00
addmsg("Removed vote for App #".$vars['iVersionId'], "green");
}
}
2007-01-21 18:06:53 +00:00
util_redirect_and_exit(apidb_fullurl(
"appview.php?iVersionId=".$vars['iVersionId']));
2004-03-15 16:22:00 +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
function is_vote_in_slot($iSlot, $iUserId = null)
{
if(!$iUserId)
{
if($_SESSION['current']->isLoggedIn())
$iUserId = $_SESSION['current']->iUserId;
else
return;
}
$sQuery = "SELECT COUNT(*) as count from appVotes WHERE userId = '?' AND slot = '?'";
if($hResult = query_parameters($sQuery, $iUserId, $iSlot))
{
$oRow = mysql_fetch_object($hResult);
if($oRow->count != 0)
return true;
else
return false;
}
return false;
}
2004-03-15 16:22:00 +00:00
?>