/**
  * Book object - either of type or specific - for given dates
  */
 function confirmedBookingObject()
 {
     include_once 'Modules/BookingManager/classes/class.ilBookingObject.php';
     include_once 'Modules/BookingManager/classes/class.ilBookingReservation.php';
     $success = false;
     if ($this->object->getScheduleType() == ilObjBookingPool::TYPE_NO_SCHEDULE) {
         if ($_POST['object_id']) {
             $object_id = $_POST['object_id'];
             if ($object_id) {
                 if (ilBookingReservation::isObjectAvailableNoSchedule($object_id)) {
                     $this->processBooking($object_id);
                     $success = $object_id;
                 } else {
                     // #11852
                     ilUtil::sendFailure($this->lng->txt('book_reservation_failed_overbooked'), true);
                     $this->ctrl->redirect($this, 'render');
                 }
             }
         }
     } else {
         if (!isset($_POST['date'])) {
             ilUtil::sendFailure($this->lng->txt('select_one'));
             return $this->bookObject();
         }
         // single object reservation(s)
         if (isset($_GET['object_id'])) {
             $confirm = array();
             $object_id = (int) $_GET['object_id'];
             if ($object_id) {
                 $group_id = null;
                 $nr = ilBookingObject::getNrOfItemsForObjects(array($object_id));
                 if ($nr[$object_id] > 1 || sizeof($_POST['date']) > 1) {
                     $group_id = ilBookingReservation::getNewGroupId();
                 }
                 foreach ($_POST['date'] as $date) {
                     $fromto = explode('_', $date);
                     $fromto[1]--;
                     $counter = ilBookingReservation::getAvailableObject(array($object_id), $fromto[0], $fromto[1], false, true);
                     $counter = $counter[$object_id];
                     if ($counter) {
                         if ($counter > 1) {
                             $confirm[$object_id . "_" . $fromto[0] . "_" . ($fromto[1] + 1)] = $counter;
                         } else {
                             $this->processBooking($object_id, $fromto[0], $fromto[1], $group_id);
                             $success = $object_id;
                         }
                     }
                 }
             }
             if (sizeof($confirm)) {
                 return $this->confirmBookingNumbers($confirm, $group_id);
             }
         }
         /*
         // group object reservation(s)
         else
         {														
         	$all_object_ids = array();
         	foreach(ilBookingObject::getList((int)$_GET['type_id']) as $item)
         	{
         		$all_object_ids[] = $item['booking_object_id'];
         	}
         
         	$possible_objects = $counter = array();	
         	sort($_POST['date']);			
         	foreach($_POST['date'] as $date)
         	{
         		$fromto = explode('_', $date);
         		$fromto[1]--;
         		$possible_objects[$date] = ilBookingReservation::getAvailableObject($all_object_ids, $fromto[0], $fromto[1], false);		
         		foreach($possible_objects[$date] as $obj_id)
         		{
         			$counter[$obj_id]++;
         		}
         	}
         
         	if(max($counter))
         	{			
         		// we prefer the objects which are available for most slots
         		arsort($counter);
         		$counter = array_keys($counter);
         
         		// book each slot
         		foreach($possible_objects as $date => $available_ids)
         		{
         			$fromto = explode('_', $date);
         			$fromto[1]--;
         
         			// find "best" object for slot
         			foreach($counter as $best_object_id)
         			{
         				if(in_array($best_object_id, $available_ids))
         				{
         					$object_id = $best_object_id;
         					break;
         				}
         			}				
         			$this->processBooking($object_id, $fromto[0], $fromto[1]);
         			$success = true;	
         		}
         	}
         }			 
         */
     }
     if ($success) {
         $this->handleBookingSuccess($success);
     } else {
         ilUtil::sendFailure($this->lng->txt('book_reservation_failed'), true);
         $this->ctrl->redirect($this, 'book');
     }
 }
 /**
  * Check if any of given objects are bookable
  * @param	array	$a_ids
  * @param	int		$a_from
  * @param	int		$a_to
  * @param	int		$a_return_single
  * @return	int
  */
 static function getAvailableObject(array $a_ids, $a_from, $a_to, $a_return_single = true)
 {
     global $ilDB;
     $nr_map = ilBookingObject::getNrOfItemsForObjects($a_ids);
     $from = $ilDB->quote($a_from, 'integer');
     $to = $ilDB->quote($a_to, 'integer');
     $set = $ilDB->query('SELECT count(*) cnt, object_id' . ' FROM booking_reservation' . ' WHERE ' . $ilDB->in('object_id', $a_ids, '', 'integer') . ' AND (status IS NULL OR status <> ' . $ilDB->quote(self::STATUS_CANCELLED, 'integer') . ')' . ' AND ((date_from <= ' . $from . ' AND date_to >= ' . $from . ')' . ' OR (date_from <= ' . $to . ' AND date_to >= ' . $to . ')' . ' OR (date_from >= ' . $from . ' AND date_to <= ' . $to . '))' . ' GROUP BY object_id');
     $blocked = array();
     while ($row = $ilDB->fetchAssoc($set)) {
         if ($row['cnt'] >= $nr_map[$row['object_id']]) {
             $blocked[] = $row['object_id'];
         }
     }
     $available = array_diff($a_ids, $blocked);
     if (sizeof($available)) {
         if ($a_return_single) {
             return array_shift($available);
         } else {
             return $available;
         }
     }
 }
 static function isObjectAvailableNoSchedule($a_obj_id)
 {
     global $ilDB;
     $all = ilBookingObject::getNrOfItemsForObjects(array($a_obj_id));
     $all = (int) $all[$a_obj_id];
     $set = $ilDB->query('SELECT COUNT(*) cnt' . ' FROM booking_reservation r' . ' JOIN booking_object o ON (o.booking_object_id = r.object_id)' . ' WHERE (status IS NULL OR status <> ' . $ilDB->quote(self::STATUS_CANCELLED, 'integer') . ')' . ' AND r.object_id = ' . $ilDB->quote($a_obj_id, 'integer'));
     $cnt = $ilDB->fetchAssoc($set);
     $cnt = (int) $cnt['cnt'];
     return (bool) ($all - $cnt);
     // #11864
 }