ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBookingSchedule.php
Go to the documentation of this file.
1<?php
2
24{
25 protected ilDBInterface $db;
26 protected int $id = 0;
27 protected string $title = "";
28 protected int $pool_id = 0;
29 protected int $raster = 0;
30 protected int $rent_min = 0;
31 protected int $rent_max = 0;
32 protected int $auto_break = 0;
33 protected int $deadline = 0;
34 protected array $definition;
35 protected ?ilDateTime $av_from = null;
36 protected ?ilDateTime $av_to = null;
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 }
47
48 public function setTitle(
49 string $a_title
50 ): void {
51 $this->title = $a_title;
52 }
53
54 public function getTitle(): string
55 {
56 return $this->title;
57 }
58
59 public function setPoolId(
60 int $a_pool_id
61 ): void {
62 $this->pool_id = $a_pool_id;
63 }
64
65 public function getPoolId(): int
66 {
67 return $this->pool_id;
68 }
69
73 public function setRaster(
74 int $a_raster
75 ): void {
76 $this->raster = $a_raster;
77 }
78
79 public function getRaster(): int
80 {
81 return $this->raster;
82 }
83
87 public function setMinRental(
88 int $a_min
89 ): void {
90 $this->rent_min = $a_min;
91 }
92
93 public function getMinRental(): int
94 {
95 return $this->rent_min;
96 }
97
101 public function setMaxRental(
102 int $a_max
103 ): void {
104 $this->rent_max = $a_max;
105 }
106
107 public function getMaxRental(): int
108 {
109 return $this->rent_max;
110 }
111
112 // set break time
113 public function setAutoBreak(int $a_break): void
114 {
115 $this->auto_break = $a_break;
116 }
117
118 public function getAutoBreak(): int
119 {
120 return $this->auto_break;
121 }
122
126 public function setDeadline(int $a_deadline): void
127 {
128 $this->deadline = $a_deadline;
129 }
130
131 public function getDeadline(): int
132 {
133 return $this->deadline;
134 }
135
139 public function setDefinition(
140 array $a_definition
141 ): void {
142 $this->definition = $a_definition;
143 }
144
145 public function getDefinition(): array
146 {
147 return $this->definition;
148 }
149
150 public function setAvailabilityFrom(
151 ?ilDateTime $a_date = null
152 ): void {
153 $this->av_from = $a_date;
154 }
155
156 public function getAvailabilityFrom(): ?ilDateTime
157 {
158 return $this->av_from;
159 }
160
161 public function setAvailabilityTo(
162 ?ilDateTime $a_date = null
163 ): void {
164 $this->av_to = $a_date;
165 }
166
167 public function getAvailabilityTo(): ?ilDateTime
168 {
169 return $this->av_to;
170 }
171
172 protected function read(): void
173 {
174 $ilDB = $this->db;
175
176 if ($this->id) {
177 $set = $ilDB->query('SELECT title,raster,rent_min,rent_max,auto_break,' .
178 'deadline,av_from,av_to' .
179 ' FROM booking_schedule' .
180 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
181 $row = $ilDB->fetchAssoc($set);
182 $this->setTitle($row['title'] ?? "");
183 $this->setDeadline($row['deadline'] ?? 0);
184 $this->setAvailabilityFrom($row['av_from'] ? new ilDateTime($row['av_from'], IL_CAL_UNIX) : null);
185 $this->setAvailabilityTo($row['av_to'] ? new ilDateTime($row['av_to'], IL_CAL_UNIX) : null);
186 if ($row['raster']) {
187 $this->setRaster($row['raster']);
188 $this->setMinRental($row['rent_min']);
189 $this->setMaxRental($row['rent_max']);
190 $this->setAutoBreak($row['auto_break']);
191 }
192
193 // load definition
194 $definition = array();
195 $set = $ilDB->query('SELECT day_id,slot_id,times' .
196 ' FROM booking_schedule_slot' .
197 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
198 while ($row = $ilDB->fetchAssoc($set)) {
199 $definition[$row["day_id"]][$row["slot_id"]] = $row["times"];
200 }
201 $this->setDefinition($definition);
202 }
203 }
204
205 public function save(): ?int
206 {
207 $ilDB = $this->db;
208
209 if ($this->id) {
210 return null;
211 }
212
213 $this->id = $ilDB->nextId('booking_schedule');
214
215 $av_from = ($this->getAvailabilityFrom() && !$this->getAvailabilityFrom()->isNull())
216 ? $this->getAvailabilityFrom()->get(IL_CAL_UNIX)
217 : null;
218 $av_to = ($this->getAvailabilityTo() && !$this->getAvailabilityTo()->isNull())
219 ? $this->getAvailabilityTo()->get(IL_CAL_UNIX)
220 : null;
221
222 $ilDB->manipulate('INSERT INTO booking_schedule' .
223 ' (booking_schedule_id,title,pool_id,raster,rent_min,rent_max,auto_break,' .
224 'deadline,av_from,av_to)' .
225 ' VALUES (' . $ilDB->quote($this->id, 'integer') . ',' . $ilDB->quote($this->getTitle(), 'text') .
226 ',' . $ilDB->quote($this->getPoolId(), 'integer') . ',' . $ilDB->quote($this->getRaster(), 'integer') .
227 ',' . $ilDB->quote($this->getMinRental(), 'integer') . ',' . $ilDB->quote($this->getMaxRental(), 'integer') .
228 ',' . $ilDB->quote($this->getAutoBreak(), 'integer') . ',' . $ilDB->quote($this->getDeadline(), 'integer') .
229 ',' . $ilDB->quote($av_from, 'integer') . ',' . $ilDB->quote($av_to, 'integer') . ')');
230
231 $this->saveDefinition();
232
233 return $this->id;
234 }
235
236 public function update(): bool
237 {
238 $ilDB = $this->db;
239
240 if (!$this->id) {
241 return false;
242 }
243
244 $av_from = ($this->getAvailabilityFrom() && !$this->getAvailabilityFrom()->isNull())
245 ? $this->getAvailabilityFrom()->get(IL_CAL_UNIX)
246 : null;
247 $av_to = ($this->getAvailabilityTo() && !$this->getAvailabilityTo()->isNull())
248 ? $this->getAvailabilityTo()->get(IL_CAL_UNIX)
249 : null;
250
251 $ilDB->manipulate('UPDATE booking_schedule' .
252 ' SET title = ' . $ilDB->quote($this->getTitle(), 'text') .
253 ', pool_id = ' . $ilDB->quote($this->getPoolId(), 'integer') .
254 ', raster = ' . $ilDB->quote($this->getRaster(), 'integer') .
255 ', rent_min = ' . $ilDB->quote($this->getMinRental(), 'integer') .
256 ', rent_max = ' . $ilDB->quote($this->getMaxRental(), 'integer') .
257 ', auto_break = ' . $ilDB->quote($this->getAutoBreak(), 'integer') .
258 ', deadline = ' . $ilDB->quote($this->getDeadline(), 'integer') .
259 ', av_from = ' . $ilDB->quote($av_from, 'integer') .
260 ', av_to = ' . $ilDB->quote($av_to, 'integer') .
261 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
262
263 $this->saveDefinition();
264 return true;
265 }
266
267 public function doClone(int $a_pool_id): int
268 {
269 $new_obj = new self();
270 $new_obj->setPoolId($a_pool_id);
271 $new_obj->setTitle($this->getTitle());
272 $new_obj->setRaster($this->getRaster());
273 $new_obj->setMinRental($this->getMinRental());
274 $new_obj->setMaxRental($this->getMaxRental());
275 $new_obj->setAutoBreak($this->getAutoBreak());
276 $new_obj->setDeadline($this->getDeadline());
277 $new_obj->setDefinition($this->getDefinition());
278 $new_obj->setAvailabilityFrom($this->getAvailabilityFrom());
279 $new_obj->setAvailabilityTo($this->getAvailabilityTo());
280 return $new_obj->save();
281 }
282
286 protected function saveDefinition(): bool
287 {
288 $ilDB = $this->db;
289
290 if (!$this->id) {
291 return false;
292 }
293
294 $ilDB->manipulate('DELETE FROM booking_schedule_slot' .
295 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
296
297 $definition = $this->getDefinition();
298 if ($definition) {
299 foreach ($definition as $day_id => $slots) {
300 foreach ($slots as $slot_id => $times) {
301 $fields = array(
302 "booking_schedule_id" => array('integer', $this->id),
303 "day_id" => array('text', $day_id),
304 "slot_id" => array('integer', $slot_id),
305 "times" => array('text', $times)
306 );
307 $ilDB->insert('booking_schedule_slot', $fields);
308 }
309 }
310 }
311 return true;
312 }
313
317 public static function hasExistingSchedules(int $a_pool_id): bool
318 {
319 global $DIC;
320
321 $ilDB = $DIC->database();
322
323 $set = $ilDB->query("SELECT booking_schedule_id" .
324 " FROM booking_schedule" .
325 " WHERE pool_id = " . $ilDB->quote($a_pool_id, 'integer'));
326 return (bool) $ilDB->numRows($set);
327 }
328
332 public static function getList(int $a_pool_id): array
333 {
334 global $DIC;
335
336 $ilDB = $DIC->database();
337
338 $set = $ilDB->query('SELECT s.booking_schedule_id,s.title,' .
339 'MAX(o.schedule_id) AS object_has_schedule' .
340 ' FROM booking_schedule s' .
341 ' LEFT JOIN booking_object o ON (s.booking_schedule_id = o.schedule_id)' .
342 ' WHERE s.pool_id = ' . $ilDB->quote($a_pool_id, 'integer') .
343 ' GROUP BY s.booking_schedule_id,s.title' .
344 ' ORDER BY s.title');
345 $res = array();
346 while ($row = $ilDB->fetchAssoc($set)) {
347 if (!$row['object_has_schedule']) {
348 $row['is_used'] = false;
349 } else {
350 $row['is_used'] = true;
351 }
352 $res[] = $row;
353 }
354 return $res;
355 }
356
357 public function delete(): int
358 {
359 $ilDB = $this->db;
360
361 if ($this->id) {
362 return $ilDB->manipulate('DELETE FROM booking_schedule' .
363 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
364 }
365 return 0;
366 }
367
371 public function getDefinitionBySlots(): array
372 {
373 $def = $this->getDefinition();
374 $slots = array();
375 foreach ($def as $day => $times) {
376 foreach ($times as $time) {
377 $slots[$time][] = $day;
378 }
379 }
380 foreach ($slots as $time => $days) {
381 $slots[$time] = array_unique($days);
382 }
383 ksort($slots);
384 return $slots;
385 }
386
387 public function setDefinitionBySlots(array $a_def): void
388 {
389 $slots = array();
390 foreach ($a_def as $time => $days) {
391 foreach ($days as $day) {
392 $slots[$day][] = $time;
393 }
394 }
395 $this->setDefinition($slots);
396 }
397}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const IL_CAL_UNIX
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setAvailabilityFrom(?ilDateTime $a_date=null)
static hasExistingSchedules(int $a_pool_id)
Check if given pool has any defined schedules.
setRaster(int $a_raster)
Set booking raster (in minutes)
setDefinitionBySlots(array $a_def)
setMinRental(int $a_min)
Set minimum rental time.
static getList(int $a_pool_id)
Get list of booking objects for given pool.
setMaxRental(int $a_max)
Set maximum rental time.
saveDefinition()
Save current definition (slots)
getDefinitionBySlots()
Return definition grouped by slots (not days)
setDeadline(int $a_deadline)
Set deadline.
setAvailabilityTo(?ilDateTime $a_date=null)
setDefinition(array $a_definition)
Set definition.
__construct(?int $a_id=null)
@classDescription Date and time handling
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26