You are here

function opigno_learning_path_entity_presave in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 opigno_learning_path.module \opigno_learning_path_entity_presave()

Implements hook_entity_presave().

File

./opigno_learning_path.module, line 2124
Contains opigno_learning_path.module.

Code

function opigno_learning_path_entity_presave(EntityInterface $entity) {
  if ($entity
    ->bundle() == 'learning_path-group_membership') {

    /** @var \Drupal\group\Entity\GroupContentInterface $entity */
    LearningPathAccess::membershipPreSave($entity);
    $uid = $entity
      ->getEntity()
      ->id();
    $group = $entity
      ->getGroup();
    $gid = $group
      ->id();

    // Add notifications to user.
    if ($entity
      ->isNew()) {
      $message = t('Enrolled to a new training "@name"', [
        '@name' => $group
          ->label(),
      ]);
      $url = Url::fromRoute('entity.group.canonical', [
        'group' => $group
          ->id(),
      ])
        ->toString();
      opigno_set_message($uid, $message, $url);
      $user = User::load($uid);
      $visibility = $group->field_learning_path_visibility->value;

      // Check if we need to wait validation.
      $validation = LearningPathAccess::requiredValidation($group, $user);
      if ($visibility === 'semiprivate' && $validation) {

        // Send an email to the managers of group.
        $module = 'opigno_learning_path';
        $key = 'opigno_learning_path_membership_needs_validate';
        $site_config = \Drupal::config('system.site');
        $link = Url::fromUri('internal:/group/' . $gid . '/members')
          ->setAbsolute()
          ->toString();
        $receivers = opigno_learning_path_get_student_managers($group);
        foreach ($receivers as $uid => $receiver) {
          if ($user
            ->id() === $uid) {
            continue;
          }
          $email = $receiver
            ->getEmail();
          $lang = $receiver
            ->getPreferredLangcode();
          $params = [
            'group' => $group,
            'account' => $user,
          ];
          $params['subject'] = t('A user @username has requested to joining the group @training', [
            '@username' => $user
              ->getDisplayName(),
            '@training' => $group
              ->label(),
          ]);
          $args = [
            '@username' => $receiver
              ->getDisplayName(),
            '@request_username' => $user
              ->getDisplayName(),
            '@training' => $group
              ->label(),
            ':link' => $link,
            '@link_text' => $link,
            '@platform' => $site_config
              ->get('name'),
          ];
          $params['message'] = t('Dear @username

A user @request_username has requested to joining the group @training.
You can manage the membership at: <a href=":link">@link_text</a>

@platform', $args);
          \Drupal::service('plugin.manager.mail')
            ->mail($module, $key, $email, $lang, $params);
        }
      }
    }
  }
}