/** * @param \Illuminate\Http\Request $request * @param Question $question * * @return \Illuminate\Http\Response */ public function answer(Request $request, Question $question) { // TODO: If question has been answered can't answer again // TODO: Validate // TODO: AI. Global Badges // Obtain how many points has its answer obtained $points = 0; $success = false; foreach ($request->choices as $answer) { $choice = $question->choices()->find($answer); $points += $choice->points; $success = $success || $choice->correct; } // minimun points for answer is '1' if ($points < 1) { $points = 1; } // Create relation between User and Question Auth::user()->answeredQuestions()->attach($question, ['points' => $points, 'answers' => implode(',', $request->choices)]); // Add XP to user Game::addExperience(Auth::user(), $points, 'has earned ' . $points . ' points.'); // Deal with Question specific Badges if ($success) { $answerStatus = 'correct'; } else { $answerStatus = 'incorrect'; } $badges = $question->actions()->whereIn('when', ['always', $answerStatus]); // AI. Increment actions foreach ($badges as $badge) { Game::incrementBadge(Auth::user(), $badge); } // AI. Add notifications and return view return redirect()->route('questions.show', $question->shortname); }
public function giveBadge(RewardBadgeRequest $request) { $user = User::findOrFail($request->input('username')); $badge = Badge::findOrFail($request->input('badge')); if (Game::incrementBadge($user, $badge)) { return redirect()->route('admin.rewards.index')->with('success', trans('admin/reward/messages.badge_given.success', ['username' => $user->username, 'badge' => $badge->name])); } else { return redirect()->route('admin.rewards.index')->with('error', trans('admin/reward/messages.badge_given.error', ['username' => $user->username])); } }