ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
CollectionDBRepository.php
Go to the documentation of this file.
1<?php
2
20
27
35{
36 public const COLLECTION_TABLE_NAME = 'il_resource_rc';
37 public const COLLECTION_ASSIGNMENT_TABLE_NAME = 'il_resource_rca';
38 public const R_IDENTIFICATION = 'rid';
39 public const C_IDENTIFICATION = 'rcid';
40
42 private array $resource_ids_cache = [];
43
44 public function __construct(protected \ilDBInterface $db)
45 {
46 }
47
51 public function getNamesForLocking(): array
52 {
54 }
55
56 public function blank(
58 ?int $owner_id = null,
59 ?string $title = null
61 $collection = new ResourceCollection(
62 $identification,
63 $owner_id ?? ResourceCollection::NO_SPECIFIC_OWNER,
64 $title ?? ''
65 );
66
67 $rcid = $identification->serialize();
68 if (!isset($this->resource_ids_cache[$rcid])) {
69 $this->resource_ids_cache[$rcid] = [];
70 }
71
72 return $collection;
73 }
74
76 {
77 $q = "SELECT owner_id, title FROM " . self::COLLECTION_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s";
78 $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
79 $d = $this->db->fetchObject($r);
80 $owner_id = (int) ($d->owner_id ?? ResourceCollection::NO_SPECIFIC_OWNER);
81 $title = (string) ($d->title ?? '');
82
83 return $this->blank($identification, $owner_id, $title);
84 }
85
86 public function has(ResourceCollectionIdentification $identification): bool
87 {
88 $q = "SELECT EXISTS (
89 SELECT 1 FROM " . self::COLLECTION_TABLE_NAME . "
90 WHERE " . self::C_IDENTIFICATION . " = %s
91 ) AS found";
92
93 $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
94 $d = $this->db->fetchAssoc($r);
95
96 return (bool) ($d['found'] ?? false);
97 }
98
102 public function getResourceIdStrings(ResourceCollectionIdentification $identification): \Generator
103 {
104 $rcid = $identification->serialize();
105
106 if (!isset($this->resource_ids_cache[$rcid])) {
107 $this->preload([$rcid]);
108 }
109
110 foreach ($this->resource_ids_cache[$rcid] ?? [] as $rid) {
111 yield $rid;
112 }
113 }
114
115 public function clear(ResourceCollectionIdentification $identification): void
116 {
117 $rcid = $identification->serialize();
118 $q = "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s";
119 $this->db->manipulateF($q, ['text'], [$rcid]);
120
121 $this->resource_ids_cache[$rcid] = [];
122 }
123
124 public function update(ResourceCollection $collection, DataContainer $event_data_container): void
125 {
126 $identification = $collection->getIdentification();
127 $resource_identifications = $collection->getResourceIdentifications();
128 $owner_id = $collection->getOwner();
129 $title = $collection->getTitle();
130
131 $resource_identification_strings = array_map(
132 static fn(ResourceIdentification $i): string => $i->serialize(),
133 $resource_identifications
134 );
135
136 $q = "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s AND "
137 . $this->db->in(self::R_IDENTIFICATION, $resource_identification_strings, true, 'text');
138 $r = $this->db->manipulateF($q, ['text'], [$identification->serialize()]);
139
140 $missing_resource_identification_string = array_diff(
141 $resource_identification_strings,
142 iterator_to_array($this->getResourceIdStrings($identification))
143 );
144 foreach ($missing_resource_identification_string as $position => $resource_identification_string) {
145 $this->db->insert(self::COLLECTION_ASSIGNMENT_TABLE_NAME, [
146 self::C_IDENTIFICATION => ['text', $identification->serialize()],
147 self::R_IDENTIFICATION => ['text', $resource_identification_string],
148 'position' => ['integer', (int) $position + 1],
149 ]);
150 $event_data_container->append(
151 new CollectionData(['rid' => $resource_identification_string, 'rcid' => $identification->serialize()])
152 );
153 }
154 foreach ($resource_identification_strings as $position => $resource_identification_string) {
155 $this->db->update(
156 self::COLLECTION_ASSIGNMENT_TABLE_NAME,
157 [
158 self::C_IDENTIFICATION => ['text', $identification->serialize()],
159 self::R_IDENTIFICATION => ['text', $resource_identification_string],
160 'position' => ['integer', (int) $position + 1],
161 ],
162 [
163 self::C_IDENTIFICATION => ['text', $identification->serialize()],
164 self::R_IDENTIFICATION => ['text', $resource_identification_string],
165 ]
166 );
167 }
168
169 $this->resource_ids_cache[$identification->serialize()] = array_values($resource_identification_strings);
170 if ($this->has($identification)) {
171 $this->db->update(
172 self::COLLECTION_TABLE_NAME,
173 [
174 self::C_IDENTIFICATION => ['text', $identification->serialize()],
175 'title' => ['text', $title ?? ''],
176 'owner_id' => ['integer', $owner_id],
177 ],
178 [
179 self::C_IDENTIFICATION => ['text', $identification->serialize()]
180 ]
181 );
182 } else {
183 $this->db->insert(
184 self::COLLECTION_TABLE_NAME,
185 [
186 self::C_IDENTIFICATION => ['text', $identification->serialize()],
187 'title' => ['text', $title ?? ''],
188 'owner_id' => ['integer', $owner_id],
189 ]
190 );
191 }
192 }
193
194 public function removeResourceFromAllCollections(ResourceIdentification $resource_identification): void
195 {
196 $rid = $resource_identification->serialize();
197
198 $this->db->manipulateF(
199 "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::R_IDENTIFICATION . " = %s",
200 ['text'],
201 [$rid]
202 );
203
204 foreach ($this->resource_ids_cache as $rcid => $rids) {
205 if (in_array($rid, $rids, true)) {
206 $this->resource_ids_cache[$rcid] = array_values(array_diff($rids, [$rid]));
207 }
208 }
209 }
210
211 public function delete(ResourceCollectionIdentification $identification): void
212 {
213 $rcid = $identification->serialize();
214
215 $this->db->manipulateF(
216 "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s",
217 ['text'],
218 [$rcid]
219 );
220 $this->db->manipulateF(
221 "DELETE FROM " . self::COLLECTION_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s",
222 ['text'],
223 [$rcid]
224 );
225
226 unset($this->resource_ids_cache[$rcid]);
227 }
228
229 public function preload(array $identification_strings): void
230 {
231 if ($identification_strings === []) {
232 return;
233 }
234
235 $identification_strings = array_values(array_unique($identification_strings));
236
237 $to_load = [];
238 foreach ($identification_strings as $rcid) {
239 if (!isset($this->resource_ids_cache[$rcid])) {
240 $this->resource_ids_cache[$rcid] = [];
241 $to_load[] = $rcid;
242 }
243 }
244
245 if ($to_load === []) {
246 return;
247 }
248
249 $q = "SELECT " . self::C_IDENTIFICATION . ", " . self::R_IDENTIFICATION .
250 " FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME .
251 " WHERE " . $this->db->in(self::C_IDENTIFICATION, $to_load, false, 'text') .
252 " ORDER BY position ASC";
253
254 $res = $this->db->query($q);
255 while ($row = $this->db->fetchAssoc($res)) {
256 $rcid = (string) $row[self::C_IDENTIFICATION];
257 $rid = (string) $row[self::R_IDENTIFICATION];
258 $this->resource_ids_cache[$rcid][] = $rid;
259 }
260 }
261
266 public function getResourceIdsForCollections(array $collection_identifications): array
267 {
268 if ($collection_identifications === []) {
269 return [];
270 }
271
272 $collection_identifications = array_values(array_unique($collection_identifications));
273
274 $to_preload = [];
275 foreach ($collection_identifications as $rcid) {
276 if (!isset($this->resource_ids_cache[$rcid])) {
277 $to_preload[] = $rcid;
278 }
279 }
280 if ($to_preload !== []) {
281 $this->preload($to_preload);
282 }
283
284 $result_rids = [];
285 foreach ($collection_identifications as $rcid) {
286 foreach ($this->resource_ids_cache[$rcid] ?? [] as $rid) {
287 $result_rids[] = $rid;
288 }
289 }
290
291 $result_rids = array_values(array_unique($result_rids));
292
293 return array_map(
294 static fn(string $rid): ResourceIdentification => new ResourceIdentification($rid),
295 $result_rids
296 );
297 }
298
299 public function populateFromArray(array $data): void
300 {
301 // Nothing to do here
302 }
303}
update(ResourceCollection $collection, DataContainer $event_data_container)
clear(ResourceCollectionIdentification $identification)
blank(ResourceCollectionIdentification $identification, ?int $owner_id=null, ?string $title=null)
removeResourceFromAllCollections(ResourceIdentification $resource_identification)
has(ResourceCollectionIdentification $identification)
existing(ResourceCollectionIdentification $identification)
getResourceIdStrings(ResourceCollectionIdentification $identification)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
has(string $class_name)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$q
Definition: shib_logout.php:25