ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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
34
38 private array $communities = array();
39 private array $participants = array();
40 private array $own_ids = array();
41
48 private function __construct(ilECSSetting $setting)
49 {
50 global $DIC;
51
52 $this->logger = $DIC->logger()->wsrv();
53 $this->logger->debug(print_r($setting->getServerId(), true));
54 $this->database = $DIC->database();
55 $this->settings = $setting;
56
57 $this->connector = new ilECSConnector($this->settings);
58
59 $this->read();
60 $this->logger->debug(__METHOD__ . ': Finished reading communities');
61 }
62
66 public static function getInstanceByServerId(int $a_server_id): \ilECSCommunityReader
67 {
68 return self::$instances[$a_server_id] ?? (self::$instances[$a_server_id] = new ilECSCommunityReader(ilECSSetting::getInstanceByServerId($a_server_id)));
69 }
70
74 public function getServer(): \ilECSSetting
75 {
76 return $this->settings;
77 }
78
83 public function getParticipants(): array
84 {
86 }
87
88
92 public function getOwnMIDs(): array
93 {
94 return $this->own_ids ?: [];
95 }
96
103 public function getCommunities(): array
104 {
105 return $this->communities ?: [];
106 }
107
114 public function getCommunityById($a_id): ?ilECSCommunity
115 {
116 foreach ($this->communities as $community) {
117 if ($community->getId() === $a_id) {
118 return $community;
119 }
120 }
121 return null;
122 }
123
127 public function getParticipantsByPid(int $a_pid): array
128 {
129 $participants = [];
130 foreach ($this->getCommunities() as $community) {
131 foreach ($community->getParticipants() as $participant) {
132 if ($participant->getPid() === $a_pid) {
133 $participants[] = $participant;
134 }
135 }
136 }
137 return $participants;
138 }
139
146 public function getParticipantByMID($a_mid)
147 {
148 return $this->participants[$a_mid] ?? false;
149 }
150
151 public function getParticipantNameByMid($a_mid): string
152 {
153 return isset($this->participants[$a_mid]) ?
154 $this->participants[$a_mid]->getParticipantName() :
155 '';
156 }
157
161 public function getCommunityByMID(int $a_mid): ?\ilECSCommunity
162 {
163 foreach ($this->communities as $community) {
164 foreach ($community->getParticipants() as $part) {
165 if ($part->getMID() === $a_mid) {
166 return $community;
167 }
168 }
169 }
170 return null;
171 }
172
176 public function getEnabledParticipants(): array
177 {
178 $ps = ilECSParticipantSettings::getInstanceByServerId($this->getServer()->getServerId());
179 $en = $ps->getEnabledParticipants();
180 $e_part = [];
181 foreach ($this->getCommunities() as $community) {
182 foreach ($community->getParticipants() as $participant) {
183 if (in_array($participant->getMid(), $en, true)) {
184 $e_part[] = $participant;
185 }
186 }
187 }
188 return $e_part;
189 }
190
196 private function read(): void
197 {
198 try {
199 $res = $this->connector->getMemberships();
200
201 if (!is_array($res->getResult())) {
202 return;
203 }
204 foreach ($res->getResult() as $community) {
205 $tmp_comm = new ilECSCommunity($community);
206 foreach ($tmp_comm->getParticipants() as $participant) {
207 $this->participants[$participant->getMID()] = $participant;
208 if ($participant->isSelf()) {
209 $this->own_ids[] = $participant->getMID();
210 }
211 }
212 $this->communities[] = $tmp_comm;
213 }
214
215 $this->createNewParticipants();
216 } catch (ilECSConnectorException $e) {
217 $this->logger->error(__METHOD__ . ': Error connecting to ECS server. ' . $e->getMessage());
218 throw $e;
219 }
220 }
221
222 private function createNewParticipants(): void
223 {
224 $query = $this->database->queryF(
225 "SELECT mid FROM ecs_part_settings WHERE sid = %s",
227 [$this->connector->getServer()->getServerId()]
228 );
229 $existing_ids = array_map(fn($row) => $row['mid'], $this->database->fetchAll($query));
230 $missing_ids = array_diff(array_keys($this->participants), $existing_ids);
231
232 foreach ($missing_ids as $mid) {
233 $participant = $this->getParticipantByMID($mid);
234 $community = $this->getCommunityByMID($mid);
235
236 $part_settings = ilECSParticipantSetting::getInstance($this->getServer()->getServerId(), $mid);
237 $part_settings->setTitle($participant->getParticipantName());
238 $part_settings->setCommunityName($community->getTitle());
239 $part_settings->update();
240 }
241 }
242}
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 getInstance(int $a_server_id, int $mid)
Get instance by server id and mid.
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.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26