You are here

function _opigno_tincan_ilt_create_and_send_tincan_statement in Opigno TinCan API 8

Same name and namespace in other branches
  1. 3.x modules/opigno_tincan_ilt/opigno_tincan_ilt.module \_opigno_tincan_ilt_create_and_send_tincan_statement()

Creates and sends tincan statement.

1 call to _opigno_tincan_ilt_create_and_send_tincan_statement()
opigno_tincan_ilt_opigno_ilt_result_presave in modules/opigno_tincan_ilt/opigno_tincan_ilt.module
Implements hook_ENTITY_TYPE_presave().

File

modules/opigno_tincan_ilt/opigno_tincan_ilt.module, line 70
Contains opigno_tincan_ilt.module.

Code

function _opigno_tincan_ilt_create_and_send_tincan_statement($entity) {

  /****
   * - When user attempted Instructor-Led Training
   * Actor: user
   * Verb: xAPI/attended
   * Object: xAPI/meeting
   * Context: Training
   */

  /* @var Drupal\opigno_ilt\Entity\ILTResult $opigno_ilt_result*/
  $opigno_ilt_result = $entity;

  /* @var Drupal\opigno_ilt\Entity\ILT $opigno_ilt*/
  $opigno_ilt = $opigno_ilt_result
    ->getILT();

  // Statement creation.
  $user = $opigno_ilt_result
    ->getUser();
  $statement = OpignoTinCanApiStatements::statementBaseCreation(OpignoTincanApiTinCanVerbs::$attended, OpignoTincanApiTinCanActivityDefinitionTypes::$meeting, $opigno_ilt, $user);
  if ($statement === FALSE) {
    return;
  }

  // Context creation.
  $context = new Context();

  // Get group id.
  $parent_id = $opigno_ilt
    ->getTrainingId();
  OpignoTinCanApiStatements::contextSetGrouping($context, [
    $parent_id,
  ]);

  // Set language in context.
  OpignoTinCanApiStatements::contextSetLanguage($context, $opigno_ilt
    ->language()
    ->getId());

  // Get duration.
  $start_date = $opigno_ilt
    ->getStartDate();
  $end_date = $opigno_ilt
    ->getEndDate();
  $duration_s = strtotime($end_date) - strtotime($start_date);
  $attended = $opigno_ilt_result
    ->getStatus();
  $user_score = $opigno_ilt_result
    ->getScore();

  // Raw score can not be negative.
  $user_score = $user_score > 0 ? $user_score : 0;

  // Set result.
  OpignoTinCanApiStatements::setResult($statement, $user_score, 100, NULL, $attended, NULL, abs($duration_s));

  // Set statement context.
  $statement
    ->setContext($context);

  // Sending statement.
  OpignoTinCanApiStatements::sendStatement($statement);
}