You are here

public function LearningPathMembershipController::validate in Opigno Learning path 8

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

Ajax callback used in opigno_learning_path_member_overview.js.

Validates user role in learning path.

1 string reference to 'LearningPathMembershipController::validate'
opigno_learning_path.routing.yml in ./opigno_learning_path.routing.yml
opigno_learning_path.routing.yml

File

src/Controller/LearningPathMembershipController.php, line 515

Class

LearningPathMembershipController
Controller for the actions related to LP membership.

Namespace

Drupal\opigno_learning_path\Controller

Code

public function validate() {

  /** @var \Drupal\group\Entity\Group $group */
  $group = \Drupal::routeMatch()
    ->getParameter('group');
  $gid = $group
    ->id();
  $uid = \Drupal::request()->query
    ->get('user_id');
  $user = User::load($uid);
  if (!isset($group) || !isset($user)) {
    throw new NotFoundHttpException();
  }
  $member = $group
    ->getMember($user);
  if (!isset($member)) {
    throw new NotFoundHttpException();
  }
  $group_content = $member
    ->getGroupContent();
  $query = \Drupal::database()
    ->merge('opigno_learning_path_group_user_status')
    ->key('mid', $group_content
    ->id())
    ->insertFields([
    'mid' => $group_content
      ->id(),
    'uid' => $uid,
    'gid' => $gid,
    'status' => 1,
    'message' => '',
  ])
    ->updateFields([
    'uid' => $uid,
    'gid' => $gid,
    'status' => 1,
    'message' => '',
  ]);
  $result = $query
    ->execute();
  if ($result) {

    // Invalidate cache.
    $tags = $member
      ->getCacheTags();
    \Drupal::service('cache_tags.invalidator')
      ->invalidateTags($tags);

    // Set notification.
    $message = $this
      ->t('Enrollment validated to a new training "@name"', [
      '@name' => $group
        ->label(),
    ]);
    $url = Url::fromRoute('entity.group.canonical', [
      'group' => $group
        ->id(),
    ])
      ->toString();
    opigno_set_message($uid, $message, $url);
    $config = \Drupal::config('opigno_learning_path.learning_path_settings');
    $send_to_users = $config
      ->get('opigno_learning_path_notify_users');
    if ($send_to_users) {

      // Send email.
      $module = 'opigno_learning_path';
      $key = 'opigno_learning_path_membership_validated';
      $email = $user
        ->getEmail();
      $lang = $user
        ->getPreferredLangcode();
      $params = [];
      $params['subject'] = $this
        ->t('Your membership to the training @training has been approved', [
        '@training' => $group
          ->label(),
      ]);
      $site_config = \Drupal::config('system.site');
      $link = $group
        ->toUrl()
        ->setAbsolute()
        ->toString();
      $args = [
        '@username' => $user
          ->getDisplayName(),
        '@training' => $group
          ->label(),
        ':link' => $link,
        '@link_text' => $link,
        '@platform' => $site_config
          ->get('name'),
      ];
      $params['message'] = $this
        ->t('Dear @username

Your membership to the training @training has been approved. You can now access this training at: <a href=":link">@link_text</a>

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