You are here

public function OpignoUserSelectionController::training in Opigno Learning path 3.x

1 string reference to 'OpignoUserSelectionController::training'
opigno_user_selection.routing.yml in modules/opigno_user_selection/opigno_user_selection.routing.yml
modules/opigno_user_selection/opigno_user_selection.routing.yml

File

modules/opigno_user_selection/src/Controller/OpignoUserSelectionController.php, line 200

Class

OpignoUserSelectionController
Returns responses for Opigno User Selection routes.

Namespace

Drupal\opigno_user_selection\Controller

Code

public function training($data = NULL) {
  $content = $this->currentRequest
    ->getContent();
  if (!empty($content)) {

    // 2nd param to get as array.
    $data = json_decode($content, TRUE);
  }
  $response_data = [];
  $meta = new CacheableMetadata();
  $meta
    ->setCacheMaxAge(Cache::PERMANENT);
  $map = [
    'learning_path' => 'training',
    'opigno_class' => 'class',
  ];

  /** @var \Drupal\group\Entity\Group[] $groups */
  $groups = $this
    ->entityTypeManager()
    ->getStorage('group')
    ->loadMultiple($data ?: []);

  // Response_data key should be "users",
  $response_data['users'] = array_map(function ($group) use ($meta, $map) {
    $meta
      ->addCacheableDependency($group);

    /** @var \Drupal\group\Entity\Group $group */
    return [
      'id' => $group
        ->id(),
      'name' => $group
        ->label(),
      'email' => '',
      'avatar' => $this
        ->getGroupImage($map[$group
        ->bundle()], $group),
    ];
  }, $groups);
  $response = new CacheableJsonResponse($response_data);
  $response
    ->addCacheableDependency($meta);
  return $response;
}