public function defaultVotesSet() { // check that the count of GroupVotes with this voting's id is equal to the count of all groups * number of voting items of this voting $groupCount = Group::all()->count(); $votingItemCount = VotingItem::ofVoting($this->id)->count(); $correctAmount = $groupCount * $votingItemCount; if (GroupVote::ofVoting($this->id)->count() == $correctAmount) { return true; } return false; }
/** * Function that returns the default answer of this member, * for a specified voting and voting item, based on the * first group that they are in * * @param $votingId The id of the voting * @param $votingItemId The id of the votingItem * @return int The id of the answer */ public function groupAnswer($votingId, $votingItemId) { // Check if member is in ANY group if ($this->groups()->count() > 0) { $firstGroup = $this->groups()->firstOrFail(); // Get the first group // Get the group vote of the above group in the specified voting and get the answer id $answerId = GroupVote::where(['voting_id' => $votingId, 'voting_item_id' => $votingItemId, 'group_id' => $firstGroup->id])->first()->answer->id; // If there was an answer for this group, voting and voting item, return it (otherwise will be null) return $answerId; } return null; // If the member isn't in any groups, return null so the first answer is selected }
/** * Deletes a voting's default answers * * @param $id The id of the voting to delete answers for */ private function deleteGroupVotes($id) { $prevVotes = GroupVote::ofVoting($id)->get(); foreach ($prevVotes as $gv) { $gv->delete(); } }
/** * Returns this group's default answer id for * the specified voting * * @param $voting_id Id of voting * @return mixed The id of the group vote */ public function defaultAnswer($voting_id, $voting_item_id) { $gv = GroupVote::where(['voting_id' => $voting_id, 'voting_item_id' => $voting_item_id, 'group_id' => $this->id])->first(); // there can only be one return $gv->answer_id; }