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