Skip to content

Commit

Permalink
XAPI: init plugin - refs BT#16742
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Nov 27, 2020
1 parent a129d2a commit 7586546
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugin/xapi/install.php
@@ -0,0 +1,4 @@
<?php
/* For licensing terms, see /license.txt */

XApiPlugin::create()->install();
14 changes: 14 additions & 0 deletions plugin/xapi/lang/english.php
@@ -0,0 +1,14 @@
<?php
/* For licensing terms, see /license.txt */

$strings['plugin_title'] = 'Experience API (xAPI)';
$strings['plugin_comment'] = 'Allow to incorporate an Learning Record Store and clients to xAPI';

$strings[XApiPlugin::SETTING_UUID_NAMESPACE] = 'UUID Namespace';
$strings[XApiPlugin::SETTING_UUID_NAMESPACE.'_help'] = 'Namespace for universally unique identifiers used as statement IDs.'
.'<br>This is automatically by Chamilo LMS. <strong>Don\'t replace it.</strong>';
$strings[XApiPlugin::SETTING_LRS_URL] = 'LRS: URL for API';
$strings[XApiPlugin::SETTING_LRS_URL.'_help'] = 'Sets the LRS base URL.';
$strings[XApiPlugin::SETTING_LRS_AUTH] = 'LRS: Authentication method';
$strings[XApiPlugin::SETTING_LRS_AUTH.'_help'] = 'Sets HTTP authentication credentials.<br>';
$strings[XApiPlugin::SETTING_LRS_AUTH.'_help'] .= 'Choose one auth method: Basic (<code>basic:username:password</code>) or OAuth1 (<code>oauth:key:secret</code>)';
4 changes: 4 additions & 0 deletions plugin/xapi/plugin.php
@@ -0,0 +1,4 @@
<?php
/* For licensing terms, see /license.txt */

$plugin_info = XApiPlugin::create()->get_info();
142 changes: 142 additions & 0 deletions plugin/xapi/src/XApiPlugin.php
@@ -0,0 +1,142 @@
<?php
/* For licensing terms, see /license.txt */

use GuzzleHttp\RequestOptions;
use Http\Adapter\Guzzle6\Client;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Ramsey\Uuid\Uuid;
use Xabbuh\XApi\Client\XApiClientBuilder;
use Xabbuh\XApi\Client\XApiClientBuilderInterface;

/**
* Class XApiPlugin.
*/
class XApiPlugin extends Plugin
{
const SETTING_LRS_URL = 'lrs_url';
const SETTING_LRS_AUTH = 'lrs_auth';
const SETTING_UUID_NAMESPACE = 'uuid_namespace';

/**
* XApiPlugin constructor.
*/
protected function __construct()
{
$version = '0.1';
$author = [
'Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>',
];
$settings = [
self::SETTING_UUID_NAMESPACE => 'text',
self::SETTING_LRS_URL => 'text',
self::SETTING_LRS_AUTH => 'text',
];

parent::__construct(
$version,
implode(', ', $author),
$settings
);
}

/**
* @return \XApiPlugin
*/
public static function create()
{
static $result = null;

return $result ? $result : $result = new self();
}

/**
* Process to install plugin.
*/
public function install()
{
$this->installUuid();
}

/**
* @throws \Exception
*/
private function installUuid()
{
$uuidNamespace = Uuid::uuid1();

$pluginName = $this->get_name();
$urlId = api_get_current_access_url_id();

api_add_setting(
$uuidNamespace,
$pluginName.'_'.self::SETTING_UUID_NAMESPACE,
$pluginName,
'setting',
'Plugins',
$pluginName,
'',
'',
'',
$urlId,
1
);
}

/**
* Process to uninstall plugin.
*/
public function uninstall()
{
}

/**
* @return \Xabbuh\XApi\Client\Api\StatementsApiClientInterface
*/
public function getXApiStatementClient()
{
return $this->createXApiClient()->getStatementsApiClient();
}

/**
* @return \Xabbuh\XApi\Client\XApiClientInterface
*/
public function createXApiClient()
{
$baseUrl = trim($this->get(self::SETTING_LRS_URL), "/ \t\n\r\0\x0B");

$clientBuilder = new XApiClientBuilder();
$clientBuilder
->setHttpClient(Client::createWithConfig([RequestOptions::VERIFY => false]))
->setRequestFactory(new GuzzleMessageFactory())
->setBaseUrl($baseUrl);

return $this
->setAuthMethodToClient($clientBuilder)
->build();
}

/**
* @param \Xabbuh\XApi\Client\XApiClientBuilderInterface $clientBuilder
*
* @return \Xabbuh\XApi\Client\XApiClientBuilderInterface
*/
private function setAuthMethodToClient(XApiClientBuilderInterface $clientBuilder)
{
$authString = $this->get(self::SETTING_LRS_AUTH);

$parts = explode(':', $authString);

if (!empty($parts)) {
$method = strtolower($parts[0]);

switch ($method) {
case 'basic':
return $clientBuilder->setAuth($parts[1], $parts[2]);
case 'oauth':
return $clientBuilder->setOAuthCredentials($parts[1], $parts[2]);
}
}

return $clientBuilder;
}
}
4 changes: 4 additions & 0 deletions plugin/xapi/uninstall.php
@@ -0,0 +1,4 @@
<?php
/* For licensing terms, see /license.txt */

XApiPlugin::create()->uninstall();

0 comments on commit 7586546

Please sign in to comment.