/** * Save in database the score, update his profile and return a message to the user */ public function post_synonyms(Request $request) { $points = 0; $user = Auth::user(); $response = ''; $score_table = '<table class="table"> <tr> <th>The word</th> <th>Your input</th> <th>The response</th> </tr>'; try { $inputs = array($request->radio1, $request->radio2, $request->radio3, $request->radio4); $decode_values = json_decode($request->values); // games data for ($i = 0; $i < sizeof($inputs); $i++) { $score_table .= '<tr> <td>' . $decode_values[$i]->word . '</td> <td>' . $inputs[$i] . '</td> <td>' . $decode_values[$i]->response . '</td> </tr>'; if ($inputs[$i] == $decode_values[$i]->response) { $points += 10; } } $score_table .= '</table>'; $response = '<div class="jumbotron alert-success"> <div class="container"> <h1>You win ' . $points . ' points</h1> <p>There are your results :</p> </div> </div>' . $score_table; $user->points = $user->points + $points; $user->save(); $history = new GameHistory(); $history->user_id = Auth::id(); $history->game_id = 1; $history->points = $points; $history->save(); $achievement = $this->check_achievements(); /*if($achievement) { $response .= '<br>'; $response .= '<div>' .'<h3>New achievement !</h3> <h4>'.$achievement['title'].'</h4>' .'<img src="{{ asset('.$achievement['link'].') }}" class="img-responsive"/>' .'</div>'; }*/ } catch (Exception $e) { return Response::json($response); } $return = json_encode(['response' => $response, 'achievement' => $achievement]); return Response::json($return); }
/** * store the mark, give points to the user and check if he win a new achievement * @param Request $request * @param $id the record id * @return redirect to the page */ public function post_evaluate(Request $request, $id) { $points = $request->mark * 5; $record = GameSpeakAboutRecord::find($id); $record->points = $points; $history = new GameHistory(); $history->user_id = $record->user_id; $history->game_id = 2; $history->points = $points; $student = User::find($record->user_id); $student->points = $student->points + $points; $student->save(); $history->save(); $record->save(); $achievement = $this->check_achievements($record->user_id); // send notification to the user if achievement return Redirect::to('administration/games/evaluate/speak_about'); }
/** * Return the view achievements with all the users games informations */ public function achievements() { $user = User::find(Auth::id()); $users = DB::table('users')->orderBy('points', 'desc')->get(); $i = 0; $row = 0; $length = sizeof($users); while ($row == 0 && $i < $length) { if ($users[$i]->id == $user->id) { $row = $i + 1; } $i++; } $topranking = DB::table('users')->orderBy('points', 'desc')->where('points', '>', $user->points)->take(2)->get(); $lowranking = DB::table('users')->orderBy('points', 'desc')->where('points', '<', $user->points)->take(2)->get(); $games = GameHistory::with('game')->orderBy('created_at', 'desc')->where('user_id', '=', $user->id)->take(5)->get(); $badges = Auth::user()->achievements()->get(); return view('profile/achievements', compact('user', 'topranking', 'lowranking', 'games', 'badges', 'row')); }