You are here

function opigno_learning_path_entity_update in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x opigno_learning_path.module \opigno_learning_path_entity_update()

Implements hook_entity_update().

File

./opigno_learning_path.module, line 2036
Contains opigno_learning_path.module.

Code

function opigno_learning_path_entity_update(EntityInterface $entity) {
  if ($entity
    ->bundle() == 'learning_path-group_membership') {
    LearningPathAccess::mergeUserStatus($entity);
  }
  if ($entity
    ->bundle() == 'learning_path') {
    $db_connection = \Drupal::service('database');
    try {
      $db_connection
        ->update('opigno_learning_path_achievements')
        ->fields([
        'name' => $entity
          ->label(),
      ])
        ->condition('gid', $entity
        ->id())
        ->execute();
    } catch (\Exception $e) {
      \Drupal::logger('opigno_learning_path')
        ->error($e
        ->getMessage());
      \Drupal::messenger()
        ->addMessage($e
        ->getMessage(), 'error');
    }
  }
  if ($entity
    ->getEntityTypeId() === 'user_module_status') {

    // Try to get training id from path.
    $route = \Drupal::routeMatch();

    /* @var \Drupal\group\Entity\Group $group */
    $group = $route
      ->getParameter('group');
    if (!empty($group) && $group instanceof Group && $group
      ->getGroupType() == 'learning_path') {
      $group_id = $group
        ->id();
    }
    else {

      // Try to get training id from context.
      $group_id = OpignoGroupContext::getCurrentGroupId();
    }
    if ($group_id) {

      /** @var \Drupal\opigno_module\Entity\UserModuleStatus $entity */

      /** @var \Drupal\opigno_module\Entity\OpignoModule $module */
      $module = $entity
        ->getModule();
      $module_id = $module
        ->id();
      $account = \Drupal::currentUser();

      // Update latest group activity.
      LatestActivity::insertGroupActivity($group_id, $module_id, $account
        ->id());
    }
  }
}