You are here

function _opigno_tincan_courses_create_and_send_course_statements in Opigno TinCan API 8

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

Creates and sends course statements.

Parameters

int $training_id: Group id.

array $step: Array with course details.

Throws

\Exception

1 call to _opigno_tincan_courses_create_and_send_course_statements()
opigno_tincan_courses_user_module_status_presave in modules/opigno_tincan_courses/opigno_tincan_courses.module
Implements hook_ENTITY_TYPE_presave().

File

modules/opigno_tincan_courses/opigno_tincan_courses.module, line 97
Contains opigno_tincan_courses.module.

Code

function _opigno_tincan_courses_create_and_send_course_statements($training_id, array $step) {

  /****
   * - When user finish a course
   * Actor: user
   * Verb: xAPI/passed | xAPI/failed
   * Object: xAPI/module
   * Result: Get final course result
   * Context: Training
   */

  // Get course group entity.
  $course = Group::load($step['id']);

  // Check if course is passed or not.
  $required_score = isset($step['required score']) ? intval($step['required score']) : 0;
  $user_score = isset($step['best score']) && intval($step['best score']) > 0 ? intval($step['best score']) : 0;
  $passed = $user_score >= $required_score;

  // Statement creation.
  $statement = OpignoTinCanApiStatements::statementBaseCreation($passed ? OpignoTincanApiTinCanVerbs::$passed : OpignoTincanApiTinCanVerbs::$failed, OpignoTincanApiTinCanActivityDefinitionTypes::$module, $course);
  if ($statement === FALSE) {
    return;
  }

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

  // Get parents.
  $parent_ids = [
    $training_id,
  ];
  OpignoTinCanApiStatements::contextSetGrouping($context, $parent_ids, OpignoTincanApiTinCanActivityDefinitionTypes::$group);

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

  // Get duration.
  $duration_s = isset($step['time spent']) ? intval($step['time spent']) : 0;

  // Set result.
  $response = NULL;
  OpignoTinCanApiStatements::setResult($statement, $user_score, 100, NULL, $passed, $response, $duration_s);

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

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