From 1aef9a527df5ee121f8dd5bf97dc9e54f24edff2 Mon Sep 17 00:00:00 2001 From: Viktoria Wiebe Date: Tue, 17 May 2022 14:12:55 +0200 Subject: add linked error message to request list under room management and make code DRYer --- app/controllers/resources/room_request.php | 71 +++++++++++++++++++ lib/models/resources/ResourceBooking.class.php | 89 ++++++++++++++++++++++++ lib/raumzeit/SingleDate.class.php | 95 +------------------------- 3 files changed, 161 insertions(+), 94 deletions(-) diff --git a/app/controllers/resources/room_request.php b/app/controllers/resources/room_request.php index ca483d9..6f185a8 100644 --- a/app/controllers/resources/room_request.php +++ b/app/controllers/resources/room_request.php @@ -1702,6 +1702,12 @@ class Resources_RoomRequestController extends AuthenticatedController ]; } } + } catch (ResourceBookingOverlapException $e) { + $errors[] = $this->get_booking_overlap_message( + $course_date->date, + $course_date->end_time, + $room); + continue; } catch (Exception $e) { $errors[] = $e->getMessage(); continue; @@ -1741,6 +1747,12 @@ class Resources_RoomRequestController extends AuthenticatedController if ($booking instanceof ResourceBooking) { $bookings[] = $booking; } + } catch (ResourceBookingOverlapException $e) { + $errors[] = $this->get_booking_overlap_message( + $date->date, + $date->end_time, + $room); + continue; } catch (Exception $e) { $errors[] = $e->getMessage(); continue; @@ -1777,6 +1789,12 @@ class Resources_RoomRequestController extends AuthenticatedController if ($booking instanceof ResourceBooking) { $bookings[] = $booking; } + } catch (ResourceBookingOverlapException $e) { + $errors[] = $this->get_booking_overlap_message( + $this->request->begin, + $this->request->end, + $room); + continue; } catch (Exception $e) { $errors[] = $e->getMessage(); continue; @@ -2312,6 +2330,11 @@ class Resources_RoomRequestController extends AuthenticatedController if ($booking instanceof ResourceBooking) { $bookings[] = $booking; } + } catch (ResourceBookingOverlapException $e) { + $errors[] = $this->get_booking_overlap_message( + $course_date->date, + $course_date->end_time, + $room); } catch (Exception $e) { $errors[] = $e->getMessage(); } @@ -2349,6 +2372,12 @@ class Resources_RoomRequestController extends AuthenticatedController if ($booking instanceof ResourceBooking) { $bookings[] = $booking; } + } catch (ResourceBookingOverlapException $e) { + $errors[] = $this->get_booking_overlap_message( + $date->date, + $date->end_time, + $room); + continue; } catch (Exception $e) { $errors[] = $e->getMessage(); continue; @@ -2385,6 +2414,11 @@ class Resources_RoomRequestController extends AuthenticatedController if ($booking instanceof ResourceBooking) { $bookings[] = $booking; } + } catch (ResourceBookingOverlapException $e) { + $errors[] = $this->get_booking_overlap_message( + $this->request->begin, + $this->request->end, + $room); } catch (Exception $e) { $errors[] = $e->getMessage(); } @@ -2602,4 +2636,41 @@ class Resources_RoomRequestController extends AuthenticatedController $this->event_color = $request_colour; } } + + /** + * Returns a formatted error message about the overlapping bookings with a + * link to the lecture and a link to the planner if the required permissions + * are met. + * + * @param $begin_time string The timestamp of the beginning of the booking + * @param $end_time string The timestamp of the end of the booking + * @param $room Resource The room that should be booked + * + * @return string A formatted error message about overlapping bookings + */ + protected function get_booking_overlap_message($begin_time, $end_time, $room) + { + $end_time_format = date('d.m.Y', $begin_time) == date('d.m.Y', $end_time) ? + date('H:i', $end_time) : + date('d.m.Y H:i', $end_time); + $error_message = sprintf( + _('%1$s: Die Buchung vom %2$s bis %3$s konnte wegen Überlappungen nicht gespeichert werden: '), + htmlReady($room->getFullName()), + date('d.m.Y H:i', $begin_time), + $end_time_format + ); + $begin = new DateTime(); + $begin->setTimestamp($begin_time); + $end = new DateTime(); + $end->setTimestamp($end_time); + $overlapping_bookings = array_merge( + $room->getResourceBookings($begin, $end), + $room->getResourceLocks($begin, $end) + ); + foreach ($overlapping_bookings as $overlapping_booking) { + $error_message .= $overlapping_booking->getOverlapMessage(); + $error_message .= '
'; + } + return $error_message; + } } diff --git a/lib/models/resources/ResourceBooking.class.php b/lib/models/resources/ResourceBooking.class.php index 7ecf281..d9c51eb 100644 --- a/lib/models/resources/ResourceBooking.class.php +++ b/lib/models/resources/ResourceBooking.class.php @@ -1872,4 +1872,93 @@ class ResourceBooking extends SimpleORMap implements PrivacyObject, Studip\Calen restoreLanguage(); } + + /** + * This method converts overlap data about an overlapping booking + * to a string that can be used to output overlap information to the user. + * Only one overlap is converted by this method. For multiple overlaps + * this method must be called multiple times. + * + * @param ResourceBooking $booking The overlapping booking. + * + * @return string A string representation of the overlap. + */ + public function getOverlapMessage() + { + $message = ''; + + if ($this->booking_type == '2') { + $message .= sprintf( + _('Vom %1$s, %2$s Uhr bis zum %3$s, %4$s Uhr (Sperrzeit)') . "\n", + date("d.m.Y", $this->begin), + date("H:i", $this->begin), + date("d.m.Y", $this->end), + date("H:i", $this->end) + ); + } else { + $course = Course::find($this->course_id); + + if ($course) { + $user_has_permissions = $GLOBALS['perm']->have_studip_perm( + 'dozent', + $course->id, + $GLOBALS['user']->id + ); + $course_link = null; + if ($user_has_permissions) { + $course_link = URLHelper::getLink( + 'dispatch.php/course/timesrooms/index', + [ + 'cid' => $course->id + ] + ); + $planner_link = $this->resource->getLinkForAction( + 'booking_plan', + $this->resource->id, + ['defaultDate' => date('Y-m-d', $this->begin)] + ); + $planner_msg = sprintf( + _('%2$s von %3$s bis %4$s'), + $planner_link, + date('d.m.Y', $this->begin), + date('H:i', $this->begin), + date('H:i', $this->end) + ); + } else { + $course_link = URLHelper::getLink( + 'dispatch.php/course/details', + [ + 'sem_id' => $course->id + ] + ); + $planner_msg = sprintf( + _('%1$s von %2$s bis %3$s'), + date('d.m.Y', $this->begin), + date('H:i', $this->begin), + date('H:i', $this->end) + ); + } + + $message .= sprintf( + _('Am %1$s Uhr durch Veranstaltung %2$s') . "\n", + $planner_msg, + sprintf( + '%2$s', + $course_link, + htmlReady($course->name) + ) + ); + } else { + $message .= sprintf( + _('Am %1$s von %2$s bis %3$s Uhr belegt von "%4$s"') . "\n", + date("d.m.Y", $this->begin), + date("H:i", $this->begin), + date("H:i", $this->end), + htmlReady($this->description) + ); + } + } + + return $message; + } } diff --git a/lib/raumzeit/SingleDate.class.php b/lib/raumzeit/SingleDate.class.php index 9e9d2af..6faeb4b 100644 --- a/lib/raumzeit/SingleDate.class.php +++ b/lib/raumzeit/SingleDate.class.php @@ -456,97 +456,6 @@ class SingleDate return $room; } - - /** - * This method converts overlap data about an overlapping booking - * to a string that can be used to output overlap information to the user. - * Only one overlap is converted by this method. For multiple overlaps - * this method must be called multiple times. - * - * @param ResourceBooking $booking The overlapping booking. - * - * @return string A string representation of the overlap. - */ - protected function getOverlapMessage(ResourceBooking $booking) - { - $message = ''; - - if ($booking->booking_type == '2') { - $message .= sprintf( - _('Vom %1$s, %2$s Uhr bis zum %3$s, %4$s Uhr (Sperrzeit)') . "\n", - date("d.m.Y", $booking->begin), - date("H:i", $booking->begin), - date("d.m.Y", $booking->end), - date("H:i", $booking->end) - ); - } else { - $course = Course::find($booking->course_id); - - if ($course) { - $user_has_permissions = $GLOBALS['perm']->have_studip_perm( - 'dozent', - $course->id, - $GLOBALS['user']->id - ); - $course_link = null; - if ($user_has_permissions) { - $course_link = URLHelper::getLink( - 'dispatch.php/course/timesrooms/index', - [ - 'cid' => $course->id - ] - ); - $planner_link = $booking->resource->getLinkForAction( - 'booking_plan', - $booking->resource->id, - ['defaultDate' => date('Y-m-d', $booking->begin)] - ); - $planner_msg = sprintf( - _('%2$s von %3$s bis %4$s'), - $planner_link, - date('d.m.Y', $booking->begin), - date('H:i', $booking->begin), - date('H:i', $booking->end) - ); - } else { - $course_link = URLHelper::getLink( - 'dispatch.php/course/details', - [ - 'sem_id' => $course->id - ] - ); - $planner_msg = sprintf( - _('%1$s von %2$s bis %3$s'), - date('d.m.Y', $booking->begin), - date('H:i', $booking->begin), - date('H:i', $booking->end) - ); - } - - $message .= sprintf( - _('Am %1$s Uhr durch Veranstaltung %2$s') . "\n", - $planner_msg, - sprintf( - '%2$s', - $course_link, - htmlReady($course->name) - ) - ); - } else { - $message .= sprintf( - _('Am %1$s von %2$s bis %3$s Uhr belegt von "%4$s"') . "\n", - date("d.m.Y", $booking->begin), - date("H:i", $booking->begin), - date("H:i", $booking->end), - htmlReady($booking->description) - ); - } - } - - return $message; - } - - private function insertAssign(Room $room, $preparation_time = 0) { $begin = new DateTime(); @@ -601,9 +510,7 @@ class SingleDate $room->getResourceLocks($begin, $end) ); foreach ($overlapping_bookings as $overlapping_booking) { - $error_message .= $this->getOverlapMessage( - $overlapping_booking - ); + $error_message .= $overlapping_booking->getOverlapMessage(); } $this->messages['error'][] = $error_message; return false; -- cgit v1.0