ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ilBookingObject.php
Go to the documentation of this file.
1<?php
2
24{
25 protected \ILIAS\BookingManager\Objects\ObjectsManager $objects_manager;
26 protected \ILIAS\BookingManager\InternalRepoService $repo;
27 protected ilDBInterface $db;
28 protected int $id = 0;
29 protected int $pool_id = 0;
30 protected string $title = "";
31 protected string $description = "";
32 protected int $nr_of_items = 0;
33 protected ?int $schedule_id = null;
34 protected string $info_file = "";
35 protected string $post_text = "";
36 protected string $post_file = "";
37 protected string $object_info_rid = "";
38 protected string $booking_info_rid = "";
39
40 public function __construct(
41 ?int $a_id = null
42 ) {
43 global $DIC;
44
45 $this->db = $DIC->database();
46 $this->id = (int) $a_id;
47 $this->read();
48 $this->repo = $DIC->bookingManager()->internal()->repo();
49 $this->objects_manager = $DIC->bookingManager()->internal()->domain()->objects($this->getPoolId());
50 }
51
52 public function getId(): int
53 {
54 return $this->id;
55 }
56
57 public function setTitle(string $a_title): void
58 {
59 $this->title = $a_title;
60 }
61
62 public function getTitle(): string
63 {
64 return $this->title;
65 }
66
67 public function setDescription(string $a_value): void
68 {
69 $this->description = $a_value;
70 }
71
72 public function getDescription(): string
73 {
74 return $this->description;
75 }
76
77 public function setPoolId(int $a_pool_id): void
78 {
79 $this->pool_id = $a_pool_id;
80 }
81
82 public function getPoolId(): int
83 {
84 return $this->pool_id;
85 }
86
87 public function setScheduleId(?int $a_schedule_id): void
88 {
89 $this->schedule_id = $a_schedule_id;
90 }
91
92 public function getScheduleId(): ?int
93 {
94 return $this->schedule_id;
95 }
96
97 public function setNrOfItems(int $a_value): void
98 {
99 $this->nr_of_items = $a_value;
100 }
101
102 public function getNrOfItems(): int
103 {
104 return $this->nr_of_items;
105 }
106
107 public function setFile(string $a_value): void
108 {
109 $this->info_file = $a_value;
110 }
111
112 public function getFile(): string
113 {
114 return $this->info_file;
115 }
116
117 public function getFileFullPath(): string
118 {
119 if ($this->id && $this->info_file) {
120 return $this->objects_manager->getObjectInfoPath($this->id);
121 }
122 return "";
123 }
124
125 public function setPostText(string $a_value): void
126 {
127 $this->post_text = $a_value;
128 }
129
130 public function getPostText(): string
131 {
132 return $this->post_text;
133 }
134
135 public function setPostFile(string $a_value): void
136 {
137 $this->post_file = $a_value;
138 }
139
140 public function getPostFile(): string
141 {
142 return $this->post_file;
143 }
144
145 public function getPostFileFullPath(): string
146 {
147 if ($this->id && $this->post_file) {
148 return $this->objects_manager->getBookingInfoPath($this->id);
149 }
150 return "";
151 }
152
153
154 public function deleteFiles(): void
155 {
156 if ($this->id) {
157 $this->objects_manager->deleteObjectInfo($this->id);
158 $this->objects_manager->deleteBookingInfo($this->id);
159 $this->setFile("");
160 $this->setPostFile("");
161 }
162 }
163
164 protected function read(): void
165 {
166 if (!$this->id) {
167 return;
168 }
169
170 $set = $this->db->queryF(
171 'SELECT * FROM booking_object WHERE booking_object_id = %s',
173 [$this->id]
174 );
175 $row = $this->db->fetchAssoc($set);
176
177 if ($row === null || $row === []) {
178 return;
179 }
180
181 $this->setTitle((string) $row['title']);
182 $this->setDescription((string) $row['description']);
183 $this->setPoolId((int) $row['pool_id']);
184 $this->setScheduleId($row['schedule_id']);
185 $this->setNrOfItems((int) $row['nr_items']);
186 $this->setFile((string) $row['info_file']);
187 $this->setPostText((string) $row['post_text']);
188 $this->setPostFile((string) $row['post_file']);
189 }
190
194 protected function getDBFields(): array
195 {
196 return [
197 'title' => [ilDBConstants::T_TEXT, $this->getTitle()],
198 'description' => [ilDBConstants::T_TEXT, $this->getDescription()],
199 'schedule_id' => [ilDBConstants::T_TEXT, $this->getScheduleId()],
200 'nr_items' => [ilDBConstants::T_TEXT, $this->getNrOfItems()],
201 'info_file' => [ilDBConstants::T_TEXT, $this->getFile()],
202 'post_text' => [ilDBConstants::T_TEXT, $this->getPostText()],
203 'post_file' => [ilDBConstants::T_TEXT, $this->getPostFile()]
204 ];
205 }
206
207 public function save(): ?int
208 {
209 if ($this->id !== 0) {
210 return null;
211 }
212
213 $this->id = $this->db->nextId('booking_object');
214
215 $fields = $this->getDBFields();
216 $fields['booking_object_id'] = [ilDBConstants::T_INTEGER, $this->id];
217 $fields['pool_id'] = [ilDBConstants::T_INTEGER, $this->getPoolId()];
218
219 return $this->db->insert('booking_object', $fields);
220 }
221
222 public function update(): ?int
223 {
224 if ($this->id === 0) {
225 return null;
226 }
227
228 return $this->db->update(
229 'booking_object',
230 $this->getDBFields(),
231 ['booking_object_id' => [ilDBConstants::T_INTEGER, $this->id]]
232 );
233 }
234
238 public static function getList(
239 int $a_pool_id,
240 ?string $a_title = null
241 ): array {
242 global $DIC;
243
244 $ilDB = $DIC->database();
245
246 $sql = 'SELECT *' .
247 ' FROM booking_object' .
248 ' WHERE pool_id = ' . $ilDB->quote($a_pool_id, 'integer');
249
250 if ($a_title) {
251 $sql .= ' AND (' . $ilDB->like('title', 'text', '%' . $a_title . '%') .
252 ' OR ' . $ilDB->like('description', 'text', '%' . $a_title . '%') . ')';
253 }
254
255 $sql .= ' ORDER BY title';
256
257 $set = $ilDB->query($sql);
258 $res = array();
259 while ($row = $ilDB->fetchAssoc($set)) {
260 $res[] = $row;
261 }
262 return $res;
263 }
264
268 public static function getNumberOfObjectsForPool(
269 int $a_pool_id
270 ): int {
271 global $DIC;
272
273 $ilDB = $DIC->database();
274
275 $sql = 'SELECT count(*) as count' .
276 ' FROM booking_object' .
277 ' WHERE pool_id = ' . $ilDB->quote($a_pool_id, 'integer');
278 $set = $ilDB->query($sql);
279 $rec = $ilDB->fetchAssoc($set);
280
281 return (int) $rec["count"];
282 }
283
287 public static function getObjectsForPool(
288 int $a_pool_id
289 ): array {
290 global $DIC;
291 $ilDB = $DIC->database();
292
293 $set = $ilDB->query("SELECT booking_object_id" .
294 " FROM booking_object" .
295 " WHERE pool_id = " . $ilDB->quote($a_pool_id, 'integer'));
296
297 $objects = array();
298 while ($row = $ilDB->fetchAssoc($set)) {
299 $objects[] = (int) $row['booking_object_id'];
300 }
301
302 return $objects;
303 }
304
305
309 public function delete(): int
310 {
311 $ilDB = $this->db;
312
313 if ($this->id) {
314 $this->deleteFiles();
315
316 return $ilDB->manipulate('DELETE FROM booking_object' .
317 ' WHERE booking_object_id = ' . $ilDB->quote($this->id, 'integer'));
318 }
319 return 0;
320 }
321
322 public function deleteReservationsAndCalEntries(int $object_id): void
323 {
324 $reservation_db = $this->repo->reservation();
325 $reservation_ids = $reservation_db->getReservationIdsByBookingObjectId($object_id);
326
327 foreach ($reservation_ids as $reservation_id) {
328 $reservation = new ilBookingReservation($reservation_id);
329 $entry = new ilCalendarEntry($reservation->getCalendarEntry());
330 $reservation_db->delete($reservation_id);
331 $entry->delete();
332 }
333 }
334
340 public static function getNrOfItemsForObjects(
341 array $a_obj_ids
342 ): array {
343 global $DIC;
344
345 $ilDB = $DIC->database();
346
347 $map = array();
348
349 $set = $ilDB->query("SELECT booking_object_id,nr_items" .
350 " FROM booking_object" .
351 " WHERE " . $ilDB->in("booking_object_id", $a_obj_ids, "", "integer"));
352 while ($row = $ilDB->fetchAssoc($set)) {
353 $map[(int) $row["booking_object_id"]] = (int) $row["nr_items"];
354 }
355
356 return $map;
357 }
358
359 public function doClone(
360 int $a_pool_id,
361 ?array $a_schedule_map = null
362 ): void {
363 $new_obj = new self();
364 $new_obj->setPoolId($a_pool_id);
365 $new_obj->setTitle($this->getTitle());
366 $new_obj->setDescription($this->getDescription());
367 $new_obj->setNrOfItems($this->getNrOfItems());
368 $new_obj->setFile($this->getFile());
369 $new_obj->setPostText($this->getPostText());
370 $new_obj->setPostFile($this->getPostFile());
371
372 if ($a_schedule_map) {
373 $schedule_id = $this->getScheduleId();
374 if ($schedule_id) {
375 $new_obj->setScheduleId($a_schedule_map[$schedule_id] ?? null);
376 }
377 }
378
379 $new_obj->save();
380
381 $this->objects_manager->cloneTo($this->getId(), $new_obj->getId());
382 }
383
384 public static function lookupPoolId(int $object_id): int
385 {
386 global $DIC;
387
388 $db = $DIC->database();
389 $set = $db->queryF(
390 "SELECT pool_id FROM booking_object " .
391 " WHERE booking_object_id = %s ",
392 array("integer"),
393 array($object_id)
394 );
395 $rec = $db->fetchAssoc($set);
396 return (int) $rec["pool_id"];
397 }
398
399 public static function lookupTitle(int $object_id): string
400 {
401 global $DIC;
402
403 $db = $DIC->database();
404 $set = $db->queryF(
405 "SELECT title FROM booking_object " .
406 " WHERE booking_object_id = %s ",
407 array("integer"),
408 array($object_id)
409 );
410 $rec = $db->fetchAssoc($set);
411 return $rec["title"];
412 }
413}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getNumberOfObjectsForPool(int $a_pool_id)
Get number of booking objects for given booking pool id.
ILIAS BookingManager InternalRepoService $repo
static getList(int $a_pool_id, ?string $a_title=null)
Get list of booking objects.
setPostText(string $a_value)
doClone(int $a_pool_id, ?array $a_schedule_map=null)
static getNrOfItemsForObjects(array $a_obj_ids)
Get nr of available items for a set of object ids.
setFile(string $a_value)
setPoolId(int $a_pool_id)
setDescription(string $a_value)
__construct(?int $a_id=null)
static lookupTitle(int $object_id)
static lookupPoolId(int $object_id)
setNrOfItems(int $a_value)
ILIAS BookingManager Objects ObjectsManager $objects_manager
deleteReservationsAndCalEntries(int $object_id)
setScheduleId(?int $a_schedule_id)
static getObjectsForPool(int $a_pool_id)
Get all booking pool object ids from an specific booking pool.
setPostFile(string $a_value)
setTitle(string $a_title)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Model for a calendar entry.
Interface ilDBInterface.
fetchAssoc(ilDBStatement $statement)
queryF(string $query, array $types, array $values)
foreach($_POST as $key=> $value) $res
global $DIC
Definition: shib_login.php:26