Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lbplanner/classes/model/reservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
26 changes: 11 additions & 15 deletions lbplanner/services/slots/book_reservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down
Loading