ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
24include_once './Services/Calendar/classes/class.ilCalendarUserSettings.php';
25include_once './Services/Calendar/classes/iCal/class.ilICalWriter.php';
26include_once './Services/Calendar/classes/class.ilCalendarCategory.php';
27include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
28include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
29
38{
41
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['DIC']['ilUser']->getId());
64 }
65
70 public function getUserSettings()
71 {
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 {
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()) {
117 $this->addCategories();
118 break;
119
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['DIC']['ilUser']->getTimeZone());
134
135 include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
136 $tzid_file = ilCalendarUtil::getZoneInfoFile($GLOBALS['DIC']['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 $DIC;
187
188 $ilUser = $DIC['ilUser'];
189
190 if (!$app->getStart() instanceof ilDateTime) {
191 $this->logger->notice('Cannot create appointment for app_id: ' . $app->getEntryId());
192 return false;
193 }
194
195 $this->writer->addLine('BEGIN:VEVENT');
196
197 $now = new ilDateTime(time(), IL_CAL_UNIX);
198 $this->writer->addLine('DTSTAMP:' . $now->get(IL_CAL_FKT_DATE, 'Ymd\THis\Z', ilTimeZone::UTC));
199
200 $this->writer->addLine('UID:' . ilICalWriter::escapeText(
201 $app->getEntryId() . '_' . CLIENT_ID . '@' . ILIAS_HTTP_PATH
202 ));
203
204
205 $last_mod = $app->getLastUpdate()->get(IL_CAL_FKT_DATE, 'Ymd\THis\Z', ilTimeZone::UTC);
206 #$last_mod = $app->getLastUpdate()->get(IL_CAL_FKT_DATE,'Ymd\THis\Z',$ilUser->getTimeZone());
207 $this->writer->addLine('LAST-MODIFIED:' . $last_mod);
208
209 // begin-patch aptar
210 include_once './Services/Calendar/classes/class.ilCalendarRecurrences.php';
211 if ($rec = ilCalendarRecurrences::_getFirstRecurrence($app->getEntryId())) {
212 // Set starting time to first appointment that matches the recurrence rule
213 include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
214 $calc = new ilCalendarRecurrenceCalculator($app, $rec);
215
216 $pStart = $app->getStart();
217 $pEnd = clone $app->getStart();
218 $pEnd->increment(IL_CAL_YEAR, 5);
219 $appDiff = $app->getEnd()->get(IL_CAL_UNIX) - $app->getStart()->get(IL_CAL_UNIX);
220 $recs = $calc->calculateDateList($pStart, $pEnd);
221
222 // defaults
223 $startInit = $app->getStart();
224 $endInit = $app->getEnd();
225 foreach ($recs as $dt) {
226 $startInit = $dt;
227 $endInit = clone($dt);
228 $endInit->setDate($startInit->get(IL_CAL_UNIX) + $appDiff, IL_CAL_UNIX);
229 break;
230 }
231 } else {
232 $startInit = $app->getStart();
233 $endInit = $app->getEnd();
234 }
235
236
237 if ($app->isFullday()) {
238 // According to RFC 5545 3.6.1 DTEND is not inclusive.
239 // But ILIAS stores inclusive dates in the database.
240 $endInit->increment(IL_CAL_DAY, 1);
241
242 $start = $startInit->get(IL_CAL_FKT_DATE, 'Ymd', $ilUser->getTimeZone());
243 $end = $endInit->get(IL_CAL_FKT_DATE, 'Ymd', $ilUser->getTimeZone());
244
245 $this->writer->addLine('DTSTART;VALUE=DATE:' . $start);
246 $this->writer->addLine('DTEND;VALUE=DATE:' . $end);
247 } else {
248 if ($this->getUserSettings()->getExportTimeZoneType() == ilCalendarUserSettings::CAL_EXPORT_TZ_UTC) {
249 $start = $app->getStart()->get(IL_CAL_FKT_DATE, 'Ymd\THis\Z', ilTimeZone::UTC);
250 $end = $app->getEnd()->get(IL_CAL_FKT_DATE, 'Ymd\THis\Z', ilTimeZone::UTC);
251 $this->writer->addLine('DTSTART:' . $start);
252 $this->writer->addLine('DTEND:' . $end);
253 } else {
254 $start = $startInit->get(IL_CAL_FKT_DATE, 'Ymd\THis', $ilUser->getTimeZone());
255 $end = $endInit->get(IL_CAL_FKT_DATE, 'Ymd\THis', $ilUser->getTimeZone());
256 $this->writer->addLine('DTSTART;TZID=' . $ilUser->getTimezone() . ':' . $start);
257 $this->writer->addLine('DTEND;TZID=' . $ilUser->getTimezone() . ':' . $end);
258 }
259 }
260 // end-patch aptar
261
262 $this->createRecurrences($app);
263
264 $this->writer->addLine('SUMMARY:' . ilICalWriter::escapeText($app->getPresentationTitle(false)));
265 if (strlen($app->getDescription())) {
266 $this->writer->addLine('DESCRIPTION:' . ilICalWriter::escapeText($app->getDescription()));
267 }
268 if (strlen($app->getLocation())) {
269 $this->writer->addLine('LOCATION:' . ilICalWriter::escapeText($app->getLocation()));
270 }
271
272 // TODO: URL
274
275 $this->writer->addLine('END:VEVENT');
276 }
277
278 protected function createRecurrences($app)
279 {
280 global $DIC;
281
282 $ilUser = $DIC['ilUser'];
283
284 include_once './Services/Calendar/classes/class.ilCalendarRecurrences.php';
285 foreach (ilCalendarRecurrences::_getRecurrences($app->getEntryId()) as $rec) {
286 foreach (ilCalendarRecurrenceExclusions::getExclusionDates($app->getEntryId()) as $excl) {
287 $this->writer->addLine($excl->toICal());
288 }
289 $this->writer->addLine($rec->toICal($ilUser->getId()));
290 }
291 }
292
293
294 public function getExportString()
295 {
296 return $this->writer->__toString();
297 }
298
304 protected function buildAppointmentUrl(ilCalendarEntry $entry)
305 {
308 );
309
310 if ($cat->getType() != ilCalendarCategory::TYPE_OBJ) {
311 $this->writer->addLine('URL;VALUE=URI:' . ILIAS_HTTP_PATH);
312 } else {
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}
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
const IL_CAL_YEAR
const IL_CAL_FKT_DATE
const IL_CAL_DAY
static _getAssignedAppointments($a_cat_id)
Get assigned apointments.
static _lookupCategories($a_cal_id)
lookup categories
static getInstanceByCategoryId($a_cat_id)
Get instance by category id.
Model for a calendar entry.
@classDescription Export calendar(s) to ical format
__construct($a_calendar_ids=array())
buildAppointmentUrl(ilCalendarEntry $entry)
Build url from calendar entry.
getUserSettings()
Get user settings.
createVEVENT($app)
Create VEVENT entry @global ilObjUser $ilUser.
Calculates an ilDateList for a given calendar entry and recurrence rule.
static getExclusionDates($a_cal_id)
Read exclusion dates.
static _getRecurrences($a_cal_id)
get all recurrences of an appointment
static _getFirstRecurrence($a_cal_id)
get first recurrence
static _getInstanceByUserId($a_user_id)
get singleton instance
static getZoneInfoFile($a_tz)
@classDescription Date and time handling
static escapeText($a_text)
static _getAllReferences($a_id)
get all reference ids of object
$app
Definition: cli.php:38
$ilUser
Definition: imgupload.php:18
$a_type
Definition: workflow.php:92
$DIC
Definition: xapitoken.php:46