ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
117  case self::EXPORT_CALENDARS:
118  $this->addCategories();
119  break;
120 
121  case self::EXPORT_APPOINTMENTS:
122  $this->addAppointments();
123  break;
124  }
125  $this->writer->addLine('END:VCALENDAR');
126  }
127 
128  protected function addTimezone()
129  {
130  if($this->getUserSettings()->getExportTimeZoneType() == ilCalendarUserSettings::CAL_EXPORT_TZ_UTC)
131  {
132  return;
133  }
134 
135  $this->writer->addLine('X-WR-TIMEZONE:'.$GLOBALS['ilUser']->getTimeZone());
136 
137  include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
138  $tzid_file = ilCalendarUtil::getZoneInfoFile($GLOBALS['ilUser']->getTimeZone());
139  if(!is_file($tzid_file))
140  {
141  $tzid_file = ilCalendarUtil::getZoneInfoFile('Europe/Berlin');
142  }
143  $reader = fopen($tzid_file,'r');
144  while($line = fgets($reader))
145  {
146  $line = str_replace("\n", '', $line);
147  $this->writer->addLine($line);
148  }
149  }
150 
151  protected function addCategories()
152  {
153  foreach($this->calendars as $category_id)
154  {
155  foreach(ilCalendarCategoryAssignments::_getAssignedAppointments(array($category_id)) as $app_id)
156  {
157  $this->addAppointment($app_id);
158  }
159  }
160  }
161 
162  protected function addAppointments()
163  {
164  foreach($this->getAppointments() as $app)
165  {
166  $this->addAppointment($app);
167  }
168  }
169 
170  protected function addAppointment($a_app_id)
171  {
172  $app = new ilCalendarEntry($a_app_id);
173  if($app->isMilestone())
174  {
175  $this->createVTODO($app);
176  }
177  else
178  {
179  $this->createVEVENT($app);
180  }
181  }
182 
183  protected function createVTODO($app)
184  {
185  // TODO
186  return true;
187  }
188 
194  protected function createVEVENT($app)
195  {
196  global $ilUser;
197 
198  if(!$app->getStart() instanceof ilDateTime)
199  {
200  $this->logger->notice('Cannot create appointment for app_id: ' . $app->getEntryId());
201  }
202 
203  $this->writer->addLine('BEGIN:VEVENT');
204 
205  $now = new ilDateTime(time(), IL_CAL_UNIX);
206  $this->writer->addLine('DTSTAMP:'.$now->get(IL_CAL_FKT_DATE,'Ymd\THis\Z', ilTimeZone::UTC));
207 
208  $this->writer->addLine('UID:'.ilICalWriter::escapeText(
209  $app->getEntryId().'_'.CLIENT_ID.'@'.ILIAS_HTTP_PATH));
210 
211 
212  $last_mod = $app->getLastUpdate()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',ilTimeZone::UTC);
213  #$last_mod = $app->getLastUpdate()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',$ilUser->getTimeZone());
214  $this->writer->addLine('LAST-MODIFIED:'.$last_mod);
215 
216  // begin-patch aptar
217  include_once './Services/Calendar/classes/class.ilCalendarRecurrences.php';
218  if($rec = ilCalendarRecurrences::_getFirstRecurrence($app->getEntryId()))
219  {
220  // Set starting time to first appointment that matches the recurrence rule
221  include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
222  $calc = new ilCalendarRecurrenceCalculator($app,$rec);
223 
224  $pStart = $app->getStart();
225  $pEnd = clone $app->getStart();
226  $pEnd->increment(IL_CAL_YEAR,5);
227  $appDiff = $app->getEnd()->get(IL_CAL_UNIX) - $app->getStart()->get(IL_CAL_UNIX);
228  $recs = $calc->calculateDateList($pStart, $pEnd);
229 
230  // defaults
231  $startInit = $app->getStart();
232  $endInit = $app->getEnd();
233  foreach($recs as $dt)
234  {
235  $startInit = $dt;
236  $endInit = clone($dt);
237  $endInit->setDate($startInit->get(IL_CAL_UNIX) + $appDiff,IL_CAL_UNIX);
238  break;
239  }
240 
241  }
242  else
243  {
244  $startInit = $app->getStart();
245  $endInit = $app->getEnd();
246  }
247 
248 
249  if($app->isFullday())
250  {
251  // According to RFC 5545 3.6.1 DTEND is not inklusive.
252  // But ILIAS stores inklusive dates in the database.
253  #$app->getEnd()->increment(IL_CAL_DAY,1);
254  $endInit->increment(IL_CAL_DATE,1);
255 
256  #$start = $app->getStart()->get(IL_CAL_FKT_DATE,'Ymd\Z',ilTimeZone::UTC);
257  #$start = $app->getStart()->get(IL_CAL_FKT_DATE,'Ymd',$ilUser->getTimeZone());
258  $start = $startInit->get(IL_CAL_FKT_DATE,'Ymd',$ilUser->getTimeZone());
259  #$end = $app->getEnd()->get(IL_CAL_FKT_DATE,'Ymd\Z',ilTimeZone::UTC);
260  #$end = $app->getEnd()->get(IL_CAL_FKT_DATE,'Ymd',$ilUser->getTimeZone());
261  $endInit->increment(IL_CAL_DAY,1);
262  $end = $endInit->get(IL_CAL_FKT_DATE,'Ymd',$ilUser->getTimeZone());
263 
264  $this->writer->addLine('DTSTART;VALUE=DATE:' . $start);
265  $this->writer->addLine('DTEND;VALUE=DATE:'.$end);
266  }
267  else
268  {
269  if($this->getUserSettings()->getExportTimeZoneType() == ilCalendarUserSettings::CAL_EXPORT_TZ_UTC)
270  {
271  $start = $app->getStart()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',ilTimeZone::UTC);
272  $end = $app->getEnd()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',ilTimeZone::UTC);
273  $this->writer->addLine('DTSTART:'. $start);
274  $this->writer->addLine('DTEND:'.$end);
275 
276  }
277  else
278  {
279  $start = $startInit->get(IL_CAL_FKT_DATE,'Ymd\THis',$ilUser->getTimeZone());
280  $end = $endInit->get(IL_CAL_FKT_DATE,'Ymd\THis',$ilUser->getTimeZone());
281  $this->writer->addLine('DTSTART;TZID='.$ilUser->getTimezone().':'. $start);
282  $this->writer->addLine('DTEND;TZID='.$ilUser->getTimezone().':'.$end);
283  }
284  }
285  // end-patch aptar
286 
287  $this->createRecurrences($app);
288 
289  $this->writer->addLine('SUMMARY:'.ilICalWriter::escapeText($app->getPresentationTitle(false)));
290  if(strlen($app->getDescription()))
291  $this->writer->addLine('DESCRIPTION:'.ilICalWriter::escapeText($app->getDescription()));
292  if(strlen($app->getLocation()))
293  $this->writer->addLine('LOCATION:'.ilICalWriter::escapeText($app->getLocation()));
294 
295  // TODO: URL
296  $this->buildAppointmentUrl($app);
297 
298  $this->writer->addLine('END:VEVENT');
299 
300  }
301 
302  protected function createRecurrences($app)
303  {
304  global $ilUser;
305 
306  include_once './Services/Calendar/classes/class.ilCalendarRecurrences.php';
307  foreach(ilCalendarRecurrences::_getRecurrences($app->getEntryId()) as $rec)
308  {
309  foreach(ilCalendarRecurrenceExclusions::getExclusionDates($app->getEntryId()) as $excl)
310  {
311  $this->writer->addLine($excl->toICal());
312  }
313  $this->writer->addLine($rec->toICal($ilUser->getId()));
314  }
315  }
316 
317 
318  public function getExportString()
319  {
320  return $this->writer->__toString();
321  }
322 
328  protected function buildAppointmentUrl(ilCalendarEntry $entry)
329  {
332  );
333 
334  if($cat->getType() != ilCalendarCategory::TYPE_OBJ)
335  {
336  $this->writer->addLine('URL;VALUE=URI:'.ILIAS_HTTP_PATH);
337  }
338  else
339  {
340  $refs = ilObject::_getAllReferences($cat->getObjId());
341 
342  include_once './Services/Link/classes/class.ilLink.php';
343  $this->writer->addLine(
344  'URL;VALUE=URI:'.ilLink::_getLink(current((array) $refs))
345  );
346  }
347  }
348 }
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
$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:93
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.
const IL_CAL_DATE
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