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