Fetching statements from Learning Locker LRS using TinCan API - php

I am trying to execute queries on statements stored in Learning Locker via PHP and TinCanPHP API. Going by this answer, I was able to fetch a response from Learning Locker. Here is a part of the response:
TinCan\LRSResponse Object
(
[success] => 1
[content] => TinCan\StatementsResult Object
(
[statements:protected] => Array
(
[0] => TinCan\Statement Object
(
[id:protected] => 9ea9e6b6-8278-4545-a02c-c46113f3ba30
[stored:protected] => 2016-02-28T12:04:01.670600+00:00
[authority:protected] => TinCan\Agent Object
(
[objectType:protected] => Agent
[name:protected] => New Client
[mbox:protected] => mailto:hello#learninglocker.net
[mbox_sha1sum:protected] =>
[openid:protected] =>
[account:protected] =>
)
[version:protected] => 1.0.0
[attachments:protected] => Array
(
)
[actor:protected] => TinCan\Agent Object
(
[objectType:protected] => Agent
[name:protected] => Subhayan Roy
[mbox:protected] => mailto:subhayanroy5#gmail.com
[mbox_sha1sum:protected] =>
[openid:protected] =>
[account:protected] =>
)
[verb:protected] => TinCan\Verb Object
(
[id:protected] => http://activitystrea.ms/schema/1.0/search
[display:protected] => TinCan\LanguageMap Object
(
[_map:protected] => Array
(
[en-US] => Searched
)
)
)
The list of statements returned has permission protected, so I'm not being able to access them. How do I access the statements? What am I missing here?

You need to use the methods built into the library. In this case the one you want is getStatements.
$statementResult->content->getStatements();
See the documentation here: http://rusticisoftware.github.io/TinCanPHP/doc/api/latest/classes/TinCan.StatementsResult.html#method_getStatements
To answer the question which you will ask next (Why aren't I getting all statements returned in the result?), Take a look at the Statement Result getMore method which gives you the more URL and the Remote LRS moreStatements method which accepts a more URL and fetches the next batch of statements. See this code sample: https://github.com/garemoko/TinBadgesPHP/blob/b8789042f4af23f0f7927596e8e7f2a06655db72/TinBadges/RemoteLRS.php#L84-L96

Related

PHP read first item in object array

I have my code in PHP which is returning this Array of data:
GoCardlessPro\Core\ListResponse Object
(
[records] => Array
(
[0] => GoCardlessPro\Resources\Mandate Object
(
[model_name:protected] => Mandate
[created_at:protected] => 2017-04-01T16:49:09.642Z
[id:protected] => ID001
[links:protected] => stdClass Object
(
[customer_bank_account] => CB001
[creditor] => CR001
[customer] => CU001
)
[metadata:protected] => stdClass Object
(
)
[next_possible_charge_date:protected] => 2017-04-06
[payments_require_approval:protected] =>
[reference:protected] => RE001
[scheme:protected] => bacs
[status:protected] => active
[data:GoCardlessPro\Resources\BaseResource:private] => stdClass Object
(
[id] => 123
[created_at] => 2017-04-01T16:49:09.642Z
[reference] => RE001
[status] => active
[scheme] => bacs
[next_possible_charge_date] => 2017-04-06
[payments_require_approval] =>
[metadata] => stdClass Object
(
)
[links] => stdClass Object
(
[customer_bank_account] => 001
[creditor] => CR001
[customer] => CU001
)
)
[api_response] =>
)
)
)
I want to be able to read the ID of the first item in therecords array.
This data is contained inside a variable called $GC_Mandate;
I have tried these:
echo $GC_Mandate->records->{0}->id;
echo $GC_Mandate->records->0->id;
echo $GC_Mandate->records->[0]->id;
$GC_Mandate = $GC_Mandate->records;
echo $GC_Mandate->{0}->id;
But none will return the data
To get the first record, the syntax you need is $GC_Mandate->records[ 0 ].
However, that object is a GoCardlessPro\Resources\Mandate object and its member id is protected1, so we'd need to know the interface of GoCardlessPro\Resources\Mandate (its public methods1), to know if we can somehow retrieve the value of id.
My guess would be getId(), so the full syntax would become
$GC_Mandate->records[ 0 ]->getId()
But, that's just a guess. You'd have to look into the documentation/class definition of GoCardlessPro\Resources\Mandate, to be sure if you can retrieve id.
Turns out (provided I'm linking to the correct github repository) you can do:
$GC_Mandate->records[ 0 ]->id
since GoCardlessPro\Resources\Mandate extends GoCardlessPro\Resources\BaseResource, which exposes the protected members through GoCardlessPro\Resources\BaseResource::__get()2.
1. visibility in PHP
2. magic methods in PHP
I can't comment so I guess I'll have to post.
You should try to print_r($GC_Mandate); and see what it gives out and then go from there.
Try $GC_Mandate->records[0]->__get('id')
it will return all data ..for perticulat data put this in foreach loop
print_r($GC_Mandate['records']);

PHP's Solr plugin returning different spellcheck data

When I do a query (for example 'gluue') utilizing PHP's Solr plugin I get different spellcheck collation results then when using Solr's url based system.
http://xxx:8080/solr/example/browse?q=gluue%0A&wt=xml&spellcheck=true
returns :
<lst name="spellcheck"><lst name="suggestions"><lst name="gluue"><int name="numFound">10</int><int name="startOffset">0</int><int name="endOffset">5</int><arr name="suggestion"><str>glue</str><str>gauge</str><str>glide</str><str>glove</str><str>glued</str><str>globe</str><str>glaze</str><str>gluten</str><str>gauze</str><str>glues</str></arr></lst><lst name="collation"><str name="collationQuery">glue
</str><int name="hits">74</int><lst name="misspellingsAndCorrections"><str name="gluue">glue</str></lst></lst><lst name="collation"><str name="collationQuery">gauge
</str><int name="hits">6</int><lst name="misspellingsAndCorrections"><str name="gluue">gauge</str></lst></lst><lst name="collation"><str name="collationQuery">glide
</str><int name="hits">7</int><lst name="misspellingsAndCorrections"><str name="gluue">glide</str></lst></lst></lst></lst>
PHP plugin (replicating the query above exactly confirming within solrs log file)
[spellcheck] => SolrObject Object
(
[suggestions] => SolrObject Object
(
[gluue] => SolrObject Object
(
[numFound] => 10
[startOffset] => 0
[endOffset] => 5
[suggestion] => Array
(
[0] => glue
[1] => gauge
[2] => glide
[3] => glove
[4] => glued
[5] => globe
[6] => glaze
[7] => gluten
[8] => gauze
[9] => glues
)
)
[collation] => SolrObject Object
(
[collationQuery] => glide
[hits] => 7
[misspellingsAndCorrections] => SolrObject Object
(
[gluue] => glide
)
)
)
)
Note the 3 collationQuery values in the first response and only one in the PHP plugin.
The boss likes the first response ("glue") better so I'm going to have to bypass the plugin just for the spellcheck feature (and add an additional query to my system) because of this. Any ideas? I would really like to avoid this.

How to pull out certain value using php and this output

I've come across a weird scenario I do not know how to code around. I'm creating a JSON API for a wordpress site. I'm using the Connections plugin and trying to pull out the "original" image filename. The output of my sql command is this:
{
["options"]=>
string(396) "a:4:{s:5:"entry";a:1:{s:4:"type";s:12:"organization";}s:5:"group";a:1:{s:6:"family";a:0:{}}s:4:"logo";a:2:{s:6:"linked";b:0;s:7:"display";b:0;}s:5:"image";a:3:{s:6:"linked";b:1;s:7:"display";b:1;s:4:"name";a:4:{s:9:"thumbnail";s:25:"invoicelogo_thumbnail.jpg";s:5:"entry";s:21:"invoicelogo_entry.jpg";s:7:"profile";s:23:"invoicelogo_profile.jpg";s:8:"original";s:24:"invoicelogo_original.jpg";}}}"
}
}
I'm using the following command to acquire that:
querystr = "SELECT options FROM {$wpdb->prefix}connections WHERE id= '{$_GET['companyID']}'";
$options = $wpdb->get_results($querystr);
I'm not sure how to pull out the "original" part of this code though as it's not all that organized. Any help would be appreciated.
What you are seeing is the results of a php serialize call
To get at the original name just do this.
$decodedOptions = unserialize($options);
$original = $decodedOptions["image"]["name"]["original"];
Hope that helps
As a side note the deserialized data looks like
Array
(
[entry] => Array
(
[type] => organization
)
[group] => Array
(
[family] => Array
(
)
)
[logo] => Array
(
[linked] =>
[display] =>
)
[image] => Array
(
[linked] => 1
[display] => 1
[name] => Array
(
[thumbnail] => invoicelogo_thumbnail.jpg
[entry] => invoicelogo_entry.jpg
[profile] => invoicelogo_profile.jpg
[original] => invoicelogo_original.jpg
)
)
)

Magento Event / Observer Object getData problems

I have a Magento Module I built that allows you to save a string via the admin interface to the core config table in Magento. I have an observer setup to run a method when the string is saved in the backend. I'm killing myself trying to intercept the string and encode it before saving it to the database.
So when my event is triggered it runs this :
public function myModSaved($observer)
{
echo "<h1> WOWSERS IT ACTUALLY WORKED!!</h1>";
$data = $observer->getData();
print_r($data);
}
The output looks like this:
Array ( [event] => Varien_Event Object ( [_observers:protected] => Varien_Event_Observer_Collection Object ( [_observers:protected] => Array ( ) ) [_data:protected] => Array ( [website] => [store] => [name] => admin_system_config_changed_section_mymodule_section ) [_hasDataChanges:protected] => [_origData:protected] => [_idFieldName:protected] => [_isDeleted:protected] => [_oldFieldsMap:protected] => Array ( ) [_syncFieldsMap:protected] => Array ( ) ) [website] => [store] => )
Now there is only one string being written to the database, how do I get that string before it is saved, then add my new modified string to the object so that one saves in the DB?
Thanks in advance!
In your system.xml file where the config field is defined add this declaration:
<backend_model>adminhtml/system_config_backend_encrypted</backend_model>
That will take care of all the encoding/decoding both before and after the database access. Also to get the 'password' type field it is customary to use this:
<frontend_type>obscure</frontend_type>

how to get to a specific part of this nested array/object

This is driving me crazy, I am trying to get to a specific part of this object and it is driving me crazy, here is the object contents:
XMLHandler Object
(
[doc:XMLHandler:private] => SimpleXMLElement Object
(
[#attributes] => Array
(
[state] => Live
)
[newsListItem] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://api.contentplus.co.uk/6cb5ea15-d6b1-4c40-9db7-cb2a3315080b/news/800773226/
)
[id] => 800773226
[publishDate] => 2011-10-24T10:04:49
[lastModifiedDate] => 2011-10-24T11:20:40
[headline] => Relationships matter on social media
)
)
)
[format] => html
)
I want to get the value of [id] I am trying to access it like this:
echo $niList->doc->newsListItem[0]->id;
but this is giving me nothing, I know I am close (well I hope I am) but I just cant quite get it right, could anyone help please.
Thanks all.
Your object dump says
doc:XMLHandler:private
which means doc is a private property of XMLHandler. As such, you can only access it from within that object via $this. But you are trying to access it from outside the object when you do
echo $niList->newsListItem[0]->id;
This wont work. Add a method to that XMLHandler object that does what you want to do with that newslistitem id. Also see the chapter on Visibility in the PHP Manual:
http://docs.php.net/manual/en/language.oop5.visibility.php

Categories