You are here

function opigno_tincan_modules_user_module_status_insert in Opigno TinCan API 8

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

Implements hook_ENTITY_TYPE_insert().

Here tincan statement will be send when user starts a module.

File

modules/opigno_tincan_modules/opigno_tincan_modules.module, line 40
Contains opigno_tincan_modules.module.

Code

function opigno_tincan_modules_user_module_status_insert(EntityInterface $entity) {

  /****
   * - When user start a module
   * Actor: user
   * Verb: xAPI/launched
   * Object: xAPI/module
   * Result:
   * Context: Course or Learning Path
   */

  // Check if Tincan PHP library is installed.
  $has_library = opigno_tincan_api_tincanphp_is_installed();
  if (!$has_library) {
    return;
  }
  $opigno_module = $entity
    ->getModule();

  // Statement creation.
  $statement = OpignoTinCanApiStatements::statementBaseCreation(OpignoTincanApiTinCanVerbs::$launched, OpignoTincanApiTinCanActivityDefinitionTypes::$module, $opigno_module);
  if ($statement === FALSE) {
    return;
  }

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

  // If there is a parent, set it.
  $gid = OpignoGroupContext::getCurrentGroupId();
  if (!$gid) {
    return;
  }
  $managed_content = OpignoGroupManagedContent::loadByProperties([
    'group_id' => $gid,
  ]);
  foreach ($managed_content as $content) {
    $content_type_id = $content
      ->getGroupContentTypeId();
    $id = $content
      ->getEntityId();

    // Check if module is a part of Learning Path.
    if ($opigno_module
      ->id() == $id) {
      $parent_id = $gid;
      OpignoTinCanApiStatements::contextSetGrouping($context, [
        $parent_id,
      ], OpignoTincanApiTinCanActivityDefinitionTypes::$group);
      break;
    }
    elseif ($content_type_id == 'ContentTypeCourse') {
      $course_id = _opigno_tincan_modules_is_module_in_course($content, $opigno_module
        ->id());
      if ($course_id) {
        $parent_id = $course_id;
        OpignoTinCanApiStatements::contextSetGrouping($context, [
          $parent_id,
        ], OpignoTincanApiTinCanActivityDefinitionTypes::$course);
        break;
      }
    }
  }

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

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