Grant_Bailey Grant_Bailey - 1 month ago 18
PHP Question

Tin Can PHP library: processing results of query

I am trying to extract the name of an actor agent from LRS records using the Tin Can PHP library. I only have the mbox value (email address) of the person so my retrieval attempt proceeds like this:

$actor = new TinCan\Agent();
$actor
->setMbox('mailto:bob@downe.com');

// return raw statement
$retrieve = $lrs->queryStatements(['agent' => $actor]);


If I print out the value of
$retrieve
I get the following raw statement (truncated for brevity):

TinCan\LRSResponse Object (
[success] => 1
[content] => TinCan\StatementsResult Object
(
[statements:protected] => Array
(
[0] => TinCan\Statement Object
(
[id:protected] => 4c707377-384d-4547-a858-61696b386b6d
[stored:protected] => 2016-10-24T15:57:43.358Z
[authority:protected] => TinCan\Agent Object
(
[objectType:protected] => Agent
[name:protected] => Grant
[mbox:protected] =>
[mbox_sha1sum:protected] =>
[openid:protected] =>
[account:protected] => TinCan\AgentAccount Object
(
[name:protected] => ###
[homePage:protected] => http://cloud.scorm.com/
)

)

[version:protected] => 1.0.0
[attachments:protected] => Array
(
)

[actor:protected] => TinCan\Agent Object
(
[objectType:protected] => Agent
[name:protected] => Bob Downe
[mbox:protected] => mailto:bob@downe.com
[mbox_sha1sum:protected] =>
[openid:protected] =>
[account:protected] =>
)


I then try to extract the name from the raw statement as follows:

// take content from raw statements using getStatements() method
$further_output = $retrieve->content->getStatements();


This produces an array of objects of the Statement class, in this case an array of one value.

I then have to get the object out of the array somehow in order to access the methods used to extract the information I want. This is how I have done it:

// Get actor out of object
$extracted = $further_output[0]->getActor()->getName();
echo "<p>$extracted</p>"; // produces 'Bob Downe'


This seems very inefficient and I am sure that there must be a better way of doing it.

I have two questions:


  1. What is the most efficient way of extracting the information that I want?

  2. Why does the raw statement display 'protected' for each of the properties e.g. [statements:protected], [id:protected], [stored:protected] etc?



I have studied these links of relevance but they have not resolved my issue:

how to execute a query on tin-can statements

Fetching statements from Learning Locker LRS using TinCan API

I would be grateful for any assistance.

Answer
  1. This is the most efficient means of extracting that specific piece of information with the library. Is it just the public interface that you feel is inefficient? What would be more "efficient"? Note that you need to be doing error checking, in other words check that the request was successful, that there are statements in the array, and that the name property is defined. The library can't know these things ahead of time, and by design is considered a low level interface for communicating with the LRS.

  2. This is fundamental OOP development, see http://php.net/manual/en/language.oop5.visibility.php or Google "OOP protected". Ultimately in TinCanPHP you access the data via public methods so that we can maintain encapsulation.

Comments