ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilCalendarRecurrence.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2006 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.ilDate.php');
25include_once './Services/Calendar/classes/class.ilCalendarRecurrenceExclusions.php';
26include_once './Services/Calendar/interfaces/interface.ilCalendarRecurrenceCalculation.php';
27
38define('IL_CAL_FREQ_DAILY', 'DAILY');
39define('IL_CAL_FREQ_WEEKLY', 'WEEKLY');
40define('IL_CAL_FREQ_MONTHLY', 'MONTHLY');
41define('IL_CAL_FREQ_YEARLY', 'YEARLY');
42
44{
45 const REC_RECURRENCE = 0;
46 const REC_EXCLUSION = 1;
47
48 const FREQ_DAILY = 'DAILY';
49 const FREQ_WEEKLY = 'WEEKLY';
50 const FREQ_MONTHLY = 'MONTHLY';
51 const FREQ_YEARLY = 'YEARLY';
52
53
54 protected $db;
55
57 private $cal_id;
59
60 private $freq_type = '';
62 private $freq_until_date = null;
64
65 private $interval = 0;
66 private $byday = '';
67 private $byweekno = '';
68 private $bymonth = '';
69 private $bymonthday = '';
70 private $byyearday = '';
71 private $bysetpos = '';
72 private $weekstart = '';
73
74 private $exclusion_dates = array();
75
76 private $timezone = 'Europe/Berlin';
77
85 public function __construct($a_rec_id = 0)
86 {
87 global $DIC;
88
89 $ilDB = $DIC['ilDB'];
90
91 $this->db = $ilDB;
92 $this->recurrence_id = $a_rec_id;
93 if ($a_rec_id) {
94 $this->read();
95 }
96 }
97
106 public static function _delete($a_cal_id)
107 {
108 global $DIC;
109
110 $ilDB = $DIC['ilDB'];
111
112 $query = "DELETE FROM cal_recurrence_rules " .
113 "WHERE cal_id = " . $ilDB->quote($a_cal_id, 'integer') . " ";
114 $res = $ilDB->manipulate($query);
115
117 }
118
124 public function toICal($a_user_id)
125 {
126 include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
127 $entry = new ilCalendarEntry($this->getEntryId());
128
129 $ical = 'RRULE:';
130 $ical .= ('FREQ=' . $this->getFrequenceType());
131
132 if ($this->getInterval()) {
133 $ical .= (';INTERVAL=' . $this->getInterval());
134 }
135 if ($this->getFrequenceUntilCount()) {
136 $ical .= (';COUNT=' . $this->getFrequenceUntilCount());
137 } elseif ($this->getFrequenceUntilDate()) {
138 if ($entry->isFullday()) {
139 $ical .= (';UNTIL=' . $this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE, 'Ymd'));
140 } else {
141 $his = $entry->getStart()->get(IL_CAL_FKT_DATE, 'His');
142 $ical .= (';UNTIL=' . $this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE, 'Ymd') . 'T' . $his);
143 }
144 }
145 if ($this->getBYMONTH()) {
146 $ical .= (';BYMONTH=' . $this->getBYMONTH());
147 }
148 if ($this->getBYWEEKNO()) {
149 $ical .= (';BYWEEKNO=' . $this->getBYWEEKNO());
150 }
151 if ($this->getBYYEARDAY()) {
152 $ical .= (';BYYEARDAY=' . $this->getBYYEARDAY());
153 }
154 if ($this->getBYMONTHDAY()) {
155 $ical .= (';BYMONTHDAY=' . $this->getBYMONTHDAY());
156 }
157 if ($this->getBYDAY()) {
158 $ical .= (';BYDAY=' . $this->getBYDAY());
159 }
160 if ($this->getBYSETPOS()) {
161 $ical .= (';BYSETPOS=' . $this->getBYSETPOS());
162 }
163
164 // Required in outlook
165 if ($this->getBYDAY()) {
166 include_once './Services/Calendar/classes/class.ilCalendarUserSettings.php';
167 include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
169 if ($us->getWeekStart() == ilCalendarSettings::WEEK_START_MONDAY) {
170 $ical .= (';WKST=MO');
171 } else {
172 $ical .= (';WKST=SU');
173 }
174 }
175
176 return $ical;
177 }
178
179
186 public function reset()
187 {
188 $this->setBYDAY('');
189 $this->setBYMONTHDAY('');
190 $this->setBYMONTH('');
191 $this->setBYSETPOS('');
192 $this->setBYWEEKNO('');
193 $this->setBYYEARDAY('');
194 $this->setFrequenceType('');
195 $this->setInterval(1);
196 $this->setFrequenceUntilCount(0);
197
198 return true;
199 }
200
207 public function getRecurrenceId()
208 {
210 }
211
212
220 public function setEntryId($a_id)
221 {
222 $this->cal_id = $a_id;
223 }
224
229 public function getEntryId()
230 {
231 return $this->cal_id;
232 }
233
241 public function setRecurrence($a_type)
242 {
243 $this->recurrence_type = $a_type;
244 }
245
253 public function isRecurrence()
254 {
255 return $this->recurrence_type == self::REC_RECURRENCE;
256 }
257
265 public function setFrequenceType($a_type)
266 {
267 $this->freq_type = $a_type;
268 }
269
276 public function getFrequenceType()
277 {
278 return $this->freq_type;
279 }
280
287 public function getFrequenceUntilDate()
288 {
289 return is_object($this->freq_until_date) ? $this->freq_until_date : null;
290 }
291
298 public function setFrequenceUntilDate(ilDateTime $a_date = null)
299 {
300 $this->freq_until_date = $a_date;
301 }
302
310 public function setFrequenceUntilCount($a_count)
311 {
312 $this->freq_until_count = $a_count;
313 }
314
322 public function getFrequenceUntilCount()
323 {
325 }
326
334 public function setInterval($a_interval)
335 {
336 $this->interval = $a_interval;
337 }
338
345 public function getInterval()
346 {
347 return $this->interval ? $this->interval : 1;
348 }
349
357 public function setBYDAY($a_byday)
358 {
359 $this->byday = $a_byday;
360 }
361
368 public function getBYDAY()
369 {
370 return $this->byday;
371 }
372
379 public function getBYDAYList()
380 {
381 if (!trim($this->getBYDAY())) {
382 return array();
383 }
384 foreach (explode(',', $this->getBYDAY()) as $byday) {
385 $bydays[] = trim($byday);
386 }
387 return $bydays ? $bydays : array();
388 }
389
397 public function setBYWEEKNO($a_byweekno)
398 {
399 $this->byweekno = $a_byweekno;
400 }
401
408 public function getBYWEEKNOList()
409 {
410 if (!trim($this->getBYWEEKNO())) {
411 return array();
412 }
413 foreach (explode(',', $this->getBYWEEKNO()) as $week_num) {
414 $weeks[] = (int) $week_num;
415 }
416 return $weeks ? $weeks : array();
417 }
418
419
426 public function getBYWEEKNO()
427 {
428 return $this->byweekno;
429 }
430
438 public function setBYMONTH($a_by)
439 {
440 $this->bymonth = $a_by;
441 }
442
449 public function getBYMONTH()
450 {
451 return $this->bymonth;
452 }
453
460 public function getBYMONTHList()
461 {
462 if (!trim($this->getBYMONTH())) {
463 return array();
464 }
465 foreach (explode(',', $this->getBYMONTH()) as $month_num) {
466 $months[] = (int) $month_num;
467 }
468 return $months ? $months : array();
469 }
470
478 public function setBYMONTHDAY($a_by)
479 {
480 $this->bymonthday = $a_by;
481 }
482
489 public function getBYMONTHDAY()
490 {
491 return $this->bymonthday;
492 }
493
499 public function getBYMONTHDAYList()
500 {
501 if (!trim($this->getBYMONTHDAY())) {
502 return array();
503 }
504 foreach (explode(',', $this->getBYMONTHDAY()) as $month_num) {
505 $months[] = (int) $month_num;
506 }
507 return $months ? $months : array();
508 }
509
510
518 public function setBYYEARDAY($a_by)
519 {
520 $this->byyearday = $a_by;
521 }
522
529 public function getBYYEARDAY()
530 {
531 return $this->byyearday;
532 }
533
540 public function getBYYEARDAYList()
541 {
542 if (!trim($this->getBYYEARDAY())) {
543 return array();
544 }
545 foreach (explode(',', $this->getBYYEARDAY()) as $year_day) {
546 $days[] = (int) $year_day;
547 }
548 return $days ? $days : array();
549 }
550
558 public function setBYSETPOS($a_by)
559 {
560 $this->bysetpos = $a_by;
561 }
562
569 public function getBYSETPOS()
570 {
571 return $this->bysetpos;
572 }
573
580 public function getBYSETPOSList()
581 {
582 if (!trim($this->getBYSETPOS())) {
583 return array();
584 }
585 foreach (explode(',', $this->getBYSETPOS()) as $pos) {
586 $positions[] = (int) $pos;
587 }
588 return $positions ? $positions : array();
589 }
590
591
599 public function setWeekstart($a_start)
600 {
601 $this->weekstart = $a_start;
602 }
603
610 public function getWeekstart()
611 {
612 return $this->weekstart;
613 }
614
621 public function getTimeZone()
622 {
623 return $this->timezone;
624 }
625
633 public function setTimeZone($a_tz)
634 {
635 $this->timezone = $a_tz;
636 }
637
642 public function getExclusionDates()
643 {
644 return (array) $this->exclusion_dates;
645 }
646
653 public function validate()
654 {
656 if (!in_array($this->getFrequenceType(), $valid_frequences)) {
657 return false;
658 }
659 if ($this->getFrequenceUntilCount() < 0) {
660 return false;
661 }
662 if ($this->getInterval() <= 0) {
663 return false;
664 }
665 return true;
666 }
667
668
675 public function save()
676 {
677 global $DIC;
678
679 $ilDB = $DIC['ilDB'];
680
681 $until_date = is_null($this->getFrequenceUntilDate()) ?
682 null :
683 $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME, '', 'UTC');
684 $next_id = $ilDB->nextId('cal_recurrence_rules');
685
686 $query = "INSERT INTO cal_recurrence_rules (rule_id,cal_id,cal_recurrence,freq_type,freq_until_date,freq_until_count,intervall, " .
687 "byday,byweekno,bymonth,bymonthday,byyearday,bysetpos,weekstart) " .
688 "VALUES( " .
689 $ilDB->quote($next_id, 'integer') . ", " .
690 $this->db->quote($this->cal_id, 'integer') . ", " .
691 $ilDB->quote(1, 'integer') . ", " .
692 $ilDB->quote((string) $this->getFrequenceType(), 'text') . ", " .
693 $this->db->quote($until_date, 'timestamp') . ", " .
694 $this->db->quote((int) $this->getFrequenceUntilCount(), 'integer') . ", " .
695 $this->db->quote((int) $this->getInterval(), 'integer') . ", " .
696 $this->db->quote((string) $this->getBYDAY(), 'text') . ", " .
697 $this->db->quote((string) $this->getBYWEEKNO(), 'text') . ", " .
698 $this->db->quote((string) $this->getBYMONTH(), 'text') . ", " .
699 $this->db->quote((string) $this->getBYMONTHDAY(), 'text') . ", " .
700 $this->db->quote((string) $this->getBYYEARDAY(), 'text') . ", " .
701 $this->db->quote((string) $this->getBYSETPOS(), 'text') . ", " .
702 $this->db->quote((string) $this->getWeekstart(), 'text') . " " .
703 ")";
704 $res = $ilDB->manipulate($query);
705 $this->recurrence_id = $next_id;
706 return true;
707 }
708
715 public function update()
716 {
717 global $DIC;
718
719 $ilDB = $DIC['ilDB'];
720
721 $until_date = is_null($this->getFrequenceUntilDate()) ?
722 null :
723 $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME, '', 'UTC');
724
725 $query = "UPDATE cal_recurrence_rules SET " .
726 "cal_id = " . $this->db->quote($this->cal_id, 'integer') . ", " .
727 "cal_recurrence = 1," .
728 "freq_type = " . $this->db->quote($this->getFrequenceType(), 'text') . ", " .
729 "freq_until_date = " . $this->db->quote($until_date, 'timestamp') . ", " .
730 "freq_until_count = " . $this->db->quote($this->getFrequenceUntilCount(), 'integer') . ", " .
731 "intervall = " . $this->db->quote($this->getInterval(), 'integer') . ", " .
732 "byday = " . $this->db->quote($this->getBYDAY(), 'text') . ", " .
733 "byweekno = " . $this->db->quote($this->getBYWEEKNO(), 'text') . ", " .
734 "bymonth = " . $this->db->quote($this->getBYMONTH(), 'text') . ", " .
735 "bymonthday = " . $this->db->quote($this->getBYMONTHDAY(), 'text') . ", " .
736 "byyearday = " . $this->db->quote($this->getBYYEARDAY(), 'text') . ", " .
737 "bysetpos = " . $this->db->quote($this->getBYSETPOS(), 'text') . ", " .
738 "weekstart = " . $this->db->quote($this->getWeekstart(), 'text') . " " .
739 "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer') . " ";
740 $res = $ilDB->manipulate($query);
741 return true;
742 }
743
750 public function delete()
751 {
752 global $DIC;
753
754 $ilDB = $DIC['ilDB'];
755
756 $query = "DELETE FROM cal_recurrence_rules " .
757 "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer');
758 $res = $ilDB->manipulate($query);
759 return true;
760 }
761
768 private function read()
769 {
770 global $DIC;
771
772 $ilDB = $DIC['ilDB'];
773
774 $query = "SELECT * FROM cal_recurrence_rules " .
775 "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer') . " ";
776 $res = $this->db->query($query);
777 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
778 $this->cal_id = $row->cal_id;
779 $this->recurrence_type = $row->cal_recurrence;
780 $this->freq_type = $row->freq_type;
781
782 if ($row->freq_until_date != null) {
783 $this->freq_until_date = new ilDateTime($row->freq_until_date, IL_CAL_DATETIME, 'UTC');
784 }
785 $this->freq_until_count = $row->freq_until_count;
786 $this->interval = $row->intervall;
787 $this->byday = $row->byday;
788 $this->byweekno = $row->byweekno;
789 $this->bymonth = $row->bymonth;
790 $this->bymonthday = $row->bymonthday;
791 $this->byyearday = $row->byyearday;
792 $this->bysetpos = $row->bysetpos;
793 $this->weekstart = $row->week_start;
794 }
795
796 $this->exclusion_dates = ilCalendarRecurrenceExclusions::getExclusionDates($this->cal_id);
797 }
798}
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_FREQ_YEARLY
const IL_CAL_FREQ_MONTHLY
const IL_CAL_FREQ_WEEKLY
const IL_CAL_DATETIME
const IL_CAL_FKT_DATE
Model for a calendar entry.
static delete($a_cal_id)
Delete exclusion dates of calendar entry.
static getExclusionDates($a_cal_id)
Read exclusion dates.
setRecurrence($a_type)
set type of recurrence
setFrequenceType($a_type)
set frequence type
toICal($a_user_id)
Get ical presentation for calendar recurrence.
setBYWEEKNO($a_byweekno)
set by day
getFrequenceUntilCount()
get frequence until count
getExclusionDates()
Get exclusion dates.
setFrequenceUntilCount($a_count)
set frequence count
getBYMONTHDAYList()
get BYMONTHDAY list
getEntryId()
Get calendar entry id.
setInterval($a_interval)
set interval
static _delete($a_cal_id)
delete
setWeekstart($a_start)
set weekstart
setFrequenceUntilDate(ilDateTime $a_date=null)
set freq until date
__construct($a_rec_id=0)
Constructor.
getBYYEARDAYList()
get BYYEARDAY list
static _getInstanceByUserId($a_user_id)
get singleton instance
@classDescription Date and time handling
const IL_CAL_FREQ_DAILY
Model of calendar entry recurrcences.
$row
$query
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
global $ilDB
$a_type
Definition: workflow.php:92