aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMoritz Strohm <strohm@data-quest.de>2024-09-09 15:33:40 +0000
committerMoritz Strohm <strohm@data-quest.de>2024-09-09 15:33:40 +0000
commite034e414e80feb25b497172d07eeaa7b6486467d (patch)
tree5b5ad9c56fe81e68edbb1f908ccded46068391e4 /lib
parent48c69ed3ad2ddcb567dfaef4ab43b5c934611c55 (diff)
add repetition from monday until friday for resource bookings, re #2013
Merge request studip/studip!2539
Diffstat (limited to 'lib')
-rw-r--r--lib/models/resources/BrokenResource.php3
-rw-r--r--lib/models/resources/Building.php3
-rw-r--r--lib/models/resources/Location.php3
-rw-r--r--lib/models/resources/Resource.php8
-rw-r--r--lib/models/resources/ResourceBooking.php39
-rw-r--r--lib/models/resources/ResourceLabel.php3
6 files changed, 50 insertions, 9 deletions
diff --git a/lib/models/resources/BrokenResource.php b/lib/models/resources/BrokenResource.php
index fc44e82..38d0c81 100644
--- a/lib/models/resources/BrokenResource.php
+++ b/lib/models/resources/BrokenResource.php
@@ -104,7 +104,8 @@ class BrokenResource extends Resource
$description = '',
$internal_comment = '',
$booking_type = ResourceBooking::TYPE_NORMAL,
- $force_booking = false
+ $force_booking = false,
+ string $weekdays = ''
) {
return null;
}
diff --git a/lib/models/resources/Building.php b/lib/models/resources/Building.php
index a1c071a..7f1402f 100644
--- a/lib/models/resources/Building.php
+++ b/lib/models/resources/Building.php
@@ -458,7 +458,8 @@ class Building extends Resource
$description = '',
$internal_comment = '',
$booking_type = ResourceBooking::TYPE_NORMAL,
- $force_booking = false
+ $force_booking = false,
+ string $weekdays = ''
)
{
return null;
diff --git a/lib/models/resources/Location.php b/lib/models/resources/Location.php
index 9da2e11..788b4b3 100644
--- a/lib/models/resources/Location.php
+++ b/lib/models/resources/Location.php
@@ -379,7 +379,8 @@ class Location extends Resource
$description = '',
$internal_comment = '',
$booking_type = ResourceBooking::TYPE_NORMAL,
- $force_booking = false
+ $force_booking = false,
+ string $weekdays = ''
)
{
return null;
diff --git a/lib/models/resources/Resource.php b/lib/models/resources/Resource.php
index 32766e4..e1d8b67 100644
--- a/lib/models/resources/Resource.php
+++ b/lib/models/resources/Resource.php
@@ -661,6 +661,10 @@ class Resource extends SimpleORMap implements StudipItem
* @param bool $force_booking If this parameter is set to true,
* overlapping bookings are removed before storing this booking.
*
+ * @param string $weekdays The weekdays (1 - 7) on which the booking
+ * shall take place. This is only used when a booking with repetitions
+ * shall only take place on some weekdays.
+ *
* @return ResourceBooking object.
* @throws InvalidArgumentException If no time ranges are specified
* or if there is an error regarding the time ranges.
@@ -684,7 +688,8 @@ class Resource extends SimpleORMap implements StudipItem
$description = '',
$internal_comment = '',
$booking_type = ResourceBooking::TYPE_NORMAL,
- $force_booking = false
+ $force_booking = false,
+ string $weekdays = ''
)
{
if (!is_array($time_ranges)) {
@@ -843,6 +848,7 @@ class Resource extends SimpleORMap implements StudipItem
}
$booking->repetition_interval = $repetition_interval->format('P%YY%MM%DD');
+ $booking->weekdays = $weekdays;
}
if ($preparation_time) {
diff --git a/lib/models/resources/ResourceBooking.php b/lib/models/resources/ResourceBooking.php
index 977cf32..3c84cb8 100644
--- a/lib/models/resources/ResourceBooking.php
+++ b/lib/models/resources/ResourceBooking.php
@@ -41,6 +41,7 @@
* @property int $booking_type database column
* @property string $booking_user_id database column
* @property string $repetition_interval database column
+ * @property string $weekdays database column
* @property SimpleORMapCollection|ResourceBookingInterval[] $time_intervals has_many ResourceBookingInterval
* @property Resource $resource belongs_to Resource
* @property User $assigned_user belongs_to User
@@ -663,8 +664,13 @@ class ResourceBooking extends SimpleORMap implements PrivacyObject, Studip\Calen
$duration = $repetition_begin->diff($date_end);
+ $weekdays = [];
+ if (preg_match('/^1?2?3?4?5?6?7?$/', $this->weekdays)) {
+ $weekdays = str_split($this->weekdays);
+ }
+
//Loop over all exceptions and check if they belong to
- //one of the repetions:
+ //one of the repetitions:
$obsolete_exception_ids = [];
foreach ($exceptions as $exception) {
@@ -697,7 +703,13 @@ class ResourceBooking extends SimpleORMap implements PrivacyObject, Studip\Calen
break;
}
- $current_repetition->add($repetition_interval);
+ if ($weekdays) {
+ while (!in_array($current_repetition->format('N'), $weekdays)) {
+ $current_repetition = $current_repetition->add(new DateInterval('P1D'));
+ }
+ } else {
+ $current_repetition->add($repetition_interval);
+ }
}
if ($exception_obsolete) {
@@ -1280,8 +1292,19 @@ class ResourceBooking extends SimpleORMap implements PrivacyObject, Studip\Calen
//Check if end is later than begin to avoid
//infinite loops.
if ($repetition_end > $booking_begin) {
+ $weekdays = [];
+ if ($this->weekdays && preg_match('/^1?2?3?4?5?6?7?$/', $this->weekdays)) {
+ $weekdays = str_split($this->weekdays);
+ }
$current_begin = clone $booking_begin;
- $current_begin->add($repetition_interval);
+ //Move to the first weekday of the booking or the next interval:
+ if ($weekdays) {
+ while (!in_array($current_begin->format('N'), $weekdays)) {
+ $current_begin = $current_begin->add(new DateInterval('P1D'));
+ }
+ } else {
+ $current_begin->add($repetition_interval);
+ }
while ($current_begin < $repetition_end) {
$current_end = clone $current_begin;
$current_end->add($duration);
@@ -1297,7 +1320,15 @@ class ResourceBooking extends SimpleORMap implements PrivacyObject, Studip\Calen
: $current_end->getTimestamp()
)
];
- $current_begin->add($repetition_interval);
+ if ($weekdays) {
+ //Move to the next weekday that is in the array of weekday numbers:
+ do {
+ $current_begin = $current_begin->add(new DateInterval('P1D'));
+ } while (!in_array($current_begin->format('N'), $weekdays));
+ } else {
+ //Move to the next step according to the repetition interval:
+ $current_begin->add($repetition_interval);
+ }
}
} else {
//end timestamp is before begin timestamp:
diff --git a/lib/models/resources/ResourceLabel.php b/lib/models/resources/ResourceLabel.php
index c465723..1d1968d 100644
--- a/lib/models/resources/ResourceLabel.php
+++ b/lib/models/resources/ResourceLabel.php
@@ -97,7 +97,8 @@ class ResourceLabel extends Resource
$description = '',
$internal_comment = '',
$booking_type = ResourceBooking::TYPE_NORMAL,
- $force_booking = false
+ $force_booking = false,
+ string $weekdays = ''
)
{
return null;