Skip to content
Snippets Groups Projects
ResourceBooking.class.php 67.2 KiB
Newer Older
<?php

/**
 * ResourceBooking.class.php - model class for resource bookings
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @copyright   2017-2019
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @package     resources
 * @since       4.5
 */


/**
 * The ResourceBooking class is responsible for storing
 * bookings of resources in a specified time range
 * or a time interval.
 *
 * @property string id database column
 * @property string resource_id database column
 * @property string range_id database column
 *     The user, course etc. where the booking (booking)
 *     is associated with.
 * @property string booking_user_id database column
 *     The user who created the booking (booking).
 * @property string description database column
 * @property string begin database column
 * @property string end database column
 * @property string booking_type database column: The booking type.
 *     The following types are defined:
 *     0 = normal booking
 *     1 = reservation
 *     2 = lock
 *     3 = planned booking (reservation from external tools)
 *
 * @property string repeat_end database column
 * @property string repeat_quantity database column
 * @property string repetition_interval database column
 *     The repetition_interval column contains a date interval string in a
 *     format that is accepted by the DateInterval class constructor.
 *     Examples for values of the repetition_interval column:
 *     - For an one month interval, the value is "P1M".
 *     - For an interval of two days, the value is "P2D".
 *     See the DateInterval documentation for more examples:
 *     https://secure.php.net/manual/en/class.dateinterval.php
 *
 * @property string internal_comment database column
 * @property string mkdate database column
 * @property string chdate database column
 * @property Resource resource belongs_to Resource
 * @property User assigned_user belongs_to User
 * @property CourseDate assigned_course_date belongs_to CourseDate
 */
class ResourceBooking extends SimpleORMap implements PrivacyObject, Studip\Calendar\EventSource
{
    private $assigned_user_type;

    protected static function configure($config = [])
    {
        $config['db_table'] = 'resource_bookings';

        $config['belongs_to']['resource'] = [
            'class_name' => 'Resource',
            'foreign_key' => 'resource_id',
            'assoc_func' => 'find'
        ];
        $config['belongs_to']['assigned_user'] = [
            'class_name' => 'User',
            'foreign_key' => 'range_id',
            'assoc_func' => 'find'
        ];
        $config['belongs_to']['assigned_course_date'] = [
            'class_name' => 'CourseDate',
            'foreign_key' => 'range_id',
            'assoc_func' => 'find'
        ];
        $config['has_many']['time_intervals'] = [
            'class_name' => 'ResourceBookingInterval',
            'assoc_foreign_key' => 'booking_id',
            'on_delete' => 'delete'
        ];
        $config['belongs_to']['booking_user'] = [
            'class_name' => 'User',
            'foreign_key' => 'booking_user_id',
            'assoc_func' => 'find'
        ];

        $config['additional_fields']['course_id'] = ['assigned_course_date', 'range_id'];
        $config['additional_fields']['room_name'] = ['resource', 'name'];

        $config['registered_callbacks']['after_store'][] = 'updateIntervals';
        $config['registered_callbacks']['after_store'][] = 'createStoreLogEntry';
        $config['registered_callbacks']['after_delete'][] = 'sendDeleteNotification';
        $config['registered_callbacks']['after_delete'][] = 'createDeleteLogEntry';

        //In regard to TIC 6460:
        //As long as TIC 6460 is not implemented, we must add the validate
        //method as a callback before storing the object.
        if (!method_exists('SimpleORMap', 'validate')) {
            $config['registered_callbacks']['before_store'][] = 'validate';
        }

        parent::configure($config);
    }


    public function createStoreLogEntry()
    {
        if ($this->isSimpleBooking()) {
            StudipLog::log(
                'RES_ASSIGN_SINGLE',
                $this->resource_id,
                null,
                $this->__toString(),
                null,
                $GLOBALS['user']->id
            );
        } else {
            StudipLog::log(
                'RES_ASSIGN_SEM',
                $this->resource_id,
André Noack's avatar
André Noack committed
                $this->course_id,
                $this->__toString(),
                null,
                $GLOBALS['user']->id
            );
        }
    }


    public function createDeleteLogEntry()
    {
        if ($this->isSimpleBooking()) {
            StudipLog::log(
                'RES_ASSIGN_DEL_SINGLE',
                $this->resource_id,
                null,
                $this->__toString(),
                null,
                $GLOBALS['user']->id
            );
        } else {
            StudipLog::log(
                'RES_ASSIGN_DEL_SEM',
                $this->resource_id,
André Noack's avatar
André Noack committed
                $this->course_id,
                $this->__toString(),
                null,
                $GLOBALS['user']->id
            );
        }
    }


    /**
     * Internal method that generated the SQL query used in
     * findByResourceAndTimeRanges and countByResourceAndTimeRanges.
     *
     * @see findByResourceAndTimeRanges
     * @inheritDoc
     */
    protected static function buildResourceAndTimeRangesSqlQuery(
        Resource $resource,
        $time_ranges = [],
        $booking_types = [],
        $excluded_booking_ids = []
    )
    {
        if (!is_array($time_ranges)) {
            throw new InvalidArgumentException(
                _('Es wurde keine Liste mit Zeiträumen angegeben!')
            );
        }

        //Check the array:
        foreach ($time_ranges as $time_range) {
            if ($time_range['begin'] > $time_range['end']) {
                throw new InvalidArgumentException(
                    _('Der Startzeitpunkt darf nicht hinter dem Endzeitpunkt liegen!')
                );
            }

            if ($time_range['begin'] == $time_range['end']) {
                throw new InvalidArgumentException(
                    _('Startzeitpunkt und Endzeitpunkt dürfen nicht identisch sein!')
                );
            }
        }

        $sql_params = [
            'resource_id' => $resource->id
        ];

        //First we build the SQL snippet for the case that the $booking_type
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
        //variable is set to something different than null.
        $booking_type_sql = '';
        if (is_array($booking_types) && count($booking_types)) {
            $booking_type_sql = ' AND booking_type IN ( :booking_types )';
            $sql_params['booking_types'] = $booking_types;
        }

        //Then we build the snippet for excluded booking IDs, if specified.
        $excluded_booking_ids_sql = '';
        if (is_array($excluded_booking_ids) && count($excluded_booking_ids)) {
            $excluded_booking_ids_sql = ' AND resource_booking_intervals.booking_id NOT IN ( :excluded_ids ) ';
            $sql_params['excluded_ids'] = $excluded_booking_ids;
        }

        //Now we build the SQL snippet for the time intervals.
        //These are repeated four times in the query below.
        //First we use one template ($time_sql_template) and
        //replace NUMBER with our counting variable $i.
        //BEGIN and END are replaced below since the columns for
        //BEGIN and END are different in the four cases where we
        //repeat the SQL snippet for the time intervals.
        $time_sql_template = '
            (resource_booking_intervals.begin < :endNUMBER AND :beginNUMBER < resource_booking_intervals.end)
            ';

        $time_sql = '';
        if ($time_ranges) {
            $time_sql = ' AND (';

            $i = 1;
            foreach ($time_ranges as $time_range) {
                if ($i > 1) {
                    $time_sql .= ' OR ';
                }
                $time_sql .= str_replace(
                    'NUMBER',
                    $i,
                    $time_sql_template
                );

                if ($time_range['begin'] instanceof DateTime) {
                    $sql_params[('begin' . $i)] = $time_range['begin']->getTimestamp();
                } else {
                    $sql_params[('begin' . $i)] = $time_range['begin'];
                }
                if ($time_range['end'] instanceof DateTime) {
                    $sql_params[('end' . $i)] = $time_range['end']->getTimestamp();
                } else {
                    $sql_params[('end' . $i)] = $time_range['end'];
                }

                $i++;
            }

            $time_sql .= ') ';
        }

        //Check if the booking has a start and end timestamp set
        //or if it has repetitions that have a matching timestamp.
        //This is done in the rest of the SQL query:

        $whole_sql = "resource_bookings.id IN (
                    SELECT resource_booking_intervals.booking_id FROM resource_booking_intervals WHERE
                    resource_booking_intervals.resource_id = :resource_id
                    AND resource_booking_intervals.takes_place = 1"
                    . $excluded_booking_ids_sql
                    . $time_sql
                    . " GROUP BY resource_booking_intervals.booking_id ORDER BY NULL)
                    $booking_type_sql
";

        return [
            'sql' => $whole_sql,
            'params' => $sql_params
        ];
    }


