Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions app/controllers/xapi/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,51 @@ 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 ){
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;
}
}

return $return_data;
}

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 ) ){
$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;
}


Expand Down
5 changes: 3 additions & 2 deletions app/controllers/xapi/StateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], $data['actor'] );

return \Response::json( $documents->toArray() );
}

Expand Down
52 changes: 42 additions & 10 deletions app/locker/repository/Document/EloquentDocumentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ){
Expand All @@ -38,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;
Expand All @@ -59,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";
}


Expand Down