ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilECSCommunityCache.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
27 protected static array $instance = [];
28
29 protected int $server_id = 0;
30 protected int $community_id = 0;
31 protected int $own_id = 0;
32 protected string $cname = '';
33 protected array $mids = array();
34
35 protected bool $entryExists = false;
36
38
44 protected function __construct(int $server_id, int $community_id)
45 {
46 global $DIC;
47
48 $this->db = $DIC->database();
49
50 $this->server_id = $server_id;
51 $this->community_id = $community_id;
52
53 $this->read();
54 }
55
62 public static function getInstance(int $a_server_id, int $a_community_id): ilECSCommunityCache
63 {
64 return self::$instance[$a_server_id][$a_community_id] ??
65 (self::$instance[$a_server_id][$a_community_id] = new ilECSCommunityCache(
66 $a_server_id,
67 $a_community_id
68 ));
69 }
70
71
72
73 public function getServerId(): int
74 {
75 return $this->server_id;
76 }
77
78 public function getCommunityId(): int
79 {
81 }
82
83 public function setOwnId(int $a_id): void
84 {
85 $this->own_id = $a_id;
86 }
87
88 public function getOwnId(): int
89 {
90 return $this->own_id;
91 }
92
93 public function setCommunityName(string $a_name): void
94 {
95 $this->cname = $a_name;
96 }
97
98 public function getCommunityName(): string
99 {
100 return $this->cname;
101 }
102
103 public function setMids(array $a_mids): void
104 {
105 $this->mids = $a_mids;
106 }
107
108 public function getMids(): array
109 {
110 return $this->mids;
111 }
112
116 public function update(): bool
117 {
118 if (!$this->entryExists) {
119 return $this->create();
120 }
121
122 $query = 'UPDATE ecs_community ' .
123 'SET own_id = ' . $this->db->quote($this->getOwnId(), 'integer') . ', ' .
124 'cname = ' . $this->db->quote($this->getCommunityName(), 'text') . ', ' .
125 'mids = ' . $this->db->quote(serialize($this->getMids()), 'text') . ' ' .
126 'WHERE sid = ' . $this->db->quote($this->getServerId(), 'integer') . ' ' .
127 'AND cid = ' . $this->db->quote($this->getCommunityId(), 'integer');
128 $this->db->manipulate($query);
129 return true;
130 }
131
132
133
137 protected function create(): bool
138 {
139 $query = 'INSERT INTO ecs_community (sid,cid,own_id,cname,mids) ' .
140 'VALUES( ' .
141 $this->db->quote($this->getServerId(), 'integer') . ', ' .
142 $this->db->quote($this->getCommunityId(), 'integer') . ', ' .
143 $this->db->quote($this->getOwnId(), 'integer') . ', ' .
144 $this->db->quote($this->getCommunityName(), 'text') . ', ' .
145 $this->db->quote(serialize($this->getMids()), 'text') . ' ' .
146 ')';
147 $this->db->manipulate($query);
148 return true;
149 }
150
154 private function read(): void
155 {
156 $this->entryExists = false;
157
158 $query = 'SELECT * FROM ecs_community ' .
159 'WHERE sid = ' . $this->db->quote($this->getServerId(), 'integer') . ' ' .
160 'AND cid = ' . $this->db->quote($this->getCommunityId(), 'integer');
161 $res = $this->db->query($query);
162 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
163 $this->entryExists = true;
164 $this->setOwnId((int) $row->own_id);
165 $this->setCommunityName($row->cname);
166 $this->setMids(unserialize($row->mids, ['allowed_classes' => true]));
167 }
168 }
169
176 public function deleteByServerId(int $a_server_id): bool
177 {
178 $query = 'DELETE FROM ecs_community' .
179 ' WHERE sid = ' . $this->db->quote($a_server_id, 'integer');
180 $this->db->manipulate($query);
181 return true;
182 }
183}
static getInstance(int $a_server_id, int $a_community_id)
Get instance.
__construct(int $server_id, int $community_id)
Singleton constructor.
update()
Create or update ecs community.
deleteByServerId(int $a_server_id)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26