View source
<?php
namespace Drupal\opigno_tincan_api;
use DateInterval;
use DateTime;
use Drupal\Core\Language\Language;
use Drupal\Core\Url;
use Exception;
use TinCan\Activity;
use TinCan\Agent;
use TinCan\Context;
use TinCan\RemoteLRS;
use TinCan\Result;
use TinCan\Statement;
use TinCan\Verb;
class OpignoTinCanApiStatements {
public static function setObjectActivity(Statement &$statement, $activity_definition_type, $activity_object) {
if (empty($activity_definition_type)) {
throw new Exception('The definition type given is empty.');
}
if (empty($activity_object)) {
\Drupal::logger('opigno_tincan_api')
->notice('The entity given is not loaded or is not a entity: <pre>' . print_r($activity_object, TRUE) . '</pre>', []);
return FALSE;
}
$entity_type_id = $activity_object
->getEntityTypeId();
switch ($entity_type_id) {
case 'group':
$url = Url::fromRoute('entity.group.canonical', [
'group' => $activity_object
->id(),
], [
'absolute' => TRUE,
])
->toString();
$title = $activity_object
->label();
break;
case 'opigno_module':
$url = Url::fromRoute('entity.opigno_module.canonical', [
'opigno_module' => $activity_object
->id(),
], [
'absolute' => TRUE,
])
->toString();
$title = $activity_object
->getName();
break;
case 'opigno_activity':
$url = Url::fromRoute('entity.opigno_activity.canonical', [
'opigno_activity' => $activity_object
->id(),
], [
'absolute' => TRUE,
])
->toString();
$title = $activity_object
->getName();
break;
case 'opigno_moxtra_meeting':
$url = Url::fromRoute('entity.opigno_moxtra_meeting.canonical', [
'opigno_moxtra_meeting' => $activity_object
->id(),
], [
'absolute' => TRUE,
])
->toString();
$title = $activity_object
->getTitle();
break;
case 'opigno_ilt':
$url = Url::fromRoute('entity.opigno_ilt.canonical', [
'opigno_ilt' => $activity_object
->id(),
], [
'absolute' => TRUE,
])
->toString();
$title = $activity_object
->getTitle();
break;
case 'opigno_certificate':
$url = Url::fromRoute('entity.opigno_certificate.canonical', [
'opigno_certificate' => $activity_object
->id(),
], [
'absolute' => TRUE,
])
->toString();
$title = $activity_object
->label();
break;
}
$object = new Activity([
'id' => $url,
'definition' => [
'name' => [
'en-US' => $title,
],
'type' => $activity_definition_type,
],
]);
$statement
->setObject($object);
return $statement;
}
public static function contextSetLanguage(Context &$context, $language) {
if (!empty($language) && $language != Language::LANGCODE_NOT_SPECIFIED) {
$context
->setLanguage($language);
}
return $context;
}
public static function contextSetParents(Context &$context, array $group_ids, $definition_type = NULL) {
$parents = [];
foreach ($group_ids as $group_id) {
$parent = [];
$options = [
'absolute' => TRUE,
];
$url = Url::fromRoute('entity.group.canonical', [
'group' => $group_id,
], $options)
->toString();
$parent['id'] = $url;
if (!empty($definition_type)) {
$parent['definition'] = [
'type' => $definition_type,
];
}
$parents[] = $parent;
}
if (!empty($parents)) {
$context
->getContextActivities()
->setParent($parents);
}
return $context;
}
public static function contextSetGrouping(Context &$context, $group_ids, $definition_type = NULL) {
$grouping = [];
foreach ($group_ids as $group_id) {
$statement_group = [];
$url = Url::fromRoute('entity.group.canonical', [
'group' => $group_id,
], [
'absolute' => TRUE,
])
->toString();
$statement_group['id'] = $url;
if (!empty($definition_type)) {
$statement_group['definition'] = [
'type' => $definition_type,
];
}
$grouping[] = $statement_group;
}
if (!empty($statement_group)) {
$context
->getContextActivities()
->setGrouping($statement_group);
}
return $context;
}
public static function setResult(Statement &$statement, $user_score = NULL, $score_max = NULL, $score_min = NULL, $is_success = NULL, $response = NULL, $duration_s = NULL) {
$result = new Result();
if ($user_score !== NULL) {
self::setScore($result, $user_score, $score_max, $score_min);
}
if ($is_success !== NULL) {
$result
->setSuccess($is_success);
}
$result
->setCompletion(TRUE);
if ($response !== NULL) {
$result
->setResponse($response);
}
if ($duration_s !== NULL) {
$time_now = new DateTime();
$time_more = new DateTime();
$time_more
->add(new DateInterval('PT' . (int) $duration_s . 'S'));
$time = $time_now
->diff($time_more);
$duration_string = $time
->format('P%yY%mM%dDT%hH%iM%sS');
$duration_string = preg_replace('/(\\D)0{1}\\D/i', '$1', $duration_string);
if (strpos($duration_string, 'P0M') !== FALSE) {
$duration_string = preg_replace('/(\\D)0{1}\\D/i', '$1', $duration_string);
}
$duration_string == 'PT' ? $duration_string = 'PT0S' : NULL;
$result
->setDuration($duration_string);
}
$statement
->setResult($result);
return $statement;
}
public static function sendStatement(Statement $statement) {
$queue_factory = \Drupal::service('queue');
$queue = $queue_factory
->get('opigno_tincan_send_tincan_statement');
$item = new \stdClass();
$item->statement = $statement;
$queue
->createItem($item);
}
public static function statementBaseCreation(array $verb, $activity_definition_type, $activity_object, $user = NULL) {
$statement = new Statement();
self::setActor($statement, $user);
self::setVerb($statement, $verb);
self::setObjectActivity($statement, $activity_definition_type, $activity_object);
$statement
->stamp();
return $statement;
}
public static function setActor(Statement &$statement, $user = NULL) {
if ($user === NULL) {
$user = \Drupal::currentUser();
}
else {
if (empty($user) || empty($user
->getAccountName()) || empty($user
->getEmail())) {
throw new Exception('The user given was not loaded');
}
}
$actor = new Agent([
'name' => $user
->getAccountName(),
'mbox_sha1sum' => sha1('mailto:' . $user
->getEmail()),
]);
$statement
->setActor($actor);
}
public static function setVerb(Statement &$statement, array $verb) {
if (empty($verb) || empty($verb['name']) || empty($verb['id'])) {
throw new Exception('The verb given does not exist');
}
$verb = new Verb([
'id' => $verb['id'],
'display' => [
'en-US' => $verb['name'],
],
]);
$statement
->setVerb($verb);
}
public static function setScore(Result &$result, $raw_score, $max_score = NULL, $min_score = NULL) {
$score = [];
if ($min_score === NULL) {
$min_score = 0;
}
if ($max_score === NULL || $max_score === 0 && $min_score === 0) {
$score['raw'] = $raw_score;
$result
->setScore($score);
return FALSE;
}
if ($max_score <= $min_score) {
$max_score = $min_score + 1;
}
$scaled_max = $max_score - $min_score;
$scaled_raw = $raw_score - $min_score;
$scaled = $scaled_max > 0 ? round($scaled_raw / $scaled_max, 2) : 0;
$result
->setScore([
'min' => $min_score,
'max' => $max_score,
'raw' => $raw_score,
'scaled' => $scaled < -1 ? -1 : ($scaled > 1 ? 1 : $scaled),
]);
return TRUE;
}
}