/**
  * Create calendar appointments
  * @param ilBookingEntry $booking
  * @return 
  */
 protected function createAppointments(ilBookingEntry $booking)
 {
     include_once './Services/Calendar/classes/class.ilDateList.php';
     $concurrent_dates = new ilDateList(ilDateList::TYPE_DATETIME);
     $start = clone $this->form->getItemByPostVar('st')->getDate();
     for ($i = 0; $i < $this->form->getItemByPostVar('ap')->getValue(); $i++) {
         $concurrent_dates->add(clone $start);
         $start->increment(ilDateTime::MINUTE, $this->form->getItemByPostVar('du')->getMinutes());
         $start->increment(ilDateTime::HOUR, $this->form->getItemByPostVar('du')->getHours());
         #$start = new ilDateTime(,IL_CAL_UNIX);
     }
     include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
     $def_cat = ilCalendarUtil::initDefaultCalendarByType(ilCalendarCategory::TYPE_CH, $this->getUserId(), $this->lng->txt('cal_ch_personal_ch'), true);
     // Add calendar appointment for each
     include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
     include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
     include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
     include_once './Services/Booking/classes/class.ilBookingPeriod.php';
     foreach ($concurrent_dates as $dt) {
         $end = clone $dt;
         $end->increment(ilDateTime::MINUTE, $this->form->getItemByPostVar('du')->getMinutes());
         $end->increment(ilDateTime::HOUR, $this->form->getItemByPostVar('du')->getHours());
         $calc = new ilCalendarRecurrenceCalculator(new ilBookingPeriod($dt, $end), $this->form->getItemByPostVar('frequence')->getRecurrence());
         // Calculate with one year limit
         $limit = clone $dt;
         $limit->increment(ilDAteTime::YEAR, 1);
         $date_list = $calc->calculateDateList($dt, $limit);
         foreach ($date_list as $app_start) {
             $app_end = clone $app_start;
             $app_end->increment(ilDateTime::MINUTE, $this->form->getItemByPostVar('du')->getMinutes());
             $entry = new ilCalendarEntry();
             $entry->setContextId($booking->getId());
             $entry->setTitle($this->form->getInput('ti'));
             $entry->setSubtitle("#consultationhour#");
             // dynamic, see ilCalendarEntry
             $entry->setDescription($this->form->getInput('de'));
             $entry->setLocation($this->form->getInput('lo'));
             $entry->setStart($app_start);
             $entry->setEnd($app_end);
             $entry->setTranslationType(IL_CAL_TRANSLATION_SYSTEM);
             $entry->save();
             $cat_assign = new ilCalendarCategoryAssignments($entry->getEntryId());
             $cat_assign->addAssignment($def_cat->getCategoryID());
         }
     }
 }
 /**
  * Calculate booking appointments
  * Returns an array with booking appointments
  *
  * @param ilDate $start
  * @param ilDate $end
  * @param object $booking
  * @return array
  */
 public static function calculateBookingAppointments(ilDateTime $start, ilDateTime $end, $booking)
 {
     include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
     $rcalc = new ilCalendarRecurrenceCalculator(new ilViteroBookingDatePeriod($booking), new ilViteroBookingRecurrence($booking));
     return $rcalc->calculateDateList($start, $end);
 }
 protected function createVEVENT($app)
 {
     global $ilUser;
     $this->writer->addLine('BEGIN:VEVENT');
     // TODO only domain
     $this->writer->addLine('UID:' . ilICalWriter::escapeText($app->getEntryId() . '_' . CLIENT_ID . '@' . ILIAS_HTTP_PATH));
     $last_mod = $app->getLastUpdate()->get(IL_CAL_FKT_DATE, 'Ymd\\THis\\Z', ilTimeZone::UTC);
     #$last_mod = $app->getLastUpdate()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',$ilUser->getTimeZone());
     $this->writer->addLine('LAST-MODIFIED:' . $last_mod);
     // begin-patch aptar
     include_once './Services/Calendar/classes/class.ilCalendarRecurrences.php';
     if ($rec = ilCalendarRecurrences::_getFirstRecurrence($app->getEntryId())) {
         // Set starting time to first appointment that matches the recurrence rule
         include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
         $calc = new ilCalendarRecurrenceCalculator($app, $rec);
         $pStart = $app->getStart();
         $pEnd = clone $app->getStart();
         $pEnd->increment(IL_CAL_YEAR, 5);
         $appDiff = $app->getEnd()->get(IL_CAL_UNIX) - $app->getStart()->get(IL_CAL_UNIX);
         $recs = $calc->calculateDateList($pStart, $pEnd);
         foreach ($recs as $dt) {
             $startInit = $dt;
             $endInit = clone $dt;
             $endInit->setDate($startInit->get(IL_CAL_UNIX) + $appDiff, IL_CAL_UNIX);
             break;
         }
     } else {
         $startInit = $app->getStart();
         $endInit = $app->getEnd();
     }
     if ($app->isFullday()) {
         // According to RFC 5545 3.6.1 DTEND is not inklusive.
         // But ILIAS stores inklusive dates in the database.
         #$app->getEnd()->increment(IL_CAL_DAY,1);
         $endInit->increment(IL_CAL_DATE, 1);
         #$start = $app->getStart()->get(IL_CAL_FKT_DATE,'Ymd\Z',ilTimeZone::UTC);
         #$start = $app->getStart()->get(IL_CAL_FKT_DATE,'Ymd',$ilUser->getTimeZone());
         $start = $startInit->get(IL_CAL_FKT_DATE, 'Ymd', $ilUser->getTimeZone());
         #$end = $app->getEnd()->get(IL_CAL_FKT_DATE,'Ymd\Z',ilTimeZone::UTC);
         #$end = $app->getEnd()->get(IL_CAL_FKT_DATE,'Ymd',$ilUser->getTimeZone());
         $endInit->increment(IL_CAL_DAY, 1);
         $end = $endInit->get(IL_CAL_FKT_DATE, 'Ymd', $ilUser->getTimeZone());
         $this->writer->addLine('DTSTART;VALUE=DATE:' . $start);
         $this->writer->addLine('DTEND;VALUE=DATE:' . $end);
     } else {
         #$start = $app->getStart()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',ilTimeZone::UTC);
         #$start = $app->getStart()->get(IL_CAL_FKT_DATE,'Ymd\THis',$ilUser->getTimeZone());
         $start = $startInit->get(IL_CAL_FKT_DATE, 'Ymd\\THis', $ilUser->getTimeZone());
         #$end = $app->getEnd()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',ilTimeZone::UTC);
         #$end = $app->getEnd()->get(IL_CAL_FKT_DATE,'Ymd\THis',$ilUser->getTimeZone());
         $end = $endInit->get(IL_CAL_FKT_DATE, 'Ymd\\THis', $ilUser->getTimeZone());
         $this->writer->addLine('DTSTART;TZID=' . $ilUser->getTimezone() . ':' . $start);
         $this->writer->addLine('DTEND;TZID=' . $ilUser->getTimezone() . ':' . $end);
     }
     // end-patch aptar
     $this->createRecurrences($app);
     $this->writer->addLine('SUMMARY:' . ilICalWriter::escapeText($app->getPresentationTitle()));
     if (strlen($app->getDescription())) {
         $this->writer->addLine('DESCRIPTION:' . ilICalWriter::escapeText($app->getDescription()));
     }
     if (strlen($app->getLocation())) {
         $this->writer->addLine('LOCATION:' . ilICalWriter::escapeText($app->getLocation()));
     }
     // TODO: URL
     $this->buildAppointmentUrl($app);
     $this->writer->addLine('END:VEVENT');
 }
 /**
  * edit appointment
  *
  * @access protected
  * @param
  * @return
  */
 protected function edit($a_edit_single_app = false)
 {
     global $tpl, $ilUser, $ilErr, $ilHelp;
     $ilHelp->setScreenIdComponent("cal");
     $ilHelp->setScreenId("app");
     if ($this->app->isMilestone()) {
         $ilHelp->setSubScreenId("edit_milestone");
     } else {
         $ilHelp->setSubScreenId("edit");
     }
     include_once './Services/Calendar/classes/class.ilCalendarCategory.php';
     include_once './Services/Calendar/classes/class.ilCalendarCategories.php';
     include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
     $GLOBALS['ilCtrl']->saveParameter($this, array('seed', 'app_id', 'dt', 'idate'));
     if ($_REQUEST['rexl']) {
         $GLOBALS['ilCtrl']->setParameter($this, 'rexl', 1);
         // Calculate new appointment time
         $duration = $this->getAppointment()->getEnd()->get(IL_CAL_UNIX) - $this->getAppointment()->getStart()->get(IL_CAL_UNIX);
         include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
         $calc = new ilCalendarRecurrenceCalculator($this->getAppointment(), $this->rec);
         $current_date = new ilDateTime($_REQUEST['dt'], IL_CAL_UNIX);
         $yesterday = clone $current_date;
         $yesterday->increment(IL_CAL_DAY, -1);
         $tomorrow = clone $current_date;
         $tomorrow->increment(IL_CAL_DAY, 1);
         foreach ($calc->calculateDateList($current_date, $tomorrow, 1) as $date_entry) {
             if (ilDateTime::_equals($current_date, $date_entry, IL_CAL_DAY)) {
                 $this->getAppointment()->setStart(new ilDateTime($date_entry->get(IL_CAL_UNIX), IL_CAL_UNIX));
                 $this->getAppointment()->setEnd(new ilDateTime($date_entry->get(IL_CAL_UNIX) + $duration, IL_CAL_UNIX));
                 break;
             }
         }
         // Finally reset recurrence
         $this->rec = new ilCalendarRecurrence();
     }
     $cat_id = ilCalendarCategoryAssignments::_lookupCategory($this->app->getEntryId());
     $cats = ilCalendarCategories::_getInstance($ilUser->getId());
     if (!$cats->isVisible($cat_id)) {
         $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->WARNING);
         return false;
     }
     if (!$cats->isEditable($cat_id) or $this->app->isAutoGenerated()) {
         $this->showInfoScreen();
         return true;
     }
     $this->initForm('edit', $this->app->isMilestone(), $a_edit_single_app);
     $tpl->setContent($this->form->getHTML());
 }
 /**
  * create recurring sessions
  *
  * @access protected
  * @param
  * @return
  */
 protected function createRecurringSessions()
 {
     global $tree;
     if (!$this->rec->getFrequenceType()) {
         return true;
     }
     include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
     $calc = new ilCalendarRecurrenceCalculator($this->object->getFirstAppointment(), $this->rec);
     $period_start = clone $this->object->getFirstAppointment()->getStart();
     $period_end = clone $this->object->getFirstAppointment()->getStart();
     $period_end->increment(IL_CAL_YEAR, 5);
     $date_list = $calc->calculateDateList($period_start, $period_end);
     $period_diff = $this->object->getFirstAppointment()->getEnd()->get(IL_CAL_UNIX) - $this->object->getFirstAppointment()->getStart()->get(IL_CAL_UNIX);
     $parent_id = $tree->getParentId($this->object->getRefId());
     include_once './Modules/Session/classes/class.ilEventItems.php';
     $evi = new ilEventItems($this->object->getId());
     $eitems = $evi->getItems();
     $counter = 0;
     foreach ($date_list->get() as $date) {
         if (!$counter++) {
             continue;
         }
         $new_obj = $this->object->cloneObject($parent_id);
         $new_obj->read();
         $new_obj->getFirstAppointment()->setStartingTime($date->get(IL_CAL_UNIX));
         $new_obj->getFirstAppointment()->setEndingTime($date->get(IL_CAL_UNIX) + $period_diff);
         $new_obj->getFirstAppointment()->update();
         $new_obj->update();
         $new_evi = new ilEventItems($new_obj->getId());
         $new_evi->setItems($eitems);
         $new_evi->update();
     }
 }
 /**
  * calculate 
  *
  * @access protected
  */
 public function calculate()
 {
     global $ilDB;
     $events = $this->getEvents();
     // we need category type for booking handling
     $ids = array();
     foreach ($events as $event) {
         $ids[] = $event->getEntryId();
     }
     include_once 'Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
     $cat_map = ilCalendarCategoryAssignments::_getAppointmentCalendars($ids);
     include_once 'Services/Calendar/classes/class.ilCalendarCategory.php';
     $cat_types = array();
     foreach (array_unique($cat_map) as $cat_id) {
         $cat = new ilCalendarCategory($cat_id);
         $cat_types[$cat_id] = $cat->getType();
     }
     $counter = 0;
     foreach ($events as $event) {
         // Calculdate recurring events
         include_once 'Services/Calendar/classes/class.ilCalendarRecurrences.php';
         if ($recs = ilCalendarRecurrences::_getRecurrences($event->getEntryId())) {
             $duration = $event->getEnd()->get(IL_CAL_UNIX) - $event->getStart()->get(IL_CAL_UNIX);
             foreach ($recs as $rec) {
                 $calc = new ilCalendarRecurrenceCalculator($event, $rec);
                 foreach ($calc->calculateDateList($this->start, $this->end)->get() as $rec_date) {
                     $this->schedule[$counter]['event'] = $event;
                     $this->schedule[$counter]['dstart'] = $rec_date->get(IL_CAL_UNIX);
                     $this->schedule[$counter]['dend'] = $this->schedule[$counter]['dstart'] + $duration;
                     $this->schedule[$counter]['fullday'] = $event->isFullday();
                     $this->schedule[$counter]['category_id'] = $cat_map[$event->getEntryId()];
                     $this->schedule[$counter]['category_type'] = $cat_types[$cat_map[$event->getEntryId()]];
                     switch ($this->type) {
                         case self::TYPE_DAY:
                         case self::TYPE_WEEK:
                             // store date info (used for calculation of overlapping events)
                             $tmp_date = new ilDateTime($this->schedule[$counter]['dstart'], IL_CAL_UNIX, $this->timezone);
                             $this->schedule[$counter]['start_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE, '', $this->timezone);
                             $tmp_date = new ilDateTime($this->schedule[$counter]['dend'], IL_CAL_UNIX, $this->timezone);
                             $this->schedule[$counter]['end_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE, '', $this->timezone);
                             break;
                         default:
                             break;
                     }
                     $counter++;
                     if ($this->areEventsLimited() && $counter >= $this->getEventsLimit()) {
                         break;
                     }
                 }
             }
         } else {
             $this->schedule[$counter]['event'] = $event;
             $this->schedule[$counter]['dstart'] = $event->getStart()->get(IL_CAL_UNIX);
             $this->schedule[$counter]['dend'] = $event->getEnd()->get(IL_CAL_UNIX);
             $this->schedule[$counter]['fullday'] = $event->isFullday();
             $this->schedule[$counter]['category_id'] = $cat_map[$event->getEntryId()];
             $this->schedule[$counter]['category_type'] = $cat_types[$cat_map[$event->getEntryId()]];
             if (!$event->isFullday()) {
                 switch ($this->type) {
                     case self::TYPE_DAY:
                     case self::TYPE_WEEK:
                         // store date info (used for calculation of overlapping events)
                         $tmp_date = new ilDateTime($this->schedule[$counter]['dstart'], IL_CAL_UNIX, $this->timezone);
                         $this->schedule[$counter]['start_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE, '', $this->timezone);
                         $tmp_date = new ilDateTime($this->schedule[$counter]['dend'], IL_CAL_UNIX, $this->timezone);
                         $this->schedule[$counter]['end_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE, '', $this->timezone);
                         break;
                     default:
                         break;
                 }
             }
             $counter++;
             if ($this->areEventsLimited() && $counter >= $this->getEventsLimit()) {
                 break;
             }
         }
     }
 }