/** * 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 }
/** * 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; }