Ejemplo n.º 1
0
 /**
  * @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);
 }
 /**
  * Update the specified resource in storage.
  *
  * @param QuestionUpdateRequest $request
  * @param $question
  *
  * @return Response
  */
 public function update(QuestionUpdateRequest $request, Question $question)
 {
     // Save Question Tags
     if (count($request->tag_list)) {
         $question->retag($request->tag_list);
     } else {
         $question->untag();
     }
     // Save Question Choices
     // 1st. Deletes the old ones
     $question->choices()->delete();
     // 2nd. Adds the new ones
     for ($i = 0; $i < count($request->choice_text); $i++) {
         if (empty($request->choice_text[$i])) {
             continue;
         }
         $question->choices()->create(['text' => $request->choice_text[$i], 'points' => $request->choice_points[$i], 'correct' => $request->choice_points[$i] > 0]);
     }
     // Are you trying to publish a question?
     if ($request->status == 'publish') {
         if (!$question->canBePublished()) {
             return redirect()->back()->with('error', trans('admin/question/messages.publish.error'));
         }
     }
     $question->fill($request->only(['name', 'question', 'solution', 'type', 'hidden', 'status']))->save();
     return redirect()->route('admin.questions.index')->with('success', trans('admin/question/messages.update.success'));
 }