|
28 | 28 | use local_lbplanner\model\reservation; |
29 | 29 |
|
30 | 30 | /** |
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. |
32 | 33 | * |
33 | 34 | * @package local_lbplanner |
34 | 35 | * @subpackage services_slots |
@@ -145,31 +146,26 @@ public static function book_reservation(int $slotid, string $date, int $userid): |
145 | 146 | $existingreservations = slot_helper::get_reservations_for_user($userid); |
146 | 147 | foreach ($existingreservations as $exres) { |
147 | 148 | if ($reservation->check_overlaps($exres)) { |
148 | | - array_push($overlapreservations, $exres); |
| 149 | + array_push($overlapreservations, $exres->id); |
149 | 150 | } |
150 | 151 | } |
151 | 152 |
|
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. |
157 | 154 | $id = $DB->insert_record(slot_helper::TABLE_RESERVATIONS, $reservation->prepare_for_db()); |
158 | 155 | $reservation->set_fresh($id, $slot); |
159 | 156 |
|
160 | 157 | // If this is a supervisor reserving for a student, notify the student. |
161 | 158 | if ($userid !== $curuserid) { |
162 | 159 | 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 | | - } |
171 | 160 | } |
172 | 161 |
|
| 162 | + // Remove user from each overlapping reservation. |
| 163 | + $DB->delete_records_list( |
| 164 | + slot_helper::TABLE_RESERVATIONS, |
| 165 | + 'id', |
| 166 | + $overlapreservations |
| 167 | + ); |
| 168 | + |
173 | 169 | return $reservation->prepare_for_api(); |
174 | 170 | } |
175 | 171 |
|
|
0 commit comments