    /**
     * Retrieves all resource booking for the given resource and
     * time range. By default, all booking are returned.
     * To get only bookings of a certain type
     * set the $booking_type parameter.
     *
     * @param Resource $resource The resource whose requests shall be retrieved.
     * @param array $time_ranges An array with time ranges as DateTime objects.
     *     The array has the following structure:
     *     [
     *         [
     *             'begin' => begin timestamp,
     *             'end' => end timestamp
     *         ],
     *         ...
     *     ]
     * @param array $booking_types An optional specification for the
     *     booking_type column in the database. More than one booking
     *     type can be specified.
     *     By default this is set to an empty array which means
     *     that resource booking are not filtered by the type column.
     *     The allowed resource booking types are specified in the
     *     class documentation.
     *
     * @param array $excluded_booking_ids An array of strings representing
     *     resource booking IDs. IDs specified in this array are excluded
     *     from the search.
     * @return ResourceBooking[] An array of ResourceRequest objects.
     *     If no requests can be found, the array is empty.
     *
     * @throws InvalidArgumentException, if the time ranges are either not in an
     *     array matching the format description from above or if one of the
     *     following conditions is met in one of the time ranges:
     *     - begin > end
     *     - begin == end
     */
    public static function findByResourceAndTimeRanges(
        Resource $resource,
        $time_ranges = [],
        $booking_types = [],
        $excluded_booking_ids = []
    )
    {
        //Build the SQL query and the parameter array.

        $sql_data = self::buildResourceAndTimeRangesSqlQuery(
            $resource,
            $time_ranges,
            $booking_types,
            $excluded_booking_ids
        );

        //Call findBySql:
        return self::findBySql($sql_data['sql'], $sql_data['params']);
    }


    /**
     * Counts all resource bookings for the specified resource and
     * time range. By default, all bookings are counted.
     * To count only bookings of a certain type
     * set the $booking_type parameter.
     *
     * @see findByResourceAndTimeRanges
     * @inheritDoc
     */
    public static function countByResourceAndTimeRanges(
        Resource $resource,
        $time_ranges = [],
        $booking_types = [],
        $excluded_booking_ids = []
    )
    {
        //Build the SQL query and the parameter array.

        $sql_data = self::buildResourceAndTimeRangesSqlQuery(
            $resource,
            $time_ranges,
            $booking_types,
            $excluded_booking_ids
        );

        //Call countBySql:
        return self::countBySql($sql_data['sql'], $sql_data['params']);
    }


    /**
     * Deletes all resource bookings for the specified resource and
     * time range. By default, all bookings are counted.
     * To count only bookings of a certain type
     * set the $booking_type parameter.
     *
     * @see findByResourceAndTimeRanges
     * @inheritDoc
     */
    public static function deleteByResourceAndTimeRanges(
        Resource $resource,
        $time_ranges = [],
        $booking_types = [],
        $excluded_booking_ids = []
    )
    {
        //Build the SQL query and the parameter array.
        $sql_data = self::buildResourceAndTimeRangesSqlQuery(
            $resource,
            $time_ranges,
            $booking_types,
            $excluded_booking_ids
        );

        //Call deleteBySql:
        return self::deleteBySql($sql_data['sql'], $sql_data['params']);
    }


    /**
     * The SimpleORMap::store method is overloaded to allow forced booking
     * of resource bookings, meaning that all other bookings of the
     * resource of a booking are deleted when they overlap with this booking.
     *
     * @param bool $force_booking Whether booking shall be forced (true)
     *     or not (false). Defaults to false.
     * @return bool
     */
    public function store($force_booking = false)
    {
        if ($force_booking == true) {
            $this->deleteOverlappingBookings();
        }
        $this->deleteOverlappingReservations();

        if (parent::store()) {
            //Check if the booking is bound to a course date.
            //If this is the case, check for existing bookings
            //and delete them, so that there is only one booking
            //for a course date:
            $course_date_exists = CourseDate::exists($this->range_id);
            if ($course_date_exists) {
                self::deleteBySql(
                    'range_id = :range_id AND id <> :this_id',
                    [
                        'this_id' => $this->id,
                        'range_id' => $this->range_id
                    ]
                );
            }
            return true;
        }
        return false;
    }


