public function generateQuestionChart($question_id, $title) { $answers = Question::find($question_id)->answers; $content = $answers->lists('content'); $total = 0; $index = 0; $votes = array(); foreach ($answers as $answer) { $count = UserAnswer::where('answer_id', '=', $answer->id)->count(); $total += $count; $votes[$index++] = $count; } $index = 0; $percentages = array(); foreach ($votes as $vote) { $percentages[$index++] = round($vote / $total * 100, 0, PHP_ROUND_HALF_UP); } return array('title' => $title, 'xaxis' => $content, 'percentages' => $percentages, 'votes' => $votes, 'total' => $total); }
public function submitQuestion() { $answer_id = Input::get('answer_id'); $user_answer = UserAnswer::create(array('answer_id' => $answer_id, 'user_id' => Auth::user()->id)); $user_answer->save(); return View::make('question-chart')->with('question', Answer::find($answer_id)->question); }
/** * @return \yii\db\ActiveQuery */ public function getUserAnswers() { return $this->hasMany(UserAnswer::className(), ['user_id' => 'id']); }