Guide:PHP Simple Learning Record Store (LRS) With Tin Can API

Script for Self hosted Learning Record Store Supporting Tin Can API

This guide was originally posted at http://www.tincanapi.co.uk/wiki/Guide:PHP_Simple_LRS . Due to an error this site is not accessible now. I collected this guide form a google cached data. I post this content here just for my personal purpose. Now the main author posted his codes on gitHub Project PHPCan


Tin Can API Logo

current product status

The LRS is receiving statements via POST (and PUT – untested), generating a statement ID and stored time-stamp and storing these with the raw statement in a 3 field database.

Next Steps

  • json decode statement to add statement ID and stored fields if they are not present, then re-encode to store.
  • Ensure no duplicate statement IDs and return appropriate http headers in event of conflict
  • test PUT
  • Implement GET for statements (single and all)
  • State
  • Actor and Activity profiles
  • dot Ts and cross Is of compliance with spec

notes for guide and code

Notes

http://www.slimframework.com/

SQL

CREATE TABLE lrs_statements
(
statementID VARCHAR(255) NOT NULL COLLATE utf8_general_ci,
statement TEXT NOT NULL COLLATE utf8_general_ci,
stored TIMESTAMP NOT NULL,
PRIMARY KEY (statementID)
)

PHP code

<!--?php require 'Slim/Slim.php';   $app = new Slim(); //POST route $app--->post('/public', 'lrsPost');

//PUT route
$app->put('/public/:id', 'lrsPut');

$app->run();

function lrsPost(){
$DBConnectionArray = returnDBConnectionArray();

    $inputStatement= file_get_contents("php://input");

//decode the JSON as an associative array.
$statementObj = json_decode($inputStatement, TRUE);

//Stored
$dateNow = new DateTime();
$stored = $dateNow->format('Y-m-d H:i:s');
$statementObj[0]['stored'] = $stored;

//Add a statement ID if it doesn't already exist or if it's NULL. If it does exist, use the existing one.
$statementID;
if (isset($statementObj[0]['id']))
{
$statementID = $statementObj[0]['id'];
}
else
{
$statementID = make_uuid();
$statementObj[0]['id'] = $statementID;
}

//re-encode the statement
$statement = json_encode($statementObj);

$query = "INSERT INTO lrs_statements VALUES ('".$statementID."','".$statement."','".$stored."')";

mysql_connect($DBConnectionArray["DBserver"],$DBConnectionArray["DBuser"], $DBConnectionArray["DBpassword"] );

mysql_select_db($DBConnectionArray["DBname"]) or die( "Unable to select database");

mysql_query($query);

mysql_close();
}

function lrsPut(){
$DBConnectionArray = returnDBConnectionArray();

    $statement= file_get_contents("php://input");
$date = new DateTime();

$query = "INSERT INTO lrs_statements VALUES ('".make_uuid()."','".$statement."','".$date->format('Y-m-d H:i:s')."')";

mysql_connect($DBConnectionArray["DBserver"],$DBConnectionArray["DBuser"], $DBConnectionArray["DBpassword"] );

mysql_select_db($DBConnectionArray["DBname"]) or die( "Unable to select database");

mysql_query($query);

mysql_close();
}

function make_uuid() {

    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}

function returnDBConnectionArray()
{
$array = array(
    "DBtype" => "mysql",
"DBserver" => "cust-mysql-123-05",
"DBname" => "tincanapicouk_715945_db1",
"DBuser" => "utincan_715945_1",
"DBpassword" => "Taztaz1",
);
return $array;
}

?>

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/LRS/
RewriteRule ^/?LRS(/.*)?$ %{DOCUMENT_ROOT}/LRS/index.php [QSA,L]

This Post May Be The Answer of Following Queries:

  • A Basic LRS for TinCanAPI
  • Self Hosted LRS example
  • Self hosted Learning Record Store Script
  • Simple LRS example with PHP
  • Build your own Learning Record Store (LRS)
  • PHP Mysql basic starter script for LRS
  • Tutorial to build TinCanAPI LRS
  • TinCanAPI Learning Record Store Script

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.