ILIAS  release_8 Revision v8.23
class.ilCalendarRecurrence.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
30 {
31  protected const REC_RECURRENCE = 0;
32  protected const REC_EXCLUSION = 1;
33 
34  public const FREQ_NONE = 'NONE';
35  public const FREQ_DAILY = 'DAILY';
36  public const FREQ_WEEKLY = 'WEEKLY';
37  public const FREQ_MONTHLY = 'MONTHLY';
38  public const FREQ_YEARLY = 'YEARLY';
39 
40  protected ilDBInterface $db;
41 
42  private int $recurrence_id = 0;
43  private int $cal_id = 0;
44  private int $recurrence_type = 0;
45 
46  private string $freq_type = '';
47  private string $freq_until_type = '';
48  private ?ilDate $freq_until_date = null;
49  private int $freq_until_count = 0;
50 
51  private int $interval = 1;
52  private string $byday = '';
53  private string $byweekno = '';
54  private string $bymonth = '';
55  private string $bymonthday = '';
56  private string $byyearday = '';
57  private string $bysetpos = '';
58  private string $weekstart = '';
59 
60  private array $exclusion_dates = array();
61 
62  private string $timezone = 'Europe/Berlin';
63 
64  public function __construct(int $a_rec_id = 0)
65  {
66  global $DIC;
67 
68  $this->db = $DIC->database();
69  $this->recurrence_id = $a_rec_id;
70  if ($a_rec_id) {
71  $this->read();
72  }
73  }
74 
75  public static function _delete(int $a_cal_id): void
76  {
77  global $DIC;
78 
79  $ilDB = $DIC['ilDB'];
80  $query = "DELETE FROM cal_recurrence_rules " .
81  "WHERE cal_id = " . $ilDB->quote($a_cal_id, 'integer') . " ";
82  $res = $ilDB->manipulate($query);
83 
85  }
86 
90  public function toICal(int $a_user_id): string
91  {
92  $entry = new ilCalendarEntry($this->getEntryId());
93 
94  if (!$this->getFrequenceType()) {
95  return '';
96  }
97 
98  $ical = 'RRULE:';
99  $ical .= ('FREQ=' . $this->getFrequenceType());
100 
101  if ($this->getInterval()) {
102  $ical .= (';INTERVAL=' . $this->getInterval());
103  }
104  if ($this->getFrequenceUntilCount()) {
105  $ical .= (';COUNT=' . $this->getFrequenceUntilCount());
106  } elseif ($this->getFrequenceUntilDate()) {
107  if ($entry->isFullday()) {
108  $ical .= (';UNTIL=' . $this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE, 'Ymd'));
109  } else {
110  $his = $entry->getStart()->get(IL_CAL_FKT_DATE, 'His');
111  $ical .= (';UNTIL=' . $this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE, 'Ymd') . 'T' . $his);
112  }
113  }
114  if ($this->getBYMONTH()) {
115  $ical .= (';BYMONTH=' . $this->getBYMONTH());
116  }
117  if ($this->getBYWEEKNO()) {
118  $ical .= (';BYWEEKNO=' . $this->getBYWEEKNO());
119  }
120  if ($this->getBYYEARDAY()) {
121  $ical .= (';BYYEARDAY=' . $this->getBYYEARDAY());
122  }
123  if ($this->getBYMONTHDAY()) {
124  $ical .= (';BYMONTHDAY=' . $this->getBYMONTHDAY());
125  }
126  if ($this->getBYDAY()) {
127  $ical .= (';BYDAY=' . $this->getBYDAY());
128  }
129  if ($this->getBYSETPOS()) {
130  $ical .= (';BYSETPOS=' . $this->getBYSETPOS());
131  }
132 
133  // Required in outlook
134  if ($this->getBYDAY()) {
136  if ($us->getWeekStart() == ilCalendarSettings::WEEK_START_MONDAY) {
137  $ical .= (';WKST=MO');
138  } else {
139  $ical .= (';WKST=SU');
140  }
141  }
142 
143  return $ical;
144  }
145 
149  public function reset(): void
150  {
151  $this->setBYDAY('');
152  $this->setBYMONTHDAY('');
153  $this->setBYMONTH('');
154  $this->setBYSETPOS('');
155  $this->setBYWEEKNO('');
156  $this->setBYYEARDAY('');
157  $this->setFrequenceType('');
158  $this->setInterval(1);
159  $this->setFrequenceUntilCount(0);
160  }
161 
162  public function getRecurrenceId(): int
163  {
164  return $this->recurrence_id;
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  {
396  return $this->exclusion_dates;
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 }
static delete(int $a_cal_id)
Delete exclusion dates of calendar entry.
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setBYDAY(string $a_byday)
3.8.5.3.
const IL_CAL_DATETIME
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setFrequenceUntilDate(ilDateTime $a_date=null)
getBYYEARDAYList()
Get BYYEARDAYLIST.
getBYMONTHDAYList()
Get BYMONTHDAY List.
setRecurrence(int $a_type)
set type of recurrence public
static _getInstanceByUserId(int $a_user_id)
getFrequenceType()
Get Frequence type of recurrence.
global $DIC
Definition: feed.php:28
getTimeZone()
Get timezone of recurrence.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
getBYSETPOSList()
Get BYSETPOS List.
getBYWEEKNOList()
Get BYWEEKNOList.
const IL_CAL_FKT_DATE
toICal(int $a_user_id)
Get ical presentation for calendar recurrence.
$query
getFrequenceUntilDate()
Get end data of recurrence.
static getExclusionDates($a_cal_id)
Read exclusion dates.
getInterval()
Get interval of recurrence.
getFrequenceUntilCount()
Get number of recurrences.
static _delete(int $a_cal_id)