ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
CachedRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\ILIASObject\Properties\ObjectTypeSpecificProperties\Factory as ObjectTypeSpecificPropertiesFactory;
25 
30 class CachedRepository implements Repository
31 {
32  private const CORE_PROPERTIES_TABLE = 'object_data';
33  private const DESCRIPTION_TABLE = 'object_description';
34 
35  private array $data_cache = [];
36 
37  public function __construct(
38  private readonly \ilDBInterface $database,
39  private readonly \ilObjectDefinition $obj_definition,
40  private readonly ResourceStorageService $storage_services,
41  private readonly TileImage\Stakeholder $storage_stakeholder,
42  private readonly TileImage\FlavourDefinition $flavour_definition,
43  private readonly ObjectTypeSpecificPropertiesFactory $object_type_specific_properties_factory
44  ) {
45  }
46 
47  public function preload(array $object_ids): void
48  {
49  $this->data_cache += $this->retrieveDataForObjectIds($object_ids);
50  }
51 
52  public function resetPreloadedData(): void
53  {
54  $this->data_cache = [];
55  }
56 
57  public function getFor(?int $object_id, ?string $type = null): CoreProperties
58  {
59  if ($object_id === null
60  || $object_id === 0) {
61  return $this->getDefaultCoreProperties($type);
62  }
63 
64  if (!isset($this->data_cache[$object_id])) {
65  $this->data_cache[$object_id] = $this->retrieveDataForObjectId($object_id);
66  }
67 
68  $data = $this->data_cache[$object_id];
69 
70  $object_type_specific_properties = $this->object_type_specific_properties_factory->getForObjectTypeString($data['type']);
71  $providers = null;
72  $modifications = null;
73  if ($object_type_specific_properties !== null) {
74  $providers = $object_type_specific_properties->getProviders();
75  $modifications = $object_type_specific_properties->getModifications();
76  }
77  return new CoreProperties(
79  array_shift($data),
80  array_shift($data),
81  $modifications
82  ),
83  new Online(array_shift($data)),
84  new TileImage\Property(
85  new TileImage\TileImage(
86  $object_id,
87  $data['type'],
88  array_shift($data),
89  $this->storage_services,
90  $this->storage_stakeholder,
91  $this->flavour_definition,
92  $providers
93  )
94  ),
95  $data
96  );
97  }
98 
99  public function store(CoreProperties $properties): CoreProperties
100  {
101  if ($properties->getObjectId() === null || $properties->getOwner() === null) {
102  throw new \Exception('The current configuration cannot be saved.');
103  }
104 
105  if ($properties->getPropertyTileImage()->getDeletedFlag()) {
106  $this->deleteOldTileImage($properties->getPropertyTileImage()->getTileImage());
107  $properties = $properties->withPropertyTileImage(
108  $properties->getPropertyTileImage()->withTileImage(
109  $properties->getPropertyTileImage()->getTileImage()->withRid(null)
110  )
111  );
112  }
113 
114  $where = [
115  'obj_id' => [\ilDBConstants::T_INTEGER, $properties->getObjectId()]
116  ];
117 
118  $storage_array = [
119  'type' => [\ilDBConstants::T_TEXT, $properties->getType()],
120  'title' => [\ilDBConstants::T_TEXT, $properties->getPropertyTitleAndDescription()->getTitle()],
121  'description' => [\ilDBConstants::T_TEXT, $properties->getPropertyTitleAndDescription()->getDescription()],
122  'owner' => [\ilDBConstants::T_INTEGER, $properties->getOwner()],
123  'create_date' => [\ilDBConstants::T_DATETIME, $properties->getCreateDate()?->format('Y-m-d H:i:s')],
124  'last_update' => [\ilDBConstants::T_DATETIME, (new \DateTimeImmutable('now', new \DateTimeZone('UTC')))->format('Y-m-d H:i:s')],
125  'import_id' => [\ilDBConstants::T_TEXT, $properties->getImportId()],
126  'offline' => [\ilDBConstants::T_INTEGER, (int) !$properties->getPropertyIsOnline()->getIsOnline()],
127  'tile_image_rid' => [\ilDBConstants::T_TEXT, $properties->getPropertyTileImage()->getTileImage()->getRid()]
128  ];
129  $this->database->update(self::CORE_PROPERTIES_TABLE, $storage_array, $where);
130 
131  if ($this->obj_definition->isRBACObject($properties->getType())) {
132  $this->storeLongDescription($properties->getPropertyTitleAndDescription()->getLongDescription(), $where);
133  }
134 
135  unset($this->data_cache[$properties->getObjectId()]);
136 
137  return $properties;
138  }
139 
140  private function deleteOldTileImage(TileImage\TileImage $tile_image): void
141  {
142  if ($tile_image->getRid() === null) {
143  return;
144  }
145 
146  $i = $this->storage_services->manage()->find($tile_image->getRid());
147  if ($i === null) {
148  return;
149  }
150 
151  $this->storage_services->manage()->remove(
152  $i,
153  $this->storage_stakeholder
154  );
155  }
156 
157  private function getDefaultCoreProperties(?string $type): CoreProperties
158  {
159  return (new CoreProperties(
160  new TitleAndDescription(),
161  new Online(),
162  new TileImage\Property()
163  ))->withType($type);
164  }
165 
169  protected function retrieveDataForObjectId(int $object_id): array
170  {
171  $where = 'WHERE obj.obj_id=' . $this->database->quote($object_id, 'integer');
172  $data = $this->retrieveDataForWhereClause($where);
173 
174  if ($data === []) {
175  throw new \Exception('The object with the following id does not exist: '
176  . (string) $object_id);
177  }
178 
179  return $data[$object_id];
180  }
181 
185  protected function retrieveDataForObjectIds(array $object_ids): array
186  {
187  $where = 'WHERE ' . $this->database->in('obj.obj_id', $object_ids, false, \ilDBConstants::T_INTEGER);
188  return $this->retrieveDataForWhereClause($where);
189  }
190 
191  protected function retrieveDataForWhereClause(string $where): array
192  {
193  $query = 'SELECT '
194  . 'obj.obj_id, obj.type, obj.title, obj.description, obj.owner,' . PHP_EOL
195  . 'obj.create_date, obj.last_update, obj.import_id, obj.offline,' . PHP_EOL
196  . 'obj.tile_image_rid, descr.description' . PHP_EOL
197  . 'FROM ' . self::CORE_PROPERTIES_TABLE . ' AS obj' . PHP_EOL
198  . 'LEFT JOIN ' . self::DESCRIPTION_TABLE . ' AS descr' . PHP_EOL
199  . 'ON obj.obj_id = descr.obj_id' . PHP_EOL
200  . $where;
201 
202  $statement = $this->database->query($query);
203  $num_rows = $this->database->numRows($statement);
204 
205  if ($num_rows === 0) {
206  return [];
207  }
208 
209  $data = [];
210  while ($row = $this->database->fetchAssoc($statement)) {
211  $data[$row['obj_id']] = [
212  'title' => $row['title'],
213  'long_description' => $row['description'] ?? '',
214  'is_online' => !((bool) $row['offline']),
215  'tile_image_rid' => $row['tile_image_rid'],
216  'object_id' => $row['obj_id'],
217  'type' => $row['type'],
218  'owner' => $row['owner'],
219  'import_id' => $row['import_id'],
220  'create_date' => $row['create_date'] !== null ? new \DateTimeImmutable($row['create_date']) : null,
221  'update_date' => $row['last_update'] !== null ? new \DateTimeImmutable($row['last_update']) : null
222  ];
223  }
224 
225  return $data;
226  }
227 
228  protected function storeLongDescription(string $long_description, array $where): void
229  {
230  $description_array = [
231  'description' => [\ilDBConstants::T_TEXT, $long_description]
232  ];
233  $this->database->update(self::DESCRIPTION_TABLE, $description_array, $where);
234  }
235 }
storeLongDescription(string $long_description, array $where)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Builds data types.
Definition: Factory.php:35
withPropertyTileImage(PropertyTileImage $property_tile_image)
__construct(private readonly \ilDBInterface $database, private readonly \ilObjectDefinition $obj_definition, private readonly ResourceStorageService $storage_services, private readonly TileImage\Stakeholder $storage_stakeholder, private readonly TileImage\FlavourDefinition $flavour_definition, private readonly ObjectTypeSpecificPropertiesFactory $object_type_specific_properties_factory)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...