ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
43 
44 
45  protected $calendars = array();
46  protected $user_settings = NULL;
47  protected $appointments = array();
48  protected $writer = null;
49 
50  public function __construct($a_calendar_ids = array())
51  {
52  $this->calendars = $a_calendar_ids;
53  $this->writer = new ilICalWriter();
54 
55  $this->user_settings = ilCalendarUserSettings::_getInstanceByUserId($GLOBALS['ilUser']->getId());
56  }
57 
62  public function getUserSettings()
63  {
64  return $this->user_settings;
65  }
66 
67 
68  public function setExportType($a_type)
69  {
70  $this->export_type = $a_type;
71  }
72 
73  public function setAppointments($a_apps)
74  {
75  $this->appointments = $a_apps;
76  }
77 
78  public function getAppointments()
79  {
80  return $this->appointments;
81  }
82 
83  public function setCalendarIds($a_cal_ids)
84  {
85  $this->calendars = $a_cal_ids;
86  }
87 
88  public function getCalendarIds()
89  {
90  return (array) $this->calendars;
91  }
92 
93  public function getExportType()
94  {
95  return $this->export_type;
96  }
97 
98  public function export()
99  {
100  $this->writer->addLine('BEGIN:VCALENDAR');
101  $this->writer->addLine('VERSION:2.0');
102  $this->writer->addLine('METHOD:PUBLISH');
103  $this->writer->addLine('PRODID:-//ilias.de/NONSGML ILIAS Calendar V4.1//EN');
104  $this->addTimezone();
105 
106  switch($this->getExportType())
107  {
108  case self::EXPORT_CALENDARS:
109  $this->addCategories();
110  break;
111 
112  case self::EXPORT_APPOINTMENTS:
113  $this->addAppointments();
114  break;
115  }
116  $this->writer->addLine('END:VCALENDAR');
117  }
118 
119  protected function addTimezone()
120  {
121  if($this->getUserSettings()->getExportTimeZoneType() == ilCalendarUserSettings::CAL_EXPORT_TZ_UTC)
122  {
123  return;
124  }
125 
126  $this->writer->addLine('X-WR-TIMEZONE:'.$GLOBALS['ilUser']->getTimeZone());
127 
128  include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
129  $tzid_file = ilCalendarUtil::getZoneInfoFile($GLOBALS['ilUser']->getTimeZone());
130  if(!is_file($tzid_file))
131  {
132  $tzid_file = ilCalendarUtil::getZoneInfoFile('Europe/Berlin');
133  }
134  $reader = fopen($tzid_file,'r');
135  while($line = fgets($reader))
136  {
137  $line = str_replace("\n", '', $line);
138  $this->writer->addLine($line);
139  }
140  }
141 
142  protected function addCategories()
143  {
144  foreach($this->calendars as $category_id)
145  {
146  foreach(ilCalendarCategoryAssignments::_getAssignedAppointments(array($category_id)) as $app_id)
147  {
148  $this->addAppointment($app_id);
149  }
150  }
151  }
152 
153  protected function addAppointments()
154  {
155  foreach($this->getAppointments() as $app)
156  {
157  $this->addAppointment($app);
158  }
159  }
160 
161  protected function addAppointment($a_app_id)
162  {
163  $app = new ilCalendarEntry($a_app_id);
164  if($app->isMilestone())
165  {
166  $this->createVTODO($app);
167  }
168  else
169  {
170  $this->createVEVENT($app);
171  }
172  }
173 
174  protected function createVTODO($app)
175  {
176  // TODO
177  return true;
178  }
179 
180  protected function createVEVENT($app)
181  {
182  global $ilUser;
183 
184  $this->writer->addLine('BEGIN:VEVENT');
185  // TODO only domain
186  $this->writer->addLine('UID:'.ilICalWriter::escapeText(
187  $app->getEntryId().'_'.CLIENT_ID.'@'.ILIAS_HTTP_PATH));
188 
189  $last_mod = $app->getLastUpdate()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',ilTimeZone::UTC);
190  #$last_mod = $app->getLastUpdate()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',$ilUser->getTimeZone());
191  $this->writer->addLine('LAST-MODIFIED:'.$last_mod);
192 
193  // begin-patch aptar
194  include_once './Services/Calendar/classes/class.ilCalendarRecurrences.php';
195  if($rec = ilCalendarRecurrences::_getFirstRecurrence($app->getEntryId()))
196  {
197  // Set starting time to first appointment that matches the recurrence rule
198  include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
199  $calc = new ilCalendarRecurrenceCalculator($app,$rec);
200 
201  $pStart = $app->getStart();
202  $pEnd = clone $app->getStart();
203  $pEnd->increment(IL_CAL_YEAR,5);
204  $appDiff = $app->getEnd()->get(IL_CAL_UNIX) - $app->getStart()->get(IL_CAL_UNIX);
205  $recs = $calc->calculateDateList($pStart, $pEnd);
206  foreach($recs as $dt)
207  {
208  $startInit = $dt;
209  $endInit = clone($dt);
210  $endInit->setDate($startInit->get(IL_CAL_UNIX) + $appDiff,IL_CAL_UNIX);
211  break;
212  }
213 
214  }
215  else
216  {
217  $startInit = $app->getStart();
218  $endInit = $app->getEnd();
219  }
220 
221 
222  if($app->isFullday())
223  {
224  // According to RFC 5545 3.6.1 DTEND is not inklusive.
225  // But ILIAS stores inklusive dates in the database.
226  #$app->getEnd()->increment(IL_CAL_DAY,1);
227  $endInit->increment(IL_CAL_DATE,1);
228 
229  #$start = $app->getStart()->get(IL_CAL_FKT_DATE,'Ymd\Z',ilTimeZone::UTC);
230  #$start = $app->getStart()->get(IL_CAL_FKT_DATE,'Ymd',$ilUser->getTimeZone());
231  $start = $startInit->get(IL_CAL_FKT_DATE,'Ymd',$ilUser->getTimeZone());
232  #$end = $app->getEnd()->get(IL_CAL_FKT_DATE,'Ymd\Z',ilTimeZone::UTC);
233  #$end = $app->getEnd()->get(IL_CAL_FKT_DATE,'Ymd',$ilUser->getTimeZone());
234  $endInit->increment(IL_CAL_DAY,1);
235  $end = $endInit->get(IL_CAL_FKT_DATE,'Ymd',$ilUser->getTimeZone());
236 
237  $this->writer->addLine('DTSTART;VALUE=DATE:' . $start);
238  $this->writer->addLine('DTEND;VALUE=DATE:'.$end);
239  }
240  else
241  {
242  if($this->getUserSettings()->getExportTimeZoneType() == ilCalendarUserSettings::CAL_EXPORT_TZ_UTC)
243  {
244  $start = $app->getStart()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',ilTimeZone::UTC);
245  $end = $app->getEnd()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',ilTimeZone::UTC);
246  $this->writer->addLine('DTSTART:'. $start);
247  $this->writer->addLine('DTEND:'.$end);
248 
249  }
250  else
251  {
252  $start = $startInit->get(IL_CAL_FKT_DATE,'Ymd\THis',$ilUser->getTimeZone());
253  $end = $endInit->get(IL_CAL_FKT_DATE,'Ymd\THis',$ilUser->getTimeZone());
254  $this->writer->addLine('DTSTART;TZID='.$ilUser->getTimezone().':'. $start);
255  $this->writer->addLine('DTEND;TZID='.$ilUser->getTimezone().':'.$end);
256  }
257  }
258  // end-patch aptar
259 
260  $this->createRecurrences($app);
261 
262  $this->writer->addLine('SUMMARY:'.ilICalWriter::escapeText($app->getPresentationTitle(false)));
263  if(strlen($app->getDescription()))
264  $this->writer->addLine('DESCRIPTION:'.ilICalWriter::escapeText($app->getDescription()));
265  if(strlen($app->getLocation()))
266  $this->writer->addLine('LOCATION:'.ilICalWriter::escapeText($app->getLocation()));
267 
268  // TODO: URL
269  $this->buildAppointmentUrl($app);
270 
271  $this->writer->addLine('END:VEVENT');
272 
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  {
282  foreach(ilCalendarRecurrenceExclusions::getExclusionDates($app->getEntryId()) as $excl)
283  {
284  $this->writer->addLine($excl->toICal());
285  }
286  $this->writer->addLine($rec->toICal($ilUser->getId()));
287  }
288  }
289 
290 
291  public function getExportString()
292  {
293  return $this->writer->__toString();
294  }
295 
301  protected function buildAppointmentUrl(ilCalendarEntry $entry)
302  {
305  );
306 
307  if($cat->getType() != ilCalendarCategory::TYPE_OBJ)
308  {
309  $this->writer->addLine('URL;VALUE=URI:'.ILIAS_HTTP_PATH);
310  }
311  else
312  {
313  $refs = ilObject::_getAllReferences($cat->getObjId());
314 
315  include_once './Services/Link/classes/class.ilLink.php';
316  $this->writer->addLine(
317  'URL;VALUE=URI:'.ilLink::_getLink(current((array) $refs))
318  );
319  }
320  }
321 }