Skip to content

Commit e536faf

Browse files
authored
fix: Automatically unreserve overlapping slots on book_reservation (#86)
1 parent ec14167 commit e536faf

2 files changed

Lines changed: 13 additions & 17 deletions

File tree

lbplanner/classes/model/reservation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ public function check_overlaps(reservation $other): bool {
188188
}
189189
// Now we only need to check whether the exact day is the same.
190190
static $format = 'Y-m-d';
191-
$thisdate = $this->get_datetime()->format($format);
192-
$otherdate = $other->get_datetime()->format($format);
191+
$thisdate = $this->date->format($format);
192+
$otherdate = $other->date->format($format);
193193
return $thisdate === $otherdate;
194194
}
195195

lbplanner/services/slots/book_reservation.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
use local_lbplanner\model\reservation;
2929

3030
/**
31-
* Books a reservation for the user
31+
* Books a reservation for the user.
32+
* Will unbook any overlapping reservations the user may already have.
3233
*
3334
* @package local_lbplanner
3435
* @subpackage services_slots
@@ -145,31 +146,26 @@ public static function book_reservation(int $slotid, string $date, int $userid):
145146
$existingreservations = slot_helper::get_reservations_for_user($userid);
146147
foreach ($existingreservations as $exres) {
147148
if ($reservation->check_overlaps($exres)) {
148-
array_push($overlapreservations, $exres);
149+
array_push($overlapreservations, $exres->id);
149150
}
150151
}
151152

152-
// If this is not a supervisor doing supervising, we throw an error if the user is in an oevrlapping reservation.
153-
if ($userid === $curuserid && count($overlapreservations) > 0) {
154-
throw new \moodle_exception('you\'re already in another reservation at this date and time…');
155-
}
156-
153+
// Save new reservation.
157154
$id = $DB->insert_record(slot_helper::TABLE_RESERVATIONS, $reservation->prepare_for_db());
158155
$reservation->set_fresh($id, $slot);
159156

160157
// If this is a supervisor reserving for a student, notify the student.
161158
if ($userid !== $curuserid) {
162159
notifications_helper::notify_user($userid, $reservation->id, NOTIF_TRIGGER::BOOK_FORCED);
163-
164-
// Remove user from each overlapping reservation and notify them about it.
165-
foreach ($overlapreservations as $overlapres) {
166-
$DB->delete_records(
167-
slot_helper::TABLE_RESERVATIONS,
168-
['id' => $overlapres->id]
169-
);
170-
}
171160
}
172161

162+
// Remove user from each overlapping reservation.
163+
$DB->delete_records_list(
164+
slot_helper::TABLE_RESERVATIONS,
165+
'id',
166+
$overlapreservations
167+
);
168+
173169
return $reservation->prepare_for_api();
174170
}
175171

0 commit comments

Comments
 (0)