Url === null){ $this->Url = '/patrons-circle/polls/' . $this->UrlName; } return $this->Url; } protected function GetVoteCount(): int{ if($this->VoteCount === null){ $this->VoteCount = Db::QueryInt('select count(*) from Votes v inner join PollItems pi on v.PollItemId = pi.PollItemId where pi.PollId = ?', [$this->PollId]); } return $this->VoteCount; } /** * @return array */ protected function GetPollItems(): array{ if($this->PollItems === null){ $this->PollItems = Db::Query('SELECT * from PollItems where PollId = ? order by SortOrder asc', [$this->PollId], 'PollItem'); } return $this->PollItems; } /** * @return array */ protected function GetPollItemsByWinner(): array{ if($this->PollItemsByWinner === null){ $this->__get('PollItems'); $this->PollItemsByWinner = $this->PollItems; usort($this->PollItemsByWinner, function(PollItem $a, PollItem $b){ return $a->VoteCount <=> $b->VoteCount; }); $this->PollItemsByWinner = array_reverse($this->PollItemsByWinner); } return $this->PollItemsByWinner; } public function IsActive(): bool{ $now = new DateTime(); if( ($this->Start !== null && $this->Start > $now) || ($this->End !== null && $this->End < $now)){ return false; } return true; } public static function Get(?int $pollId): Poll{ $result = Db::Query('SELECT * from Polls where PollId = ?', [$pollId], 'Poll'); if(sizeof($result) == 0){ throw new Exceptions\InvalidPollException(); } return $result[0]; } public static function GetByUrlName(?string $urlName): Poll{ $result = Db::Query('SELECT * from Polls where UrlName = ?', [$urlName], 'Poll'); if(sizeof($result) == 0){ throw new Exceptions\InvalidPollException(); } return $result[0]; } }