ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilCalendarExport.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once './Services/Calendar/classes/class.ilCalendarUserSettings.php';
25 include_once './Services/Calendar/classes/iCal/class.ilICalWriter.php';
26 include_once './Services/Calendar/classes/class.ilCalendarCategory.php';
27 include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
28 include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
29 
38 {
39  const EXPORT_CALENDARS = 1;
41 
42  protected $export_type = self::EXPORT_CALENDARS;
43 
47  private $logger = null;
48 
49 
50  protected $calendars = array();
51  protected $user_settings = null;
52  protected $appointments = array();
53  protected $writer = null;
54 
55  public function __construct($a_calendar_ids = array())
56  {
57  $this->logger = $GLOBALS['DIC']->logger()->cal();
58 
59 
60  $this->calendars = $a_calendar_ids;
61  $this->writer = new ilICalWriter();
62 
63  $this->user_settings = ilCalendarUserSettings::_getInstanceByUserId($GLOBALS['ilUser']->getId());
64  }
65 
70  public function getUserSettings()
71  {
72  return $this->user_settings;
73  }
74 
75 
76  public function setExportType($a_type)
77  {
78  $this->export_type = $a_type;
79  }
80 
81  public function setAppointments($a_apps)
82  {
83  $this->appointments = $a_apps;
84  }
85 
86  public function getAppointments()
87  {
88  return $this->appointments;
89  }
90 
91  public function setCalendarIds($a_cal_ids)
92  {
93  $this->calendars = $a_cal_ids;
94  }
95 
96  public function getCalendarIds()
97  {
98  return (array) $this->calendars;
99  }
100 
101  public function getExportType()
102  {
103  return $this->export_type;
104  }
105 
106  public function export()
107  {
108  $this->writer->addLine('BEGIN:VCALENDAR');
109  $this->writer->addLine('VERSION:2.0');
110  $this->writer->addLine('METHOD:PUBLISH');
111  $this->writer->addLine('PRODID:-//ilias.de/NONSGML ILIAS Calendar V4.4//EN');
112 
113  $this->addTimezone();
114 
115  switch ($this->getExportType()) {
116  case self::EXPORT_CALENDARS:
117  $this->addCategories();
118  break;
119 
120  case self::EXPORT_APPOINTMENTS:
121  $this->addAppointments();
122  break;
123  }
124  $this->writer->addLine('END:VCALENDAR');
125  }
126 
127  protected function addTimezone()
128  {
129  if ($this->getUserSettings()->getExportTimeZoneType() == ilCalendarUserSettings::CAL_EXPORT_TZ_UTC) {
130  return;
131  }
132 
133  $this->writer->addLine('X-WR-TIMEZONE:' . $GLOBALS['ilUser']->getTimeZone());
134 
135  include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
136  $tzid_file = ilCalendarUtil::getZoneInfoFile($GLOBALS['ilUser']->getTimeZone());
137  if (!is_file($tzid_file)) {
138  $tzid_file = ilCalendarUtil::getZoneInfoFile('Europe/Berlin');
139  }
140  $reader = fopen($tzid_file, 'r');
141  while ($line = fgets($reader)) {
142  $line = str_replace("\n", '', $line);
143  $this->writer->addLine($line);
144  }
145  }
146 
147  protected function addCategories()
148  {
149  foreach ($this->calendars as $category_id) {
150  foreach (ilCalendarCategoryAssignments::_getAssignedAppointments(array($category_id)) as $app_id) {
151  $this->addAppointment($app_id);
152  }
153  }
154  }
155 
156  protected function addAppointments()
157  {
158  foreach ($this->getAppointments() as $app) {
159  $this->addAppointment($app);
160  }
161  }
162 
163  protected function addAppointment($a_app_id)
164  {
165  $app = new ilCalendarEntry($a_app_id);
166  if ($app->isMilestone()) {
167  $this->createVTODO($app);
168  } else {
169  $this->createVEVENT($app);
170  }
171  }
172 
173  protected function createVTODO($app)
174  {
175  // TODO
176  return true;
177  }
178 
184  protected function createVEVENT($app)
185  {
186  global $ilUser;
187 
188  if (!$app->getStart() instanceof ilDateTime) {
189  $this->logger->notice('Cannot create appointment for app_id: ' . $app->getEntryId());
190  }
191 
192  $this->writer->addLine('BEGIN:VEVENT');
193 
194  $now = new ilDateTime(time(), IL_CAL_UNIX);
195  $this->writer->addLine('DTSTAMP:' . $now->get(IL_CAL_FKT_DATE, 'Ymd\THis\Z', ilTimeZone::UTC));
196 
197  $this->writer->addLine('UID:' . ilICalWriter::escapeText(
198  $app->getEntryId() . '_' . CLIENT_ID . '@' . ILIAS_HTTP_PATH
199  ));
200 
201 
202  $last_mod = $app->getLastUpdate()->get(IL_CAL_FKT_DATE, 'Ymd\THis\Z', ilTimeZone::UTC);
203  #$last_mod = $app->getLastUpdate()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',$ilUser->getTimeZone());
204  $this->writer->addLine('LAST-MODIFIED:' . $last_mod);
205 
206  // begin-patch aptar
207  include_once './Services/Calendar/classes/class.ilCalendarRecurrences.php';
208  if ($rec = ilCalendarRecurrences::_getFirstRecurrence($app->getEntryId())) {
209  // Set starting time to first appointment that matches the recurrence rule
210  include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
211  $calc = new ilCalendarRecurrenceCalculator($app, $rec);
212 
213  $pStart = $app->getStart();
214  $pEnd = clone $app->getStart();
215  $pEnd->increment(IL_CAL_YEAR, 5);
216  $appDiff = $app->getEnd()->get(IL_CAL_UNIX) - $app->getStart()->get(IL_CAL_UNIX);
217  $recs = $calc->calculateDateList($pStart, $pEnd);
218 
219  // defaults
220  $startInit = $app->getStart();
221  $endInit = $app->getEnd();
222  foreach ($recs as $dt) {
223  $startInit = $dt;
224  $endInit = clone($dt);
225  $endInit->setDate($startInit->get(IL_CAL_UNIX) + $appDiff, IL_CAL_UNIX);
226  break;
227  }
228  } else {
229  $startInit = $app->getStart();
230  $endInit = $app->getEnd();
231  }
232 
233 
234  if ($app->isFullday()) {
235  // According to RFC 5545 3.6.1 DTEND is not inclusive.
236  // But ILIAS stores inclusive dates in the database.
237  $endInit->increment(IL_CAL_DAY, 1);
238 
239  $start = $startInit->get(IL_CAL_FKT_DATE, 'Ymd', $ilUser->getTimeZone());
240  $end = $endInit->get(IL_CAL_FKT_DATE, 'Ymd', $ilUser->getTimeZone());
241 
242  $this->writer->addLine('DTSTART;VALUE=DATE:' . $start);
243  $this->writer->addLine('DTEND;VALUE=DATE:' . $end);
244  } else {
245  if ($this->getUserSettings()->getExportTimeZoneType() == ilCalendarUserSettings::CAL_EXPORT_TZ_UTC) {
246  $start = $app->getStart()->get(IL_CAL_FKT_DATE, 'Ymd\THis\Z', ilTimeZone::UTC);
247  $end = $app->getEnd()->get(IL_CAL_FKT_DATE, 'Ymd\THis\Z', ilTimeZone::UTC);
248  $this->writer->addLine('DTSTART:' . $start);
249  $this->writer->addLine('DTEND:' . $end);
250  } else {
251  $start = $startInit->get(IL_CAL_FKT_DATE, 'Ymd\THis', $ilUser->getTimeZone());
252  $end = $endInit->get(IL_CAL_FKT_DATE, 'Ymd\THis', $ilUser->getTimeZone());
253  $this->writer->addLine('DTSTART;TZID=' . $ilUser->getTimezone() . ':' . $start);
254  $this->writer->addLine('DTEND;TZID=' . $ilUser->getTimezone() . ':' . $end);
255  }
256  }
257  // end-patch aptar
258 
259  $this->createRecurrences($app);
260 
261  $this->writer->addLine('SUMMARY:' . ilICalWriter::escapeText($app->getPresentationTitle(false)));
262  if (strlen($app->getDescription())) {
263  $this->writer->addLine('DESCRIPTION:' . ilICalWriter::escapeText($app->getDescription()));
264  }
265  if (strlen($app->getLocation())) {
266  $this->writer->addLine('LOCATION:' . ilICalWriter::escapeText($app->getLocation()));
267  }
268 
269  // TODO: URL
270  $this->buildAppointmentUrl($app);
271 
272  $this->writer->addLine('END:VEVENT');
273  }
274 
275  protected function createRecurrences($app)
276  {
277  global $ilUser;
278 
279  include_once './Services/Calendar/classes/class.ilCalendarRecurrences.php';
280  foreach (ilCalendarRecurrences::_getRecurrences($app->getEntryId()) as $rec) {
281  foreach (ilCalendarRecurrenceExclusions::getExclusionDates($app->getEntryId()) as $excl) {
282  $this->writer->addLine($excl->toICal());
283  }
284  $this->writer->addLine($rec->toICal($ilUser->getId()));
285  }
286  }
287 
288 
289  public function getExportString()
290  {
291  return $this->writer->__toString();
292  }
293 
299  protected function buildAppointmentUrl(ilCalendarEntry $entry)
300  {
303  );
304 
305  if ($cat->getType() != ilCalendarCategory::TYPE_OBJ) {
306  $this->writer->addLine('URL;VALUE=URI:' . ILIAS_HTTP_PATH);
307  } else {
308  $refs = ilObject::_getAllReferences($cat->getObjId());
309 
310  include_once './Services/Link/classes/class.ilLink.php';
311  $this->writer->addLine(
312  'URL;VALUE=URI:' . ilLink::_getLink(current((array) $refs))
313  );
314  }
315  }
316 }
static _getRecurrences($a_cal_id)
get all recurrences of an appointment
Model for a calendar entry.
getUserSettings()
Get user settings.
createVEVENT($app)
Create VEVENT entry ilObjUser $ilUser.
static _getInstanceByUserId($a_user_id)
get singleton instance
static _getFirstRecurrence($a_cal_id)
get first recurrence
$end
Definition: saml1-acs.php:18
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
getEntryId()
get entry id
static getZoneInfoFile($a_tz)
static _lookupCategories($a_cal_id)
lookup categories
const IL_CAL_UNIX
__construct($a_calendar_ids=array())
static _getAllReferences($a_id)
get all reference ids of object
$a_type
Definition: workflow.php:92
const IL_CAL_DAY
Calculates an ilDateList for a given calendar entry and recurrence rule.
buildAppointmentUrl(ilCalendarEntry $entry)
Build url from calendar entry.
Export calendar(s) to ical format
const IL_CAL_FKT_DATE
Date and time handling
$ilUser
Definition: imgupload.php:18
static getInstanceByCategoryId($a_cat_id)
Get instance by category id.
Create styles array
The data for the language used.
static _getAssignedAppointments($a_cat_id)
Get assigned apointments.
static getExclusionDates($a_cal_id)
Read exclusion dates.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static escapeText($a_text)
const IL_CAL_YEAR