You are here

public function LearningPathMembershipController::addUserToTrainingAutocomplete 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::addUserToTrainingAutocomplete()

Returns response for the autocompletion.

Parameters

\Drupal\group\Entity\Group $group: Group.

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON response containing the autocomplete suggestions.

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

File

src/Controller/LearningPathMembershipController.php, line 105

Class

LearningPathMembershipController
Controller for the actions related to LP membership.

Namespace

Drupal\opigno_learning_path\Controller

Code

public function addUserToTrainingAutocomplete(Group $group) {
  $matches = [];
  $is_class = $group
    ->getGroupType()
    ->id() == 'opigno_class';
  $string = \Drupal::request()->query
    ->get('q');
  if ($string !== NULL) {
    $like_string = '%' . $this->connection
      ->escapeLike($string) . '%';

    // Find users by email or name.
    $query = \Drupal::entityQuery('user')
      ->condition('uid', 0, '<>');
    $cond_group = $query
      ->orConditionGroup()
      ->condition('mail', $like_string, 'LIKE')
      ->condition('name', $like_string, 'LIKE');
    $query = $query
      ->condition($cond_group)
      ->sort('name')
      ->range(0, 20);
    $uids = $query
      ->execute();
    $users = User::loadMultiple($uids);

    /** @var \Drupal\user\Entity\User $user */
    foreach ($users as $user) {
      $id = $user
        ->id();
      $name = $user
        ->getDisplayName();

      // Skip users that already members of the current group.
      if ($group
        ->getMember($user) !== FALSE) {
        continue;
      }
      $matches[] = [
        'value' => "{$name} (User #{$id})",
        'label' => "{$name} (User #{$id})",
        'type' => 'user',
        'id' => 'user_' . $id,
      ];
    }
    if (!$is_class) {

      // Find classes by name.
      $query = \Drupal::entityQuery('group')
        ->condition('type', 'opigno_class')
        ->condition('label', $like_string, 'LIKE')
        ->sort('label')
        ->range(0, 20);
      $gids = $query
        ->execute();
      $classes = Group::loadMultiple($gids);
      $db_connection = \Drupal::service('database');

      /** @var \Drupal\group\Entity\Group $class */
      foreach ($classes as $class) {

        // Check if class already added.
        $is_class_added = $db_connection
          ->select('group_content_field_data', 'g_c_f_d')
          ->fields('g_c_f_d', [
          'id',
        ])
          ->condition('gid', $group
          ->id())
          ->condition('entity_id', $class
          ->id())
          ->condition('type', 'group_content_type_27efa0097d858')
          ->execute()
          ->fetchField();
        if (!$is_class_added) {

          // If class haven't added yet.
          $id = $class
            ->id();
          $name = $class
            ->label();
          $matches[] = [
            'value' => "{$name} (Group #{$id})",
            'label' => "{$name} (Group #{$id})",
            'type' => 'group',
            'id' => 'class_' . $id,
          ];
        }
      }
    }
  }
  return new JsonResponse($matches);
}