ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
CollectionDBRepository.php
Go to the documentation of this file.
1 <?php
2 
20 
30 
38 {
39  public const COLLECTION_TABLE_NAME = 'il_resource_rc';
40  public const COLLECTION_ASSIGNMENT_TABLE_NAME = 'il_resource_rca';
41  public const R_IDENTIFICATION = 'rid';
42  public const C_IDENTIFICATION = 'rcid';
43  protected \ilDBInterface $db;
44 
45  public function __construct(\ilDBInterface $db)
46  {
47  $this->db = $db;
48  }
49 
53  public function getNamesForLocking(): array
54  {
55  return [self::COLLECTION_TABLE_NAME, self::COLLECTION_ASSIGNMENT_TABLE_NAME];
56  }
57 
58  public function blank(
59  ResourceCollectionIdentification $identification,
60  ?int $owner_id = null,
61  ?string $title = null
63  return new ResourceCollection(
64  $identification,
66  $title ?? ''
67  );
68  }
69 
71  {
72  $q = "SELECT owner_id, title FROM " . self::COLLECTION_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s";
73  $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
74  $d = $this->db->fetchObject($r);
75  $owner_id = (int) ($d->owner_id ?? ResourceCollection::NO_SPECIFIC_OWNER);
76  $title = (string) ($d->title ?? '');
77 
78  return $this->blank($identification, $owner_id, $title);
79  }
80 
81  public function has(ResourceCollectionIdentification $identification): bool
82  {
83  $q = "SELECT EXISTS (
84  SELECT 1 FROM " . self::COLLECTION_TABLE_NAME . "
85  WHERE " . self::C_IDENTIFICATION . " = %s
86  ) AS found";
87 
88  $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
89  $d = $this->db->fetchAssoc($r);
90 
91  return (bool) ($d['found'] ?? false);
92  }
93 
98  {
99  $q = "SELECT " . self::R_IDENTIFICATION . " FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s ORDER BY position ASC";
100  $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
101  while ($d = $this->db->fetchAssoc($r)) {
102  yield (string) $d[self::R_IDENTIFICATION];
103  }
104  }
105 
106  public function clear(ResourceCollectionIdentification $identification): void
107  {
108  $q = "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s";
109  $r = $this->db->manipulateF($q, ['text'], [$identification->serialize()]);
110  }
111 
112  public function update(ResourceCollection $collection, DataContainer $event_data_container): void
113  {
114  $identification = $collection->getIdentification();
115  $resource_identifications = $collection->getResourceIdentifications();
116  $owner_id = $collection->getOwner();
117  $title = $collection->getTitle();
118 
119  $resource_identification_strings = array_map(
120  static fn(ResourceIdentification $i): string => $i->serialize(),
121  $resource_identifications
122  );
123 
124  $q = "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s AND "
125  . $this->db->in(self::R_IDENTIFICATION, $resource_identification_strings, true, 'text');
126  $r = $this->db->manipulateF($q, ['text'], [$identification->serialize()]);
127 
128  $missing_resource_identification_string = array_diff(
129  $resource_identification_strings,
130  iterator_to_array($this->getResourceIdStrings($identification))
131  );
132  foreach ($missing_resource_identification_string as $position => $resource_identification_string) {
133  $this->db->insert(self::COLLECTION_ASSIGNMENT_TABLE_NAME, [
134  self::C_IDENTIFICATION => ['text', $identification->serialize()],
135  self::R_IDENTIFICATION => ['text', $resource_identification_string],
136  'position' => ['integer', (int) $position + 1],
137  ]);
138  $event_data_container->append(
139  new CollectionData(['rid' => $resource_identification_string, 'rcid' => $identification->serialize()])
140  );
141  }
142  foreach ($resource_identification_strings as $position => $resource_identification_string) {
143  $this->db->update(
144  self::COLLECTION_ASSIGNMENT_TABLE_NAME,
145  [
146  self::C_IDENTIFICATION => ['text', $identification->serialize()],
147  self::R_IDENTIFICATION => ['text', $resource_identification_string],
148  'position' => ['integer', (int) $position + 1],
149  ],
150  [
151  self::C_IDENTIFICATION => ['text', $identification->serialize()],
152  self::R_IDENTIFICATION => ['text', $resource_identification_string],
153  ]
154  );
155  }
156  if ($this->has($identification)) {
157  $this->db->update(
158  self::COLLECTION_TABLE_NAME,
159  [
160  self::C_IDENTIFICATION => ['text', $identification->serialize()],
161  'title' => ['text', $title ?? ''],
162  'owner_id' => ['integer', $owner_id],
163  ],
164  [
165  self::C_IDENTIFICATION => ['text', $identification->serialize()]
166  ]
167  );
168  } else {
169  $this->db->insert(
170  self::COLLECTION_TABLE_NAME,
171  [
172  self::C_IDENTIFICATION => ['text', $identification->serialize()],
173  'title' => ['text', $title ?? ''],
174  'owner_id' => ['integer', $owner_id],
175  ]
176  );
177  }
178  }
179 
180  public function removeResourceFromAllCollections(ResourceIdentification $resource_identification): void
181  {
182  $this->db->manipulateF(
183  "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::R_IDENTIFICATION . " = %s",
184  ['text'],
185  [$resource_identification->serialize()]
186  );
187  }
188 
189  public function delete(ResourceCollectionIdentification $identification): void
190  {
191  $this->db->manipulateF(
192  "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s",
193  ['text'],
194  [$identification->serialize()]
195  );
196  $this->db->manipulateF(
197  "DELETE FROM " . self::COLLECTION_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s",
198  ['text'],
199  [$identification->serialize()]
200  );
201  }
202 
203  public function preload(array $identification_strings): void
204  {
205  // TODO: Implement preload() method.
206  }
207 
208  public function populateFromArray(array $data): void
209  {
210  // TODO: Implement populateFromArray() method.
211  }
212 }
removeResourceFromAllCollections(ResourceIdentification $resource_identification)
update(ResourceCollection $collection, DataContainer $event_data_container)
clear(ResourceCollectionIdentification $identification)
getResourceIdStrings(ResourceCollectionIdentification $identification)
$q
Definition: shib_logout.php:18
existing(ResourceCollectionIdentification $identification)
has(ResourceCollectionIdentification $identification)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
blank(ResourceCollectionIdentification $identification, ?int $owner_id=null, ?string $title=null)
$r