diff --git a/include/objectManager.php b/include/objectManager.php index 48366e5..fec70ff 100644 --- a/include/objectManager.php +++ b/include/objectManager.php @@ -272,7 +272,7 @@ class ObjectManager } /* 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", "update", "create")); @@ -281,7 +281,7 @@ class ObjectManager echo "
\n"; // link back to the previous page - echo html_back_link(1, $sBackLink); + echo html_back_link(1, null); $oObject = new $this->sClass($this->iId); @@ -296,7 +296,6 @@ class ObjectManager /* Display errors, if any, and fetch form data */ if($this->displayErrors($sErrors)) { - global $aClean; $oObject->getOutputEditorValues($aClean); if($sErrors === PREVIEW_ENTRY) @@ -308,7 +307,12 @@ class ObjectManager echo $this->makeUrlFormData(); - $oObject->outputEditor(); + $aCustomVars = $this->get_custom_vars($aClean, "edit"); + + if($aCustomVars) + $oObject->outputEditor($aClean); + else + $oObject->outputEditor(); /* if this is a queue add a dialog for replying to the submitter of the queued entry */ @@ -760,7 +764,7 @@ class ObjectManager "name=\"sSubmit\" />\n"; $this->handle_preview_button(); echo "
\n"; - echo html_back_link(1, $sBackLink); + echo html_back_link(1); echo "\n"; } diff --git a/include/version.php b/include/version.php index 8e2d75a..1fdf04c 100644 --- a/include/version.php +++ b/include/version.php @@ -789,10 +789,12 @@ class version { // main URL echo " URL".$appLinkURL."\n"; + $oM = new objectManager("voteManager", "Vote"); + $oM->setReturnTo($this->objectMakeUrl()); // Votes echo html_tr(array( "Votes", - vote_count_version_total($this->iVersionId)), + vote_count_version_total($this->iVersionId).'   iUserId).'&iVersionId='.$this->iVersionId.'">Vote'), "color0"); if($this->sTestedRating != "/" && $this->sTestedRating) diff --git a/include/vote.php b/include/vote.php index 814e99d..32303da 100644 --- a/include/vote.php +++ b/include/vote.php @@ -3,6 +3,189 @@ require_once(BASE."include/util.php"); /* max votes per user */ 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(''); + $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 */ diff --git a/objectManager.php b/objectManager.php index abf8e54..6d35efe 100644 --- a/objectManager.php +++ b/objectManager.php @@ -101,7 +101,7 @@ if($oObject->getId() && $sAction != "add") break; case "edit": - $oObject->display_entry_for_editing($_SERVER['REQUEST_URI'], $sErrors); + $oObject->display_entry_for_editing($aClean, $sErrors); break; case "showMoveChildren": diff --git a/unit_test/run_tests.php b/unit_test/run_tests.php index 74ec659..ffc78f4 100644 --- a/unit_test/run_tests.php +++ b/unit_test/run_tests.php @@ -44,6 +44,8 @@ echo "\n"; include_once("test_appData.php"); echo "\n"; include_once("test_testData.php"); +echo "\n"; +include_once("test_voting.php"); // purge any session messages we generated during the test purgeSessionMessages(); diff --git a/unit_test/test_common.php b/unit_test/test_common.php index 59de94b..b498a80 100644 --- a/unit_test/test_common.php +++ b/unit_test/test_common.php @@ -100,4 +100,17 @@ function error($sMsg) 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"; + } +} + ?> diff --git a/unit_test/test_voting.php b/unit_test/test_voting.php new file mode 100644 index 0000000..9b882b3 --- /dev/null +++ b/unit_test/test_voting.php @@ -0,0 +1,72 @@ +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"); + +?> \ No newline at end of file