You are here

public static function LearningPathAccess::notifyUsersByMail in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/LearningPathAccess.php \Drupal\opigno_learning_path\LearningPathAccess::notifyUsersByMail()

Prepares and sends emails to users.

2 calls to LearningPathAccess::notifyUsersByMail()
LearningPathAccess::deleteUserStatus in src/LearningPathAccess.php
Deletes Learning Path group user status.
LearningPathAccess::mergeUserStatus in src/LearningPathAccess.php
Merges Learning Path group user status.

File

src/LearningPathAccess.php, line 366

Class

LearningPathAccess
Class LearningPathAccess.

Namespace

Drupal\opigno_learning_path

Code

public static function notifyUsersByMail(Group $group, $uid, $status, $token = FALSE) {
  $user = \Drupal::currentUser();
  $config = \Drupal::config('opigno_learning_path.learning_path_settings');
  $send_to_admins = $config
    ->get('opigno_learning_path_notify_admin');
  $send_to_users = $config
    ->get('opigno_learning_path_notify_users');
  if ($send_to_admins || $send_to_users) {
    $account = User::load($uid);
    $events = LearningPathAccess::getUserMemberEvents();
    $subject = \Drupal::config('system.site')
      ->get('name') . ' ' . t('subscription');
    $host = \Drupal::request()
      ->getSchemeAndHttpHost();
    if ($status) {
      $membership = $group
        ->getMember($account);
      $statusName = LearningPathAccess::getMembershipStatus($membership
        ->getGroupContent()
        ->id(), TRUE);
      $roles = $membership
        ->getRoles();
      $roles_array = [];
      if (!empty($roles)) {
        foreach ($roles as $role) {
          $roles_array[] = $role
            ->label();
        }
      }
    }
    else {
      $status = 1;
      $statusName = t('Unsubscribed');
    }
    $roles = !empty($roles_array) ? implode(', ', $roles_array) : '';
    if ($send_to_admins) {
      $mails = $config
        ->get('opigno_learning_path_notify_admin_mails');
      if (!empty($mails)) {
        $message = $config
          ->get('opigno_learning_path_notify_admin_user_' . $events[$status]);
        $params = [
          'group' => $group,
          'account' => $account,
          'link' => $host . '/group/' . $group
            ->id() . '/members',
          'roles' => $roles,
          'status' => $statusName,
        ];
        LearningPathAccess::replaceGroupUserTokens($message, $params, $token);
        $mails = explode("\r\n", $mails);
        foreach ($mails as $to) {
          if (!empty($to) && !empty($message)) {
            LearningPathAccess::sendMail($to, $subject, $message, $params);
          }
        }
      }
    }

    // Set flag for notifying users by email.
    $send = FALSE;
    $send_message = \Drupal::request()
      ->get('send_message');
    if (empty($send_message)) {
      $route_name = \DRUPAL::routeMatch()
        ->getRouteName();
      if ($route_name != 'entity.group_content.add_form') {
        $send = $send_to_users && $uid !== $user
          ->id();
      }
    }
    if ($send) {
      $to = $account
        ->getEmail();
      $message = $config
        ->get('opigno_learning_path_notify_user_user_' . $events[$status]);
      $params = [
        'group' => $group,
        'account' => $account,
        'link' => $host . '/group/' . $group
          ->id(),
        'roles' => $roles,
        'status' => $statusName,
      ];
      LearningPathAccess::replaceGroupUserTokens($message, $params, $token);
      if (!empty($to) && !empty($message)) {
        LearningPathAccess::sendMail($to, $subject, $message, $params);
      }
    }
  }
}