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; } diff --git a/lbplanner/services/slots/book_reservation.php b/lbplanner/services/slots/book_reservation.php index 98241378..9fb08199 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 @@ -145,31 +146,26 @@ 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); } } - // 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); // 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 and notify them about it. - 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(); }