ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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 
82  public function has(ResourceCollectionIdentification $identification): bool
83  {
84  $q = "SELECT " . self::C_IDENTIFICATION . " FROM " . self::COLLECTION_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s";
85  $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
86 
87  return ($r->numRows() === 1);
88  }
89 
94  {
95  $q = "SELECT " . self::R_IDENTIFICATION . " FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s ORDER BY position ASC";
96  $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
97  while ($d = $this->db->fetchAssoc($r)) {
98  yield (string)$d[self::R_IDENTIFICATION];
99  }
100  }
101 
102  public function clear(ResourceCollectionIdentification $identification): void
103  {
104  $q = "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s";
105  $r = $this->db->manipulateF($q, ['text'], [$identification->serialize()]);
106  }
107 
108  public function update(ResourceCollection $collection, DataContainer $event_data_container): void
109  {
110  $identification = $collection->getIdentification();
111  $resource_identifications = $collection->getResourceIdentifications();
112  $owner_id = $collection->getOwner();
113  $title = $collection->getTitle();
114 
115  $resource_identification_strings = array_map(
116  fn(ResourceIdentification $i): string => $i->serialize(),
117  $resource_identifications
118  );
119 
120  $q = "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s AND "
121  . $this->db->in(self::R_IDENTIFICATION, $resource_identification_strings, true, 'text');
122  $r = $this->db->manipulateF($q, ['text'], [$identification->serialize()]);
123 
124  $missing_resource_identification_string = array_diff(
125  $resource_identification_strings,
126  iterator_to_array($this->getResourceIdStrings($identification))
127  );
128  foreach ($missing_resource_identification_string as $position => $resource_identification_string) {
129  $this->db->insert(self::COLLECTION_ASSIGNMENT_TABLE_NAME, [
130  self::C_IDENTIFICATION => ['text', $identification->serialize()],
131  self::R_IDENTIFICATION => ['text', $resource_identification_string],
132  'position' => ['integer', (int)$position + 1],
133  ]);
134  $event_data_container->append(
135  new CollectionData(['rid' => $resource_identification_string, 'rcid' => $identification->serialize()])
136  );
137  }
138  foreach ($resource_identification_strings as $position => $resource_identification_string) {
139  $this->db->update(
140  self::COLLECTION_ASSIGNMENT_TABLE_NAME,
141  [
142  self::C_IDENTIFICATION => ['text', $identification->serialize()],
143  self::R_IDENTIFICATION => ['text', $resource_identification_string],
144  'position' => ['integer', (int)$position + 1],
145  ],
146  [
147  self::C_IDENTIFICATION => ['text', $identification->serialize()],
148  self::R_IDENTIFICATION => ['text', $resource_identification_string],
149  ]
150  );
151  }
152  if ($this->has($identification)) {
153  $this->db->update(
154  self::COLLECTION_TABLE_NAME,
155  [
156  self::C_IDENTIFICATION => ['text', $identification->serialize()],
157  'title' => ['text', $title ?? ''],
158  'owner_id' => ['integer', $owner_id],
159  ],
160  [
161  self::C_IDENTIFICATION => ['text', $identification->serialize()]
162  ]
163  );
164  } else {
165  $this->db->insert(
166  self::COLLECTION_TABLE_NAME,
167  [
168  self::C_IDENTIFICATION => ['text', $identification->serialize()],
169  'title' => ['text', $title ?? ''],
170  'owner_id' => ['integer', $owner_id],
171  ]
172  );
173  }
174  }
175 
176  public function removeResourceFromAllCollections(ResourceIdentification $resource_identification): void
177  {
178  $this->db->manipulateF(
179  "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::R_IDENTIFICATION . " = %s",
180  ['text'],
181  [$resource_identification->serialize()]
182  );
183  }
184 
185  public function delete(ResourceCollectionIdentification $identification): void
186  {
187  $this->db->manipulateF(
188  "DELETE FROM " . self::COLLECTION_ASSIGNMENT_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s",
189  ['text'],
190  [$identification->serialize()]
191  );
192  $this->db->manipulateF(
193  "DELETE FROM " . self::COLLECTION_TABLE_NAME . " WHERE " . self::C_IDENTIFICATION . " = %s",
194  ['text'],
195  [$identification->serialize()]
196  );
197  }
198 
199 
200  public function preload(array $identification_strings): void
201  {
202  // TODO: Implement preload() method.
203  }
204 
205  public function populateFromArray(array $data): void
206  {
207  // TODO: Implement populateFromArray() method.
208  }
209 }
removeResourceFromAllCollections(ResourceIdentification $resource_identification)
update(ResourceCollection $collection, DataContainer $event_data_container)
clear(ResourceCollectionIdentification $identification)
getResourceIdStrings(ResourceCollectionIdentification $identification)
$q
Definition: shib_logout.php:21
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