From 424d46107740adc67bea364b8d4e95c18e1f7dab Mon Sep 17 00:00:00 2001 From: James Mullaney Date: Tue, 11 Feb 2014 16:23:01 +0000 Subject: [PATCH 1/2] State index method --- app/controllers/xapi/DocumentController.php | 27 +++++++------- app/controllers/xapi/StateController.php | 7 ++-- .../Document/EloquentDocumentRepository.php | 35 ++++++++++++++++--- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/app/controllers/xapi/DocumentController.php b/app/controllers/xapi/DocumentController.php index 182350f925..a8df834b7f 100644 --- a/app/controllers/xapi/DocumentController.php +++ b/app/controllers/xapi/DocumentController.php @@ -47,7 +47,7 @@ public function checkParams( $required = array(), $optional=array(), $data = nul $return_data[$name] = $data[$name]; } - foreach( $optional as $name=>$expected_types ){ + foreach( $optional as $name=>&$expected_types ){ if( isset($data[$name]) ){ if( !empty($expected_types) ){ $this->checkTypes( $name, $data[$name], $expected_types ); @@ -61,23 +61,24 @@ public function checkParams( $required = array(), $optional=array(), $data = nul } public function checkTypes($name, $value, $expected_types ){ + //convert expected type string into array - if( is_string($expected_types) ) $expected_types = array($expected_types); + $expected_types = ( is_string($expected_types) ) ? array($expected_types) : $expected_types; - //get the paramter type - $type = gettype($value); + //get the paramter type + $type = gettype($value); - //error on any unexpected parameter types - if( !in_array( $type, $expected_types ) ){ - \App::abort(400, sprintf( "`%s` is not an accepted type - expected %s - received %s", $name, implode(',', $expected_types), $type ) ); - } + //error on any unexpected parameter types + if( !in_array( $type, $expected_types ) ){ + \App::abort(400, sprintf( "`%s` is not an accepted type - expected %s - received %s", $name, implode(',', $expected_types), $type ) ); + } - //Check if we haev requested a JSON parameter - if( in_array('json', $expected_types ) ){ - if( !is_object( json_decode($value) ) ){ - \App::abort(400, sprintf( "`%s` is not an accepted type - expected a JSON formatted string", $name ) ); - } + //Check if we haev requested a JSON parameter + if( in_array('json', $expected_types ) ){ + if( !is_object( json_decode($value) ) ){ + \App::abort(400, sprintf( "`%s` is not an accepted type - expected a JSON formatted string", $name ) ); } + } } diff --git a/app/controllers/xapi/StateController.php b/app/controllers/xapi/StateController.php index 923553d544..90e2720841 100644 --- a/app/controllers/xapi/StateController.php +++ b/app/controllers/xapi/StateController.php @@ -29,10 +29,11 @@ public function __construct(Document $document){ public function index(){ $data = $this->checkParams(array( 'activityId' => 'string', - 'actor' => array('string', 'json'), - ), $this->params ); + 'actor' => array('string', 'json') + ), array(), $this->params ); + + $documents = $this->document->all( $this->lrs->_id, $this->document_type, $data['activityId'], json_decode($data['actor']) ); - $documents = $this->document->all( $this->lrs->_id, $this->document_type, $data['activityId'], $data['actor'] ); return \Response::json( $documents->toArray() ); } diff --git a/app/locker/repository/Document/EloquentDocumentRepository.php b/app/locker/repository/Document/EloquentDocumentRepository.php index b20f83d53f..692cdf6ce5 100644 --- a/app/locker/repository/Document/EloquentDocumentRepository.php +++ b/app/locker/repository/Document/EloquentDocumentRepository.php @@ -20,13 +20,38 @@ public function __construct( DocumentAPI $documentapi ){ } + public function all( $lrs, $documentType, $activityId, $actor ){ - return $this->documentapi->where('lrs', $lrs) + $query = $this->documentapi->where('lrs', $lrs) ->where('documentType', $documentType) - ->where('activityId', $activityId) - ->where('actor', json_decode($actor)) - ->select('stateId') - ->get(); + ->where('activityId', $activityId); + + + //Do some checking on what actor field we are filtering with + if( isset($actor->mbox) ){ //check for mbox + $actor_query = array('field' => 'actor.mbox', 'value'=>$actor->mbox); + } else if( isset($actor->mbox_sha1sum) ) {//check for mbox_sha1sum + $actor_query = array('field' => 'actor.mbox_sha1sum', 'value'=>$actor->mbox_sha1sum); + } else if( isset($actor->openid) ){ //check for open id + $actor_query = array('field' => 'actor.openid', 'value'=>$actor->openid); + } + + if( isset($actor_query) ){ //if we have actor query params lined up... + $query = $query->where( $actor_query['field'], $actor_query['value'] ); + + } else if( isset($actor->account) ){ //else if there is an account + if( isset($actor->account->homePage) && isset($actor->account->name ) ){ + $query = $query->where('actor.account.homePage', $actor->account->homePage) + ->where('actor.account.name', $actor->account->name ); + } else { + \App::abort(400, 'Missing required paramaters in the actor.account'); + } + + } else { + \App::abort(400, 'Missing required paramaters in the actor'); + } + + return $query->select('stateId')->get(); } public function find( $lrs, $stateId ){ From acc4ed0fce25d5f0ba30364be3b24aa53669dcd4 Mon Sep 17 00:00:00 2001 From: James Mullaney Date: Tue, 11 Feb 2014 16:38:48 +0000 Subject: [PATCH 2/2] Allow param validator to handle object casts. Add content type to documents --- app/controllers/xapi/DocumentController.php | 19 +++++++++++++------ app/controllers/xapi/StateController.php | 2 +- .../Document/EloquentDocumentRepository.php | 17 ++++++++++++----- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/controllers/xapi/DocumentController.php b/app/controllers/xapi/DocumentController.php index a8df834b7f..a55720e4ed 100644 --- a/app/controllers/xapi/DocumentController.php +++ b/app/controllers/xapi/DocumentController.php @@ -41,19 +41,23 @@ public function checkParams( $required = array(), $optional=array(), $data = nul } if( !empty($expected_types) ){ - $this->checkTypes( $name, $data[$name], $expected_types ); - } + $value = $this->checkTypes( $name, $data[$name], $expected_types ); + } else { + $value = $data[$name]; + } - $return_data[$name] = $data[$name]; + $return_data[$name] = $value; } foreach( $optional as $name=>&$expected_types ){ if( isset($data[$name]) ){ if( !empty($expected_types) ){ - $this->checkTypes( $name, $data[$name], $expected_types ); + $value = $this->checkTypes( $name, $data[$name], $expected_types ); + } else { + $value = $data[$name]; } - $return_data[$name] = $data[$name]; + $return_data[$name] = $value; } } @@ -75,10 +79,13 @@ public function checkTypes($name, $value, $expected_types ){ //Check if we haev requested a JSON parameter if( in_array('json', $expected_types ) ){ - if( !is_object( json_decode($value) ) ){ + $value = json_decode($value); + if( !is_object( $value ) ){ \App::abort(400, sprintf( "`%s` is not an accepted type - expected a JSON formatted string", $name ) ); } } + + return $value; } diff --git a/app/controllers/xapi/StateController.php b/app/controllers/xapi/StateController.php index 90e2720841..79a600dfb1 100644 --- a/app/controllers/xapi/StateController.php +++ b/app/controllers/xapi/StateController.php @@ -32,7 +32,7 @@ public function index(){ 'actor' => array('string', 'json') ), array(), $this->params ); - $documents = $this->document->all( $this->lrs->_id, $this->document_type, $data['activityId'], json_decode($data['actor']) ); + $documents = $this->document->all( $this->lrs->_id, $this->document_type, $data['activityId'], $data['actor'] ); return \Response::json( $documents->toArray() ); } diff --git a/app/locker/repository/Document/EloquentDocumentRepository.php b/app/locker/repository/Document/EloquentDocumentRepository.php index 692cdf6ce5..5da0eee3da 100644 --- a/app/locker/repository/Document/EloquentDocumentRepository.php +++ b/app/locker/repository/Document/EloquentDocumentRepository.php @@ -63,13 +63,13 @@ public function find( $lrs, $stateId ){ public function store( $lrs, $data, $documentType ){ $new_document = $this->documentapi; - $new_document->lrs = $lrs; //LL specific + $new_document->lrs = $lrs; //LL specific $new_document->documentType = $documentType; //LL specific switch( $new_document->documentType ){ case DocumentType::STATE: $new_document->activityId = $data['activityId']; - $new_document->actor = json_decode($data['actor']); + $new_document->actor = $data['actor']; $new_document->stateId = $data['stateId']; $new_document->registration = isset($data['registration']) ? $data['registration'] : null; break; @@ -84,10 +84,17 @@ public function store( $lrs, $data, $documentType ){ //@todo add update as per spec https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#miscdocument - if( is_object( json_decode($data['content'] ) ) ){ + if( is_object( json_decode($data['content'] ) ) ){ //save json as an object + $new_document->contentType = 'application/json'; $new_document->content = json_decode($data['content']); - } else { - $new_document->content = "..path/to/file/to.do"; + } else if( is_string($data['content']) ){ //save text as raw text + $new_document->contentType = 'text/plain'; + $new_document->content = $data['content']; + } else { + //TODO - save file content and reference through file path + //Need to actually check this is a binary file still + $new_document->contentType = 'file/mimetype'; //use this value to return an actual file when requesting the document - may want to use mimetype here? + $new_document->content = "..path/to/file/to.do"; }