    /**
     * This validation method is called before storing an object.
     */
    public function validate()
    {
        if ((!$this->resource_id) || !($this->resource instanceof Resource)) {
            throw new InvalidArgumentException(
                _('Es wurde keine Ressource zur Buchung angegeben!')
            );
        }

        if (!$this->range_id && !$this->description) {
            throw new ResourceBookingRangeException(
                _('Es muss eine Person oder ein Text zur Buchung eingegeben werden!')
            );
        }

        if ((!$this->booking_user_id) || !($this->booking_user instanceof User)) {
            /*throw new InvalidArgumentException(
                _('Die buchende Person wurde nicht gesetzt!')
            );*/
            $this->booking_user = User::findCurrent();
        }

        if ($this->begin >= $this->end) {
            throw new InvalidArgumentException(
                _('Der Startzeitpunkt darf nicht hinter dem Endzeitpunkt liegen!')
            );
        }

        if ($this->repetition_interval) {
            if (!($this->repeat_quantity || $this->repeat_end)) {
                throw new InvalidArgumentException(
                    _('Es wurde ein Wiederholungsintervall ohne Begrenzung angegeben!')
                );
            }
            if ((!$this->repeat_quantity) && ($real_begin > $this->repeat_end)) {
                throw new InvalidArgumentException(
                    _('Der Startzeitpunkt darf nicht hinter dem Ende der Wiederholungen liegen!')
                );
            }
        }

        //Check if the user has booking rights on the resource.
        //The user must have either permanent permissions or they have to
        //have booking rights by a temporary permission in this moment
        //(the moment this booking is saved).
        $derived_resource = $this->resource->getDerivedClassInstance();
        $user_has_booking_rights = $derived_resource->userHasBookingRights(
            $this->booking_user
        );
        if (!$user_has_booking_rights) {
            throw new ResourcePermissionException(
                sprintf(
                    _('Unzureichende Berechtigungen zum Buchen der Ressource %s!'),
                    $this->resource->name
                )
            );
        }

        $time_intervals = $this->calculateTimeIntervals(true);
        $time_interval_overlaps = [];
        foreach ($time_intervals as $time_interval) {
            $is_locked = $derived_resource->isLocked(
                $time_interval['begin'],
                $time_interval['end'],
                ($this->isNew() ? [] : [$this->id])
            );
            if ($is_locked) {
                if ($time_interval['begin']->format('Ymd') == $time_interval['end']->format('Ymd')) {
                    $time_interval_overlaps[] = sprintf(
                        _('Gesperrt im Bereich vom %1$s bis %2$s'),
                        $time_interval['begin']->format('d.m.Y H:i'),
                        $time_interval['end']->format('H:i')
                    );
                } else {
                    $time_interval_overlaps[] = sprintf(
                        _('Gesperrt im Bereich vom %1$s bis zum %2$s'),
                        $time_interval['begin']->format('d.m.Y H:i'),
                        $time_interval['end']->format('d.m.Y H:i')
                    );
                }
            } else {
                $is_assigned = $derived_resource->isAssigned(
                    $time_interval['begin'],
                    $time_interval['end'],
                    ($this->isNew() ? [] : [$this->id])
                );
                if ($is_assigned) {
                    if ($time_interval['begin']->format('Ymd') == $time_interval['end']->format('Ymd')) {
                        $time_interval_overlaps[] = sprintf(
                            _('Gebucht im Bereich vom %1$s bis %2$s'),
                            $time_interval['begin']->format('d.m.Y H:i'),
                            $time_interval['end']->format('H:i')
                        );
                    } else {
                        $time_interval_overlaps[] = sprintf(
                            _('Gebucht im Bereich vom %1$s bis zum %2$s'),
                            $time_interval['begin']->format('d.m.Y H:i'),
                            $time_interval['end']->format('d.m.Y H:i')
                        );
                    }
                }
            }
        }
        if ($time_interval_overlaps) {
            throw new ResourceBookingOverlapException(
                implode(', ', $time_interval_overlaps)
            );
        }

        NotificationCenter::postNotification('ResourceBookingWillValidate', $this);
    }


    /**
     * This method updates the intervals of this resource booking
     * which are stored in the resource_booking_intervals table.
     */
    public function updateIntervals($keep_exceptions = true)
    {
        if ($keep_exceptions) {
            //Delete all intervals with takes_place > 0
            //and update the time intervals of the exceptions:
            ResourceBookingInterval::deleteBySql(
                "booking_id = :booking_id
                AND
                takes_place > '0'",
                [
                    'booking_id' => $this->id
                ]
            );

            //Get all interval exceptions:
            $exceptions = ResourceBookingInterval::findBySql(
                "booking_id = :booking_id
                AND
                takes_place < '1'",
                [
                    'booking_id' => $this->id
                ]
            );

            if ($exceptions) {
                //Now we must compare the time intervals of the booking
                //with the time intervals of the exceptions.
                //If there is only a difference in hours we update
                //the exceptions. Otherwise we delete the exceptions.

                $repetition_interval = $this->getRepetitionInterval();
                if (!$repetition_interval) {
                    //No repetition interval also means nothing left to do.
                    return;
                }

                $repetition_begin = new DateTime();
                $repetition_begin->setTimestamp($this->begin - $this->preparation_time);
                $date_end = new DateTime();
                $date_end->setTimestamp($this->end);
                $repetition_end = new DateTime();
                $repetition_end->setTimestamp($this->repeat_end);
                $repetition_interval = $this->getRepetitionInterval();

                $duration = $repetition_begin->diff($date_end);

                //Loop over all exceptions and check if they belong to
                //one of the repetions:

                $obsolete_exception_ids = [];
                foreach ($exceptions as $exception) {
                    $exception_begin = new DateTime();
                    $exception_begin->setTimestamp($exception->begin);
                    $exception_end = new DateTime();
                    $exception_end->setTimestamp($exception->end);
                    $exc_begin_str = $exception_begin->format('Y-m-d');
                    $exc_end_str = $exception_end->format('Y-m-d');

                    $exception_obsolete = true;

                    $current_repetition = clone $repetition_begin;
                    while ($current_repetition < $repetition_end) {
                        $current_end = clone $current_repetition;
                        $current_end->add($duration);

                        $current_begin_str = $current_repetition->format('Y-m-d');
                        $current_end_str = $current_end->format('Y-m-d');

                        if ($current_begin_str == $exc_begin_str && $current_end_str == $exc_end_str) {
                            //We found one exception which needs to be updated.
                            $exception_obsolete = false;
                            $exception->begin = $current_repetition->getTimestamp();
                            $exception->end = $current_end->getTimestamp();
                            if ($exception->isDirty()) {
                                $exception->store();
                            }
                            //No need to loop the rest of the repetitions:
                            break;
                        }

                        $current_repetition->add($repetition_interval);
                    }

                    if ($exception_obsolete) {
                        $obsolete_exception_ids[] = $exception->id;
                    }
                }

                if ($obsolete_exception_ids) {
                    ResourceBookingInterval::deleteBySql(
                        'interval_id IN ( :ids )',
                        [
                            'ids' => $obsolete_exception_ids
                        ]
                    );
                }
            }
        } else {
            //We delete all existing intervals for this booking
            //and re-create them.
            ResourceBookingInterval::deleteBySql(
                'booking_id = :booking_id',
                [
                    'booking_id' => $this->id
                ]
            );
        }

        ResourceBookingInterval::createFromBooking($this);
    }


    /**
     * Deletes the ResourceBooking object if there are no
     * ResourceBookingInterval objects attachted to it.
     *
     * @return null|bool If the ResourceBooking object still has
     *     ResourceBookingInterval objects attachted to it,
     *     null is returned. Otherwise the return value of the
     *     SimpleORMap::delete method for the ResourceBooking object
     *     is returned.
     */
    public function deleteIfNoInterval()
    {
        $intervals = ResourceBookingInterval::countBySql(
            'booking_id = :id',
            [
                'id' => $this->id
            ]
        );
        if ($intervals == 0) {
            return $this->delete();
        }

        return null;
    }


