function _opigno_tincan_badges_create_and_send_statements in Opigno TinCan API 3.x
Same name and namespace in other branches
- 8 modules/opigno_tincan_badges/opigno_tincan_badges.module \_opigno_tincan_badges_create_and_send_statements()
Function for creating and sending tincan statemens when user get a badge.
Can be send only for Course or Module.
Parameters
mixed $entity: Course or Module.
Throws
\Exception
1 call to _opigno_tincan_badges_create_and_send_statements()
- opigno_tincan_badges_user_module_status_presave in modules/
opigno_tincan_badges/ opigno_tincan_badges.module - Implements hook_ENTITY_TYPE_presave().
File
- modules/
opigno_tincan_badges/ opigno_tincan_badges.module, line 127 - Contains opigno_tincan_modules.module.
Code
function _opigno_tincan_badges_create_and_send_statements($entity) {
// Get badge properties.
$badge_name = $entity
->get('badge_name')->value;
$badge_description = $entity
->get('badge_description')->value;
$badge_criteria = $entity
->get('badge_criteria')->value;
$media_entity = $entity
->get('badge_media_image')->entity;
// Image file url and id are required for statement.
if (!$media_entity) {
return;
}
// Create statement.
$statement = new Statement();
// Set Actor.
OpignoTinCanApiStatements::setActor($statement);
// Set verb.
OpignoTinCanApiStatements::setVerb($statement, OpignoTincanApiTinCanVerbs::$earned);
$statement
->stamp();
// Load image.
$badge_image_id = $media_entity
->get('field_media_image')->target_id;
$badge_image = File::load($badge_image_id);
$image_url = file_create_url($badge_image
->getFileUri());
// Add an extension in the result.
$statement
->setResult([
'extensions' => [
'http://specification.openbadges.org/xapi/extensions/badgeassertion' => [
'@id' => $image_url,
],
],
]);
// Add the object. This object contains an extension of the definition,
// a name, a description and a badge type.
$statement
->setObject([
'id' => $image_url,
'objectType' => 'Activity',
'definition' => [
'extensions' => [
'http://specification.openbadges.org/xapi/extensions/badgeclass' => [
'@id' => $image_url,
'image' => $badge_image_id,
'criteria' => $badge_criteria,
],
],
'name' => [
'en-US' => $badge_name ? $badge_name : '',
],
'description' => [
'en-US' => $badge_description ? $badge_description : '',
],
'type' => OpignoTincanApiTinCanActivityDefinitionTypes::$badge,
],
]);
// Context creation.
$context = new Context();
// Set parents.
if ($entity instanceof OpignoModule) {
// Set patents context to opigno_module entity.
$definition_type = OpignoTincanApiTinCanActivityDefinitionTypes::$module;
$url = Url::fromRoute('entity.opigno_module.canonical', [
'opigno_module' => $entity
->id(),
], [
'absolute' => TRUE,
])
->toString();
$parent['id'] = $url;
if (!empty($definition_type)) {
$parent['definition'] = [
'type' => $definition_type,
];
}
if (!empty($parent)) {
$context
->getContextActivities()
->setParent([
$parent,
]);
}
}
elseif ($entity instanceof Group) {
$definition_type = OpignoTincanApiTinCanActivityDefinitionTypes::$course;
OpignoTinCanApiStatements::contextSetParents($context, [
$entity
->id(),
], $definition_type);
}
// Set statement context.
$statement
->setContext($context);
// Sending statement.
OpignoTinCanApiStatements::sendStatement($statement);
}