public function save(\Illuminate\Http\Request $request) { $points = 0; $question = Question::findOrfail(Input::get('id')); $course = $question->test()->first()->course()->first(); $this->change($request); if ($request->guess1 == 1 && $question->correct1 == 1) { $points++; } if ($request->guess2 == 1 && $question->correct2 == 1) { $points++; } if ($request->guess3 == 1 && $question->correct3 == 1) { $points++; } if ($request->guess4 == 1 && $question->correct4 == 1) { $points++; } if ($request->guess5 == 1 && $question->correct5 == 1) { $points++; } if ($request->guess1 == 1 && $question->correct1 == 0) { $points--; } if ($request->guess2 == 1 && $question->correct2 == 0) { $points--; } if ($request->guess3 == 1 && $question->correct3 == 0) { $points--; } if ($request->guess4 == 1 && $question->correct4 == 0) { $points--; } if ($request->guess5 == 1 && $question->correct5 == 0) { $points--; } $enroll = Enroll::where(['course_id' => $course->id, 'user_id' => Auth::user()->id])->first(); $enroll->points += $points; if ($enroll->points < 0) { $enroll->points = 0; } $enroll->save(); }