public function OpignoUserSelectionController::post in Opigno Learning path 3.x
1 string reference to 'OpignoUserSelectionController::post'
- 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 124
Class
- OpignoUserSelectionController
- Returns responses for Opigno User Selection routes.
Namespace
Drupal\opigno_user_selection\ControllerCode
public function post($data = NULL) {
$content = $this->currentRequest
->getContent();
if (!empty($content)) {
// 2nd param to get as array.
$data = json_decode($content, TRUE);
}
$groups_id = [];
$response_data = [];
$meta = new CacheableMetadata();
$meta
->setCacheMaxAge(Cache::PERMANENT);
/** @var \Drupal\user\Entity\User[] $users */
$users = $this
->entityTypeManager()
->getStorage('user')
->loadMultiple($data ?: []);
$map = [
'learning_path' => 'training',
'opigno_class' => 'class',
];
$response_data['users'] = array_map(function ($user) use (&$groups_id, $meta, $map) {
$meta
->addCacheableDependency($user);
$memberships = $this->groupMembershipLoader
->loadByUser($user);
/** @var \Drupal\user\Entity\User $user */
return [
'id' => $user
->id(),
'name' => $user
->getDisplayName(),
'email' => '',
'avatar' => $this
->getuserAvatar($user),
'member' => array_filter(array_map(function (GroupMembership $membership) use (&$groups_id, $map) {
$group = $membership
->getGroup();
if (array_key_exists($group
->bundle(), $map)) {
$groups_id[] = $gid = (int) $group
->id();
return $gid;
}
return NULL;
}, $memberships)),
];
}, $users);
/** @var \Drupal\group\Entity\Group[] $groups */
$groups = $this
->entityTypeManager()
->getStorage('group')
->loadMultiple($groups_id ?: []);
$response_data['members'] = array_map(function ($group) use ($meta, $map) {
$meta
->addCacheableDependency($group);
$memberships = $this->groupMembershipLoader
->loadByGroup($group);
/** @var \Drupal\group\Entity\Group $group */
return [
"id" => $group
->id(),
"type" => $map[$group
->bundle()],
"info" => [
"name" => $group
->label(),
'avatar' => $this
->getGroupImage($map[$group
->bundle()], $group),
],
"key" => $map[$group
->bundle()] . "_" . $group
->id(),
"loaded" => TRUE,
"members" => array_map(function (GroupMembership $membership) {
return (int) $membership
->getGroupContent()
->getEntity()
->id();
}, $memberships),
];
}, $groups);
$response = new CacheableJsonResponse($response_data);
$response
->addCacheableDependency($meta);
return $response;
}