You are here

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

Ajax callback for searching user in a training classes.

Parameters

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

string $class_id: Class group ID.

string $uid: User ID.

Return value

\Drupal\Core\Ajax\AjaxResponse Ajax command or empty.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

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

File

src/Controller/LearningPathMembershipController.php, line 307

Class

LearningPathMembershipController
Controller for the actions related to LP membership.

Namespace

Drupal\opigno_learning_path\Controller

Code

public function findGroupMember(Group $group, $class_id, $uid) {
  $response = new AjaxResponse();
  if ($class_id === '0') {
    $content_types = [
      'group_content_type_27efa0097d858',
      'group_content_type_af9d804582e19',
      'learning_path-group_membership',
    ];
    $group_content_ids = \Drupal::entityQuery('group_content')
      ->condition('gid', $group
      ->id())
      ->condition('type', $content_types, 'IN')
      ->sort('changed', 'DESC')
      ->execute();
    $content = \Drupal::entityTypeManager()
      ->getStorage('group_content')
      ->loadMultiple($group_content_ids);
    $users = [];
    $classes = [];

    /** @var \Drupal\group\Entity\GroupContentInterface $item */
    foreach ($content as $item) {
      $entity = $item
        ->getEntity();
      if ($entity === NULL) {
        continue;
      }
      $type = $entity
        ->getEntityTypeId();
      $bundle = $entity
        ->bundle();
      if ($type === 'user') {
        $users[$entity
          ->id()] = [
          'group content' => $item,
          'entity' => $entity,
        ];
      }
      elseif ($type === 'group' && $bundle === 'opigno_class') {
        $classes[$entity
          ->id()] = [
          'group content' => $item,
          'entity' => $entity,
        ];
      }
    }
    if ($classes) {
      foreach ($classes as $class) {
        $view_id = 'opigno_group_members_table';
        $display = 'group_members_block';
        $args = [
          $class['entity']
            ->id(),
        ];
        $members_view = Views::getView($view_id);
        if (is_object($members_view)) {
          $members_view->storage
            ->set('group_members', array_keys($users));
          $members_view
            ->setArguments($args);
          $members_view
            ->setDisplay($display);
          $members_view
            ->setItemsPerPage(0);
          $members_view
            ->execute();
          if (!empty($members_view->result)) {
            foreach ($members_view->result as $key => $item) {
              $member = $item->_entity
                ->getEntity();
              if ($member
                ->id() == $uid) {
                $display_default = $members_view->storage
                  ->getDisplay('default');
                $per_page = $display_default["display_options"]["pager"]["options"]["items_per_page"];
                $current_page = intdiv($key, $per_page);
                $class_id = $class['entity']
                  ->id();
                break 2;
              }
            }
          }
        }
      }
      if (isset($current_page)) {
        $selector = '#class-' . $class_id . ' .views-element-container';
        $members_view = Views::getView($view_id);
        if (is_object($members_view)) {
          $members_view->storage
            ->set('group_members', array_keys($users));
          $members_view
            ->setArguments($args);
          $members_view
            ->setDisplay($display);
          $members_view
            ->setCurrentPage($current_page);
          $members_view
            ->preExecute();
          $members_view
            ->execute();
          $members_view_renderable = $members_view
            ->buildRenderable($display, $args);
          $response
            ->addCommand(new ReplaceCommand($selector, $members_view_renderable));
        }
      }
    }
  }
  return $response;
}