/**
  * Cancel a booking
  * @param type $a_usr_id
  * @param type $a_app_id
  * @return bool
  */
 public static function cancelBooking($a_usr_id, $a_app_id, $a_send_notification = true)
 {
     // Delete personal copy of appointment
     include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
     $app = new ilCalendarEntry($a_app_id);
     include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourAppointments.php';
     $user_apps = ilConsultationHourAppointments::getAppointmentIds($a_usr_id, $app->getContextId(), $app->getStart(), ilCalendarCategory::TYPE_CH, false);
     foreach ($user_apps as $uapp_id) {
         $uapp = new ilCalendarEntry($uapp_id);
         $uapp->delete();
         include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
         ilCalendarCategoryAssignments::_deleteByAppointmentId($uapp_id);
         break;
     }
     // Delete booking entries
     // Send notification
     $booking = new ilBookingEntry($app->getContextId());
     if ($a_send_notification) {
         $booking->cancelBooking($a_app_id, $a_usr_id);
     } else {
         $booking->deleteBooking($a_app_id, $a_usr_id);
     }
     return true;
 }
 public function isValidEvent(ilCalendarEntry $a_event)
 {
     global $ilUser;
     $booking = new ilBookingEntry($a_event->getContextId());
     // portfolio embedded: filter by consultation hour groups?
     if (!is_array($this->group_ids) || in_array($booking->getBookingGroup(), $this->group_ids)) {
         // do not filter against course/group in portfolio
         if ($this->cats->getMode() == ilCalendarCategories::MODE_PORTFOLIO_CONSULTATION) {
             $booking->setTargetObjIds(null);
         }
         if (($this->user_id == $ilUser->getId() || !$booking->isBookedOut($a_event->getEntryId(), true)) && $booking->isTargetObjectVisible($this->cats->getTargetRefId())) {
             return true;
         }
     }
     return false;
 }
 /**
  * Edit multiple sequence items
  */
 public function edit()
 {
     global $ilTabs;
     if (!isset($_REQUEST['apps'])) {
         ilUtil::sendFailure($this->lng->txt('select_one'));
         return $this->appointmentList();
     }
     $this->initFormSequence(self::MODE_MULTI);
     if ($_REQUEST['apps'] && !is_array($_REQUEST['apps'])) {
         $_REQUEST['apps'] = explode(';', $_REQUEST['apps']);
     }
     $hidden = new ilHiddenInputGUI('apps');
     $hidden->setValue(implode(';', $_REQUEST['apps']));
     $this->form->addItem($hidden);
     include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
     $first = $_REQUEST['apps'];
     $first = array_shift($_REQUEST['apps']);
     $entry = new ilCalendarEntry($first);
     $this->form->getItemByPostVar('ti')->setValue($entry->getTitle());
     $this->form->getItemByPostVar('lo')->setValue($entry->getLocation());
     $this->form->getItemByPostVar('de')->setValue($entry->getDescription());
     include_once 'Services/Booking/classes/class.ilBookingEntry.php';
     $booking = new ilBookingEntry($entry->getContextId());
     $this->form->getItemByPostVar('bo')->setValue($booking->getNumberOfBookings());
     $ref_ids = array();
     foreach ($booking->getTargetObjIds() as $obj_id) {
         $refs = ilObject::_getAllReferences($obj_id);
         $ref_ids[] = end($refs);
     }
     $this->form->getItemByPostVar('tgt')->setValue(implode(',', $ref_ids));
     $deadline = $booking->getDeadlineHours();
     $this->form->getItemByPostVar('dead')->setDays(floor($deadline / 24));
     $this->form->getItemByPostVar('dead')->setHours($deadline % 24);
     if ($booking->getBookingGroup()) {
         $this->form->getItemByPostVar('grp')->setValue($booking->getBookingGroup());
     }
     $this->tpl->setContent($this->form->getHTML());
 }
 /**
  * Book consultation appointment, was confirmed
  */
 public function bookconfirmed()
 {
     global $ilUser;
     $entry = (int) $_REQUEST['app_id'];
     $user = (int) $_REQUEST['bkid'];
     $form = $this->initFormConfirmBooking();
     if ($form->checkInput()) {
         // check if appointment is bookable
         include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
         $cal_entry = new ilCalendarEntry($entry);
         include_once './Services/Booking/classes/class.ilBookingEntry.php';
         $booking = new ilBookingEntry($cal_entry->getContextId());
         if (!$booking->isAppointmentBookableForUser($entry, $GLOBALS['ilUser']->getId())) {
             ilUtil::sendFailure($this->lng->txt('cal_booking_failed_info'), true);
             $this->ctrl->returnToParent($this);
         }
         include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourUtils.php';
         ilConsultationHourUtils::bookAppointment($ilUser->getId(), $entry);
         include_once './Services/Booking/classes/class.ilBookingEntry.php';
         ilBookingEntry::writeBookingMessage($entry, $ilUser->getId(), $form->getInput('comment'));
     }
     ilUtil::sendSuccess($this->lng->txt('cal_booking_confirmed'), true);
     $this->ctrl->returnToParent($this);
 }
 /**
  * 
  * @return 
  */
 public function send()
 {
     global $rbacreview, $lng;
     switch ($this->getType()) {
         case self::TYPE_USER:
             $rcp = array_pop($this->getRecipients());
             $this->initLanguage($rcp);
             $this->getLanguage()->loadLanguageModule('dateplaner');
             $this->initMail();
             $this->setSubject(sprintf($this->getLanguageText('cal_mail_notification_subject'), $this->getAppointment()->getTitle()));
             $this->setBody(ilMail::getSalutation($rcp, $this->getLanguage()));
             $this->appendBody("\n\n");
             $this->appendBody($this->getLanguageText('cal_mail_notification_body'));
             $this->appendBody("\n\n");
             $this->appendAppointmentDetails();
             $this->appendBody("\n\n");
             $this->getMail()->appendInstallationSignature(true);
             $this->addAttachment();
             $this->sendMail($this->getRecipients(), array('system'), true);
             break;
         case self::TYPE_USER_ANONYMOUS:
             $rcp = array_pop($this->getRecipients());
             $this->setLanguage(ilLanguageFactory::_getLanguage($lng->getDefaultLanguage()));
             $this->getLanguage()->loadLanguageModule('dateplaner');
             $this->getLanguage()->loadLanguageModule('mail');
             $this->initMail();
             $this->setSubject(sprintf($this->getLanguageText('cal_mail_notification_subject'), $this->getAppointment()->getTitle()));
             $this->setBody(ilMail::getSalutation(0, $this->getLanguage()));
             $this->appendBody("\n\n");
             $this->appendBody($this->getLanguageText('cal_mail_notification_body'));
             $this->appendBody("\n\n");
             $this->appendAppointmentDetails();
             $this->appendBody("\n\n");
             $this->getMail()->appendInstallationSignature(true);
             $this->addAttachment();
             $this->sendMail($this->getRecipients(), array('email'), false);
             break;
         case self::TYPE_GRP_NEW_NOTIFICATION:
             $this->setLanguage(ilLanguageFactory::_getLanguage($lng->getDefaultLanguage()));
             $this->getLanguage()->loadLanguageModule('grp');
             $this->getLanguage()->loadLanguageModule('dateplaner');
             $this->initMail();
             $this->setSubject(sprintf($this->getLanguageText('cal_grp_new_notification_sub'), $this->getObjectTitle(true)));
             $this->setBody($this->getLanguageText('grp_notification_salutation'));
             $this->appendBody("\n\n");
             $this->appendBody(sprintf($this->getLanguageText('cal_grp_new_notification_body'), $this->getObjectTitle(true)));
             $this->appendBody("\n\n");
             $this->appendBody($this->getLanguageText('grp_mail_permanent_link'));
             $this->appendBody("\n\n");
             $this->appendAppointmentDetails();
             $this->appendBody("\n\n");
             $this->appendBody($this->createPermanentLink());
             $this->getMail()->appendInstallationSignature(true);
             $this->addAttachment();
             $this->sendMail(array('#il_grp_admin_' . $this->getRefId(), '#il_grp_member_' . $this->getRefId()), array('system'), false);
             break;
         case self::TYPE_GRP_NOTIFICATION:
             $this->setLanguage(ilLanguageFactory::_getLanguage($lng->getDefaultLanguage()));
             $this->getLanguage()->loadLanguageModule('grp');
             $this->getLanguage()->loadLanguageModule('dateplaner');
             $this->initMail();
             $this->setSubject(sprintf($this->getLanguageText('cal_grp_notification_sub'), $this->getObjectTitle(true)));
             $this->setBody($this->getLanguageText('grp_notification_salutation'));
             $this->appendBody("\n\n");
             $this->appendBody(sprintf($this->getLanguageText('cal_grp_notification_body'), $this->getObjectTitle(true)));
             $this->appendBody("\n\n");
             $this->appendAppointmentDetails();
             $this->appendBody("\n\n");
             $this->appendBody($this->getLanguageText('grp_mail_permanent_link'));
             $this->appendBody("\n\n");
             $this->appendBody($this->createPermanentLink());
             $this->getMail()->appendInstallationSignature(true);
             $this->addAttachment();
             $this->sendMail(array('#il_grp_admin_' . $this->getRefId(), '#il_grp_member_' . $this->getRefId()), array('system'), false);
             break;
         case self::TYPE_CRS_NEW_NOTIFICATION:
             $this->setLanguage(ilLanguageFactory::_getLanguage($lng->getDefaultLanguage()));
             $this->getLanguage()->loadLanguageModule('crs');
             $this->getLanguage()->loadLanguageModule('dateplaner');
             $this->initMail();
             $this->setSubject(sprintf($this->getLanguageText('cal_crs_new_notification_sub'), $this->getObjectTitle(true)));
             $this->setBody($this->getLanguageText('crs_notification_salutation'));
             $this->appendBody("\n\n");
             $this->appendBody(sprintf($this->getLanguageText('cal_crs_new_notification_body'), $this->getObjectTitle(true)));
             $this->appendBody("\n\n");
             $this->appendBody($this->getLanguageText('crs_mail_permanent_link'));
             $this->appendBody("\n\n");
             $this->appendAppointmentDetails();
             $this->appendBody("\n\n");
             $this->appendBody($this->createPermanentLink());
             $this->getMail()->appendInstallationSignature(true);
             $this->addAttachment();
             $this->sendMail(array('#il_crs_admin_' . $this->getRefId(), '#il_crs_tutor_' . $this->getRefId(), '#il_crs_member_' . $this->getRefId()), array('system'), false);
             break;
         case self::TYPE_CRS_NOTIFICATION:
             $this->setLanguage(ilLanguageFactory::_getLanguage($lng->getDefaultLanguage()));
             $this->getLanguage()->loadLanguageModule('crs');
             $this->getLanguage()->loadLanguageModule('dateplaner');
             $this->initMail();
             $this->setSubject(sprintf($this->getLanguageText('cal_crs_notification_sub'), $this->getObjectTitle(true)));
             $this->setBody($this->getLanguageText('crs_notification_salutation'));
             $this->appendBody("\n\n");
             $this->appendBody(sprintf($this->getLanguageText('cal_crs_notification_body'), $this->getObjectTitle(true)));
             $this->appendBody("\n\n");
             $this->appendAppointmentDetails();
             $this->appendBody("\n\n");
             $this->appendBody($this->getLanguageText('crs_mail_permanent_link'));
             $this->appendBody("\n\n");
             $this->appendBody($this->createPermanentLink());
             $this->getMail()->appendInstallationSignature(true);
             $this->addAttachment();
             $this->sendMail(array('#il_crs_admin_' . $this->getRefId(), '#il_crs_tutor_' . $this->getRefId(), '#il_crs_member_' . $this->getRefId()), array('system'), false);
             break;
         case self::TYPE_BOOKING_CONFIRMATION:
             $user_id = array_pop($this->getRecipients());
             include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
             include_once 'Services/Booking/classes/class.ilBookingEntry.php';
             $entry = new ilCalendarEntry($this->getAppointmentId());
             $booking = new ilBookingEntry($entry->getContextId());
             $this->initLanguage($user_id);
             $this->getLanguage()->loadLanguageModule('dateplaner');
             $this->initMail();
             $this->setSubject(sprintf($this->getLanguageText('cal_booking_confirmation_subject'), $entry->getTitle()));
             $this->setBody(ilMail::getSalutation($user_id, $this->getLanguage()));
             $this->appendBody("\n\n");
             $this->appendBody(sprintf($this->getLanguageText('cal_booking_confirmation_body'), ilObjUser::_lookupFullname($booking->getObjId())));
             $this->appendBody("\n\n");
             $this->appendAppointmentDetails($booking);
             /*
             				$this->appendBody("\n\n");
             				$this->appendBody($this->getLanguageText('cal_booking_confirmation_link'));
             				$this->appendBody("\n\n");
             				$this->appendBody($this->createPermanentLink());
             */
             $this->getMail()->appendInstallationSignature(true);
             $this->sendMail(array($user_id), array('system'), true);
             $this->appendBody("\n\n");
             $this->appendBody($this->getLanguageText('cal_booking_confirmation_user') . "\n");
             $this->appendBody(ilObjUser::_lookupFullname($user_id));
             $this->sendMail(array($booking->getObjId()), array('system'), true);
             break;
         case self::TYPE_BOOKING_CANCELLATION:
             $user_id = array_pop($this->getRecipients());
             include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
             include_once 'Services/Booking/classes/class.ilBookingEntry.php';
             $entry = new ilCalendarEntry($this->getAppointmentId());
             $booking = new ilBookingEntry($entry->getContextId());
             $user_id = array_pop($this->getRecipients());
             $this->initLanguage($user_id);
             $this->getLanguage()->loadLanguageModule('dateplaner');
             $this->initMail();
             $this->setSubject(sprintf($this->getLanguageText('cal_booking_cancellation_subject'), $entry->getTitle()));
             $this->setBody(ilMail::getSalutation($user_id, $this->getLanguage()));
             $this->appendBody("\n\n");
             $this->appendBody(sprintf($this->getLanguageText('cal_booking_cancellation_body'), ilObjUser::_lookupFullname($booking->getObjId())));
             $this->appendBody("\n\n");
             $this->appendAppointmentDetails($booking);
             $this->getMail()->appendInstallationSignature(true);
             $this->sendMail(array($user_id), array('system'), true);
             $this->appendBody("\n\n");
             $this->appendBody($this->getLanguageText('cal_booking_cancellation_user') . "\n");
             $this->appendBody(ilObjUser::_lookupFullname($user_id));
             $this->sendMail(array($booking->getObjId()), array('system'), true);
             break;
     }
     $this->deleteAttachments();
 }
 /**
  * Get instance by calendar entry
  * @param	int		$id
  * @return self 
  */
 public static function getInstanceByCalendarEntryId($a_id)
 {
     include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
     $cal_entry = new ilCalendarEntry($a_id);
     $booking_id = $cal_entry->getContextId();
     if ($booking_id) {
         return new self($booking_id);
     }
 }
 /**
  * Read events (will be moved to another class, since only active and/or visible calendars are shown)
  *
  * @access protected
  */
 public function getEvents()
 {
     global $ilDB;
     include_once './Services/Calendar/classes/class.ilCalendarCategories.php';
     $cats = ilCalendarCategories::_getInstance($this->user->getId())->getCategories($this->enabledSubitemCalendars());
     if (!$this->filter_bookings) {
         $cats = $this->hidden_cat->filterHidden($cats, ilCalendarCategories::_getInstance($this->user->getId())->getCategoriesInfo());
     }
     if (!count($cats)) {
         return array();
     }
     // TODO: optimize
     $query = "SELECT ce.cal_id cal_id FROM cal_entries ce LEFT JOIN cal_recurrence_rules crr ON ce.cal_id = crr.cal_id " . "JOIN cal_cat_assignments ca ON ca.cal_id = ce.cal_id ";
     if ($this->type != self::TYPE_INBOX) {
         $query .= "WHERE ((starta <= " . $this->db->quote($this->end->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . " " . "AND enda >= " . $this->db->quote($this->start->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ") " . "OR (starta <= " . $this->db->quote($this->end->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . " " . "AND NOT rule_id IS NULL)) ";
     } else {
         $date = new ilDateTime(mktime(0, 0, 0), IL_CAL_UNIX);
         $query .= "WHERE starta >= " . $this->db->quote($date->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp');
     }
     $query .= "AND " . $ilDB->in('ca.cat_id', $cats, false, 'integer') . " " . "ORDER BY starta";
     $res = $this->db->query($query);
     $events = array();
     include_once 'Services/Booking/classes/class.ilBookingEntry.php';
     while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
         if (!$this->hidden_cat->isAppointmentVisible($row->cal_id) || $this->filter_bookings) {
             $event = new ilCalendarEntry($row->cal_id);
             if (!$this->filter_bookings) {
                 $events[] = $event;
             } else {
                 $booking = new ilBookingEntry($event->getContextId());
                 if (!$booking->isBookedOut($row->cal_id, true)) {
                     $events[] = $event;
                 }
             }
         }
     }
     return $events;
 }
 /**
  * set appointments
  *
  * @access public
  * @return
  */
 public function setAppointments($a_apps)
 {
     include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
     include_once './Services/Calendar/classes/class.ilCalendarRecurrences.php';
     include_once './Services/Calendar/classes/class.ilCalendarCategory.php';
     $cat = new ilCalendarCategory($this->cat_id);
     foreach ($a_apps as $cal_entry_id) {
         $entry = new ilCalendarEntry($cal_entry_id);
         $rec = ilCalendarRecurrences::_getFirstRecurrence($entry->getEntryId());
         // booking
         if ($cat->getType() == ilCalendarCategory::TYPE_CH) {
             include_once 'Services/Booking/classes/class.ilBookingEntry.php';
             $book = new ilBookingEntry($entry->getContextId());
             if ($book) {
                 $title = $entry->getTitle();
                 if ($book->isOwner()) {
                     $max = (int) $book->getNumberOfBookings();
                     $current = (int) $book->getCurrentNumberOfBookings($entry->getEntryId());
                     if ($max > 1) {
                         $title .= ' (' . $current . '/' . $max . ')';
                     } else {
                         if ($current == $max) {
                             $title .= ' (' . $this->lng->txt('cal_booked_out') . ')';
                         } else {
                             $title .= ' (' . $this->lng->txt('cal_book_free') . ')';
                         }
                     }
                 } else {
                     if ($book->hasBooked($entry->getEntryId())) {
                         $title .= ' (' . $this->lng->txt('cal_date_booked') . ')';
                     }
                 }
             }
         } else {
             $title = $entry->getPresentationTitle();
         }
         $tmp_arr['id'] = $entry->getEntryId();
         $tmp_arr['title'] = $title;
         $tmp_arr['description'] = $entry->getDescription();
         $tmp_arr['fullday'] = $entry->isFullday();
         $tmp_arr['begin'] = $entry->getStart()->get(IL_CAL_UNIX);
         $tmp_arr['end'] = $entry->getEnd()->get(IL_CAL_UNIX);
         $tmp_arr['dt_sort'] = $entry->getStart()->get(IL_CAL_UNIX);
         $tmp_arr['dt'] = ilDatePresentation::formatPeriod($entry->getStart(), $entry->getEnd());
         #$tmp_arr['duration'] = ($dur = $tmp_arr['end'] - $tmp_arr['begin']) ? $dur : 60 * 60 * 24;
         $tmp_arr['duration'] = $tmp_arr['end'] - $tmp_arr['begin'];
         if ($tmp_arr['fullday']) {
             $tmp_arr['duration'] += 60 * 60 * 24;
         }
         if (!$tmp_arr['fullday'] and $tmp_arr['end'] == $tmp_arr['begin']) {
             $tmp_arr['duration'] = '';
         }
         $tmp_arr['frequence'] = $rec->getFrequenceType();
         $tmp_arr['deletable'] = (!$entry->isAutoGenerated() and $this->is_editable);
         $appointments[] = $tmp_arr;
     }
     $this->setData($appointments ? $appointments : array());
 }
 /**
  * Delete automatic generated appointments
  * 
  * @access public
  * @param int obj_id
  * @param array context ids
  * @static
  */
 public static function deleteAppointments($a_obj_id, array $a_context_ids = null)
 {
     include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
     include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
     foreach (ilCalendarCategoryAssignments::_getAutoGeneratedAppointmentsByObjId($a_obj_id) as $app_id) {
         // delete only selected entries
         if (is_array($a_context_ids)) {
             $entry = new ilCalendarEntry($app_id);
             if (!in_array($entry->getContextId(), $a_context_ids)) {
                 continue;
             }
         }
         ilCalendarCategoryAssignments::_deleteByAppointmentId($app_id);
         ilCalendarEntry::_delete($app_id);
     }
 }
 /**
  * Book consultation appointment, was confirmed
  */
 public function bookconfirmed()
 {
     global $ilUser;
     $entry = (int) $_POST['app_id'];
     $user = (int) $_GET['bkid'];
     include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
     include_once 'Services/Booking/classes/class.ilBookingEntry.php';
     $entry = new ilCalendarEntry($entry);
     $booking = new ilBookingEntry($entry->getContextId());
     $booking->book($entry->getEntryId());
     // create user calendar/appointment
     include_once './Services/Calendar/classes/class.ilCalendarCategory.php';
     include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
     include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
     $user_entry = clone $entry;
     $user_entry->save();
     $def_cat = ilCalendarUtil::initDefaultCalendarByType(ilCalendarCategory::TYPE_CH, $ilUser->getId(), $this->lng->txt('cal_ch_personal_ch'), true);
     $assign = new ilCalendarCategoryAssignments($user_entry->getEntryId());
     $assign->addAssignment($def_cat->getCategoryID());
     ilUtil::sendSuccess($this->lng->txt('cal_booking_confirmed'), true);
     $this->ctrl->returnToParent($this);
 }