Add new voting system
This commit is contained in:
committed by
Chris Morgan
parent
c95da59dd1
commit
835d82e49b
@@ -272,7 +272,7 @@ class ObjectManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* display the entry for editing */
|
/* display the entry for editing */
|
||||||
public function display_entry_for_editing($sBackLink, $sErrors)
|
public function display_entry_for_editing($aClean, $sErrors)
|
||||||
{
|
{
|
||||||
$this->checkMethods(array("outputEditor", "getOutputEditorValues",
|
$this->checkMethods(array("outputEditor", "getOutputEditorValues",
|
||||||
"update", "create"));
|
"update", "create"));
|
||||||
@@ -281,7 +281,7 @@ class ObjectManager
|
|||||||
echo "<div class='default_container'>\n";
|
echo "<div class='default_container'>\n";
|
||||||
|
|
||||||
// link back to the previous page
|
// link back to the previous page
|
||||||
echo html_back_link(1, $sBackLink);
|
echo html_back_link(1, null);
|
||||||
|
|
||||||
$oObject = new $this->sClass($this->iId);
|
$oObject = new $this->sClass($this->iId);
|
||||||
|
|
||||||
@@ -296,7 +296,6 @@ class ObjectManager
|
|||||||
/* Display errors, if any, and fetch form data */
|
/* Display errors, if any, and fetch form data */
|
||||||
if($this->displayErrors($sErrors))
|
if($this->displayErrors($sErrors))
|
||||||
{
|
{
|
||||||
global $aClean;
|
|
||||||
$oObject->getOutputEditorValues($aClean);
|
$oObject->getOutputEditorValues($aClean);
|
||||||
|
|
||||||
if($sErrors === PREVIEW_ENTRY)
|
if($sErrors === PREVIEW_ENTRY)
|
||||||
@@ -308,6 +307,11 @@ class ObjectManager
|
|||||||
|
|
||||||
echo $this->makeUrlFormData();
|
echo $this->makeUrlFormData();
|
||||||
|
|
||||||
|
$aCustomVars = $this->get_custom_vars($aClean, "edit");
|
||||||
|
|
||||||
|
if($aCustomVars)
|
||||||
|
$oObject->outputEditor($aClean);
|
||||||
|
else
|
||||||
$oObject->outputEditor();
|
$oObject->outputEditor();
|
||||||
|
|
||||||
/* if this is a queue add a dialog for replying to the submitter of the
|
/* if this is a queue add a dialog for replying to the submitter of the
|
||||||
@@ -760,7 +764,7 @@ class ObjectManager
|
|||||||
"name=\"sSubmit\" />\n";
|
"name=\"sSubmit\" />\n";
|
||||||
$this->handle_preview_button();
|
$this->handle_preview_button();
|
||||||
echo "</div></form>\n";
|
echo "</div></form>\n";
|
||||||
echo html_back_link(1, $sBackLink);
|
echo html_back_link(1);
|
||||||
|
|
||||||
echo "</div>\n";
|
echo "</div>\n";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -789,10 +789,12 @@ class version {
|
|||||||
// main URL
|
// main URL
|
||||||
echo " <tr class=\"color1\"><td><b>URL</b></td><td>".$appLinkURL."</td></tr>\n";
|
echo " <tr class=\"color1\"><td><b>URL</b></td><td>".$appLinkURL."</td></tr>\n";
|
||||||
|
|
||||||
|
$oM = new objectManager("voteManager", "Vote");
|
||||||
|
$oM->setReturnTo($this->objectMakeUrl());
|
||||||
// Votes
|
// Votes
|
||||||
echo html_tr(array(
|
echo html_tr(array(
|
||||||
"<b>Votes</b>",
|
"<b>Votes</b>",
|
||||||
vote_count_version_total($this->iVersionId)),
|
vote_count_version_total($this->iVersionId).' <a href="'.$oM->makeUrl("edit", $_SESSION['current']->iUserId).'&iVersionId='.$this->iVersionId.'">Vote</a>'),
|
||||||
"color0");
|
"color0");
|
||||||
|
|
||||||
if($this->sTestedRating != "/" && $this->sTestedRating)
|
if($this->sTestedRating != "/" && $this->sTestedRating)
|
||||||
|
|||||||
183
include/vote.php
183
include/vote.php
@@ -3,6 +3,189 @@ require_once(BASE."include/util.php");
|
|||||||
/* max votes per user */
|
/* max votes per user */
|
||||||
define('MAX_VOTES',3);
|
define('MAX_VOTES',3);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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']).".";
|
||||||
|
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* count the number of votes for appId by userId
|
* count the number of votes for appId by userId
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ if($oObject->getId() && $sAction != "add")
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "edit":
|
case "edit":
|
||||||
$oObject->display_entry_for_editing($_SERVER['REQUEST_URI'], $sErrors);
|
$oObject->display_entry_for_editing($aClean, $sErrors);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "showMoveChildren":
|
case "showMoveChildren":
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ echo "\n";
|
|||||||
include_once("test_appData.php");
|
include_once("test_appData.php");
|
||||||
echo "\n";
|
echo "\n";
|
||||||
include_once("test_testData.php");
|
include_once("test_testData.php");
|
||||||
|
echo "\n";
|
||||||
|
include_once("test_voting.php");
|
||||||
|
|
||||||
// purge any session messages we generated during the test
|
// purge any session messages we generated during the test
|
||||||
purgeSessionMessages();
|
purgeSessionMessages();
|
||||||
|
|||||||
@@ -100,4 +100,17 @@ function error($sMsg)
|
|||||||
echo "$sClass::$sFunction:$sFile:$sLine $sMsg\n";
|
echo "$sClass::$sFunction:$sFile:$sLine $sMsg\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function run_test($sTestName)
|
||||||
|
{
|
||||||
|
if(!$sTestName())
|
||||||
|
{
|
||||||
|
global $bTestSuccess;
|
||||||
|
echo "$sTestName() failed!\n";
|
||||||
|
$bTestSuccess = false;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
echo "$sTestName() passed.\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
72
unit_test/test_voting.php
Normal file
72
unit_test/test_voting.php
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once("path.php");
|
||||||
|
require_once("test_common.php");
|
||||||
|
require_once(BASE."include/vote.php");
|
||||||
|
|
||||||
|
/* Test to see that voteManager::getVotes() returns the same number of votes as MAX_VOTES */
|
||||||
|
function test_voteManager_getVotes()
|
||||||
|
{
|
||||||
|
$oVoteManager = new voteManager();
|
||||||
|
|
||||||
|
$iExpected = MAX_VOTES;
|
||||||
|
$iReceived = sizeof($oVoteManager->getVotes());
|
||||||
|
|
||||||
|
if($iExpected != $iReceived)
|
||||||
|
{
|
||||||
|
echo "Expected voteManager::getVotes() to return $iExpected vote objects, got $iReceived.\n";
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Tests that the votes are saved to the database and that we cannot create more than MAX_VOTES.
|
||||||
|
Note that a user always has MAX_VOTES even though they're not in the DB, so we use update instead of create */
|
||||||
|
function test_vote_update()
|
||||||
|
{
|
||||||
|
$iUserId = 655000;
|
||||||
|
|
||||||
|
for($i = 0; $i < MAX_VOTES+1; $i++)
|
||||||
|
{
|
||||||
|
$oVote = new vote();
|
||||||
|
$oVote->iUserId = $iUserId;
|
||||||
|
$oVote->iSlotIndex = $i+1;
|
||||||
|
|
||||||
|
$oVote->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
$oVoteManager = new voteManager($iUserId);
|
||||||
|
$aVotes = $oVoteManager->getVotes();
|
||||||
|
|
||||||
|
/* First test to see that the legit votes are saved */
|
||||||
|
for($i = 0; $i < MAX_VOTES; $i++)
|
||||||
|
{
|
||||||
|
$iExpected = $i+1;
|
||||||
|
$iReceived = $aVotes[$i]->iSlotIndex;
|
||||||
|
if($iExpected != $iReceived)
|
||||||
|
{
|
||||||
|
echo "Expected slot index of $iExpected, got $iReceived instead.\n";
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* There should only be MAX_VOTES number of votes */
|
||||||
|
$iExpected = MAX_VOTES;
|
||||||
|
$iReceived = sizeof($aVotes);
|
||||||
|
if($iExpected != $iReceived)
|
||||||
|
{
|
||||||
|
echo "Expected $iExpected number of votes, got $iReceived.\n";
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We don't normally delete votes, so we have to do it manually */
|
||||||
|
query_parameters("DELETE FROM appVotes WHERE userId = '?'", $iUserId);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
run_test("test_voteManager_getVotes");
|
||||||
|
run_test("test_vote_update");
|
||||||
|
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user