ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCalendarRecurrence.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
29{
30 protected const int REC_RECURRENCE = 0;
31 protected const int REC_EXCLUSION = 1;
32
33 public const string FREQ_NONE = 'NONE';
34 public const string FREQ_DAILY = 'DAILY';
35 public const string FREQ_WEEKLY = 'WEEKLY';
36 public const string FREQ_MONTHLY = 'MONTHLY';
37 public const string FREQ_YEARLY = 'YEARLY';
38
39 protected ilDBInterface $db;
40
41 private int $recurrence_id = 0;
42 private int $cal_id = 0;
43 private int $recurrence_type = 0;
44
45 private string $freq_type = '';
46 private string $freq_until_type = '';
47 private ?ilDate $freq_until_date = null;
48 private int $freq_until_count = 0;
49
50 private int $interval = 1;
51 private string $byday = '';
52 private string $byweekno = '';
53 private string $bymonth = '';
54 private string $bymonthday = '';
55 private string $byyearday = '';
56 private string $bysetpos = '';
57 private string $weekstart = '';
58
59 private array $exclusion_dates = array();
60
61 private string $timezone = 'Europe/Berlin';
62
63 public function __construct(int $a_rec_id = 0)
64 {
65 global $DIC;
66
67 $this->db = $DIC->database();
68 $this->recurrence_id = $a_rec_id;
69 if ($a_rec_id) {
70 $this->read();
71 }
72 }
73
74 public static function _delete(int $a_cal_id): void
75 {
76 global $DIC;
77
78 $ilDB = $DIC['ilDB'];
79 $query = "DELETE FROM cal_recurrence_rules " .
80 "WHERE cal_id = " . $ilDB->quote($a_cal_id, 'integer') . " ";
81 $res = $ilDB->manipulate($query);
82
84 }
85
89 public function toICal(int $a_user_id): string
90 {
91 $entry = new ilCalendarEntry($this->getEntryId());
92
93 if (!$this->getFrequenceType()) {
94 return '';
95 }
96
97 $ical = 'RRULE:';
98 $ical .= ('FREQ=' . $this->getFrequenceType());
99
100 if ($this->getInterval()) {
101 $ical .= (';INTERVAL=' . $this->getInterval());
102 }
103 if ($this->getFrequenceUntilCount()) {
104 $ical .= (';COUNT=' . $this->getFrequenceUntilCount());
105 } elseif ($this->getFrequenceUntilDate()) {
106 if ($entry->isFullday()) {
107 $ical .= (';UNTIL=' . $this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE, 'Ymd'));
108 } else {
109 $his = $entry->getStart()->get(IL_CAL_FKT_DATE, 'His');
110 $ical .= (';UNTIL=' . $this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE, 'Ymd') . 'T' . $his);
111 }
112 }
113 if ($this->getBYMONTH()) {
114 $ical .= (';BYMONTH=' . $this->getBYMONTH());
115 }
116 if ($this->getBYWEEKNO()) {
117 $ical .= (';BYWEEKNO=' . $this->getBYWEEKNO());
118 }
119 if ($this->getBYYEARDAY()) {
120 $ical .= (';BYYEARDAY=' . $this->getBYYEARDAY());
121 }
122 if ($this->getBYMONTHDAY()) {
123 $ical .= (';BYMONTHDAY=' . $this->getBYMONTHDAY());
124 }
125 if ($this->getBYDAY()) {
126 $ical .= (';BYDAY=' . $this->getBYDAY());
127 }
128 if ($this->getBYSETPOS()) {
129 $ical .= (';BYSETPOS=' . $this->getBYSETPOS());
130 }
131
132 // Required in outlook
133 if ($this->getBYDAY()) {
135 if ($us->getWeekStart() == ilCalendarSettings::WEEK_START_MONDAY) {
136 $ical .= (';WKST=MO');
137 } else {
138 $ical .= (';WKST=SU');
139 }
140 }
141
142 return $ical;
143 }
144
148 public function reset(): void
149 {
150 $this->setBYDAY('');
151 $this->setBYMONTHDAY('');
152 $this->setBYMONTH('');
153 $this->setBYSETPOS('');
154 $this->setBYWEEKNO('');
155 $this->setBYYEARDAY('');
156 $this->setFrequenceType('');
157 $this->setInterval(1);
158 $this->setFrequenceUntilCount(0);
159 $this->setFrequenceUntilDate(null);
160 }
161
162 public function getRecurrenceId(): int
163 {
165 }
166
167 public function setEntryId(int $a_id): void
168 {
169 $this->cal_id = $a_id;
170 }
171
172 public function getEntryId(): int
173 {
174 return $this->cal_id;
175 }
176
182 public function setRecurrence(int $a_type): void
183 {
184 $this->recurrence_type = $a_type;
185 }
186
187 public function isRecurrence(): bool
188 {
189 return $this->recurrence_type == self::REC_RECURRENCE;
190 }
191
192 public function setFrequenceType(string $a_type): void
193 {
194 $this->freq_type = $a_type;
195 }
196
197 public function getFrequenceType(): string
198 {
199 return $this->freq_type;
200 }
201
202 public function getFrequenceUntilDate(): ?ilDate
203 {
204 return is_object($this->freq_until_date) ? $this->freq_until_date : null;
205 }
206
207 public function setFrequenceUntilDate(?ilDateTime $a_date = null): void
208 {
209 $this->freq_until_date = $a_date;
210 }
211
212 public function setFrequenceUntilCount(int $a_count): void
213 {
214 $this->freq_until_count = $a_count;
215 }
216
217 public function getFrequenceUntilCount(): int
218 {
220 }
221
222 public function setInterval(int $a_interval): void
223 {
224 $this->interval = $a_interval;
225 }
226
227 public function getInterval(): int
228 {
229 return $this->interval;
230 }
231
236 public function setBYDAY(string $a_byday): void
237 {
238 $this->byday = $a_byday;
239 }
240
241 public function getBYDAY(): string
242 {
243 return $this->byday;
244 }
245
249 public function getBYDAYList(): array
250 {
251 if (!trim($this->getBYDAY())) {
252 return array();
253 }
254 $bydays = [];
255 foreach (explode(',', $this->getBYDAY()) as $byday) {
256 $bydays[] = trim($byday);
257 }
258 return $bydays;
259 }
260
261 public function setBYWEEKNO(string $a_byweekno): void
262 {
263 $this->byweekno = $a_byweekno;
264 }
265
266 public function getBYWEEKNOList(): array
267 {
268 if (!trim($this->getBYWEEKNO())) {
269 return array();
270 }
271 $weeks = [];
272 foreach (explode(',', $this->getBYWEEKNO()) as $week_num) {
273 $weeks[] = (int) $week_num;
274 }
275 return $weeks;
276 }
277
278 public function getBYWEEKNO(): string
279 {
280 return $this->byweekno;
281 }
282
283 public function setBYMONTH(string $a_by): void
284 {
285 $this->bymonth = $a_by;
286 }
287
288 public function getBYMONTH(): string
289 {
290 return $this->bymonth;
291 }
292
293 public function getBYMONTHList(): array
294 {
295 if (!trim($this->getBYMONTH())) {
296 return array();
297 }
298 $months = [];
299 foreach (explode(',', $this->getBYMONTH()) as $month_num) {
300 $months[] = (int) $month_num;
301 }
302 return $months;
303 }
304
305 public function setBYMONTHDAY(string $a_by): void
306 {
307 $this->bymonthday = $a_by;
308 }
309
310 public function getBYMONTHDAY(): string
311 {
312 return $this->bymonthday;
313 }
314
315 public function getBYMONTHDAYList(): array
316 {
317 if (!trim($this->getBYMONTHDAY())) {
318 return array();
319 }
320 $month = [];
321 foreach (explode(',', $this->getBYMONTHDAY()) as $month_num) {
322 $month[] = (int) $month_num;
323 }
324 return $month;
325 }
326
327 public function setBYYEARDAY(string $a_by): void
328 {
329 $this->byyearday = $a_by;
330 }
331
332 public function getBYYEARDAY(): string
333 {
334 return $this->byyearday;
335 }
336
337 public function getBYYEARDAYList(): array
338 {
339 if (!trim($this->getBYYEARDAY())) {
340 return array();
341 }
342 $days = [];
343 foreach (explode(',', $this->getBYYEARDAY()) as $year_day) {
344 $days[] = (int) $year_day;
345 }
346 return $days;
347 }
348
349 public function setBYSETPOS(string $a_by): void
350 {
351 $this->bysetpos = $a_by;
352 }
353
354 public function getBYSETPOS(): string
355 {
356 return $this->bysetpos;
357 }
358
359 public function getBYSETPOSList(): array
360 {
361 if (!trim($this->getBYSETPOS())) {
362 return array();
363 }
364 $positions = [];
365 foreach (explode(',', $this->getBYSETPOS()) as $pos) {
366 $positions[] = (int) $pos;
367 }
368 return $positions;
369 }
370
371 public function setWeekstart(string $a_start): void
372 {
373 $this->weekstart = $a_start;
374 }
375
376 public function getWeekstart(): string
377 {
378 return $this->weekstart;
379 }
380
381 public function getTimeZone(): string
382 {
383 return $this->timezone;
384 }
385
386 public function setTimeZone(string $a_tz): void
387 {
388 $this->timezone = $a_tz;
389 }
390
394 public function getExclusionDates(): array
395 {
397 }
398
402 public function validate(): bool
403 {
404 $valid_frequences = array(self::FREQ_DAILY,
405 self::FREQ_WEEKLY,
406 self::FREQ_MONTHLY,
407 self::FREQ_YEARLY
408 );
409 if (!in_array($this->getFrequenceType(), $valid_frequences)) {
410 return false;
411 }
412 if ($this->getFrequenceUntilCount() < 0) {
413 return false;
414 }
415 if ($this->getInterval() <= 0) {
416 return false;
417 }
418 return true;
419 }
420
421 public function save(): void
422 {
423 $until_date = is_null($this->getFrequenceUntilDate()) ?
424 null :
425 $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME, '', 'UTC');
426 $next_id = $this->db->nextId('cal_recurrence_rules');
427
428 $query = "INSERT INTO cal_recurrence_rules (rule_id,cal_id,cal_recurrence,freq_type,freq_until_date,freq_until_count,intervall, " .
429 "byday,byweekno,bymonth,bymonthday,byyearday,bysetpos,weekstart) " .
430 "VALUES( " .
431 $this->db->quote($next_id, 'integer') . ", " .
432 $this->db->quote($this->cal_id, 'integer') . ", " .
433 $this->db->quote(1, 'integer') . ", " .
434 $this->db->quote($this->getFrequenceType(), 'text') . ", " .
435 $this->db->quote($until_date, 'timestamp') . ", " .
436 $this->db->quote($this->getFrequenceUntilCount(), 'integer') . ", " .
437 $this->db->quote($this->getInterval(), 'integer') . ", " .
438 $this->db->quote($this->getBYDAY(), 'text') . ", " .
439 $this->db->quote($this->getBYWEEKNO(), 'text') . ", " .
440 $this->db->quote($this->getBYMONTH(), 'text') . ", " .
441 $this->db->quote($this->getBYMONTHDAY(), 'text') . ", " .
442 $this->db->quote($this->getBYYEARDAY(), 'text') . ", " .
443 $this->db->quote($this->getBYSETPOS(), 'text') . ", " .
444 $this->db->quote($this->getWeekstart(), 'text') . " " .
445 ")";
446 $res = $this->db->manipulate($query);
447 $this->recurrence_id = $next_id;
448 }
449
450 public function update(): void
451 {
452 $until_date = is_null($this->getFrequenceUntilDate()) ?
453 null :
454 $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME, '', 'UTC');
455
456 $query = "UPDATE cal_recurrence_rules SET " .
457 "cal_id = " . $this->db->quote($this->cal_id, 'integer') . ", " .
458 "cal_recurrence = 1," .
459 "freq_type = " . $this->db->quote($this->getFrequenceType(), 'text') . ", " .
460 "freq_until_date = " . $this->db->quote($until_date, 'timestamp') . ", " .
461 "freq_until_count = " . $this->db->quote($this->getFrequenceUntilCount(), 'integer') . ", " .
462 "intervall = " . $this->db->quote($this->getInterval(), 'integer') . ", " .
463 "byday = " . $this->db->quote($this->getBYDAY(), 'text') . ", " .
464 "byweekno = " . $this->db->quote($this->getBYWEEKNO(), 'text') . ", " .
465 "bymonth = " . $this->db->quote($this->getBYMONTH(), 'text') . ", " .
466 "bymonthday = " . $this->db->quote($this->getBYMONTHDAY(), 'text') . ", " .
467 "byyearday = " . $this->db->quote($this->getBYYEARDAY(), 'text') . ", " .
468 "bysetpos = " . $this->db->quote($this->getBYSETPOS(), 'text') . ", " .
469 "weekstart = " . $this->db->quote($this->getWeekstart(), 'text') . " " .
470 "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer') . " ";
471 $res = $this->db->manipulate($query);
472 }
473
474 public function delete(): void
475 {
476 $query = "DELETE FROM cal_recurrence_rules " .
477 "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer');
478 $res = $this->db->manipulate($query);
479 }
480
481 private function read(): void
482 {
483 $query = "SELECT * FROM cal_recurrence_rules " .
484 "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer') . " ";
485 $res = $this->db->query($query);
486 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
487 $this->cal_id = (int) $row->cal_id;
488 $this->recurrence_type = (int) $row->cal_recurrence;
489 $this->freq_type = (string) $row->freq_type;
490
491 if ($row->freq_until_date != null) {
492 $this->freq_until_date = new ilDate($row->freq_until_date, IL_CAL_DATETIME);
493 }
494 $this->freq_until_count = (int) $row->freq_until_count;
495 $this->interval = (int) $row->intervall;
496 $this->byday = (string) $row->byday;
497 $this->byweekno = (string) $row->byweekno;
498 $this->bymonth = (string) $row->bymonth;
499 $this->bymonthday = (string) $row->bymonthday;
500 $this->byyearday = (string) $row->byyearday;
501 $this->bysetpos = (string) $row->bysetpos;
502 $this->weekstart = (string) $row->weekstart;
503 }
504
505 $this->exclusion_dates = ilCalendarRecurrenceExclusions::getExclusionDates($this->cal_id);
506 }
507}
const IL_CAL_DATETIME
const IL_CAL_FKT_DATE
Model for a calendar entry.
static delete(int $a_cal_id)
Delete exclusion dates of calendar entry.
static getExclusionDates($a_cal_id)
Read exclusion dates.
Model of calendar entry recurrcences based on iCalendar-RFC-5545.
getBYSETPOSList()
Get BYSETPOS List.
getFrequenceType()
Get Frequence type of recurrence.
getTimeZone()
Get timezone of recurrence.
setBYDAY(string $a_byday)
3.8.5.3.
getFrequenceUntilCount()
Get number of recurrences.
setFrequenceUntilDate(?ilDateTime $a_date=null)
getInterval()
Get interval of recurrence.
static _delete(int $a_cal_id)
getBYMONTHDAYList()
Get BYMONTHDAY List.
setRecurrence(int $a_type)
set type of recurrence @access public
getFrequenceUntilDate()
Get end data of recurrence.
toICal(int $a_user_id)
Get ical presentation for calendar recurrence.
getBYYEARDAYList()
Get BYYEARDAYLIST.
static _getInstanceByUserId(int $a_user_id)
@classDescription Date and time handling
Class for single dates.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26