1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
<?php
/**
* ResourceBookingInterval.php - model class for storing
* all resource bookings time intervals, including those for
* repetitions.
*
* The ResourceBookingEvent class contains all
* time intervals of all resources where they are
* assigned.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Moritz Strohm <strohm@data-quest.de>
* @copyright 2017
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package resources
* @since 4.1
*
* @property string $id alias column for interval_id
* @property string $interval_id database column
* @property string $resource_id database column
* @property string $booking_id database column
* @property int $begin database column
* @property int $end database column
* @property int $mkdate database column
* @property int $chdate database column
* @property int $takes_place database column
* @property ResourceBooking $booking belongs_to ResourceBooking
* @property Resource $resource belongs_to Resource
*/
class ResourceBookingInterval extends SimpleORMap
{
protected static function configure($config = [])
{
$config['db_table'] = 'resource_booking_intervals';
$config['belongs_to']['booking'] = [
'class_name' => ResourceBooking::class,
'foreign_key' => 'booking_id',
'assoc_func' => 'find'
];
$config['belongs_to']['resource'] = [
'class_name' => Resource::class,
'foreign_key' => 'resource_id',
'assoc_func' => 'find'
];
parent::configure($config);
}
/**
* Creates resource booking interval entries by checking the
* values of a specified ResourceBooking object.
*
* @param ResourceBooking $booking The resource booking object
* from which interval entries shall be generated.
*
* @return True, if all intervals have been created successfully,
* false otherwise.
*/
public static function createFromBooking(ResourceBooking $booking)
{
$time_intervals = $booking->calculateTimeIntervals();
foreach ($time_intervals as $time_interval) {
//Check if the time interval already exists:
$interval_exists = ResourceBookingInterval::countBySql(
'booking_id = :booking_id AND resource_id = :resource_id
AND begin = :begin AND end = :end',
[
'booking_id' => $booking->id,
'resource_id' => $booking->resource_id,
'begin' => $time_interval['begin'],
'end' => $time_interval['end']
]
) > 0;
if (!$interval_exists) {
$interval = new ResourceBookingInterval();
$interval->booking_id = $booking->id;
$interval->resource_id = $booking->resource_id;
$interval->begin = $time_interval['begin'];
$interval->end = $time_interval['end'];
if (!$interval->store()) {
return false;
}
}
}
return true;
}
/**
* Converts this ResourceBookingInterval object to a DateInterval object.
*
* @return DateInterval A DateInterval representing the time interval
* of this ResourceBookingInterval object.
*/
public function toDateInterval()
{
$begin = new DateTime();
$begin->setTimestamp($this->begin);
$end = new DateTime();
$end->setTimestamp($this->end);
return $begin->diff($end);
}
public function __toString()
{
if (date('Ymd', $this->begin) == date('Ymd', $this->end)) {
return strftime('%a %x %R', $this->begin) . ' - ' . strftime('%R', $this->end);
} else {
return strftime('%a %x %R', $this->begin)
. ' - '
. strftime('%a %x %R', $this->end);
}
}
}
|