ILIAS  release_8 Revision v8.23
class.ilECSCommunityReader.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
24 {
25  private static ?array $instances = null;
26 
27  private int $position = 0;
28 
29  private ilLogger $logger;
32 
36  private array $communities = array();
37  private array $participants = array();
38  private array $own_ids = array();
39 
46  private function __construct(ilECSSetting $setting)
47  {
48  global $DIC;
49 
50  $this->logger = $DIC->logger()->wsrv();
51  $this->logger->debug(print_r($setting->getServerId(), true));
52  $this->settings = $setting;
53 
54  $this->connector = new ilECSConnector($this->settings);
55 
56  $this->read();
57  $this->logger->debug(__METHOD__ . ': Finished reading communities');
58  }
59 
63  public static function getInstanceByServerId(int $a_server_id): \ilECSCommunityReader
64  {
65  return self::$instances[$a_server_id] ?? (self::$instances[$a_server_id] = new ilECSCommunityReader(ilECSSetting::getInstanceByServerId($a_server_id)));
66  }
67 
71  public function getServer(): \ilECSSetting
72  {
73  return $this->settings;
74  }
75 
80  public function getParticipants(): array
81  {
82  return $this->participants;
83  }
84 
85 
89  public function getOwnMIDs(): array
90  {
91  return $this->own_ids ?: [];
92  }
93 
100  public function getCommunities(): array
101  {
102  return $this->communities ?: [];
103  }
104 
111  public function getCommunityById($a_id): ?ilECSCommunity
112  {
113  foreach ($this->communities as $community) {
114  if ($community->getId() === $a_id) {
115  return $community;
116  }
117  }
118  return null;
119  }
120 
124  public function getParticipantsByPid(int $a_pid): array
125  {
126  $participants = [];
127  foreach ($this->getCommunities() as $community) {
128  foreach ($community->getParticipants() as $participant) {
129  if ($participant->getPid() === $a_pid) {
130  $participants[] = $participant;
131  }
132  }
133  }
134  return $participants;
135  }
136 
143  public function getParticipantByMID($a_mid)
144  {
145  return $this->participants[$a_mid] ?? false;
146  }
147 
148  public function getParticipantNameByMid($a_mid): string
149  {
150  return isset($this->participants[$a_mid]) ?
151  $this->participants[$a_mid]-> getParticipantName() :
152  '';
153  }
154 
158  public function getCommunityByMID(int $a_mid): ?\ilECSCommunity
159  {
160  foreach ($this->communities as $community) {
161  foreach ($community->getParticipants() as $part) {
162  if ($part->getMID() === $a_mid) {
163  return $community;
164  }
165  }
166  }
167  return null;
168  }
169 
173  public function getEnabledParticipants(): array
174  {
175  $ps = ilECSParticipantSettings::getInstanceByServerId($this->getServer()->getServerId());
176  $en = $ps->getEnabledParticipants();
177  $e_part = [];
178  foreach ($this->getCommunities() as $community) {
179  foreach ($community->getParticipants() as $participant) {
180  if (in_array($participant->getMid(), $en, true)) {
181  $e_part[] = $participant;
182  }
183  }
184  }
185  return $e_part;
186  }
187 
193  private function read(): void
194  {
195  try {
196  $res = $this->connector->getMemberships();
197 
198  if (!is_array($res->getResult())) {
199  return;
200  }
201  foreach ($res->getResult() as $community) {
202  $tmp_comm = new ilECSCommunity($community);
203  foreach ($tmp_comm->getParticipants() as $participant) {
204  $this->participants[$participant->getMID()] = $participant;
205  if ($participant->isSelf()) {
206  $this->own_ids[] = $participant->getMID();
207  }
208  }
209  $this->communities[] = $tmp_comm;
210  }
211  } catch (ilECSConnectorException $e) {
212  $this->logger->error(__METHOD__ . ': Error connecting to ECS server. ' . $e->getMessage());
213  throw $e;
214  }
215  }
216 }
getServer()
Get server setting.
$res
Definition: ltiservices.php:69
getOwnMIDs()
get publishable ids
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
getServerId()
Get current server id.
getParticipantByMID($a_mid)
get participant by id
global $DIC
Definition: feed.php:28
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
static getInstanceByServerId(int $a_server_id)
Get singleton instance per server.
getCommunityByMID(int $a_mid)
Get community by mid.
getCommunityById($a_id)
get community by id
getEnabledParticipants()
get enabled participants
__construct(ilECSSetting $setting)
Singleton constructor.
getParticipants()
Get participants.