diff --git a/app/controllers/ClientController.php b/app/controllers/ClientController.php index c0a3905ab2..c9bb085c98 100644 --- a/app/controllers/ClientController.php +++ b/app/controllers/ClientController.php @@ -1,103 +1,76 @@ user = $user; - $this->lrs = $lrs; - $this->client = $client; - $this->logged_in_user = Auth::user(); - + $this->lrs = $lrs; + $this->client = $client; } /** - * Load the manage clients page - * - * @param int $id + * Load the manage clients page. + * @param String $lrs_id * @return View */ - public function manage($id){ - - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); - - $clients = \Client::where('lrs_id', $lrs->id)->get(); - - - return View::make('partials.client.manage', array('clients' => $clients, - 'lrs' => $lrs, - 'list' => $lrs_list - )); + public function manage($lrs_id) { + $opts = ['user' => \Auth::user()]; + $lrs = $this->lrs->show($lrs_id, $opts); + $lrs_list = $this->lrs->index($opts); + $clients = \Client::where('lrs_id', $lrs->id)->get(); + return View::make('partials.client.manage', [ + 'clients' => $clients, + 'lrs' => $lrs, + 'list' => $lrs_list + ]); } /** - * Load the manage clients page - * - * @param int $lrs_id - * @param int $id + * Load the manage clients page. + * @param String $lrs_id + * @param String $id * @return View */ - public function edit($lrs_id, $id){ - - $lrs = $this->lrs->find( $lrs_id ); - $lrs_list = $this->lrs->all(); - - $client = $this->client->find( $id ); - - - return View::make('partials.client.edit', array('client' => $client, - 'lrs' => $lrs, - 'list' => $lrs_list - )); + public function edit($lrs_id, $id) { + $opts = ['user' => \Auth::user()]; + $lrs = $this->lrs->show($lrs_id, $opts); + $lrs_list = $this->lrs->index($opts); + $client = $this->client->find($id); + return View::make('partials.client.edit', [ + 'client' => $client, + 'lrs' => $lrs, + 'list' => $lrs_list + ]); } - /** - * Create a new client - * - * @param int $id + /** + * Create a new client. + * @param String $id * @return View - **/ - - public function create($id){ - - $lrs = $this->lrs->find( $id ); - - $data = array('lrs_id' => $lrs->id); + */ + public function create($id) { + $opts = ['user' => \Auth::user()]; + $lrs = $this->lrs->show($lrs_id, $opts); + $data = ['lrs_id' => $lrs->id]; - if( $this->client->create( $data ) ){ + if ($this->client->create($data)) { $message_type = 'success'; - $message = Lang::get('update_key'); - }else{ + $message = trans('update_key'); + } else { $message_type = 'error'; - $message = Lang::get('update_key_error'); + $message = trans('update_key_error'); } return Redirect::back()->with($message_type, $message); @@ -105,54 +78,36 @@ public function create($id){ /** * Update the specified resource in storage. - * - * @param int $id + * @param String $lrs_id Id of the LRS. + * @param String $id Id of the client. * @return View */ public function update($lrs_id, $id){ - - /*$data = Input::all(); - - //TODO :client input validation This may be able to re-use some of the statement validator - $rules['title'] = 'required|alpha_spaces'; - $rules['description'] = 'alpha_spaces'; - $validator = Validator::make($data, $rules); - if ($validator->fails()) return Redirect::back()->withErrors($validator); - */ - - - //{{ URL() }}/lrs/{{ $lrs->_id }}/client/manage#{{ $client->_id }} - - if($this->client->update( $id, Input::all() )){ - return Redirect::to('/lrs/'.$lrs_id.'/client/manage#'.$id)->with('success', Lang::get('lrs.client.updated')); + if ($this->client->update($id, Input::all())) { + $redirect_url = '/lrs/'.$lrs_id.'/client/manage#'.$id; + return Redirect::to($redirect_url)->with('success', trans('lrs.client.updated')); } return Redirect::back() - ->withInput() - ->withErrors($this->client->errors()); - + ->withInput() + ->withErrors($this->client->errors()); } - /** * Remove the specified resource from storage. - * - * @param int $lrs_id - * @param int $id + * @param String $lrs_id + * @param String $id * @return View */ public function destroy($lrs_id, $id){ - - if( $this->client->delete($id) ){ + if ($this->client->delete($id)) { $message_type = 'success'; - $message = Lang::get('delete_client_success'); - }else{ + $message = trans('delete_client_success'); + } else { $message_type = 'error'; - $message = Lang::get('delete_client_error'); + $message = trans('delete_client_error'); } - return Redirect::back()->with($message_type, $message); - + return Redirect::back()->with($message_type, $message); } - -} \ No newline at end of file +} diff --git a/app/controllers/ExplorerController.php b/app/controllers/ExplorerController.php deleted file mode 100644 index 903f6f93a3..0000000000 --- a/app/controllers/ExplorerController.php +++ /dev/null @@ -1,92 +0,0 @@ -statement = $statement; - $this->lrs = $lrs; - - $this->beforeFilter('auth'); - - } - - public function explore( $id ){ - - $vars = explode( '/', Request::path() ); - - //remove lrs and statement which is always passed - unset( $vars[0] ); //remove lrs - unset( $vars[1] ); //remove lrs id - unset( $vars[2] ); //remove statements - unset( $vars[3] ); //remove explorer - - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); - $statements = $this->statement->filter( $id, $vars, 'comments' ); - $graph_it = new \app\locker\data\Filter( $statements['data'] ); - return View::make('partials.statements.explore', array('lrs' => $lrs, - 'list' => $lrs_list, - 'statements' => $statements['statements'], - 'total' => count( $statements['data'] ), - 'filter' => $statements['filter'], - 'results' => $graph_it->results, - 'single_bar_data' => $graph_it->timeline_data, - 'statement_nav' => true)); - } - - /** - * Display the specified resource. - * - * @todo figure out how to use \Route::getCurrentRoute()->parameters() - * instead of the hack below. The problem is exploding extra - it - * also explodes any urls passed. - * - * @param int $id - * @return Response - */ - public function filter( $id, $extra ){ - - $vars = explode( '/', Request::path() ); - - //remove lrs and statement which is always passed - unset( $vars[0] ); - unset( $vars[1] ); - unset( $vars[2] ); - - $statements = $this->statement->filter( $id, $vars ); - $graph_it = new \app\locker\data\Filter( $statements['data'] ); - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); - - return View::make('partials.statements.filter', - array('statements' => $statements['statements'], - 'lrs' => $lrs, - 'single_bar_data' => $graph_it->timeline_data, - 'results' => $graph_it->results, - 'total' => count( $statements['data'] ), - 'list' => $lrs_list, - 'filter' => $statements['filter'], - 'statement_nav' => true)); - - } - -} \ No newline at end of file diff --git a/app/controllers/ExportingController.php b/app/controllers/ExportingController.php index f887af8f5b..8f29c972b7 100644 --- a/app/controllers/ExportingController.php +++ b/app/controllers/ExportingController.php @@ -1,15 +1,14 @@ lrs = $lrs; - $this->beforeFilter('auth'); $this->beforeFilter('auth.lrs'); //check user can access LRS. $this->beforeFilter('csrf', array('only' => array('update', 'store', 'destroy'))); @@ -17,12 +16,12 @@ public function __construct(Lrs $lrs){ /** * Display a listing of the resource. - * - * @return Response + * @return View */ public function index($id){ - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); + $opts = ['user' => \Auth::user()]; + $lrs = $this->lrs->show($id, $opts); + $lrs_list = $this->lrs->index($opts); return View::make("{$this->views}.index", [ 'lrs' => $lrs, 'list' => $lrs_list, diff --git a/app/controllers/LrsController.php b/app/controllers/LrsController.php index 5dc4a01a4d..3f7bb07034 100644 --- a/app/controllers/LrsController.php +++ b/app/controllers/LrsController.php @@ -1,169 +1,152 @@ lrs = $lrs; + public function __construct(LrsRepo $lrs, StatementRepo $statement){ + $this->lrs = $lrs; $this->statement = $statement; + // Defines filters. $this->beforeFilter('auth'); - $this->beforeFilter('csrf', array('only' => array('store', 'update', 'destroy', 'editCredentials', 'usersRemove', 'changeRole'))); - $this->beforeFilter('auth.lrs', array('except' => array('index','create','store'))); //check user can access LRS. - $this->beforeFilter('edit.lrs', array('only' => array('edit','update','endpoint', - 'users', 'usersRemove', 'inviteUsersForm', - 'changeRole', 'api', 'editCredentials'))); //check user can edit LRS. - $this->beforeFilter('create.lrs', array('only' => array('create','store'))); //Allowed to create an LRS? + $this->beforeFilter('csrf', ['only' => [ + 'store', 'update', 'destroy', 'editCredentials', 'usersRemove', 'changeRole' + ]]); + $this->beforeFilter('auth.lrs', ['except' => ['index','create','store']]); //check user can access LRS. + $this->beforeFilter('edit.lrs', ['only' => [ + 'edit','update','endpoint', + 'users', 'usersRemove', 'inviteUsersForm', + 'changeRole', 'api', 'editCredentials' + ]]); //check user can edit LRS. + $this->beforeFilter('create.lrs', ['only' => ['create','store']]); //Allowed to create an LRS? + } + private function getLrs($lrs_id) { + $opts = ['user' => \Auth::user()]; + return [ + 'lrs' => $this->lrs->show($lrs_id, $opts), + 'list' => $this->lrs->index($opts) + ]; } /** * Display a listing of LRSs available for user. - * * @return View */ - public function index(){ - $lrs = $this->lrs->all(); - return View::make('partials.lrs.list', array('lrs' => $lrs, 'list' => $lrs)); + public function index() { + $opts = ['user' => \Auth::user()]; + $lrs = $this->lrs->index($opts); + return \View::make('partials.lrs.list', ['lrs' => $lrs, 'list' => $lrs]); } /** * Show the form for creating a new resource. - * * @return View */ - public function create(){ - //has the user verified their email address? - $verified = Auth::user()->verified; - return View::make('partials.lrs.create', array('verified' => $verified)); + public function create() { + $verified = \Auth::user()->verified; + return \View::make('partials.lrs.create', ['verified' => $verified]); } /** * Store a newly created resource in storage. - * * @return View */ - public function store(){ - - $data = Input::all(); + public function store() { + $data = \Input::all(); //lrs input validation $rules['title'] = 'required'; $rules['description'] = ''; - $validator = Validator::make($data, $rules); - if ($validator->fails()) return Redirect::back()->withErrors($validator); + $validator = \Validator::make($data, $rules); + if ($validator->fails()) return \Redirect::back()->withErrors($validator); // Store lrs - $s = $this->lrs->create( $data ); + $opts = ['user' => \Auth::user()]; + $s = $this->lrs->store($data, $opts); if($s){ - return Redirect::to('/site#lrs')->with('success', Lang::get('lrs.created')); + return \Redirect::to('/site#lrs')->with('success', trans('lrs.created')); } - return Redirect::back() + return \Redirect::back() ->withInput() - ->with('error', Lang::get('create_problem')); + ->with('error', trans('create_problem')); } /** * Show the form for editing the specified resource. - * - * @param int $id + * @param String $lrs_id * @return View */ - public function edit( $id ){ - - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); - return View::make('partials.lrs.edit', array('account_nav' => true, - 'lrs' => $lrs, - 'list' => $lrs_list)); - + public function edit($lrs_id) { + return \View::make('partials.lrs.edit', array_merge($this->getLrs($lrs_id), [ + 'account_nav' => true + ])); } /** * Update the specified resource in storage. - * - * @param int $id + * @param String $lrs_id * @return View */ - public function update($id){ - - $data = Input::all(); + public function update($lrs_id){ + $data = \Input::all(); //lrs input validation - $rules['title'] = 'required'; - $validator = Validator::make($data, $rules); - if ($validator->fails()) return Redirect::back()->withErrors($validator); + $rules['title'] = 'required'; + $validator = \Validator::make($data, $rules); + if ($validator->fails()) { + return \Redirect::back()->withErrors($validator); + }; - $l = $this->lrs->update( $id, Input::all() ); + $opts = ['user' => \Auth::user()]; + $l = $this->lrs->update($lrs_id, $data); - if($l){ - return Redirect::back()->with('success', Lang::get('lrs.updated')); + if ($l) { + return \Redirect::back()->with('success', trans('lrs.updated')); } - return Redirect::back() - ->withInput() - ->withErrors($this->lrs->errors()); - + return \Redirect::back() + ->withInput() + ->withErrors($this->lrs->errors()); } /** * Display the specified resource. - * * This is a temp hack until the single page app for * analytics is ready. v1.0 stable. - * - * @param int $id + * @param String $lrs_id * @return View */ - public function show( $id ){ - - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); - $dashboard = new \app\locker\data\dashboards\LrsDashboard($id); - return View::make('partials.lrs.dashboard', array( - 'lrs' => $lrs, - 'list' => $lrs_list, + public function show($lrs_id) { + $dashboard = new \app\locker\data\dashboards\LrsDashboard($lrs_id); + return View::make('partials.lrs.dashboard', array_merge($this->getLrs($lrs_id), [ 'stats' => $dashboard->getStats(), 'graph_data' => $dashboard->getGraphData(), 'dash_nav' => true - )); - + ])); } - public function getStats( $id, $segment = '' ){ + public function getStats($lrs_id, $segment = '') { + $dashboard = new \app\locker\data\dashboards\LrsDashboard($lrs_id); - $dashboard = new \app\locker\data\dashboards\LrsDashboard($id); - - switch( $segment ){ + switch ($segment) { case 'topActivities': - $get_stats = $dashboard->getTopActivities( $id ); + $get_stats = $dashboard->getTopActivities($lrs_id); $get_stats = $get_stats['result']; break; case 'activeUsers': - $get_stats = $dashboard->getActiveUsers( $id ); + $get_stats = $dashboard->getActiveUsers($lrs_id); $get_stats = $get_stats['result']; break; default: @@ -173,137 +156,124 @@ public function getStats( $id, $segment = '' ){ return Response::json($get_stats); } - public function getGraphData($id) { + public function getGraphData($lrs_id) { $startDate = \LockerRequest::getParam('graphStartDate'); $endDate = \LockerRequest::getParam('graphEndDate'); $startDate = !$startDate ? null : new \Carbon\Carbon($startDate); $endDate = !$endDate ? null : new \Carbon\Carbon($endDate); - $dashboard = new \app\locker\data\dashboards\LrsDashboard($id); + $dashboard = new \app\locker\data\dashboards\LrsDashboard($lrs_id); $graph_data = $dashboard->getGraphData($startDate, $endDate); - return Response::json( $graph_data ); + return Response::json($graph_data); } /** * Remove the specified resource from storage. - * - * @param int $id + * @param String $lrs_id * @return View */ - public function destroy($id){ - - $this->lrs->delete($id); - return Response::json(array('success'=>200, 'message'=>'deleted')); - + public function destroy($lrs_id){ + $opts = ['user' => \Auth::user()]; + $this->lrs->destroy($lrs_id, $opts); + return Response::json([ + 'success' => 200, + 'message' => 'deleted' + ]); } /** * Display statements for this LRS - * + * @param String $lrs_id * @return View */ - public function statements( $id ){ - $statements = $this->statement->index($id, [], [ + public function statements($lrs_id){ + $statements = $this->statement->index($lrs_id, [], [ 'ascending' => false, - 'limit' => $this->statement->count($id) + 'limit' => $this->statement->count($lrs_id) ])->paginate(15); - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); - return View::make('partials.statements.list', - array('statements' => $statements, - 'lrs' => $lrs, - 'list' => $lrs_list, - 'statement_nav' => true)); + return View::make('partials.statements.list', array_merge($this->getLrs($lrs_id), [ + 'statements' => $statements, + 'statement_nav' => true + ])); } /** * Display the endpoint view. - * + * @param String $lrs_id * @return View */ - public function endpoint( $id ){ - - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); - return View::make('partials.lrs.endpoint', array('lrs' => $lrs, - 'endpoint_nav' => true, - 'list' => $lrs_list)); - + public function endpoint($lrs_id) { + return View::make('partials.lrs.endpoint', array_merge($this->getLrs($lrs_id), [ + 'endpoint_nav' => true + ])); } /** * Display the api view. - * + * @param String $lrs_id * @return View */ - public function api( $id ){ - - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); - return View::make('partials.lrs.api', array('lrs' => $lrs, - 'api_nav' => true, - 'list' => $lrs_list)); - + public function api($lrs_id) { + return View::make('partials.lrs.api', array_merge($this->getLrs($lrs_id), [ + 'api_nav' => true + ])); } /** * Generate a new key and secret for basic auth * **/ - public function editCredentials( $id ){ - - $lrs = $this->lrs->find( $id ); + public function editCredentials( $lrs_id ){ + $opts = ['user' => \Auth::user()]; + $lrs = $this->lrs->show($lrs_id); - $lrs->api = array('basic_key' => \Locker\Helpers\Helpers::getRandomValue(), - 'basic_secret' => \Locker\Helpers\Helpers::getRandomValue()); + $lrs->api = [ + 'basic_key' => \Locker\Helpers\Helpers::getRandomValue(), + 'basic_secret' => \Locker\Helpers\Helpers::getRandomValue() + ]; - if( $lrs->save() ){ + if ($lrs->save()) { $message_type = 'success'; - $message = Lang::get('update_key'); - }else{ + $message = trans('update_key'); + } else { $message_type = 'error'; - $message = Lang::get('update_key_error'); + $message = trans('update_key_error'); } return Redirect::back()->with($message_type, $message); - } /** * Display users with access to this lrs. - * + * @param String $lrs_id * @return View */ - public function users( $id ){ - - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); - return View::make('partials.users.list', array('lrs' => $lrs, - 'users' => $lrs->users, - 'list' => $lrs_list, - 'user_nav' => true)); + public function users($lrs_id) { + $opts = $this->getLrs($lrs_id); + return View::make('partials.users.list', array_merge($opts, [ + 'users' => $opts['lrs']->users, + 'user_nav' => true + ])); } - public function inviteUsersForm( $id ){ - $lrs = $this->lrs->find( $id ); - $lrs_list = $this->lrs->all(); - return View::make('partials.lrs.invite', array('lrs' => $lrs, - 'users' => $lrs->users, - 'list' => $lrs_list, - 'user_nav' => true)); + public function inviteUsersForm($lrs_id) { + $opts = $this->getLrs($lrs_id); + return View::make('partials.lrs.invite', array_merge($opts, [ + 'users' => $opts['lrs']->users, + 'user_nav' => true + ])); } - public function usersRemove( $id ){ - $lrs = $this->lrs->removeUser( $id, Input::get('user') ); - return Redirect::back()->with('success', Lang::get('lrs.remove_user')); + public function usersRemove($lrs_id) { + $lrs = $this->lrs->removeUser($lrs_id, Input::get('user')); + return Redirect::back()->with('success', trans('lrs.remove_user')); } - public function changeRole( $id, $user, $role ){ - $change = $this->lrs->changeRole( $id, $user, $role ); - return Response::json(array('success' => true)); + public function changeRole($lrs_id, $user, $role) { + $change = $this->lrs->changeRole($lrs_id, $user, $role); + return Response::json(['success' => true]); } - -} \ No newline at end of file +} diff --git a/app/controllers/ReportingController.php b/app/controllers/ReportingController.php index a4dd2bf94a..edf36d78e0 100644 --- a/app/controllers/ReportingController.php +++ b/app/controllers/ReportingController.php @@ -1,7 +1,7 @@ lrs = $lrs; $this->report = $report; $this->beforeFilter('auth'); @@ -55,47 +55,51 @@ public function __construct(Lrs $lrs, Report $report){ $this->beforeFilter('csrf', array('only' => array('update', 'store', 'destroy'))); } + private function getLrs($lrs_id) { + $opts = ['user' => \Auth::user()]; + return [ + 'lrs' => $this->lrs->show($lrs_id, $opts), + 'list' => $this->lrs->index($opts) + ]; + } + /** * Displays the reporting view. + * @param String $lrs_id * @return reporting view. */ - public function index($id){ - $lrs = $this->lrs->find($id); - $lrs_list = $this->lrs->all(); - $reports = $this->report->index([ - 'lrs_id' => $id - ]); - return View::make("{$this->views}.index", [ - 'lrs' => $lrs, - 'list' => $lrs_list, + public function index($lrs_id) { + return View::make("{$this->views}.index", array_merge($this->getLrs($lrs_id), [ 'reporting_nav' => true, - 'reports' => $reports - ]); + 'reports' => $this->report->index([ + 'lrs_id' => $lrs_id + ]) + ])); } /** * Displays the statements from the report. + * @param String $lrs_id + * @param String $report_id * @return reporting view. */ - public function statements($lrsId, $reportId) { - return View::make("{$this->views}.statements", [ - 'lrs' => $this->lrs->find($lrsId), - 'list' => $this->lrs->all(), + public function statements($lrs_id, $report_id) { + return View::make("{$this->views}.statements", array_merge($this->getLrs($lrs_id), [ 'reporting_nav' => true, - 'statements' => $this->report->statements($reportId, [ - 'lrs_id' => $lrsId + 'statements' => $this->report->statements($report_id, [ + 'lrs_id' => $lrs_id ])->select('statement')->paginate(20), - 'report' => $this->report->show($reportId, [ - 'lrs_id' => $lrsId + 'report' => $this->report->show($report_id, [ + 'lrs_id' => $lrs_id ]) - ]); + ])); } /** * Gets typeahead values (matching the query) in segments for the current lrs. - * @param string $lrs LRS in use. - * @param string $segement Statement segment (i.e. 'verbs'). - * @param query String to match against. + * @param String $lrs LRS in use. + * @param String $segement Statement segment (i.e. 'verbs'). + * @param String $query to match against. * @return [Typeahead values] Typeahead values. **/ public function typeahead($lrs, $segment, $query){ @@ -104,8 +108,8 @@ public function typeahead($lrs, $segment, $query){ return Response::json($this->report->setQuery( $lrs, $query, - self::statementKey . $options['return'], - self::statementKey . $options['query'] + self::statementKey.$options['return'], + self::statementKey.$options['query'] )); } diff --git a/app/controllers/SiteController.php b/app/controllers/SiteController.php index 87c40fe342..7bffb21342 100644 --- a/app/controllers/SiteController.php +++ b/app/controllers/SiteController.php @@ -1,40 +1,18 @@ site = $site; $this->lrs = $lrs; $this->statement = $statement; @@ -43,7 +21,6 @@ public function __construct(Site $site, Lrs $lrs, User $user, Statement $stateme $this->beforeFilter('auth'); $this->beforeFilter('auth.super', array('except' => array('inviteUsers'))); $this->beforeFilter('csrf', array('only' => array('update', 'verifyUser', 'inviteUsers'))); - } /** @@ -52,70 +29,63 @@ public function __construct(Site $site, Lrs $lrs, User $user, Statement $stateme * @return View */ public function index(){ - $site = $this->site->all(); - $list = $this->lrs->all(); + $opts = ['user' => \Auth::user()]; + $list = $this->lrs->index($opts); $admin_dashboard = new \app\locker\data\dashboards\AdminDashboard(); - return View::make('partials.site.dashboard', array( + return View::make('partials.site.dashboard', [ 'site' => $site, 'list' => $list, 'stats' => $admin_dashboard->getFullStats(), 'graph_data' => $admin_dashboard->getGraphData() - )); + ]); } /** * Show the form for editing the specified resource. - * - * @param int $id + * @param String $id * @return View */ - public function edit( $id ){ - - $site = $this->site->find( $id ); - return View::make('partials.site.edit', array('site' => $site, - 'settings_nav' => true)); - + public function edit($id){ + $site = $this->site->find($id); + return View::make('partials.site.edit', [ + 'site' => $site, + 'settings_nav' => true + ]); } /** * Update the specified resource in storage. - * - * @param int $id + * @param String $id * @return View */ - public function update($id){ + public function update($id) { + $s = $this->site->update($id, Input::all()); - // Update site details - $s = $this->site->update( $id, Input::all() ); - - if($s){ - return Redirect::back()->with('success', Lang::get('site.updated')); + if ($s) { + return Redirect::back()->with('success', trans('site.updated')); } return Redirect::back() ->withInput() ->withErrors($user->errors()); - } /** * Display the super admin settings. - * * @return Response */ - public function settings(){ - return Response::json( $this->site->all() ); + public function settings() { + return Response::json($this->site->all()); } /** * Grab site stats - * * @return Response **/ - public function getStats(){ + public function getStats() { $startDate = \LockerRequest::getParam('graphStartDate'); $endDate = \LockerRequest::getParam('graphEndDate'); @@ -124,13 +94,12 @@ public function getStats(){ $admin_dashboard = new \app\locker\data\dashboards\AdminDashboard(); $stats = $admin_dashboard->getFullStats(); - return Response::json( $stats ); + return Response::json($stats); } /** * Grab site stats - * * @return Response **/ public function getGraphData(){ @@ -141,71 +110,64 @@ public function getGraphData(){ $endDate = !$endDate ? null : new \Carbon\Carbon($endDate); $admin_dashboard = new \app\locker\data\dashboards\AdminDashboard(); $graph_data = $admin_dashboard->getGraphData($startDate, $endDate); - return Response::json( $graph_data ); + return Response::json($graph_data); } /** * Display the super admin lrs view. - * * @return Response */ public function lrs(){ - - $lrs = $this->lrs->all(); - if( $lrs ){ - foreach( $lrs as $l ){ + $opts = ['user' => \Auth::user()]; + $lrs = $this->lrs->index($opts); + if ($lrs) { + foreach ($lrs as $l) { $l->statement_total = $this->statement->count($l->_id); } } - return Response::json( $lrs ); + return Response::json($lrs); } - public function apps(){ + public function apps() { return OAuthApp::all(); } /** * Display the super admin user list view. - * * @return Response */ - public function users(){ - - $users = $this->user->all(); - foreach($users as &$u){ - $u->lrs_owned = $this->lrs->getLrsOwned( $u->_id ); - $u->lrs_member = $this->lrs->getLrsMember( $u->_id ); - } - return Response::json( $users ); - + public function users() { + return Response::json(array_map(function ($user) { + $user->lrs_owned = $this->lrs->getLrsOwned($user->_id); + $user->lrs_member = $this->lrs->getLrsMember($user->_id); + }, $this->user->all())); } /** * Display the invite user page - * * @return Response */ - public function inviteUsersForm(){ - return View::make('partials.site.invite', array('users_nav' => true, - 'admin_dash' => true)); + public function inviteUsersForm() { + return View::make('partials.site.invite', [ + 'users_nav' => true, + 'admin_dash' => true + ]); } /** * Invite in the users - * **/ - public function inviteUsers(){ - $invite = \Locker\Helpers\User::inviteUser( Input::all() ); - return Redirect::back()->with('success', Lang::get('users.invite.invited')); + public function inviteUsers() { + $invite = \Locker\Helpers\User::inviteUser(Input::all()); + return Redirect::back()->with('success', trans('users.invite.invited')); } /** * Verify a user. **/ - public function verifyUser($id){ + public function verifyUser($id) { $verify = $this->site->verifyUser($id); return Response::json($verify); } - -} \ No newline at end of file +} diff --git a/app/controllers/StatementController.php b/app/controllers/StatementController.php index 7cd1ade6e6..0e508c73f8 100644 --- a/app/controllers/StatementController.php +++ b/app/controllers/StatementController.php @@ -1,45 +1,35 @@ statement = $statement; - $this->lrs = $lrs; + $this->lrs = $lrs; $this->beforeFilter('auth'); $this->beforeFilter('csrf', array('only' => 'store')); $this->beforeFilter('@checkCanSubmit', array('only' => 'store')); - } /** * Show the form for creating a new resource. - * * @return View */ - public function create( $id ){ - $lrs = $this->lrs->find( $id ); - return View::make('partials.statements.create', array('lrs' => $lrs, - 'statement_nav' => true)); + public function create($lrs_id) { + $opts = ['user' => \Auth::user()]; + $lrs = $this->lrs->show($lrs_id, $opts); + return \View::make('partials.statements.create', [ + 'lrs' => $lrs, + 'statement_nav' => true + ]); } /** @@ -51,10 +41,10 @@ public function create( $id ){ * @return Response */ public function store(){ + $input = \Input::all(); - $input = Input::all(); - - $lrs = $this->lrs->find( $input['lrs'] ); + $opts = ['user' => \Auth::user()]; + $lrs = $this->lrs->show($input['lrs'], $opts); //remove lrs and _token from Input unset( $input['lrs'] ); @@ -80,9 +70,9 @@ public function store(){ * Can current user submit statements to this LRS? **/ public function checkCanSubmit( $route, $request ){ - - $user = \Auth::user(); - $lrs = $this->lrs->find( Input::get('lrs') ); + $user = \Auth::user(); + $opts = ['user' => $user]; + $lrs = $this->lrs->show(Input::get('lrs'), $opts); $get_users = array(); if( $lrs ){ diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index fe31cde3ef..c582106d54 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -5,15 +5,7 @@ class UserController extends BaseController { - /** - * User - */ - protected $user; - - /** - * Lrs - **/ - protected $lrs; + protected $user, $lrs; /** @@ -22,38 +14,30 @@ class UserController extends BaseController { * @param User $user */ public function __construct(User $user, Lrs $lrs){ - $this->user = $user; - $this->lrs = $lrs; - $this->logged_in_user = Auth::user(); - - $this->beforeFilter('auth', array('except' => array('verifyEmail'))); - $this->beforeFilter('csrf', array('only' => array('update','updateRole', 'destroy'))); - $this->beforeFilter('user.delete', array('only' => 'destroy')); - $this->beforeFilter('auth.super', array('only' => array('updateRole','index'))); - + $this->lrs = $lrs; + $this->logged_in_user = \Auth::user(); + + $this->beforeFilter('auth', ['except' => ['verifyEmail']]); + $this->beforeFilter('csrf', ['only' => ['update','updateRole', 'destroy']]); + $this->beforeFilter('user.delete', ['only' => 'destroy']); + $this->beforeFilter('auth.super', ['only' => ['updateRole','index']]); } /** * Display a listing of users. - * * @return View */ - public function index(){ - - return View::make('index', array( 'users' => $this->user->all() )); - + public function index() { + return \View::make('index', ['users' => $this->user->all()]); } /** * Show the form for creating a new resource. - * * @return View */ - public function create(){ - - return View::make('register.index'); - + public function create() { + return \View::make('register.index'); } /** @@ -62,12 +46,11 @@ public function create(){ * @param int $id * @return View */ - public function edit( $id ){ - - return View::make('partials.users.edit') - ->with( 'user', $this->user->find( $id ) ) - ->with( 'list', $this->lrs->all() ); - + public function edit($id) { + $opts = ['user' => \Auth::user()]; + return \View::make('partials.users.edit') + ->with('user', $this->user->find($id)) + ->with('list', $this->lrs->index($opts)); } /** @@ -77,8 +60,7 @@ public function edit( $id ){ * @return View */ public function update( $id ){ - - $data = Input::all(); + $data = \Input::all(); //if email being changed, verify new one, otherwise ignore if( $data['email'] != Auth::user()->email ){ diff --git a/app/controllers/api/Base.php b/app/controllers/api/Base.php index f7c98331c1..030c2029a3 100644 --- a/app/controllers/api/Base.php +++ b/app/controllers/api/Base.php @@ -7,7 +7,7 @@ use \Request as Request; use \Route as Route; use \DB as DB; -use \Locker\Repository\Lrs\EloquentLrsRepository as LrsRepository; +use \Locker\Repository\Lrs\EloquentRepository as LrsRepository; use \Lrs as Lrs; use \Client as Client; @@ -52,11 +52,11 @@ protected function returnJson($data) { public function getLrs() { $key = LockerRequest::getUser(); $secret = LockerRequest::getPassword(); - $lrs = LrsRepository::checkSecret(Lrs::where('api.basic_key', $key)->first(), $secret); + $lrs = (new LrsRepository)->checkSecret(Lrs::where('api.basic_key', $key)->first(), $secret); //if main credentials not matched, try the additional credentials if ($lrs == null) { - $client = LrsRepository::checkSecret(Client::where('api.basic_key', $key)->first(), $secret); + $client = (new LrsRepository)->checkSecret(Client::where('api.basic_key', $key)->first(), $secret); if ($client != null) { $lrs = Lrs::find($client->lrs_id); diff --git a/app/filters.php b/app/filters.php index 6a139ef121..1338acf144 100644 --- a/app/filters.php +++ b/app/filters.php @@ -1,6 +1,6 @@ where('api.basic_secret', $secret) // ->select('owner._id')->first(); - $lrs = LrsRepository::checkSecret(\Lrs::where('api.basic_key', $key)->first(), $secret); + $lrs = (new LrsRepo)->checkSecret(\Lrs::where('api.basic_key', $key)->first(), $secret); //if main credentials not matched, try the additional credentials if ( $lrs == NULL ) { // $client = \Client::where('api.basic_key', $key) // ->where('api.basic_secret', $secret) // ->first(); - $client = LrsRepository::checkSecret(\Client::where('api.basic_key', $key)->first(), $secret); + $client = (new LrsRepo)->checkSecret(\Client::where('api.basic_key', $key)->first(), $secret); if( $client != NULL ){ $lrs = \Lrs::find( $client->lrs_id ); } diff --git a/app/locker/repository/Lrs/EloquentLrsRepository.php b/app/locker/repository/Lrs/EloquentLrsRepository.php deleted file mode 100644 index 4d780aebd2..0000000000 --- a/app/locker/repository/Lrs/EloquentLrsRepository.php +++ /dev/null @@ -1,127 +0,0 @@ -lrs = $lrs; - } - - public function all(){ - if( \Auth::user()->role == 'super' ){ - return $this->lrs->all(); - }else{ - return $this->lrs->where('users._id', \Auth::user()->_id)->remember(10)->get(); - } - } - - public function find($id){ - return $this->lrs->find($id); - } - - public function validate($data){ - $lrs = new Lrs; - return $lrs->validate( $data ); - } - - public function create( $input ){ - - $user = \Auth::user(); - $lrs = new Lrs; - $lrs->title = $input['title']; - $lrs->description = $input['description']; - $lrs->api = array('basic_key' => \Locker\Helpers\Helpers::getRandomValue(), - 'basic_secret' => \Locker\Helpers\Helpers::getRandomValue()); - $lrs->owner = array( '_id' => \Auth::user()->_id ); - $lrs->users = array( array('_id' => $user->_id, - 'email' => $user->email, - 'name' => $user->name, - 'role' => 'admin' ) ); - - $lrs->save() ? $result = true : $return = false; - - //fire a create lrs event if it worked and saced - if( $result ) - \Event::fire('user.create_lrs', array('user' => $user)); - - return $result; - - } - - public function update($id, $input){ - - $lrs = $this->find($id); - - $lrs->title = $input['title']; - $lrs->description = $input['description']; - - $lrs->save(); - - return $lrs; - - } - - public function delete($id){ - - $lrs = $this->find($id); - - //first delete all statements - \Statement::where('lrs._id', $id)->delete(); - - //now delete the lrs - return $lrs->delete(); - } - - public function removeUser( $id, $user ){ - return $this->lrs->where('_id', $id)->pull('users', array('_id' => $user)); - } - - public function getLrsOwned( $user ){ - return $this->lrs->where('owner._id', $user)->select('title')->get()->toArray(); - } - - public function getLrsMember( $user ){ - return $this->lrs->where('users.user', $user)->select('title')->get()->toArray(); - } - - public function changeRole( $id, $user, $role ){ - - $lrs = $this->find($id); - $users = $lrs->users; - foreach($users as &$u){ - if( $u['_id'] == $user ){ - $u['role'] = $role; - } - } - $lrs->users = $users; - return $lrs->save(); - } - - /** - * Checks that the secret matches. - * Also used to authenticate client users. - * @param Illuminate\Database\Eloquent\Model $client - * @param string $secret - * @return Illuminate\Database\Eloquent\Model - */ - public static function checkSecret($client, $secret) { - if ($client !== null && $client->api['basic_secret'] === $secret) { - return $client; - } else { - return null; - } - } - -} \ No newline at end of file diff --git a/app/locker/repository/Lrs/EloquentRepository.php b/app/locker/repository/Lrs/EloquentRepository.php new file mode 100644 index 0000000000..62f85acbc9 --- /dev/null +++ b/app/locker/repository/Lrs/EloquentRepository.php @@ -0,0 +1,167 @@ + 'New LRS', + 'description' => '' + ]; + + /** + * Constructs a query restricted by the given options. + * @param [String => Mixed] $opts + * @return \Jenssegers\Mongodb\Eloquent\Builder + */ + protected function where(array $opts) { + return (new $this->model); + } + + /** + * Validates data. + * @param [String => Mixed] $data Properties to be changed on the model. + * @throws \Exception + */ + protected function validateData(array $data) { + if (isset($data['title'])) XAPIHelpers::checkType('title', 'string', $data['title']); + if (isset($data['description'])) XAPIHelpers::checkType('description', 'string', $data['description']); + if (isset($data['api'])) XAPIHelpers::checkType('api', 'array', $data['api']); + if (isset($data['api']['basic_key'])) XAPIHelpers::checkType('api.basic_key', 'string', $data['api']['basic_key']); + if (isset($data['api']['basic_secret'])) XAPIHelpers::checkType('api.basic_secret', 'string', $data['api']['basic_secret']); + if (isset($data['owner'])) XAPIHelpers::checkType('owner', 'array', $data['owner']); + if (isset($data['owner']['_id'])) XAPIHelpers::checkType('owner._id', 'string', $data['owner']['_id']); + if (isset($data['users'])) XAPIHelpers::checkType('users', 'array', $data['users']); + + // Validate users. + foreach ($data['users'] as $key => $field) { + XAPIHelpers::checkType("fields.$key", 'array', $field); + if (isset($field['_id'])) XAPIHelpers::checkType("fields.$key._id", 'string', $field['_id']); + if (isset($field['email'])) XAPIHelpers::checkType("fields.$key.email", 'string', $field['email']); + if (isset($field['name'])) XAPIHelpers::checkType("fields.$key.name", 'string', $field['name']); + if (isset($field['role'])) XAPIHelpers::checkType("fields.$key.role", 'string', $field['role']); + } + } + + /** + * Constructs a store. + * @param Model $model Model to be stored. + * @param [String => Mixed] $data Properties to be used on the model. + * @param [String => Mixed] $opts + * @return Model + */ + protected function constructStore(Model $model, array $data, array $opts) { + \Auth::user(); + // Merges and validates data with defaults. + $data = array_merge(array_merge($this->defaults, $data), [ + 'api' => [ + 'basic_key' => Helpers::getRandomValue(), + 'basic_secret' => Helpers::getRandomValue() + ], + 'owner' => [['_id'] => $opts['user']->_id], + 'users' => [[ + '_id' => $opts['user']->_id, + 'email' => $opts['user']->email, + 'name' => $opts['user']->name, + 'role' => 'admin' + ]] + ]); + $this->validateData($data); + + // Sets properties on model. + $model->title = $data['title']; + $model->description = $data['description']; + $model->api = $data['api']; + $model->owner = $data['owner']; + $model->users = $data['users']; + + Event::fire('user.create_lrs', array('user' => $user)); + + return $model; + } + + /** + * Constructs a update. + * @param Model $model Model to be updated. + * @param [String => Mixed] $data Properties to be changed on the model. + * @param [String => Mixed] $opts + * @return Model + */ + protected function constructUpdate(Model $model, array $data, array $opts) { + $this->validateData($data); + + // Sets properties on model. + if (isset($data['title'])) $model->title = $data['title']; + if (isset($data['description'])) $model->description = $data['description']; + + return $model; + } + + /** + * Gets all of the available models with the options. + * @param [String => Mixed] $opts + * @return [Model] + */ + public function index(array $opts) { + if ($opts['user']->role === 'super') { + return parent::index($opts); + } + + $query = $this->where('users._id', $opts['user']->_id)->remember(10); + return $query->get()->each(function (Model $model) { + return $this->format($model); + }); + } + + /** + * Destroys the model with the given ID and options. + * @param String $id ID to match. + * @param [String => Mixed] $opts + * @return Boolean + */ + public function destroy($id, array $opts) { + StatementModel::where('lrs._id', $id)->delete(); + return parent::destroy($id, $opts); + } + + public function removeUser($id, $user_id) { + return $this->where('_id', $id)->pull('users', ['_id' => $user_id]); + } + + public function getLrsOwned($user_id) { + return $this->where('owner._id', $user_id)->select('title')->get()->toArray(); + } + + public function getLrsMember($user_id) { + return $this->where('users._id', $user_id)->select('title')->get()->toArray(); + } + + public function changeRole($id, $user_id, $role) { + $lrs = $this->show($id, []); + $lrs->users = array_map(function ($user) use ($user_id, $role) { + $user['role'] = $user['_id'] === $user_id ? $role : $user['role']; + return $user; + }, $lrs->users); + return $lrs->save(); + } + + /** + * Checks that the secret matches. + * Also used to authenticate client users. + * @param Illuminate\Database\Eloquent\Model $client + * @param string $secret + * @return Illuminate\Database\Eloquent\Model + */ + public function checkSecret($client, $secret) { + if ($client !== null && $client->api['basic_secret'] === $secret) { + return $client; + } else { + return null; + } + } +} \ No newline at end of file diff --git a/app/locker/repository/Lrs/LrsRepository.php b/app/locker/repository/Lrs/LrsRepository.php deleted file mode 100644 index 385f8cdb1c..0000000000 --- a/app/locker/repository/Lrs/LrsRepository.php +++ /dev/null @@ -1,23 +0,0 @@ -app->bind( - 'Locker\Repository\Lrs\LrsRepository', - 'Locker\Repository\Lrs\EloquentLrsRepository' + 'Locker\Repository\Lrs\Repository', + 'Locker\Repository\Lrs\EloquentRepository' ); $this->app->bind( 'Locker\Repository\Client\ClientRepository',