ILIAS  release_7 Revision v7.30-3-g800a261c036
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 if (!$this->getFrequenceType()) {
130 return '';
131 }
132
133 $ical = 'RRULE:';
134 $ical .= ('FREQ=' . $this->getFrequenceType());
135
136 if ($this->getInterval()) {
137 $ical .= (';INTERVAL=' . $this->getInterval());
138 }
139 if ($this->getFrequenceUntilCount()) {
140 $ical .= (';COUNT=' . $this->getFrequenceUntilCount());
141 } elseif ($this->getFrequenceUntilDate()) {
142 if ($entry->isFullday()) {
143 $ical .= (';UNTIL=' . $this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE, 'Ymd'));
144 } else {
145 $his = $entry->getStart()->get(IL_CAL_FKT_DATE, 'His');
146 $ical .= (';UNTIL=' . $this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE, 'Ymd') . 'T' . $his);
147 }
148 }
149 if ($this->getBYMONTH()) {
150 $ical .= (';BYMONTH=' . $this->getBYMONTH());
151 }
152 if ($this->getBYWEEKNO()) {
153 $ical .= (';BYWEEKNO=' . $this->getBYWEEKNO());
154 }
155 if ($this->getBYYEARDAY()) {
156 $ical .= (';BYYEARDAY=' . $this->getBYYEARDAY());
157 }
158 if ($this->getBYMONTHDAY()) {
159 $ical .= (';BYMONTHDAY=' . $this->getBYMONTHDAY());
160 }
161 if ($this->getBYDAY()) {
162 $ical .= (';BYDAY=' . $this->getBYDAY());
163 }
164 if ($this->getBYSETPOS()) {
165 $ical .= (';BYSETPOS=' . $this->getBYSETPOS());
166 }
167
168 // Required in outlook
169 if ($this->getBYDAY()) {
170 include_once './Services/Calendar/classes/class.ilCalendarUserSettings.php';
171 include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
173 if ($us->getWeekStart() == ilCalendarSettings::WEEK_START_MONDAY) {
174 $ical .= (';WKST=MO');
175 } else {
176 $ical .= (';WKST=SU');
177 }
178 }
179
180 return $ical;
181 }
182
183
190 public function reset()
191 {
192 $this->setBYDAY('');
193 $this->setBYMONTHDAY('');
194 $this->setBYMONTH('');
195 $this->setBYSETPOS('');
196 $this->setBYWEEKNO('');
197 $this->setBYYEARDAY('');
198 $this->setFrequenceType('');
199 $this->setInterval(1);
200 $this->setFrequenceUntilCount(0);
201
202 return true;
203 }
204
211 public function getRecurrenceId()
212 {
214 }
215
216
224 public function setEntryId($a_id)
225 {
226 $this->cal_id = $a_id;
227 }
228
233 public function getEntryId()
234 {
235 return $this->cal_id;
236 }
237
245 public function setRecurrence($a_type)
246 {
247 $this->recurrence_type = $a_type;
248 }
249
257 public function isRecurrence()
258 {
259 return $this->recurrence_type == self::REC_RECURRENCE;
260 }
261
269 public function setFrequenceType($a_type)
270 {
271 $this->freq_type = $a_type;
272 }
273
280 public function getFrequenceType()
281 {
282 return $this->freq_type;
283 }
284
291 public function getFrequenceUntilDate()
292 {
293 return is_object($this->freq_until_date) ? $this->freq_until_date : null;
294 }
295
302 public function setFrequenceUntilDate(ilDateTime $a_date = null)
303 {
304 $this->freq_until_date = $a_date;
305 }
306
314 public function setFrequenceUntilCount($a_count)
315 {
316 $this->freq_until_count = $a_count;
317 }
318
326 public function getFrequenceUntilCount()
327 {
329 }
330
338 public function setInterval($a_interval)
339 {
340 $this->interval = $a_interval;
341 }
342
349 public function getInterval()
350 {
351 return $this->interval ? $this->interval : 1;
352 }
353
361 public function setBYDAY($a_byday)
362 {
363 $this->byday = $a_byday;
364 }
365
372 public function getBYDAY()
373 {
374 return $this->byday;
375 }
376
383 public function getBYDAYList()
384 {
385 if (!trim($this->getBYDAY())) {
386 return array();
387 }
388 foreach (explode(',', $this->getBYDAY()) as $byday) {
389 $bydays[] = trim($byday);
390 }
391 return $bydays ? $bydays : array();
392 }
393
401 public function setBYWEEKNO($a_byweekno)
402 {
403 $this->byweekno = $a_byweekno;
404 }
405
412 public function getBYWEEKNOList()
413 {
414 if (!trim($this->getBYWEEKNO())) {
415 return array();
416 }
417 foreach (explode(',', $this->getBYWEEKNO()) as $week_num) {
418 $weeks[] = (int) $week_num;
419 }
420 return $weeks ? $weeks : array();
421 }
422
423
430 public function getBYWEEKNO()
431 {
432 return $this->byweekno;
433 }
434
442 public function setBYMONTH($a_by)
443 {
444 $this->bymonth = $a_by;
445 }
446
453 public function getBYMONTH()
454 {
455 return $this->bymonth;
456 }
457
464 public function getBYMONTHList()
465 {
466 if (!trim($this->getBYMONTH())) {
467 return array();
468 }
469 foreach (explode(',', $this->getBYMONTH()) as $month_num) {
470 $months[] = (int) $month_num;
471 }
472 return $months ? $months : array();
473 }
474
482 public function setBYMONTHDAY($a_by)
483 {
484 $this->bymonthday = $a_by;
485 }
486
493 public function getBYMONTHDAY()
494 {
495 return $this->bymonthday;
496 }
497
503 public function getBYMONTHDAYList()
504 {
505 if (!trim($this->getBYMONTHDAY())) {
506 return array();
507 }
508 foreach (explode(',', $this->getBYMONTHDAY()) as $month_num) {
509 $months[] = (int) $month_num;
510 }
511 return $months ? $months : array();
512 }
513
514
522 public function setBYYEARDAY($a_by)
523 {
524 $this->byyearday = $a_by;
525 }
526
533 public function getBYYEARDAY()
534 {
535 return $this->byyearday;
536 }
537
544 public function getBYYEARDAYList()
545 {
546 if (!trim($this->getBYYEARDAY())) {
547 return array();
548 }
549 foreach (explode(',', $this->getBYYEARDAY()) as $year_day) {
550 $days[] = (int) $year_day;
551 }
552 return $days ? $days : array();
553 }
554
562 public function setBYSETPOS($a_by)
563 {
564 $this->bysetpos = $a_by;
565 }
566
573 public function getBYSETPOS()
574 {
575 return $this->bysetpos;
576 }
577
584 public function getBYSETPOSList()
585 {
586 if (!trim($this->getBYSETPOS())) {
587 return array();
588 }
589 foreach (explode(',', $this->getBYSETPOS()) as $pos) {
590 $positions[] = (int) $pos;
591 }
592 return $positions ? $positions : array();
593 }
594
595
603 public function setWeekstart($a_start)
604 {
605 $this->weekstart = $a_start;
606 }
607
614 public function getWeekstart()
615 {
616 return $this->weekstart;
617 }
618
625 public function getTimeZone()
626 {
627 return $this->timezone;
628 }
629
637 public function setTimeZone($a_tz)
638 {
639 $this->timezone = $a_tz;
640 }
641
646 public function getExclusionDates()
647 {
648 return (array) $this->exclusion_dates;
649 }
650
657 public function validate()
658 {
660 if (!in_array($this->getFrequenceType(), $valid_frequences)) {
661 return false;
662 }
663 if ($this->getFrequenceUntilCount() < 0) {
664 return false;
665 }
666 if ($this->getInterval() <= 0) {
667 return false;
668 }
669 return true;
670 }
671
672
679 public function save()
680 {
681 global $DIC;
682
683 $ilDB = $DIC['ilDB'];
684
685 $until_date = is_null($this->getFrequenceUntilDate()) ?
686 null :
687 $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME, '', 'UTC');
688 $next_id = $ilDB->nextId('cal_recurrence_rules');
689
690 $query = "INSERT INTO cal_recurrence_rules (rule_id,cal_id,cal_recurrence,freq_type,freq_until_date,freq_until_count,intervall, " .
691 "byday,byweekno,bymonth,bymonthday,byyearday,bysetpos,weekstart) " .
692 "VALUES( " .
693 $ilDB->quote($next_id, 'integer') . ", " .
694 $this->db->quote($this->cal_id, 'integer') . ", " .
695 $ilDB->quote(1, 'integer') . ", " .
696 $ilDB->quote((string) $this->getFrequenceType(), 'text') . ", " .
697 $this->db->quote($until_date, 'timestamp') . ", " .
698 $this->db->quote((int) $this->getFrequenceUntilCount(), 'integer') . ", " .
699 $this->db->quote((int) $this->getInterval(), 'integer') . ", " .
700 $this->db->quote((string) $this->getBYDAY(), 'text') . ", " .
701 $this->db->quote((string) $this->getBYWEEKNO(), 'text') . ", " .
702 $this->db->quote((string) $this->getBYMONTH(), 'text') . ", " .
703 $this->db->quote((string) $this->getBYMONTHDAY(), 'text') . ", " .
704 $this->db->quote((string) $this->getBYYEARDAY(), 'text') . ", " .
705 $this->db->quote((string) $this->getBYSETPOS(), 'text') . ", " .
706 $this->db->quote((string) $this->getWeekstart(), 'text') . " " .
707 ")";
708 $res = $ilDB->manipulate($query);
709 $this->recurrence_id = $next_id;
710 return true;
711 }
712
719 public function update()
720 {
721 global $DIC;
722
723 $ilDB = $DIC['ilDB'];
724
725 $until_date = is_null($this->getFrequenceUntilDate()) ?
726 null :
727 $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME, '', 'UTC');
728
729 $query = "UPDATE cal_recurrence_rules SET " .
730 "cal_id = " . $this->db->quote($this->cal_id, 'integer') . ", " .
731 "cal_recurrence = 1," .
732 "freq_type = " . $this->db->quote($this->getFrequenceType(), 'text') . ", " .
733 "freq_until_date = " . $this->db->quote($until_date, 'timestamp') . ", " .
734 "freq_until_count = " . $this->db->quote($this->getFrequenceUntilCount(), 'integer') . ", " .
735 "intervall = " . $this->db->quote($this->getInterval(), 'integer') . ", " .
736 "byday = " . $this->db->quote($this->getBYDAY(), 'text') . ", " .
737 "byweekno = " . $this->db->quote($this->getBYWEEKNO(), 'text') . ", " .
738 "bymonth = " . $this->db->quote($this->getBYMONTH(), 'text') . ", " .
739 "bymonthday = " . $this->db->quote($this->getBYMONTHDAY(), 'text') . ", " .
740 "byyearday = " . $this->db->quote($this->getBYYEARDAY(), 'text') . ", " .
741 "bysetpos = " . $this->db->quote($this->getBYSETPOS(), 'text') . ", " .
742 "weekstart = " . $this->db->quote($this->getWeekstart(), 'text') . " " .
743 "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer') . " ";
744 $res = $ilDB->manipulate($query);
745 return true;
746 }
747
754 public function delete()
755 {
756 global $DIC;
757
758 $ilDB = $DIC['ilDB'];
759
760 $query = "DELETE FROM cal_recurrence_rules " .
761 "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer');
762 $res = $ilDB->manipulate($query);
763 return true;
764 }
765
772 private function read()
773 {
774 global $DIC;
775
776 $ilDB = $DIC['ilDB'];
777
778 $query = "SELECT * FROM cal_recurrence_rules " .
779 "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer') . " ";
780 $res = $this->db->query($query);
781 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
782 $this->cal_id = $row->cal_id;
783 $this->recurrence_type = $row->cal_recurrence;
784 $this->freq_type = $row->freq_type;
785
786 if ($row->freq_until_date != null) {
787 $this->freq_until_date = new ilDateTime($row->freq_until_date, IL_CAL_DATETIME, 'UTC');
788 }
789 $this->freq_until_count = $row->freq_until_count;
790 $this->interval = $row->intervall;
791 $this->byday = $row->byday;
792 $this->byweekno = $row->byweekno;
793 $this->bymonth = $row->bymonth;
794 $this->bymonthday = $row->bymonthday;
795 $this->byyearday = $row->byyearday;
796 $this->bysetpos = $row->bysetpos;
797 $this->weekstart = $row->week_start;
798 }
799
800 $this->exclusion_dates = ilCalendarRecurrenceExclusions::getExclusionDates($this->cal_id);
801 }
802}
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
global $DIC
Definition: goto.php:24
const IL_CAL_FREQ_DAILY
Model of calendar entry recurrcences.
$query
foreach($_POST as $key=> $value) $res
global $ilDB