ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCalendarRecurrence.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 {
30  protected const REC_RECURRENCE = 0;
31  protected const REC_EXCLUSION = 1;
32 
33  public const FREQ_NONE = 'NONE';
34  public const FREQ_DAILY = 'DAILY';
35  public const FREQ_WEEKLY = 'WEEKLY';
36  public const FREQ_MONTHLY = 'MONTHLY';
37  public const 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 = '';
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  }
160 
161  public function getRecurrenceId(): int
162  {
163  return $this->recurrence_id;
164  }
165 
166  public function setEntryId(int $a_id): void
167  {
168  $this->cal_id = $a_id;
169  }
170 
171  public function getEntryId(): int
172  {
173  return $this->cal_id;
174  }
175 
181  public function setRecurrence(int $a_type): void
182  {
183  $this->recurrence_type = $a_type;
184  }
185 
186  public function isRecurrence(): bool
187  {
188  return $this->recurrence_type == self::REC_RECURRENCE;
189  }
190 
191  public function setFrequenceType(string $a_type): void
192  {
193  $this->freq_type = $a_type;
194  }
195 
196  public function getFrequenceType(): string
197  {
198  return $this->freq_type;
199  }
200 
201  public function getFrequenceUntilDate(): ?ilDate
202  {
203  return is_object($this->freq_until_date) ? $this->freq_until_date : null;
204  }
205 
206  public function setFrequenceUntilDate(?ilDateTime $a_date = null): void
207  {
208  $this->freq_until_date = $a_date;
209  }
210 
211  public function setFrequenceUntilCount(int $a_count): void
212  {
213  $this->freq_until_count = $a_count;
214  }
215 
216  public function getFrequenceUntilCount(): int
217  {
219  }
220 
221  public function setInterval(int $a_interval): void
222  {
223  $this->interval = $a_interval;
224  }
225 
226  public function getInterval(): int
227  {
228  return $this->interval;
229  }
230 
235  public function setBYDAY(string $a_byday): void
236  {
237  $this->byday = $a_byday;
238  }
239 
240  public function getBYDAY(): string
241  {
242  return $this->byday;
243  }
244 
248  public function getBYDAYList(): array
249  {
250  if (!trim($this->getBYDAY())) {
251  return array();
252  }
253  $bydays = [];
254  foreach (explode(',', $this->getBYDAY()) as $byday) {
255  $bydays[] = trim($byday);
256  }
257  return $bydays;
258  }
259 
260  public function setBYWEEKNO(string $a_byweekno): void
261  {
262  $this->byweekno = $a_byweekno;
263  }
264 
265  public function getBYWEEKNOList(): array
266  {
267  if (!trim($this->getBYWEEKNO())) {
268  return array();
269  }
270  $weeks = [];
271  foreach (explode(',', $this->getBYWEEKNO()) as $week_num) {
272  $weeks[] = (int) $week_num;
273  }
274  return $weeks;
275  }
276 
277  public function getBYWEEKNO(): string
278  {
279  return $this->byweekno;
280  }
281 
282  public function setBYMONTH(string $a_by): void
283  {
284  $this->bymonth = $a_by;
285  }
286 
287  public function getBYMONTH(): string
288  {
289  return $this->bymonth;
290  }
291 
292  public function getBYMONTHList(): array
293  {
294  if (!trim($this->getBYMONTH())) {
295  return array();
296  }
297  $months = [];
298  foreach (explode(',', $this->getBYMONTH()) as $month_num) {
299  $months[] = (int) $month_num;
300  }
301  return $months;
302  }
303 
304  public function setBYMONTHDAY(string $a_by): void
305  {
306  $this->bymonthday = $a_by;
307  }
308 
309  public function getBYMONTHDAY(): string
310  {
311  return $this->bymonthday;
312  }
313 
314  public function getBYMONTHDAYList(): array
315  {
316  if (!trim($this->getBYMONTHDAY())) {
317  return array();
318  }
319  $month = [];
320  foreach (explode(',', $this->getBYMONTHDAY()) as $month_num) {
321  $month[] = (int) $month_num;
322  }
323  return $month;
324  }
325 
326  public function setBYYEARDAY(string $a_by): void
327  {
328  $this->byyearday = $a_by;
329  }
330 
331  public function getBYYEARDAY(): string
332  {
333  return $this->byyearday;
334  }
335 
336  public function getBYYEARDAYList(): array
337  {
338  if (!trim($this->getBYYEARDAY())) {
339  return array();
340  }
341  $days = [];
342  foreach (explode(',', $this->getBYYEARDAY()) as $year_day) {
343  $days[] = (int) $year_day;
344  }
345  return $days;
346  }
347 
348  public function setBYSETPOS(string $a_by): void
349  {
350  $this->bysetpos = $a_by;
351  }
352 
353  public function getBYSETPOS(): string
354  {
355  return $this->bysetpos;
356  }
357 
358  public function getBYSETPOSList(): array
359  {
360  if (!trim($this->getBYSETPOS())) {
361  return array();
362  }
363  $positions = [];
364  foreach (explode(',', $this->getBYSETPOS()) as $pos) {
365  $positions[] = (int) $pos;
366  }
367  return $positions;
368  }
369 
370  public function setWeekstart(string $a_start): void
371  {
372  $this->weekstart = $a_start;
373  }
374 
375  public function getWeekstart(): string
376  {
377  return $this->weekstart;
378  }
379 
380  public function getTimeZone(): string
381  {
382  return $this->timezone;
383  }
384 
385  public function setTimeZone(string $a_tz): void
386  {
387  $this->timezone = $a_tz;
388  }
389 
393  public function getExclusionDates(): array
394  {
395  return $this->exclusion_dates;
396  }
397 
401  public function validate(): bool
402  {
403  $valid_frequences = array(self::FREQ_DAILY,
404  self::FREQ_WEEKLY,
405  self::FREQ_MONTHLY,
406  self::FREQ_YEARLY
407  );
408  if (!in_array($this->getFrequenceType(), $valid_frequences)) {
409  return false;
410  }
411  if ($this->getFrequenceUntilCount() < 0) {
412  return false;
413  }
414  if ($this->getInterval() <= 0) {
415  return false;
416  }
417  return true;
418  }
419 
420  public function save(): void
421  {
422  $until_date = is_null($this->getFrequenceUntilDate()) ?
423  null :
424  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME, '', 'UTC');
425  $next_id = $this->db->nextId('cal_recurrence_rules');
426 
427  $query = "INSERT INTO cal_recurrence_rules (rule_id,cal_id,cal_recurrence,freq_type,freq_until_date,freq_until_count,intervall, " .
428  "byday,byweekno,bymonth,bymonthday,byyearday,bysetpos,weekstart) " .
429  "VALUES( " .
430  $this->db->quote($next_id, 'integer') . ", " .
431  $this->db->quote($this->cal_id, 'integer') . ", " .
432  $this->db->quote(1, 'integer') . ", " .
433  $this->db->quote($this->getFrequenceType(), 'text') . ", " .
434  $this->db->quote($until_date, 'timestamp') . ", " .
435  $this->db->quote($this->getFrequenceUntilCount(), 'integer') . ", " .
436  $this->db->quote($this->getInterval(), 'integer') . ", " .
437  $this->db->quote($this->getBYDAY(), 'text') . ", " .
438  $this->db->quote($this->getBYWEEKNO(), 'text') . ", " .
439  $this->db->quote($this->getBYMONTH(), 'text') . ", " .
440  $this->db->quote($this->getBYMONTHDAY(), 'text') . ", " .
441  $this->db->quote($this->getBYYEARDAY(), 'text') . ", " .
442  $this->db->quote($this->getBYSETPOS(), 'text') . ", " .
443  $this->db->quote($this->getWeekstart(), 'text') . " " .
444  ")";
445  $res = $this->db->manipulate($query);
446  $this->recurrence_id = $next_id;
447  }
448 
449  public function update(): void
450  {
451  $until_date = is_null($this->getFrequenceUntilDate()) ?
452  null :
453  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME, '', 'UTC');
454 
455  $query = "UPDATE cal_recurrence_rules SET " .
456  "cal_id = " . $this->db->quote($this->cal_id, 'integer') . ", " .
457  "cal_recurrence = 1," .
458  "freq_type = " . $this->db->quote($this->getFrequenceType(), 'text') . ", " .
459  "freq_until_date = " . $this->db->quote($until_date, 'timestamp') . ", " .
460  "freq_until_count = " . $this->db->quote($this->getFrequenceUntilCount(), 'integer') . ", " .
461  "intervall = " . $this->db->quote($this->getInterval(), 'integer') . ", " .
462  "byday = " . $this->db->quote($this->getBYDAY(), 'text') . ", " .
463  "byweekno = " . $this->db->quote($this->getBYWEEKNO(), 'text') . ", " .
464  "bymonth = " . $this->db->quote($this->getBYMONTH(), 'text') . ", " .
465  "bymonthday = " . $this->db->quote($this->getBYMONTHDAY(), 'text') . ", " .
466  "byyearday = " . $this->db->quote($this->getBYYEARDAY(), 'text') . ", " .
467  "bysetpos = " . $this->db->quote($this->getBYSETPOS(), 'text') . ", " .
468  "weekstart = " . $this->db->quote($this->getWeekstart(), 'text') . " " .
469  "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer') . " ";
470  $res = $this->db->manipulate($query);
471  }
472 
473  public function delete(): void
474  {
475  $query = "DELETE FROM cal_recurrence_rules " .
476  "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer');
477  $res = $this->db->manipulate($query);
478  }
479 
480  private function read(): void
481  {
482  $query = "SELECT * FROM cal_recurrence_rules " .
483  "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer') . " ";
484  $res = $this->db->query($query);
485  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
486  $this->cal_id = (int) $row->cal_id;
487  $this->recurrence_type = (int) $row->cal_recurrence;
488  $this->freq_type = (string) $row->freq_type;
489 
490  if ($row->freq_until_date != null) {
491  $this->freq_until_date = new ilDate($row->freq_until_date, IL_CAL_DATETIME);
492  }
493  $this->freq_until_count = (int) $row->freq_until_count;
494  $this->interval = (int) $row->intervall;
495  $this->byday = (string) $row->byday;
496  $this->byweekno = (string) $row->byweekno;
497  $this->bymonth = (string) $row->bymonth;
498  $this->bymonthday = (string) $row->bymonthday;
499  $this->byyearday = (string) $row->byyearday;
500  $this->bysetpos = (string) $row->bysetpos;
501  $this->weekstart = (string) $row->weekstart;
502  }
503 
504  $this->exclusion_dates = ilCalendarRecurrenceExclusions::getExclusionDates($this->cal_id);
505  }
506 }
static delete(int $a_cal_id)
Delete exclusion dates of calendar entry.
$res
Definition: ltiservices.php:66
setBYDAY(string $a_byday)
3.8.5.3.
const IL_CAL_DATETIME
setFrequenceUntilDate(?ilDateTime $a_date=null)
Model of calendar entry recurrcences based on iCalendar-RFC-5545.
if(!file_exists('../ilias.ini.php'))
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.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getTimeZone()
Get timezone of recurrence.
getBYSETPOSList()
Get BYSETPOS List.
getBYWEEKNOList()
Get BYWEEKNOList.
global $DIC
Definition: shib_login.php:22
const IL_CAL_FKT_DATE
toICal(int $a_user_id)
Get ical presentation for calendar recurrence.
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)