From 5fb5f93a1e87a978cddb377513f34a3ffe3e0580 Mon Sep 17 00:00:00 2001 From: Riedler Date: Tue, 30 Sep 2025 21:01:02 +0200 Subject: [PATCH 1/4] fix: automatically unreserve overlapping slots on book_reservation --- lbplanner/services/slots/book_reservation.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lbplanner/services/slots/book_reservation.php b/lbplanner/services/slots/book_reservation.php index 98241378..be4aa7b4 100644 --- a/lbplanner/services/slots/book_reservation.php +++ b/lbplanner/services/slots/book_reservation.php @@ -28,7 +28,8 @@ use local_lbplanner\model\reservation; /** - * Books a reservation for the user + * Books a reservation for the user. + * Will unbook any overlapping reservations the user may already have. * * @package local_lbplanner * @subpackage services_slots @@ -149,11 +150,7 @@ public static function book_reservation(int $slotid, string $date, int $userid): } } - // If this is not a supervisor doing supervising, we throw an error if the user is in an oevrlapping reservation. - if ($userid === $curuserid && count($overlapreservations) > 0) { - throw new \moodle_exception('you\'re already in another reservation at this date and time…'); - } - + // Save new reservation. $id = $DB->insert_record(slot_helper::TABLE_RESERVATIONS, $reservation->prepare_for_db()); $reservation->set_fresh($id, $slot); @@ -161,7 +158,7 @@ public static function book_reservation(int $slotid, string $date, int $userid): if ($userid !== $curuserid) { notifications_helper::notify_user($userid, $reservation->id, NOTIF_TRIGGER::BOOK_FORCED); - // Remove user from each overlapping reservation and notify them about it. + // Remove user from each overlapping reservation foreach ($overlapreservations as $overlapres) { $DB->delete_records( slot_helper::TABLE_RESERVATIONS, From 4dbfc6422e26e4d090a24170ac5ee54578b01038 Mon Sep 17 00:00:00 2001 From: Riedler Date: Tue, 30 Sep 2025 21:10:18 +0200 Subject: [PATCH 2/4] fix: removed minor inconsequential mistake --- lbplanner/classes/model/reservation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lbplanner/classes/model/reservation.php b/lbplanner/classes/model/reservation.php index 5019efb6..599f50b3 100644 --- a/lbplanner/classes/model/reservation.php +++ b/lbplanner/classes/model/reservation.php @@ -188,8 +188,8 @@ public function check_overlaps(reservation $other): bool { } // Now we only need to check whether the exact day is the same. static $format = 'Y-m-d'; - $thisdate = $this->get_datetime()->format($format); - $otherdate = $other->get_datetime()->format($format); + $thisdate = $this->date->format($format); + $otherdate = $other->date->format($format); return $thisdate === $otherdate; } From 0d803f46c2d94208f14232faf0ddc2f156bcc516 Mon Sep 17 00:00:00 2001 From: Riedler Date: Tue, 30 Sep 2025 21:17:42 +0200 Subject: [PATCH 3/4] chore: pacify moodle code checker --- lbplanner/services/slots/book_reservation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lbplanner/services/slots/book_reservation.php b/lbplanner/services/slots/book_reservation.php index be4aa7b4..f41028ee 100644 --- a/lbplanner/services/slots/book_reservation.php +++ b/lbplanner/services/slots/book_reservation.php @@ -158,7 +158,7 @@ public static function book_reservation(int $slotid, string $date, int $userid): if ($userid !== $curuserid) { notifications_helper::notify_user($userid, $reservation->id, NOTIF_TRIGGER::BOOK_FORCED); - // Remove user from each overlapping reservation + // Remove user from each overlapping reservation. foreach ($overlapreservations as $overlapres) { $DB->delete_records( slot_helper::TABLE_RESERVATIONS, From 49ec0b921fb24e2832e40a5c0aa1b41745b8914c Mon Sep 17 00:00:00 2001 From: Riedler Date: Wed, 1 Oct 2025 11:09:34 +0200 Subject: [PATCH 4/4] fix: also delete overlap reservations if not forced supervisor book --- lbplanner/services/slots/book_reservation.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lbplanner/services/slots/book_reservation.php b/lbplanner/services/slots/book_reservation.php index f41028ee..9fb08199 100644 --- a/lbplanner/services/slots/book_reservation.php +++ b/lbplanner/services/slots/book_reservation.php @@ -146,7 +146,7 @@ public static function book_reservation(int $slotid, string $date, int $userid): $existingreservations = slot_helper::get_reservations_for_user($userid); foreach ($existingreservations as $exres) { if ($reservation->check_overlaps($exres)) { - array_push($overlapreservations, $exres); + array_push($overlapreservations, $exres->id); } } @@ -157,16 +157,15 @@ public static function book_reservation(int $slotid, string $date, int $userid): // If this is a supervisor reserving for a student, notify the student. if ($userid !== $curuserid) { notifications_helper::notify_user($userid, $reservation->id, NOTIF_TRIGGER::BOOK_FORCED); - - // Remove user from each overlapping reservation. - foreach ($overlapreservations as $overlapres) { - $DB->delete_records( - slot_helper::TABLE_RESERVATIONS, - ['id' => $overlapres->id] - ); - } } + // Remove user from each overlapping reservation. + $DB->delete_records_list( + slot_helper::TABLE_RESERVATIONS, + 'id', + $overlapreservations + ); + return $reservation->prepare_for_api(); }