    /**
     * Deletes all bookings in the time ranges of this resource booking.
     * Such bookings would prevent saving this booking.
     *
     * @return int The amount of deleted bookings.
     */
    public function deleteOverlappingBookings()
    {
        $real_begin = new DateTime();
        $real_begin->setTimestamp($this->begin - $this->preparation_time);
        $end = new DateTime();
        $end->setTimestamp($this->end);
        $repetition_end = new DateTime();
        $repetition_end->setTimestamp($this->repeat_end);

        $deleted_c = 0;
        if ($this->repetition_interval) {
            $repetition_interval = $this->getRepetitionInterval();

            if (!($this->repeat_quantity || $this->repeat_end)) {
                throw new InvalidArgumentException(
                    _('Es wurde ein Wiederholungsintervall ohne Begrenzung angegeben!')
                );
            }
            if ((!$this->repeat_quantity) && ($real_begin > $this->repeat_end)) {
                throw new InvalidArgumentException(
                    _('Der Startzeitpunkt darf nicht hinter dem Ende der Wiederholungen liegen!')
                );
            }

            //Look in each repetition for overlapping bookings and delete them.
            $current_date = clone $real_begin;
            while ($current_date <= $repetition_end) {
                $current_begin = clone $current_date;
                $current_end = clone $current_date;
                $current_end->setTime(
                    intval($end->format('H')),
                    intval($end->format('i')),
                    intval($end->format('s'))
                );
                $derived_resource = $this->resource->getDerivedClassInstance();
                if ($derived_resource->userHasPermission($this->booking_user, 'tutor', [$current_begin, $current_end])) {
                    //Sufficient permissions to delete bookings
                    //in the time frame.
                    $delete_sql = '(begin BETWEEN :begin AND :end
                        OR
                        end BETWEEN :begin AND :end)
                        AND
                        resource_id = :resource_id ';
                    $sql_params = [
                        'begin' => $current_begin->getTimestamp(),
                        'end' => $current_end->getTimestamp(),
                        'resource_id' => $this->resource->id
                    ];
                    if (!$this->isNew()) {
                        $delete_sql .= 'booking_id <> :booking_id';
                        $sql_params['booking_id'] = $this->id;
                    }
                    $intervals = ResourceBookingInterval::findBySQL(
                        $delete_sql,
                        $sql_params
                    );

                    $affected_bookings = [];
                    foreach ($intervals as $interval) {
                        if ($interval->booking instanceof ResourceBooking) {
                            $affected_bookings[$interval->booking_id] = $interval->booking;
                        }
                        $deleted_c += $interval->delete();
                    }

                    foreach ($affected_bookings as $booking) {
                        $booking->deleteIfNoInterval();
                    }
                }
                $current_date = $current_date->add($repetition_interval);
            }
        } else {
            $derived_resource = $this->resource->getDerivedClassInstance();
            if ($derived_resource->userHasPermission($this->booking_user, 'autor', [$real_begin, $end])) {
                $delete_sql = '(begin BETWEEN :begin AND :end
                    OR
                    end BETWEEN :begin AND :end)
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
                    AND
                    resource_id = :resource_id ';
                $sql_params = [
                    'begin' => $real_begin->getTimestamp(),
                    'end' => $end->getTimestamp(),
                    'resource_id' => $this->resource->id
                ];
                if (!$this->isNew()) {
                    $delete_sql .= 'booking_id <> :booking_id';
                    $sql_params['booking_id'] = $this->id;
                }
                $intervals = ResourceBookingInterval::findBySQL(
                    $delete_sql,
                    $sql_params
                );
                $affected_bookings = [];
                foreach ($intervals as $interval) {
                    if ($interval->booking instanceof ResourceBooking) {
                        $affected_bookings[$interval->booking_id] = $interval->booking;
                    }
                    $deleted_c += $interval->delete();
                }

                foreach ($affected_bookings as $booking) {
                    $booking->deleteIfNoInterval();
                }
            }
        }

        return $deleted_c;
    }


    /**
     * Deletes all reservations in the time ranges of this resource booking.
     *
     * @return int The amount of deleted reservations.
     */
    public function deleteOverlappingReservations()
    {
        //Notify all persons who made reservations or who are assigned
        //to the reservation about the new booking which overwrites
        //their reservation:
        $booking_resource = Resource::find($this->resource_id);
        $booking_user = User::find($this->booking_user_id);

        $real_begin = new DateTime();
        $real_begin->setTimestamp($this->begin - $this->preparation_time);
        $end = new DateTime();
        $end->setTimestamp($this->end);

        $deleted_c = 0;

        $template_factory = new Flexi_TemplateFactory(
            $GLOBALS['STUDIP_BASE_PATH'] . '/locale/'
        );

        $affected_reservations =  ResourceBooking::findByResourceAndTimeRanges(
            $booking_resource,
            [
                [
                    'begin' => $real_begin->getTimestamp(),
                    'end' => $end->getTimestamp(),
                ]
            ],
            [1, 3],
            [$this->id]
        );
        foreach ($affected_reservations as $reservation) {
            if ($reservation->id == $this->id) {
                continue;
            }
            if ($reservation->assigned_user && (
                $reservation->range_id != $reservation->booking_user_id
            )) {
                //Inform the person who is assigned to the reservation:
                setTempLanguage($reservation->assigned_user->id);
                $lang_path = getUserLanguagePath($reservation->assigned_user->id);

                $template = $template_factory->open(
                    $lang_path . '/LC_MAILS/overbooked_reservation.php'
                );
                $template->set_attribute('resource', $booking_resource);
                $template->set_attribute('reservation', $reservation);
                $template->set_attribute('booking_user', $booking_user);
                $mail_text = $template->render();

                Message::send(
                    '____%system%____',
                    [$reservation->assigned_user->username],
                    _('Reservierung überbucht'),
                    $mail_text
                );

                restoreLanguage();
            }
            if ($reservation->booking_user) {
                //Inform the person who made the reservation:
                setTempLanguage($reservation->booking_user->id);
                $lang_path = getUserLanguagePath($reservation->booking_user->id);

                $template = $template_factory->open(
                    $lang_path . '/LC_MAILS/overbooked_reservation.php'
                );
                $template->set_attribute('resource', $booking_resource);
                $template->set_attribute('reservation', $reservation);
                $template->set_attribute('booking_user', $booking_user);
                $mail_text = $template->render();

                Message::send(
                    '____%system%____',
                    [$reservation->booking_user->username],
                    _('Reservierung überbucht'),
                    $mail_text
                );

                restoreLanguage();
            }

            //Delete the reservation:
            $deleted_c += $reservation->delete();
        }
        return $deleted_c;
    }


    /**
     * Determines whether the resource booking ends on the same timestamp
     * like the lecture time of one of the defined semesters.
     *
     * @return True, if the resource booking ends with a semester,
     *     false otherwise.
     */
    public function endsWithSemester()
    {
        return Semester::countBySql(
            '(beginn <= :begin) AND (ende >= :begin)
             AND vorles_ende = :repeat_end',
            [
                'begin' => $this->begin,
                'repeat_end' => $this->repeat_end
            ]
        ) > 0;
    }


    /**
     * Check if the specified user is the owner of the booking.
     *
     * @param User $user The user whose ownership shall be tested.
     *
     * @return bool True, if the specified user is the owner of the booking,
     *     false otherwise.
     */
    public function userIsOwner(User $user)
    {
        return $this->booking_user_id == $user->id;
    }


    /**
     * Determines wheter the booking is read only for a specified user.
     *
     * @param User $user The user whose permissions shall be checked.
     *
     * @return bool True, if the specified user may only perform reading
     *     actions on the booking, false otherwise.
     */
    public function isReadOnlyForUser(User $user)
    {
        if ($this->isSimpleBooking()) {
            //In case it is a simple booking, one has to be
            //either resource tutor or the owner of the request.
            if ($this->userIsOwner($user)) {
                return false;
            }
            //Still no answer? Check, if the user is resource tutor.
            $derived_resource = $this->resource->getDerivedClassInstance();
            return !$derived_resource->userHasPermission($user, 'tutor');
        }
        //Non-simple bookings (course bookings etc.) are always read-only.
        return true;
    }


    /**
     * Determines whether this resource booking has a repetition in the
     * specified time range.
     * @param DateTime $begin
     * @param DateTime $end
     * @return bool True, if the booking has repetitions in the timeframe
     * specified by $begin and $end, false otherwise.
     */
    public function isRepetitionInTimeframe(DateTime $begin, DateTime $end)
    {
        return ResourceBookingInterval::countBySql(
            "booking_id = :booking_id
            AND (
                (begin BETWEEN :begin AND :end)
                OR
                (end BETWEEN :begin AND :end)
                OR
                (begin < :begin AND end > :end)
            )
            AND NOT (
                (begin BETWEEN :booking_begin AND :booking_end)
                OR
                (end BETWEEN :booking_begin AND :booking_end)
                OR
                (begin < :booking_begin AND end > :booking_end)
            )",
            [
                'booking_id' => $this->id,
                'begin' => $begin->getTimestamp(),
                'end' => $end->getTimestamp(),
                'booking_begin' => $this->begin,
                'booking_end' => $this->end
            ]
        ) > 0;
    }


    /**
     * Returns the DateInterval object according to the set repetition
     * interval of this resource booking object.
     *
     * @return DateInterval|null A DateInterval object or null,
     *     if this booking has no repetition interval.
     */
    public function getRepetitionInterval()
    {
        $repetition_interval = null;
        if ($this->repetition_interval) {
            try {
                if ($this->repetition_interval instanceof DateInterval) {
                    $repetition_interval = $this->repetition_interval;
                } else {
                    $repetition_interval = new DateInterval($this->repetition_interval);
                }
            } catch (Exception $e) {
                //Invalid repetition interval string.
                throw new InvalidArgumentException(
                    sprintf(
                        _('Das Wiederholungsintervall ist in einem ungültigen Format (%s)!'),
                        $this->repetition_interval
                    )
                );
            }
        }
        return $repetition_interval;
    }


    public function getRepeatModeString()
    {
        $interval = $this->getRepetitionInterval();

        if ($interval->m) {
            switch ($interval->m) {
                case 1:  return _('jeden Monat');
                case 2:  return _('jeden zweiten Monat');
                case 3:  return _('jeden dritten Monat');
                case 4:  return _('jeden vierten Monat');
                case 5:  return _('jeden fünften Monat');
                case 6:  return _('jeden sechsten Monat');
                case 7:  return _('jeden siebten Monat');
                case 8:  return _('jeden achten Monat');
                case 9:  return _('jeden neunten Monat');
                case 10: return _('jeden zehnten Monat');
                case 11: return _('jeden elften Monat');
            }
        } elseif (($interval->d % 7) == 0) {
            $week = round($interval->d / 7);
            switch ($week) {
                case 1: return _('jede Woche');
                case 2: return _('jede zweite Woche');
                case 3: return _('jede dritte Woche');
            }
        } elseif ($interval->d) {
            if ($interval->d > 1) {
                return sprintf(
                    _('jeden %d. Tag'),
                    $interval->d
                );
            } elseif ($interval->d == 1) {
                return _('jeden Tag');
            } else {
                return _('ungültiges Zeitintervall');
            }
        }
    }

    /**
     * Determines if the resource booking overlaps with another
     * resource booking.
     *
     * @return bool True, if there are other bookings which overlap
     *     with this one, false otherwise.
     */
    public function hasOverlappingBookings()
    {
        //Get all intervals of this booking, loop over them
        //and check if there are other intervals in the same
        //timeframe for the same resource.

        $intervals = ResourceBookingInterval::findBySql(
            'booking_id = :booking_id',
            [
                'booking_id' => $this->id
            ]
        );

        if ($intervals) {
            foreach ($intervals as $interval) {
                $count = ResourceBookingInterval::countBySql(
                    'booking_id <> :booking_id
                    AND
                    resource_id = :resource_id
                    AND takes_place = 1
                    AND
                    (
                        begin < :end AND :begin < end
                    )',
                    [
                        'booking_id' => $this->id,
                        'resource_id' => $this->resource_id,
                        'begin' => $interval->begin,
                        'end' => $interval->end
                    ]
                );

                if ($count) {
                    //We have found an interval which overlaps
                    //with an interval of this booking.
                    return true;
                }
            }
        }

        //If this booking has no intervals it means it doesn't have
        //any time intervals at all. Therefore there can't be any
        //overlapping bookings.
        return false;
    }

    /**
     * Gets the bookings that overlap with this booking.
     *
     * @return array Array of ResourceBooking objects
     */
    public function getOverlappingBookings()
    {
        //Get all intervals of this booking, loop over them
        //and check if there are other intervals in the same
        //timeframe for the same resource. If so, collect the
        //booking-IDs and get all the bookings.

        $intervals = ResourceBookingInterval::findBybooking_id($this->id);

        if ($intervals) {
            $db = DBManager::get();

            $get_booking_id_stmt = $db->prepare(
                'SELECT DISTINCT booking_id
                FROM resource_booking_intervals
                WHERE
                booking_id <> :booking_id
                AND
                resource_id = :resource_id
                AND takes_place = 1
                AND
                (
                    begin < :end AND :begin < end
                )'
            );
            $overlapping_booking_ids = [];
            foreach ($intervals as $interval) {
                $get_booking_id_stmt->execute(
                    [
                        'booking_id' => $this->id,
                        'resource_id' => $this->resource_id,
                        'begin' => $interval->begin,
                        'end' => $interval->end
                    ]
                );

                $overlapping_booking_ids = array_merge(
                    $overlapping_booking_ids,
                    $get_booking_id_stmt->fetchAll(
                        PDO::FETCH_COLUMN,
                        0
                    )
                );
            }

            if ($overlapping_booking_ids) {
                $overlapping_booking_ids = array_unique(
                    $overlapping_booking_ids
                );

                return ResourceBooking::findMany(
                    $overlapping_booking_ids
                );
            }
        }

        //If this booking has no intervals it means it doesn't have
        //any time intervals at all. Therefore there can't be any
        //overlapping bookings.
        return false;
    }


    /**
     * Calculates all time intervals as begin and end timestamps
     * by looking at the repetition settings, if any.
     *
     * @param bool $as_datetime Whether to return the timestamps
     *     as DateTime objects (true) or not (false). Defaults to false.
     *
     * @return array A two-dimensional array with each time interval
     *     for this booking. The array has the following structure:
     *     [
     *         [
     *             'begin' => Begin timestamp.
     *             'end' => End timestamp.
     *         ]
     *     ]
     */
    public function calculateTimeIntervals($as_datetime = false)
    {
        $interval_data = [];
        $booking_begin = new DateTime();
        $booking_begin->setTimestamp($this->begin);
        if ($this->preparation_time) {
            $booking_begin->setTimestamp(
                $this->begin - $this->preparation_time
            );
        }
        $booking_end = new DateTime();
        $booking_end->setTimestamp($this->end);

        //use begin and end to create the first interval:
        $interval_data[] = [
            'begin' => (
                $as_datetime
                ? clone $booking_begin
                : $booking_begin->getTimestamp()
            ),
            'end' => (
                $as_datetime
                ? clone $booking_end
                : $booking_end->getTimestamp()
            )
        ];

        if (($this->repeat_quantity > 0) || $this->repeat_end) {
            //Repetition: we must check which repetition interval has been
            //selected and then create entries for each repetition.
            //Repetition starts with the begin date and ends with the
            //"repeat_end" date.
            $repetition_end = new DateTime();
            $repetition_end->setTimestamp($this->repeat_end);
            //The DateInterval constructor will throw an exception,
            //if it cannot parse the string stored in $this->repetition_interval.
            $repetition_interval = $this->getRepetitionInterval();

            if ($repetition_interval instanceof DateInterval) {
                $duration = $booking_begin->diff($booking_end);

                //Check if end is later than begin to avoid
                //infinite loops.
                if ($repetition_end > $booking_begin) {
                    $current_begin = clone $booking_begin;
                    $current_begin->add($repetition_interval);
                    while ($current_begin < $repetition_end) {
                        $current_end = clone $current_begin;
                        $current_end->add($duration);
                        $interval_data[] = [
                            'begin' => (
                                $as_datetime
                                ? clone $current_begin
                                : $current_begin->getTimestamp()
                            ),
                            'end' => (
                                $as_datetime
                                ? clone $current_end
                                : $current_end->getTimestamp()
                            )
                        ];
                        $current_begin->add($repetition_interval);
                    }
                } else {
                    //end timestamp is before begin timestamp:
                    //return an empty array
                    return [];
                }
            }
            //Everything went fine.
        }
        return $interval_data;
    }


    /**
     * Retrieves all time intervals for this resource booking.
     *
     * @return ResourceBookingInterval[] An array of
     *     ResourceBookingInterval objects.
     */
    public function getTimeIntervals($with_exceptions = true)
    {
        if ($with_exceptions) {
            return ResourceBookingInterval::findBySQL(
                "booking_id = :booking_id
                ORDER BY begin ASC, end ASC",
                [
                    'booking_id' => $this->id
                ]
            );
        } else {
            return ResourceBookingInterval::findBySQL(
                "booking_id = :booking_id AND takes_place = '1'
                ORDER BY begin ASC, end ASC",
                [
                    'booking_id' => $this->id
                ]
            );
        }
    }


    /**
     * Retrieves all time intervals for this resource booking
     * in a specified time range.
     *
     * @param DateTime $begin The begin of the time range.
     * @param DateTime $end The end of the time range.
     *
     * @return ResourceBookingInterval[] An array of
     *     ResourceBookingInterval objects.
     */
    public function getTimeIntervalsInTimeRange(DateTime $begin, DateTime $end)
    {
        if ($begin > $end) {
            //We don't serve querys with invalid time ranges here.
            return [];
        }

        return ResourceBookingInterval::findBySql(
            'booking_id = :booking_id
            AND
            (
                (begin >= :begin AND begin <= :end)
                OR
                (end >= :begin AND end <= :end)
            )
            ORDER BY begin ASC, end ASC',
            [
                'booking_id' => $this->id,
                'begin' => $begin->getTimestamp(),
                'end' => $end->getTimestamp()
            ]
        );
    }


    /**
     * Returns all allocating users: Users who are associated with this booking
     * through a request or a course date for which this booking has been made
     * (all allocating users). If no user could be determined but the booking
     * is bound to a course, the course name is returned instead.
     *
     * @deprecated
     * @param bool $only_names Whether only the names of these users shall be
     *     returned (true) or user objects shall be returned (false).
     *
     * @return string[]|User[] Depending on the value of the $only_names
     *     parameter a string array or an user object array is returned.
     *     In case no user can be found, the array is empty.
     *     In case the parameter $only_names is set to true, the result
     *     may also contain a course name, if no users can be found but the
     *     booking is bound to a course.
     */
    public function getAssignedUsers($only_names = true)
    {
        //The only two cases which shall be handled are that
        //a booking is assigned to a course or an user.
        if ($this->getAssignedUserType() === 'course') {
            //Return all persons associated with that course date.
            if (count($this->assigned_course_date->dozenten)) {
                if ($only_names) {
                    $lecturers = [];
                    foreach ($this->assigned_course_date->dozenten as $lecturer) {
                        $lecturers[] = $lecturer->getFullName();
                    }
                    return $lecturers;
                } else {
                    return $this->assigned_course_date->dozenten;
                }
            } else {
                $lecturers = User::findBySql(
                    "INNER JOIN seminar_user
                    USING (user_id)
                    WHERE
                    seminar_user.seminar_id = :course_id
                    AND
                    seminar_user.status = 'dozent'
                    ORDER BY nachname ASC, vorname ASC",
                    [
                        'course_id' => $this->assigned_course_date->range_id
                    ]
                );
                if (!$lecturers) {
                    return [];
                }
                if ($only_names) {
                    $names = [];
                    foreach ($lecturers as $lecturer) {
                        $names[] = $lecturer->getFullName();
                    }
                    return $names;
                } else {
                    return $lecturers;
                }
            }
        } elseif ($this->getAssignedUserType() === 'user') {
            if ($only_names) {
                return [$this->assigned_user->getFullName()];
            }
            return [$this->assigned_user];
        }

        return [];
    }

    public function getAssignedUser()
    {
        if ($this->getAssignedUserType() === 'course') {
            return $this->assigned_course_date->course;
        }
        if ($this->getAssignedUserType() === 'user') {
            return $this->assigned_user;
        }
    }

    public function getAssignedUserType()
    {
        if (isset($this->assigned_user_type)) {
            return $this->assigned_user_type;
        }
        if ($this->assigned_course_date instanceof CourseDate) {
            return $this->assigned_user_type = 'course';
        }
        if ($this->assigned_user instanceof User) {
            return $this->assigned_user_type = 'user';
        }
        return $this->assigned_user_type = 'none';
    }

    public function getAssignedUserName()
    {
        if ($this->getAssignedUserType() === 'course') {
            $name = $this->assigned_course_date->course->getFullname();
            $name .= ' (' . implode(',', $this->assigned_course_date->course->getMembersWithStatus('dozent', true)->limit(3)->getValue('nachname')) . ')';
        } elseif ($this->getAssignedUserType() === 'user') {
            if (get_visibility_by_id($this->assigned_user->id) ||
                ($this->assigned_user->id == $GLOBALS['user']->id)
            ) {
                $name = $this->assigned_user->getFullname();
                if ($this->description) {
                    $name .= " \n" . $this->description;
                }
            } else {
                //Check if the current user has at least user permissions
                //on the resource. In that case, even invisible assigned
                //users become visible.
                $resource = $this->resource->getDerivedClassInstance();
                $current_user = User::findCurrent();
                if (($resource instanceof Resource) && ($current_user instanceof User)) {
                    if ($resource->userHasPermission($current_user, 'user')) {
                        $name = $this->assigned_user->getFullname();
                        if ($this->description) {
                            $name .= " \n" . $this->description;
                        }
                    }
                }
            }
        } else {
            $name = $this->description;
        }
        return $name;
    }


    /**
     * Determines whether the booking is a simple booking
     * that is not bound to course dates or similar Stud.IP objects.
     */
    public function isSimpleBooking()
    {
        return !($this->getAssignedUserType() === 'course');
    }


    /**
     * Determins whether the booking has exceptions in repetitions or not.
     * When the booking is bound to a course via a CourseDate instance,
     * the exceptions are looked up using the corresponding metadate (if any).
     * If a metadate exists to the course date and it has cancelled dates
     * (metadate has CourseExDate instances assigned) then the booking has
     * exceptions.
     * If the booking is a simple booking, the exception status is determined
     * by checking if the booking has ResourceBookingInterval instances
     * that don't take place assigned to it. This is only checked,
     * if a repetition interval is set for the booking so that such instances
     * can exist.
     *
     * @return bool True, if the booking has exceptions in the repetition,
     *     false otherwise.
     */
    public function hasExceptions()
    {
        if ($this->isSimpleBooking()) {
            //This is a simple booking: Check the repetition interval:
            if ($this->repetition_interval) {
                //A repetition interval exists: Check if booking intervals
                //exist that don't take place:
                return ResourceBookingInterval::countBySql(
                    "booking_id = :booking_id
                    AND takes_place = '0'",
                    [
                        'booking_id' => $this->id
                    ]
                ) > 0;
            } else {
                return false;
            }
        } else {
            //Check if the course booking has exceptions (via metadate):
            $metadate = $this->assigned_course_date->cycle;
            if ($metadate instanceof SeminarCycleDate) {
                //Check if the metadate has exceptions:
                return CourseExDate::countBySql(
                    'metadate_id = :metadate_id',
                    [
                        'metadate_id' => $metadate->id
                    ]
                ) > 0;
            } else {
                //No metadate is assigned to the course booking.
                return false;
            }
        }
    }


    public function __toString()
    {
        return date('d.m.Y H:i', $this->begin)
             . ' - '
             . date('d.m.Y H:i', $this->end);
    }


    public function getTimeIntervalStrings()
    {
        $time_intervals = ResourceBookingInterval::findBySQL(
            'booking_id = :booking_id ORDER BY begin ASC, end ASC',
            [
                'booking_id' => $this->id
            ]
        );

        $strings = [];
        foreach ($time_intervals as $interval) {
            if (date('Ymd', $interval->begin) == date('Ymd', $interval->end)) {
                $strings[] = sprintf(
                    '%1$s %2$s - %3$s',
                    date('d.m.Y', $interval->begin),
                    date('H:i', $interval->begin),
                    date('H:i', $interval->end)
                );
            } else {
                $strings[] = sprintf(
                    '%1$s - %2$s',
                    date('d.m.Y H:i', $interval->begin),
                    date('d.m.Y H:i', $interval->end)
                );
            }
        }
        return $strings;
    }


    /**
     * @inheritDoc
     */
    public static function exportUserData(StoredUserData $storage)
    {
        $user = User::find($storage->user_id);
        $bookings = self::findBySql(
            'user_id = :user_id ORDER BY mkdate',
            [
                'user_id' => $storage->user_id
            ]
        );

        $rows = [];
        foreach ($bookings as $booking) {
            $rows[] = $booking->toRawArray();
        }
        $storage->addTabularData(
            _('Buchungen von Ressourcen'),
            'resource_bookings',
            $rows,
            $user
        );
    }


    public function convertToEventData(array $time_intervals, $user)
    {
        $booking_plan_booking_bg =
            \ColourValue::find('Resources.BookingPlan.Booking.Bg');
        $booking_plan_booking_fg =
            \ColourValue::find('Resources.BookingPlan.Booking.Fg');
        $booking_plan_simple_booking_with_exceptions_bg =
            ColourValue::find('Resources.BookingPlan.SimpleBookingWithExceptions.Bg');
        $booking_plan_simple_booking_with_exceptions_fg =
            ColourValue::find('Resources.BookingPlan.SimpleBookingWithExceptions.Fg');
        $booking_plan_reservation_bg = \ColourValue::find('Resources.BookingPlan.Reservation.Bg');
        $booking_plan_reservation_fg = \ColourValue::find('Resources.BookingPlan.Reservation.Fg');
        $booking_plan_lock_bg = \ColourValue::find('Resources.BookingPlan.Lock.Bg');
        $booking_plan_lock_fg = \ColourValue::find('Resources.BookingPlan.Lock.Fg');
        $booking_plan_planned_booking_bg = \ColourValue::find('Resources.BookingPlan.PlannedBooking.Bg');
        $booking_plan_planned_booking_fg = \ColourValue::find('Resources.BookingPlan.PlannedBooking.Fg');
        $booking_plan_preparation_bg = \ColourValue::find('Resources.BookingPlan.PreparationTime.Bg');
        $booking_plan_preparation_fg = \ColourValue::find('Resources.BookingPlan.PreparationTime.Fg');
        $booking_plan_course_booking_bg = \ColourValue::find('Resources.BookingPlan.CourseBooking.Bg');
        $booking_plan_course_booking_fg = \ColourValue::find('Resources.BookingPlan.CourseBooking.Fg');

        $colour = $booking_plan_booking_bg->__toString();
        $text_colour = $booking_plan_booking_fg->__toString();
        $event_classes = [];

        if ($this->booking_type == '0') {
            $event_classes[] = 'resource-booking';
            //Check if the booking is a course booking:
            if ($this->getAssignedUserType() === 'course') {
                //It is a course date.
                $event_classes[] = 'for-course';
                $colour = $booking_plan_course_booking_bg->__toString();
                $text_colour = $booking_plan_course_booking_fg->__toString();
            }
        } elseif ($this->booking_type == '1') {
            $event_classes[] = 'resource-reservation';
            $colour = $booking_plan_reservation_bg->__toString();
            $text_colour = $booking_plan_reservation_fg->__toString();
        } elseif ($this->booking_type == '2') {
            $event_classes[] = 'resource-lock';
            $colour = $booking_plan_lock_bg->__toString();
            $text_colour = $booking_plan_lock_fg->__toString();
        } elseif ($this->booking_type == '3') {
            $event_classes[] = 'resource-planned-booking';
            $colour = $booking_plan_planned_booking_bg->__toString();
            $text_colour = $booking_plan_planned_booking_fg->__toString();
        }

        $booking_is_editable = false;
        if ($user instanceof User) {
            $booking_is_editable = !$this->isReadOnlyForUser($user);
        }

        $booking_api_urls = [];
        $booking_view_urls = [
            'show' => \URLHelper::getURL(
                'dispatch.php/resources/booking/index/'
                . $this->id
            ),
        ];
        if ($booking_is_editable) {
            $booking_view_urls['edit'] = \URLHelper::getURL(
                'dispatch.php/resources/booking/edit/'
                . $this->id
            );
        }
        $events = [];

        foreach ($time_intervals as $interval) {
            $real_begin = $interval['begin'];
            if ($this->preparation_time) {
                $real_begin += $this->preparation_time;
                $begin = new DateTime();
                $begin->setTimestamp($interval['begin']);
                $end = new DateTime();
                $end->setTimestamp($real_begin);
                $events[] = new Studip\Calendar\EventData(
                    $begin,
                    $end,
                    _('Rüstzeit'),
                    ['preparation-time'],
                    $booking_plan_preparation_fg->__toString(),
                    $booking_plan_preparation_bg->__toString(),
                    $booking_is_editable,
                    'ResourceBookingInterval',
                    $interval->id,
                    'ResourceBooking',
                    $this->id,
                    'Resource',
                    $this->resource_id,
                    $booking_view_urls
                );
            }
            $prefix = '';
            $icon = '';

            if ($user instanceof User) {
                $derived_resource = $this->resource->getDerivedClassInstance();
                if ($derived_resource->userHasPermission($user, 'user') && $this->internal_comment) {
                    $icon = 'chat2';
                }
            }

            if (!$this->isSimpleBooking()) {
                if ($this->assigned_course_date->metadate_id) {
                    $icon = 'refresh';
                }
            } elseif ($this->repeat_end > $this->end) {
                $icon = 'refresh';
            }

            $event_title = $prefix . $this->getAssignedUserName();

            $interval_api_urls = $booking_api_urls;
            if ($booking_is_editable) {
                //A note on the move URL:
                //When used in a room group booking plan, the interval_id
                //URL parameter is subject to modification in JavaScript.
                //(lib/resources.js, method dropEventInRoomGroupBookingPlan)
                $interval_api_urls = [
                    'resize' => \URLHelper::getURL(
                        'api.php/resources/booking/'
                      . $this->id . '/move',
                        [
                            'quiet' => '1',
                            'interval_id' => $interval->id
                        ]
                    ),
                    'move' => \URLHelper::getURL(
                        'api.php/resources/booking/'
                      . $this->id . '/move',
                        [
                            'quiet' => '1',
                            'interval_id' => $interval->id
                        ]
                    )
                ];
            }
            $begin = new DateTime();
            $begin->setTimestamp($real_begin);
            $end = new DateTime();
            $end->setTimestamp($interval['end']);
            $events[] = new Studip\Calendar\EventData(
                $begin,
                $end,
                $event_title,
                $event_classes,
                $text_colour,
                $colour,
                $booking_is_editable,
                'ResourceBookingInterval',
                $interval->id,
                'ResourceBooking',
                $this->id,
                'Resource',
                $this->resource_id,
                $booking_view_urls,
                $interval_api_urls,
                $icon
            );
        }

        return $events;
    }


    public function getAllEventData()
    {
        return $this->convertToEventData(
            $this->getTimeIntervals(false),
            User::findCurrent()
        );
    }


    public function getEventDataForTimeRange(DateTime $begin, DateTime $end)
    {

        return $this->getFilteredEventData(null, null, null, $begin, $end);
    }


    public function getFilteredEventData(
        $user_id = null,
        $range_id = null,
        $range_type = null,
        $begin = null,
        $end = null
    )
    {
        $sql = "booking_id = :booking_id AND takes_place = 1 ";
        $sql_array = [
            'booking_id' => $this->id
        ];

        if ($begin && $end) {
            if ($begin instanceof DateTime) {
                $begin = $begin->getTimestamp();
            }
            if ($end instanceof DateTime) {
                $end = $end->getTimestamp();
            }
            $sql .= "AND begin <= :end AND :begin <= end";
            $sql_array['begin'] = $begin;
            $sql_array['end'] = $end;
        }
        $time_intervals = ResourceBookingInterval::findBySQL(
            $sql,
            $sql_array
        );

        $user = null;
        if ($user_id) {
            $user = User::find($user_id);
        } else {
            $user = User::findCurrent();
        }

        return $this->convertToEventData($time_intervals, $user);
    }

    /**
     * @return string
     */
    public function getRepetitionType()
    {
        if ($this->getAssignedUserType() === 'course') {
            if ($this->assigned_course_date->metadate_id) {
                return 'weekly';
            } else {
                return 'single';
            }
        } else {
            $intervall = $this->getRepetitionInterval();
            if (!$intervall) {
                return 'single';
            }
            if ($intervall->m) {
                return 'monthly';
            }
            if ($intervall->d % 7 === 0) {
                return 'weekly';
            }
            if ($intervall->d) {
                return 'daily';
            }
        }
    }


    /**
     * Sends a notification that the booking has been deleted
     * to the user that created the booking.
     */
    public function sendDeleteNotification()
    {
        if ($this->booking_type != '0') {
            //We only handle real bookings in this method.
            return;
        }

        if ($this->end < time()) {
            //Bookings that lie in the past can be deleted without
            //sending notifications.
            return;
        }

        $booking_resource = Resource::find($this->resource_id);
        $booking_user = User::find($this->booking_user_id);
        if (!$booking_resource || !$booking_user) {
            //Nothing we can do here.
            return;
        }
        if (User::findCurrent()->id === $booking_user->id) {
            return;
        }

        $template_factory = new Flexi_TemplateFactory(
            $GLOBALS['STUDIP_BASE_PATH'] . '/locale/'
        );
        setTempLanguage($booking_user->id);
        $lang_path = getUserLanguagePath($booking_user->id);

        $template = $template_factory->open(
            $lang_path . '/LC_MAILS/delete_booking_notification.php'
        );
        $template->set_attribute('resource', $booking_resource->getDerivedClassInstance());
        $template->set_attribute('begin', $this->begin);
        $template->set_attribute('end', $this->end);
        $template->set_attribute('deleting_user', User::findCurrent());

        $mail_text = $template->render();

        Message::send(
            '____%system%____',
            [$booking_user->username],
            _('Ihre Buchung wurde gelöscht'),
            $mail_text
        );

        restoreLanguage();
    }
}