Created
November 23, 2014 11:13
-
-
Save YordiLorenzo/fac132782a3eaeff2cf6 to your computer and use it in GitHub Desktop.
Portfolio - Talent Accelerator sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Advanced controller for a grade system for Talent Accelerator that gets all grades on a subject and calculates the average per subject | |
| * Response::grades is a Laravel Response macro that simplifies our API responses | |
| */ | |
| <?php namespace AppTree\Controllers\API; | |
| use Cartalyst\Sentry\Facades\Laravel\Sentry as Sentry; | |
| use \Input; | |
| use \Response; | |
| use \Session; | |
| use \AppTree\Models\Subject; | |
| class GradeController extends \BaseController { | |
| /** | |
| * | |
| * Index log models of current user with pagination of 15 | |
| * @return Response macro builded response | |
| * | |
| */ | |
| public function index() | |
| { | |
| $user = Sentry::getUser(); | |
| $subjects = Subject::with(['grades' => function ($query) use ($user) { | |
| $query->where('user_id', $user->id); | |
| }])->has('grades')->get(); | |
| $subjects = $subjects->filter(function ($subject) { | |
| return (boolean) $subject->grades->count(); | |
| })->values(); | |
| foreach ($subjects as $subject) { | |
| $mark_total = 0; | |
| $mark_divider = 0; | |
| foreach ($subject['grades'] as $grade) { | |
| $mark_total += $grade['mark']; | |
| $mark_divider++; | |
| } | |
| $subject['average'] = $mark_total / $mark_divider; | |
| } | |
| return Response::grades($subjects); | |
| } | |
| public function store(){ | |
| $input = \Input::all(); | |
| $input['user_id'] = Sentry::getUser()->id; | |
| if(!is_null($input['user_id'])){ | |
| $response = \AppTree\Models\Grade::create($input); | |
| return Response::grades($response, true); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment