setID = $setID; } public function isSubmitActive() { $contest = query(" SELECT s.flPublicAdditions, sc.DateOpen, sc.DateClosed FROM tig.ggSets AS s INNER JOIN tig.ggSetsContest AS sc ON s.ID = sc.SetID WHERE SetID = $this->setID"); if (!$contest) { throw new \InvalidArgumentException('Invalid contest set ID'); } // Does the Set disallow public additions? if (!$set['flPublicAdditions']) { return false; } $today = date('Y-m-d'); // Is this a contest that hasn't opened yet? if ($today < $contest['DateOpen']) { return false; } // Is this a contest that has closed already? if ($today > $contest['DateClosed']) { return false; } return true; } public function isVotingActive() { $contest = query("SELECT VoteOpen, VoteClosed, flForceVoting, flVoting FROM tig.ggSetsContest WHERE SetID = $this->setID"); if (!$contest) { throw new \InvalidArgumentException('Invalid contest set ID'); } // Is voting disabled in general? if (!$contest['flVoting']) { return false; } // Is voting enabled, regardless of the open and closing dates? if ($contest['flForceVoting']) { return true; } $today = date('Y-m-d'); // Has voting not opened yet? if ($today < $contest['VoteOpen']) { return false; } // Has voting closed already? if ($today > $contest['VoteClosed']) { return false; } // Otherwise, allow it return true; } public function isVoter($userID) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException('Invalid user ID'); } $contest = query("SELECT VoteTypeID FROM tig.ggSetsContest AS sc WHERE SetID = $this->setID"); switch ($contest['VoteTypeID']) { // Member Voting case self::VOTETYPE_MEMBERS: return true; case self::VOTETYPE_JUDGES: return $this->isJudge($userID); } } private function isJudge($userID) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException('Invalid user ID'); } $isJudge = query(" SELECT 1 FROM tig.ggSetsContestJudges WHERE SetID = $this->setID AND MemberID = $userID "); return (boolean) $isJudge; } public function countVotesLeft($userID) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException('Invalid user ID'); } $contest = query("SELECT nVotesJudges FROM tig.ggSetsContest WHERE SetID = $this->setID"); // Retrieved from the master so that it is accurate if the user // casts multiple votes in quick succession. $votesCastQuery = query(" SELECT COUNT(ID) as cnt FROM tig.ggSubmissionVotes WHERE SetID = $this->setID AND MemberID = $userID", '', 1); $votesCast = $votesCastQuery['cnt']; $votesLeft = $contest['nVotesJudges'] - $votesCast; if ($votesLeft < 0) { return 0; } return $votesLeft; } public function createVote($userID, $submissionID) { if (!filter_var($submissionID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException('Invalid submission ID'); } if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException('Invalid user ID'); } if (!$this->hasVote($userID, $submissionID)) { safe_dbwrite(" INSERT INTO tig.ggSubmissionVotes SET ImageID = $submissionID, SetID = $this->setID, MemberID = $userID, Date = NOW() "); } } public function deleteVote($userID, $submissionID) { if (!filter_var($submissionID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException('Invalid submission ID'); } if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException('Invalid user ID'); } safe_dbwrite(" DELETE FROM tig.ggSubmissionVotes WHERE ImageID = $submissionID AND SetID = $this->setID AND MemberID = $userID "); } public function hasVote($userID, $submissionID) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException('Invalid user ID'); } if (!filter_var($submissionID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException('Invalid submission ID'); } // Has this vote been cast before? $hasVote = query(" SELECT 1 FROM tig.ggSubmissionVotes WHERE ImageID = $submissionID AND SetID = $this->setID AND MemberID = $userID ", '', 1); return (bool) $hasVote; } }