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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ There are several ways you can get involved with Learning Locker. Visit our site

* HT2 (http://ht2.co.uk)
* Dave Tosh (http://davetosh.com)
* James Mullaney [mailto:james@ht2.co.uk](james@ht2.co.uk)

#### Copyright

Expand Down
16 changes: 5 additions & 11 deletions app/controllers/xapi/ActivityController.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
<?php namespace Controllers\xAPI;

use \Locker\Repository\Document\DocumentRepository as Document;
use Locker\Repository\Document\DocumentType as DocumentType;

class ActivityController extends BaseController {

/**
* Document Repository
*/
protected $document;
class ActivityController extends DocumentController {

/**
* Construct
*
* @param DocumentRepository $document
*/
public function __construct(Document $document){

$this->document = $document;
$this->beforeFilter('@getLrs');

parent::__construct($document);
$this->document_type = DocumentType::ACTIVITY;
}

/**
Expand Down Expand Up @@ -47,7 +41,7 @@ public function store(){
//validate
if( $this->validate( $profile ) && $this->validateActivity( $profile['contents'] ) ){

$store = $this->document->store( $this->lrs->_id, $profile['id'], $profile['contents'], 'activityProfile' );
$store = $this->document->store( $this->lrs->_id, $profile['id'], $profile['contents'], $this->document_type );

if( $store ){
return \Response::json( array( 'ok', 204 ) );
Expand Down
85 changes: 85 additions & 0 deletions app/controllers/xapi/DocumentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php namespace Controllers\xAPI;

use \Locker\Repository\Document\DocumentRepository as Document;

class DocumentController extends BaseController {

/**
* Document Repository
*/
protected $document, $document_type;

/**
* Construct
*
* @param DocumentRepository $document
*/
public function __construct(Document $document){
$this->document = $document;
$this->beforeFilter('@getLrs');
$this->beforeFilter('@setParameters');
}

/**
* Used to filter required paramters on an incoming request
* @param array $required a list of expected parameters and allows types
*
*/
public function checkParams( $required = array(), $optional=array(), $data = null ){

$return_data = array();

if( is_null($data) ){
$data = $this->params;
}

//loop through all required parameters
foreach( $required as $name=>&$expected_types ){
//check the parameter has been passed
if( !isset($data[$name]) ){
\App::abort(400, 'Required parameter is missing - '.$name );
}

if( !empty($expected_types) ){
$this->checkTypes( $name, $data[$name], $expected_types );
}

$return_data[$name] = $data[$name];
}

foreach( $optional as $name=>$expected_types ){
if( isset($data[$name]) ){
if( !empty($expected_types) ){
$this->checkTypes( $name, $data[$name], $expected_types );
}

$return_data[$name] = $data[$name];
}
}

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);

//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 ) );
}

//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 ) );
}
}
}



}
75 changes: 27 additions & 48 deletions app/controllers/xapi/StateController.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<?php namespace Controllers\xAPI;

use \Locker\Repository\Document\DocumentRepository as Document;
use Locker\Repository\Document\DocumentType as DocumentType;

