ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilBookingObject.php
Go to the documentation of this file.
1 <?php
2 
24 {
25  protected \ILIAS\BookingManager\InternalRepoService $repo;
26  protected ilDBInterface $db;
27  protected int $id = 0;
28  protected int $pool_id = 0;
29  protected string $title = "";
30  protected string $description = "";
31  protected int $nr_of_items = 0;
32  protected ?int $schedule_id = null;
33  protected string $info_file = "";
34  protected string $post_text = "";
35  protected string $post_file = "";
36 
37  public function __construct(
38  int $a_id = null
39  ) {
40  global $DIC;
41 
42  $this->db = $DIC->database();
43  $this->id = (int) $a_id;
44  $this->read();
45  $this->repo = $DIC->bookingManager()->internal()->repo();
46  }
47 
48  public function getId(): int
49  {
50  return $this->id;
51  }
52 
53  public function setTitle(string $a_title): void
54  {
55  $this->title = $a_title;
56  }
57 
58  public function getTitle(): string
59  {
60  return $this->title;
61  }
62 
63  public function setDescription(string $a_value): void
64  {
65  $this->description = $a_value;
66  }
67 
68  public function getDescription(): string
69  {
70  return $this->description;
71  }
72 
73  public function setPoolId(int $a_pool_id): void
74  {
75  $this->pool_id = $a_pool_id;
76  }
77 
78  public function getPoolId(): int
79  {
80  return $this->pool_id;
81  }
82 
83  public function setScheduleId(?int $a_schedule_id): void
84  {
85  $this->schedule_id = $a_schedule_id;
86  }
87 
88  public function getScheduleId(): ?int
89  {
90  return $this->schedule_id;
91  }
92 
93  public function setNrOfItems(int $a_value): void
94  {
95  $this->nr_of_items = $a_value;
96  }
97 
98  public function getNrOfItems(): int
99  {
100  return $this->nr_of_items;
101  }
102 
103  public function setFile(string $a_value): void
104  {
105  $this->info_file = $a_value;
106  }
107 
108  public function getFile(): string
109  {
110  return $this->info_file;
111  }
112 
113  public function getFileFullPath(): string
114  {
115  if ($this->id && $this->info_file) {
116  $path = self::initStorage($this->id, "file");
117  return $path . $this->info_file;
118  }
119  return "";
120  }
121 
125  public function uploadFile(array $a_upload): bool
126  {
127  if (!$this->id) {
128  return false;
129  }
130 
131  $this->deleteFile();
132 
133  $path = self::initStorage($this->id, "file");
134  $original = $a_upload["name"];
135  if (ilFileUtils::moveUploadedFile($a_upload["tmp_name"], $original, $path . $original)) {
136  chmod($path . $original, 0770);
137 
138  $this->setFile($original);
139  return true;
140  }
141  return false;
142  }
143 
144  // remove existing info file
145  public function deleteFile(): void
146  {
147  if ($this->id) {
148  $path = $this->getFileFullPath();
149  if ($path) {
150  if (is_file($path)) {
151  unlink($path);
152  }
153  $this->setFile("");
154  }
155  }
156  }
157 
158  public function setPostText(string $a_value): void
159  {
160  $this->post_text = $a_value;
161  }
162 
163  public function getPostText(): string
164  {
165  return $this->post_text;
166  }
167 
168  public function setPostFile(string $a_value): void
169  {
170  $this->post_file = $a_value;
171  }
172 
173  public function getPostFile(): string
174  {
175  return $this->post_file;
176  }
177 
178  public function getPostFileFullPath(): string
179  {
180  if ($this->id && $this->post_file) {
181  $path = self::initStorage($this->id, "post");
182  return $path . $this->post_file;
183  }
184  return "";
185  }
186 
190  public function uploadPostFile(array $a_upload): bool
191  {
192  if (!$this->id) {
193  return false;
194  }
195 
196  $this->deletePostFile();
197 
198  $path = self::initStorage($this->id, "post");
199  $original = $a_upload["name"];
200 
201  if (ilFileUtils::moveUploadedFile($a_upload["tmp_name"], $original, $path . $original)) {
202  chmod($path . $original, 0770);
203 
204  $this->setPostFile($original);
205  return true;
206  }
207  return false;
208  }
209 
210  // remove existing post file
211  public function deletePostFile(): void
212  {
213  if ($this->id) {
214  $path = $this->getPostFileFullPath();
215  if ($path) {
216  if (is_file($path)) {
217  unlink($path);
218  }
219  $this->setPostFile("");
220  }
221  }
222  }
223 
224  public function deleteFiles(): void
225  {
226  if ($this->id) {
227  $storage = new ilFSStorageBooking($this->id);
228  $storage->delete();
229 
230  $this->setFile("");
231  $this->setPostFile("");
232  }
233  }
234 
238  public static function initStorage(
239  int $a_id,
240  string $a_subdir = ""
241  ): string {
242  $storage = new ilFSStorageBooking($a_id);
243  $storage->create();
244 
245  $path = $storage->getAbsolutePath() . "/";
246 
247  if ($a_subdir) {
248  $path .= $a_subdir . "/";
249 
250  if (!is_dir($path) && !mkdir($path)) {
251  throw new \RuntimeException(sprintf('Directory "%s" was not created', $path));
252  }
253  }
254 
255  return $path;
256  }
257 
258  protected function read(): void
259  {
260  $ilDB = $this->db;
261 
262  if ($this->id) {
263  $set = $ilDB->query('SELECT *' .
264  ' FROM booking_object' .
265  ' WHERE booking_object_id = ' . $ilDB->quote($this->id, 'integer'));
266  $row = $ilDB->fetchAssoc($set);
267  $this->setTitle((string) $row['title']);
268  $this->setDescription((string) $row['description']);
269  $this->setPoolId((int) $row['pool_id']);
270  $this->setScheduleId($row['schedule_id']);
271  $this->setNrOfItems((int) $row['nr_items']);
272  $this->setFile((string) $row['info_file']);
273  $this->setPostText((string) $row['post_text']);
274  $this->setPostFile((string) $row['post_file']);
275  }
276  }
277 
281  protected function getDBFields(): array
282  {
283  return array(
284  'title' => array('text', $this->getTitle()),
285  'description' => array('text', $this->getDescription()),
286  'schedule_id' => array('text', $this->getScheduleId()),
287  'nr_items' => array('text', $this->getNrOfItems()),
288  'info_file' => array('text', $this->getFile()),
289  'post_text' => array('text', $this->getPostText()),
290  'post_file' => array('text', $this->getPostFile())
291  );
292  }
293 
294  public function save(): ?int
295  {
296  $ilDB = $this->db;
297 
298  if ($this->id) {
299  return null;
300  }
301 
302  $this->id = $ilDB->nextId('booking_object');
303 
304  $fields = $this->getDBFields();
305  $fields['booking_object_id'] = array('integer', $this->id);
306  $fields['pool_id'] = array('integer', $this->getPoolId());
307 
308  return $ilDB->insert('booking_object', $fields);
309  }
310 
311  public function update(): ?int
312  {
313  $ilDB = $this->db;
314 
315  if (!$this->id) {
316  return null;
317  }
318 
319  $fields = $this->getDBFields();
320 
321  return $ilDB->update(
322  'booking_object',
323  $fields,
324  array('booking_object_id' => array('integer', $this->id))
325  );
326  }
327 
331  public static function getList(
332  int $a_pool_id,
333  string $a_title = null
334  ): array {
335  global $DIC;
336 
337  $ilDB = $DIC->database();
338 
339  $sql = 'SELECT *' .
340  ' FROM booking_object' .
341  ' WHERE pool_id = ' . $ilDB->quote($a_pool_id, 'integer');
342 
343  if ($a_title) {
344  $sql .= ' AND (' . $ilDB->like('title', 'text', '%' . $a_title . '%') .
345  ' OR ' . $ilDB->like('description', 'text', '%' . $a_title . '%') . ')';
346  }
347 
348  $sql .= ' ORDER BY title';
349 
350  $set = $ilDB->query($sql);
351  $res = array();
352  while ($row = $ilDB->fetchAssoc($set)) {
353  $res[] = $row;
354  }
355  return $res;
356  }
357 
361  public static function getNumberOfObjectsForPool(
362  int $a_pool_id
363  ): int {
364  global $DIC;
365 
366  $ilDB = $DIC->database();
367 
368  $sql = 'SELECT count(*) as count' .
369  ' FROM booking_object' .
370  ' WHERE pool_id = ' . $ilDB->quote($a_pool_id, 'integer');
371  $set = $ilDB->query($sql);
372  $rec = $ilDB->fetchAssoc($set);
373 
374  return (int) $rec["count"];
375  }
376 
380  public static function getObjectsForPool(
381  int $a_pool_id
382  ): array {
383  global $DIC;
384  $ilDB = $DIC->database();
385 
386  $set = $ilDB->query("SELECT booking_object_id" .
387  " FROM booking_object" .
388  " WHERE pool_id = " . $ilDB->quote($a_pool_id, 'integer'));
389 
390  $objects = array();
391  while ($row = $ilDB->fetchAssoc($set)) {
392  $objects[] = (int) $row['booking_object_id'];
393  }
394 
395  return $objects;
396  }
397 
398 
402  public function delete(): int
403  {
404  $ilDB = $this->db;
405 
406  if ($this->id) {
407  $this->deleteFiles();
408 
409  return $ilDB->manipulate('DELETE FROM booking_object' .
410  ' WHERE booking_object_id = ' . $ilDB->quote($this->id, 'integer'));
411  }
412  return 0;
413  }
414 
415  public function deleteReservationsAndCalEntries(int $object_id): void
416  {
417  $reservation_db = $this->repo->reservation();
418  $reservation_ids = $reservation_db->getReservationIdsByBookingObjectId($object_id);
419 
420  foreach ($reservation_ids as $reservation_id) {
421  $reservation = new ilBookingReservation($reservation_id);
422  $entry = new ilCalendarEntry($reservation->getCalendarEntry());
423  $reservation_db->delete($reservation_id);
424  $entry->delete();
425  }
426  }
427 
433  public static function getNrOfItemsForObjects(
434  array $a_obj_ids
435  ): array {
436  global $DIC;
437 
438  $ilDB = $DIC->database();
439 
440  $map = array();
441 
442  $set = $ilDB->query("SELECT booking_object_id,nr_items" .
443  " FROM booking_object" .
444  " WHERE " . $ilDB->in("booking_object_id", $a_obj_ids, "", "integer"));
445  while ($row = $ilDB->fetchAssoc($set)) {
446  $map[(int) $row["booking_object_id"]] = (int) $row["nr_items"];
447  }
448 
449  return $map;
450  }
451 
452  public function doClone(
453  int $a_pool_id,
454  array $a_schedule_map = null
455  ): void {
456  $new_obj = new self();
457  $new_obj->setPoolId($a_pool_id);
458  $new_obj->setTitle($this->getTitle());
459  $new_obj->setDescription($this->getDescription());
460  $new_obj->setNrOfItems($this->getNrOfItems());
461  $new_obj->setFile($this->getFile());
462  $new_obj->setPostText($this->getPostText());
463  $new_obj->setPostFile($this->getPostFile());
464 
465  if ($a_schedule_map) {
466  $schedule_id = $this->getScheduleId();
467  if ($schedule_id) {
468  $new_obj->setScheduleId($a_schedule_map[$schedule_id] ?? null);
469  }
470  }
471 
472  $new_obj->save();
473 
474  // files
475  $source = self::initStorage($this->getId());
476  $target = $new_obj::initStorage($new_obj->getId());
477  ilFileUtils::rCopy($source, $target);
478  }
479 
480  public static function lookupPoolId(int $object_id): int
481  {
482  global $DIC;
483 
484  $db = $DIC->database();
485  $set = $db->queryF(
486  "SELECT pool_id FROM booking_object " .
487  " WHERE booking_object_id = %s ",
488  array("integer"),
489  array($object_id)
490  );
491  $rec = $db->fetchAssoc($set);
492  return (int) $rec["pool_id"];
493  }
494 
495  public static function lookupTitle(int $object_id): string
496  {
497  global $DIC;
498 
499  $db = $DIC->database();
500  $set = $db->queryF(
501  "SELECT title FROM booking_object " .
502  " WHERE booking_object_id = %s ",
503  array("integer"),
504  array($object_id)
505  );
506  $rec = $db->fetchAssoc($set);
507  return $rec["title"];
508  }
509 }
doClone(int $a_pool_id, array $a_schedule_map=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setFile(string $a_value)
setPostText(string $a_value)
fetchAssoc(ilDBStatement $statement)
uploadPostFile(array $a_upload)
Upload new post file.
static getNumberOfObjectsForPool(int $a_pool_id)
Get number of booking objects for given booking pool id.
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
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.
$path
Definition: ltiservices.php:32
global $DIC
Definition: feed.php:28
static getNrOfItemsForObjects(array $a_obj_ids)
Get nr of available items for a set of object ids.
uploadFile(array $a_upload)
Upload new info file.
setScheduleId(?int $a_schedule_id)
deleteReservationsAndCalEntries(int $object_id)
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
setPostFile(string $a_value)
queryF(string $query, array $types, array $values)
static initStorage(int $a_id, string $a_subdir="")
Init file system storage.
__construct(int $a_id=null)
static lookupTitle(int $object_id)
setTitle(string $a_title)
static lookupPoolId(int $object_id)
setPoolId(int $a_pool_id)
static getList(int $a_pool_id, string $a_title=null)
Get list of booking objects.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...