You are here

public function MoxtraService::addUsersToWorkspace in Opigno Moxtra 3.x

Same name and namespace in other branches
  1. 8 src/MoxtraService.php \Drupal\opigno_moxtra\MoxtraService::addUsersToWorkspace()

Adds users to the workspace.

Parameters

int $owner_id: User ID.

string $binder_id: Binder ID.

int[] $users_ids: Array of the users IDs to add to the workspace.

Return value

array Response data.

Overrides MoxtraServiceInterface::addUsersToWorkspace

File

src/MoxtraService.php, line 425

Class

MoxtraService
Implements Moxtra REST API.

Namespace

Drupal\opigno_moxtra

Code

public function addUsersToWorkspace($owner_id, $binder_id, $users_ids) {
  $users = array_map(function ($id) {
    return [
      'user' => [
        'unique_id' => $id,
      ],
    ];
  }, $users_ids);
  $data = [
    'users' => $users,
    'suppress_feed' => TRUE,
  ];
  $url = $this
    ->getAddUsersUrl($owner_id, $binder_id);
  $response = $this->moxtraConnector
    ->request($url, $data);
  if (!empty($response) && $response['http_code'] == 200) {
    $owner = User::load($owner_id);

    /** @var \Drupal\user\Entity\User[] $users */
    $users = User::loadMultiple($users_ids);
    foreach ($users as $user) {
      $message = $this
        ->t('@owner invited @user to join this conversation.', [
        '@owner' => $owner
          ->getDisplayName(),
        '@user' => $user
          ->getDisplayName(),
      ]);
      $this
        ->sendMessage($owner_id, $binder_id, $message);
    }
  }
  return $response;
}