class StateController extends BaseController {

/**
* Document Repository
*/
protected $document;
class StateController extends DocumentController {

/**
* Construct
Expand All @@ -16,8 +12,9 @@ class StateController extends BaseController {
*/
public function __construct(Document $document){

$this->document = $document;
$this->beforeFilter('@getLrs');
parent::__construct($document);

$this->document_type = DocumentType::STATE;

}

Expand All @@ -30,11 +27,13 @@ public function __construct(Document $document){
* @return Response
*/
public function index(){
$data = $this->checkParams(array(
'activityId' => 'string',
'actor' => array('string', 'json'),
), $this->params );

$documents = $this->document->all();

$documents = $this->document->all( $this->lrs->_id, $this->document_type, $data['activityId'], $data['actor'] );
return \Response::json( $documents->toArray() );

}

/**
Expand All @@ -44,25 +43,26 @@ public function index(){
*/
public function store(){

$request = \Request::instance();
$incoming_data = $request->getContent();

//convert to array
$state = json_decode($incoming_data, TRUE);
$state = $this->checkParams(
array(
'activityId' => 'string',
'actor' => array('string', 'json'),
'stateId' => 'string',
'content' => ''
),
array(
'registration' => 'string'
),
$this->params
);

//validate
if( $this->validate( $state ) && $this->validateState( $state['contents'] ) ){

$store = $this->document->store( $this->lrs->_id, $state['id'], $state['contents'], 'state' );

if( $store ){
return \Response::json( array( 'ok', 204 ) );
}
$store = $this->document->store( $this->lrs->_id, $state, $this->document_type );

if( $store ){
return \Response::json( array( 'ok', 204 ) );
}

return \Response::json( array( 'error', 400 ) );

}

/**
Expand All @@ -75,7 +75,7 @@ public function show( $stateId ){

$document = $this->document->find( $this->lrs->_id, $stateId );

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

}

Expand Down Expand Up @@ -109,29 +109,8 @@ public function destroy($id)
**/
public function validateState( $data ){

//now check required keys exist
if( !array_key_exists('activityId', $data)
|| !array_key_exists('actor', $data)
|| !array_key_exists('stateId', $data)){
return false;
}

//check activityId is string
if( !is_string( $data['activityId'] ) ){
return false;
}

//check actor is array
if( !is_array( $data['actor'] ) ){
return false;
}

//check stateId is string
if( !is_string( $data['stateId'] ) ){
return false;
}

return true;


}

Expand Down
4 changes: 2 additions & 2 deletions app/locker/repository/Document/DocumentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

interface DocumentRepository {

public function store( $lrs, $id, $contents, $apitype );
public function store( $lrs, $data, $documentType );

public function find( $lrs, $stateId );

public function all( $lrs, $activityid );
public function all( $lrs, $documentType, $activityId, $actor );

}
7 changes: 7 additions & 0 deletions app/locker/repository/Document/DocumentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php namespace Locker\Repository\Document;

class DocumentType{
const STATE = 'state';
const ACTIVITY = 'activityProfile';
const AGENT = 'agentProfile';
}
38 changes: 31 additions & 7 deletions app/locker/repository/Document/EloquentDocumentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public function __construct( DocumentAPI $documentapi ){

}

public function all( $lrs, $activityId ){
public function all( $lrs, $documentType, $activityId, $actor ){
return $this->documentapi->where('lrs', $lrs)
->where('contents.activityId', $activityId)
->select('contents.stateId')
->where('documentType', $documentType)
->where('activityId', $activityId)
->where('actor', json_decode($actor))
->select('stateId')
->get();
}

Expand All @@ -33,14 +35,36 @@ public function find( $lrs, $stateId ){
->first();
}

public function store( $lrs, $id, $contents, $apitype ){
public function store( $lrs, $data, $documentType ){

$new_document = $this->documentapi;
$new_document->lrs = $lrs; //LL specific
$new_document->apitype = $apitype; //LL specific
$new_document->id = $id;
$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->stateId = $data['stateId'];
$new_document->registration = isset($data['registration']) ? $data['registration'] : null;
break;
case DocumentType::ACTIVITY:
$new_document->activityId = $data['activityId'];
$new_document->profileId = $data['profileId'];
break;
case DocumentType::AGENT:

break;
}


//@todo add update as per spec https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#miscdocument
$new_document->contents = $contents;
if( is_object( json_decode($data['content'] ) ) ){
$new_document->content = json_decode($data['content']);
} else {
$new_document->content = "..path/to/file/to.do";
}


if( $new_document->save() ){
return true;
Expand Down
5 changes: 5 additions & 0 deletions composer-public.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"name": "HT2",
"email": "hello@ht2.co.uk",
"role": "sponsor"
},
{
"name": "James Mullaney",
"email": "james@ht2.co.uk",
"role": "Developer"
}
],
"name": "learninglocker/learninglocker",
Expand Down