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