ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilBookingSchedule.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
13{
17 protected $db;
18
19 protected $id; // int
20 protected $title; // string
21 protected $pool_id; // int
22 protected $raster; // int
23 protected $rent_min; // int
24 protected $rent_max; // int
25 protected $auto_break; // int
26 protected $deadline; // int
27 protected $definition; // array
28 protected $av_from; // ildatetime
29 protected $av_to; // ildatetime
30
38 public function __construct($a_id = null)
39 {
40 global $DIC;
41
42 $this->db = $DIC->database();
43 $this->id = (int) $a_id;
44 $this->read();
45 }
46
51 public function setTitle($a_title)
52 {
53 $this->title = $a_title;
54 }
55
60 public function getTitle()
61 {
62 return $this->title;
63 }
64
69 public function setPoolId($a_pool_id)
70 {
71 $this->pool_id = (int) $a_pool_id;
72 }
73
78 public function getPoolId()
79 {
80 return $this->pool_id;
81 }
82
87 public function setRaster($a_raster)
88 {
89 $this->raster = (int) $a_raster;
90 }
91
96 public function getRaster()
97 {
98 return $this->raster;
99 }
100
105 public function setMinRental($a_min)
106 {
107 $this->rent_min = (int) $a_min;
108 }
109
114 public function getMinRental()
115 {
116 return $this->rent_min;
117 }
118
123 public function setMaxRental($a_max)
124 {
125 $this->rent_max = (int) $a_max;
126 }
127
132 public function getMaxRental()
133 {
134 return $this->rent_max;
135 }
136
141 public function setAutoBreak($a_break)
142 {
143 $this->auto_break = (int) $a_break;
144 }
145
150 public function getAutoBreak()
151 {
152 return $this->auto_break;
153 }
154
159 public function setDeadline($a_deadline)
160 {
161 $this->deadline = (int) $a_deadline;
162 }
163
168 public function getDeadline()
169 {
170 return $this->deadline;
171 }
172
177 public function setDefinition($a_definition)
178 {
179 $this->definition = $a_definition;
180 }
181
186 public function getDefinition()
187 {
188 return $this->definition;
189 }
190
196 public function setAvailabilityFrom(ilDateTime $a_date = null)
197 {
198 $this->av_from = $a_date;
199 }
200
206 public function getAvailabilityFrom()
207 {
208 return $this->av_from;
209 }
210
216 public function setAvailabilityTo(ilDateTime $a_date = null)
217 {
218 $this->av_to = $a_date;
219 }
220
226 public function getAvailabilityTo()
227 {
228 return $this->av_to;
229 }
230
234 protected function read()
235 {
237
238 if ($this->id) {
239 $set = $ilDB->query('SELECT title,raster,rent_min,rent_max,auto_break,' .
240 'deadline,av_from,av_to' .
241 ' FROM booking_schedule' .
242 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
243 $row = $ilDB->fetchAssoc($set);
244 $this->setTitle($row['title']);
245 $this->setDeadline($row['deadline']);
246 $this->setAvailabilityFrom($row['av_from'] ? new ilDateTime($row['av_from'], IL_CAL_UNIX) : null);
247 $this->setAvailabilityTo($row['av_to'] ? new ilDateTime($row['av_to'], IL_CAL_UNIX) : null);
248 if ($row['raster']) {
249 $this->setRaster($row['raster']);
250 $this->setMinRental($row['rent_min']);
251 $this->setMaxRental($row['rent_max']);
252 $this->setAutoBreak($row['auto_break']);
253 }
254
255 // load definition
256 $definition = array();
257 $set = $ilDB->query('SELECT day_id,slot_id,times' .
258 ' FROM booking_schedule_slot' .
259 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
260 while ($row = $ilDB->fetchAssoc($set)) {
261 $definition[$row["day_id"]][$row["slot_id"]] = $row["times"];
262 }
264 }
265 }
266
271 public function save()
272 {
274
275 if ($this->id) {
276 return false;
277 }
278
279 $this->id = $ilDB->nextId('booking_schedule');
280
281 $av_from = ($this->getAvailabilityFrom() && !$this->getAvailabilityFrom()->isNull())
282 ? $this->getAvailabilityFrom()->get(IL_CAL_UNIX)
283 : null;
284 $av_to = ($this->getAvailabilityTo() && !$this->getAvailabilityTo()->isNull())
285 ? $this->getAvailabilityTo()->get(IL_CAL_UNIX)
286 : null;
287
288 $ilDB->manipulate('INSERT INTO booking_schedule' .
289 ' (booking_schedule_id,title,pool_id,raster,rent_min,rent_max,auto_break,' .
290 'deadline,av_from,av_to)' .
291 ' VALUES (' . $ilDB->quote($this->id, 'integer') . ',' . $ilDB->quote($this->getTitle(), 'text') .
292 ',' . $ilDB->quote($this->getPoolId(), 'integer') . ',' . $ilDB->quote($this->getRaster(), 'integer') .
293 ',' . $ilDB->quote($this->getMinRental(), 'integer') . ',' . $ilDB->quote($this->getMaxRental(), 'integer') .
294 ',' . $ilDB->quote($this->getAutoBreak(), 'integer') . ',' . $ilDB->quote($this->getDeadline(), 'integer') .
295 ',' . $ilDB->quote($av_from, 'integer') . ',' . $ilDB->quote($av_to, 'integer') . ')');
296
297 $this->saveDefinition();
298
299 return $this->id;
300 }
301
306 public function update()
307 {
309
310 if (!$this->id) {
311 return false;
312 }
313
314 $av_from = ($this->getAvailabilityFrom() && !$this->getAvailabilityFrom()->isNull())
315 ? $this->getAvailabilityFrom()->get(IL_CAL_UNIX)
316 : null;
317 $av_to = ($this->getAvailabilityTo() && !$this->getAvailabilityTo()->isNull())
318 ? $this->getAvailabilityTo()->get(IL_CAL_UNIX)
319 : null;
320
321 $ilDB->manipulate('UPDATE booking_schedule' .
322 ' SET title = ' . $ilDB->quote($this->getTitle(), 'text') .
323 ', pool_id = ' . $ilDB->quote($this->getPoolId(), 'integer') .
324 ', raster = ' . $ilDB->quote($this->getRaster(), 'integer') .
325 ', rent_min = ' . $ilDB->quote($this->getMinRental(), 'integer') .
326 ', rent_max = ' . $ilDB->quote($this->getMaxRental(), 'integer') .
327 ', auto_break = ' . $ilDB->quote($this->getAutoBreak(), 'integer') .
328 ', deadline = ' . $ilDB->quote($this->getDeadline(), 'integer') .
329 ', av_from = ' . $ilDB->quote($av_from, 'integer') .
330 ', av_to = ' . $ilDB->quote($av_to, 'integer') .
331 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
332
333 $this->saveDefinition();
334 }
335
336 public function doClone($a_pool_id)
337 {
338 $new_obj = new self();
339 $new_obj->setPoolId($a_pool_id);
340 $new_obj->setTitle($this->getTitle());
341 $new_obj->setRaster($this->getRaster());
342 $new_obj->setMinRental($this->getMinRental());
343 $new_obj->setMaxRental($this->getMaxRental());
344 $new_obj->setAutoBreak($this->getAutoBreak());
345 $new_obj->setDeadline($this->getDeadline());
346 $new_obj->setDefinition($this->getDefinition());
347 $new_obj->setAvailabilityFrom($this->getAvailabilityFrom());
348 $new_obj->setAvailabilityTo($this->getAvailabilityTo());
349 return $new_obj->save();
350 }
351
355 protected function saveDefinition()
356 {
358
359 if (!$this->id) {
360 return false;
361 }
362
363 $ilDB->manipulate('DELETE FROM booking_schedule_slot' .
364 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
365
366 $definition = $this->getDefinition();
367 if ($definition) {
368 foreach ($definition as $day_id => $slots) {
369 foreach ($slots as $slot_id => $times) {
370 $fields = array(
371 "booking_schedule_id" => array('integer', $this->id),
372 "day_id" => array('text', $day_id),
373 "slot_id" => array('integer', $slot_id),
374 "times" => array('text', $times)
375 );
376 $ilDB->insert('booking_schedule_slot', $fields);
377 }
378 }
379 }
380 }
381
387 public static function hasExistingSchedules($a_pool_id)
388 {
389 global $DIC;
390
391 $ilDB = $DIC->database();
392
393 $set = $ilDB->query("SELECT booking_schedule_id" .
394 " FROM booking_schedule" .
395 " WHERE pool_id = " . $ilDB->quote($a_pool_id, 'integer'));
396 return (bool) $ilDB->numRows($set);
397 }
398
404 public static function getList($a_pool_id)
405 {
406 global $DIC;
407
408 $ilDB = $DIC->database();
409
410 $set = $ilDB->query('SELECT s.booking_schedule_id,s.title,' .
411 'MAX(o.schedule_id) AS object_has_schedule' .
412 ' FROM booking_schedule s' .
413 ' LEFT JOIN booking_object o ON (s.booking_schedule_id = o.schedule_id)' .
414 ' WHERE s.pool_id = ' . $ilDB->quote($a_pool_id, 'integer') .
415 ' GROUP BY s.booking_schedule_id,s.title' .
416 ' ORDER BY s.title');
417 $res = array();
418 while ($row = $ilDB->fetchAssoc($set)) {
419 if (!$row['object_has_schedule']) {
420 $row['is_used'] = false;
421 } else {
422 $row['is_used'] = true;
423 }
424 $res[] = $row;
425 }
426 return $res;
427 }
428
433 public function delete()
434 {
436
437 if ($this->id) {
438 return $ilDB->manipulate('DELETE FROM booking_schedule' .
439 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
440 }
441 }
442
448 public function getDefinitionBySlots()
449 {
450 $def = $this->getDefinition();
451 $slots = array();
452 foreach ($def as $day => $times) {
453 foreach ($times as $time) {
454 $slots[$time][] = $day;
455 }
456 }
457 foreach ($slots as $time => $days) {
458 $slots[$time] = array_unique($days);
459 }
460 ksort($slots);
461 return $slots;
462 }
463
464 public function setDefinitionBySlots(array $a_def)
465 {
466 $slots = array();
467 foreach ($a_def as $time => $days) {
468 foreach ($days as $day) {
469 $slots[$day][] = $time;
470 }
471 }
472 $this->setDefinition($slots);
473 }
474}
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
schedule for booking ressource
getAutoBreak()
Get break time.
setTitle($a_title)
Set object title.
getRaster()
Get booking raster.
setDefinitionBySlots(array $a_def)
setDeadline($a_deadline)
Set deadline.
static hasExistingSchedules($a_pool_id)
Check if given pool has any defined schedules.
setPoolId($a_pool_id)
Set booking pool id (aka parent obj ref id)
saveDefinition()
Save current definition.
getMaxRental()
Get maximum rental time.
getAvailabilityTo()
Get availability end.
save()
Create new entry in db.
setRaster($a_raster)
Set booking raster (in minutes)
getAvailabilityFrom()
Get availability start.
update()
Update entry in db.
setDefinition($a_definition)
Set definition.
getMinRental()
Get minimum rental time.
getDefinition()
Get definition.
getDefinitionBySlots()
Return definition grouped by slots (not days)
setAutoBreak($a_break)
Set break time.
setMaxRental($a_max)
Set maximum rental time.
__construct($a_id=null)
Constructor.
static getList($a_pool_id)
Get list of booking objects for given pool.
setMinRental($a_min)
Set minimum rental time.
setAvailabilityFrom(ilDateTime $a_date=null)
Set availability start.
read()
Get dataset from db.
setAvailabilityTo(ilDateTime $a_date=null)
Set availability end.
getTitle()
Get object title.
getPoolId()
Get booking pool id.
@classDescription Date and time handling
foreach($_POST as $key=> $value) $res
global $ilDB
$DIC
Definition: xapitoken.php:46