Hello! π
And finally, a continuation to the part 1
The component code can be downloaded from the repository
It probably looks a little horrible, but there's nothing difficult here.
On the administration side it looks like this:
And settings for Accredible Credential API:
Group settings are necessary because the certificate or badge design is implemented in the group. You can create a group through the API, but then you will need to additionally draw a design on the Accredible side.
I will describe the implementation of the component for Joomla in the following posts. Here I will only describe the points for working with the API.
The main file for working with the API Accredible Credential is found at:
src\Helper\AccrediblecertificateHelper.php
namespace Joomla\Component\Accrediblecertificate\Administrator\Helper;
use Joomla\CMS\Helper\ContentHelper;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Accredible Certificate component helper.
*
* @since 1.6
*/
class AccrediblecertificateHelper extends ContentHelper
{
private $api_key;
private $api_endpoint = "https://api.accredible.com/v1/";
/**
* Set API Key
* @param String $key
* @return null
*/
public function setAPIKey($key) {
$this->api_key = $key;
}
/**
* Get API Key
* @return String
*/
public function getAPIKey() {
return $this->api_key;
}
/**
* Contruct API instance
* @param String $api_key
* @param boolean|null $test
* @return null
*/
public function __construct($api_key, $test = null){
$this->setAPIKey($api_key);
if (null !== $test) {
//$this->api_endpoint = "https://staging.accredible.com/v1/";
}
}
/**
* Creates a Credential given an existing Group
* @param String $recipient_name
* @param String $recipient_email
* @param String $course_id
* @param Date|null $issued_on
* @param Date|null $expired_on
* @param stdObject|null $custom_attributes
* @return stdObject
*/
public function create_credential($recipient_name, $recipient_email, $course_id, $issued_on = null, $expired_on = null, $custom_attributes = null){
$data = array(
"credential" => array(
"group_id" => $course_id,
"recipient" => array(
"name" => $recipient_name,
"email" => $recipient_email
),
"issued_on" => $issued_on,
"expired_on" => $expired_on,
"custom_attributes" => $custom_attributes
)
);
return json_decode($this->execAccredible($this->getAPIKey(), 'credentials', json_encode($data)), true);
}
public function execAccredible($key, $url, $params=array()){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->api_endpoint.$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
if(!empty($params)){
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Token token=".$key
));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}
To connect, I use a regular curl, so that there is no need to connect additional libraries.
The API Accredible feature set is very large, but the certificate creation function is enough for me at the moment.
I use this file (AccrediblecertificateHelper.php) in the plugin to create the Certificate:
<?php
namespace Joomla\Plugin\User\Accregistration\Extension;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Date\Date;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Component\Accrediblecertificate\Administrator\Helper\AccrediblecertificateHelper;
use Joomla\CMS\Component\ComponentHelper;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
final class Accregistration extends CMSPlugin
{
use DatabaseAwareTrait;
public function onUserAfterSave($data, $isNew, $result, $error): void
{
if ($isNew && $result ) {
$config = ComponentHelper::getParams('com_accrediblecertificate');
$default_group_id = $config->get('default_group_id', '546130');
$api = new AccrediblecertificateHelper($config->get('api_key', 'ade377f959a7f522c67a948772f02bc6'), true);
$new_credential = $api->create_credential($data['username'], $data['email1'], $default_group_id);
$db = $this->getDatabase();
$query = $db->getQuery(true)->insert($db->quoteName('#__accrediblecertificate'));
$query->columns([
$db->quoteName('user_id'),
$db->quoteName('group_id'),
$db->quoteName('url_image'),
$db->quoteName('url_badge'),
$db->quoteName('created'),
$db->quoteName('published')
])
->values(
$db->quote($data['id']) . ', ' .
$db->quote($default_group_id) . ', ' . $db->quote($new_credential['credential']['seo_image']) . ', ' .
$db->quote($new_credential['credential']['badge']['image']['preview']) . ', "' . date('Y-m-d H:i:s'). '", ' .
( !empty($new_credential['credential']['seo_image']) ? 1 : 0 )
);
$db->setQuery($query);
$db->execute();
}
}
}
The code is very simple, in order to review it in detail.
This is just one example for adding a Certificate and this is not the final version of the plugin yet, I will be finalizing situations when the service is not available.
Also, I would like to create a script (for cron) that will automatically create badges for users for specified accomplishments on the site, similar to how it is implemented on this portal. π
I hope this material was useful...
Thanks in advance for the "π π¦ π€― π π₯" if you enjoyed it!
Top comments (0)