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