ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
24 include_once('./Services/Calendar/classes/class.ilDate.php');
25 include_once './Services/Calendar/classes/class.ilCalendarRecurrenceExclusions.php';
26 include_once './Services/Calendar/interfaces/interface.ilCalendarRecurrenceCalculation.php';
27 
38 define('IL_CAL_FREQ_DAILY','DAILY');
39 define('IL_CAL_FREQ_WEEKLY','WEEKLY');
40 define('IL_CAL_FREQ_MONTHLY','MONTHLY');
41 define('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 
56  private $recurrence_id;
57  private $cal_id;
59 
60  private $freq_type = '';
61  private $freq_until_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 $ilDB;
88 
89  $this->db = $ilDB;
90  $this->recurrence_id = $a_rec_id;
91  if($a_rec_id)
92  {
93  $this->read();
94  }
95  }
96 
105  public static function _delete($a_cal_id)
106  {
107  global $ilDB;
108 
109  $query = "DELETE FROM cal_recurrence_rules ".
110  "WHERE cal_id = ".$ilDB->quote($a_cal_id ,'integer')." ";
111  $res = $ilDB->manipulate($query);
112 
114  }
115 
121  public function toICal($a_user_id)
122  {
123  include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
124  $entry = new ilCalendarEntry($this->getEntryId());
125 
126  $ical = 'RRULE:';
127  $ical .= ('FREQ='.$this->getFrequenceType());
128 
129  if($this->getInterval())
130  {
131  $ical .= (';INTERVAL='.$this->getInterval());
132  }
133  if($this->getFrequenceUntilCount())
134  {
135  $ical .= (';COUNT='.$this->getFrequenceUntilCount());
136  }
137  elseif($this->getFrequenceUntilDate())
138  {
139  if($entry->isFullday())
140  {
141  $ical .= (';UNTIL='.$this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE,'Ymd'));
142  }
143  else
144  {
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  {
151  $ical .= (';BYMONTH='.$this->getBYMONTH());
152  }
153  if($this->getBYWEEKNO())
154  {
155  $ical .= (';BYWEEKNO='.$this->getBYWEEKNO());
156  }
157  if($this->getBYYEARDAY())
158  {
159  $ical .= (';BYYEARDAY='.$this->getBYYEARDAY());
160  }
161  if($this->getBYMONTHDAY())
162  {
163  $ical .= (';BYMONTHDAY='.$this->getBYMONTHDAY());
164  }
165  if($this->getBYDAY())
166  {
167  $ical .= (';BYDAY='.$this->getBYDAY());
168  }
169  if($this->getBYSETPOS())
170  {
171  $ical .= (';BYSETPOS='.$this->getBYSETPOS());
172  }
173 
174  // Required in outlook
175  if($this->getBYDAY())
176  {
177  include_once './Services/Calendar/classes/class.ilCalendarUserSettings.php';
178  include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
180  if($us->getWeekStart() == ilCalendarSettings::WEEK_START_MONDAY)
181  {
182  $ical .= (';WKST=MO');
183  }
184  else
185  {
186  $ical .= (';WKST=SU');
187  }
188  }
189 
190  return $ical;
191  }
192 
193 
200  public function reset()
201  {
202  $this->setBYDAY('');
203  $this->setBYMONTHDAY('');
204  $this->setBYMONTH('');
205  $this->setBYSETPOS('');
206  $this->setBYWEEKNO('');
207  $this->setBYYEARDAY('');
208  $this->setFrequenceType('');
209  $this->setInterval(1);
210  $this->setFrequenceUntilCount(0);
211 
212  return true;
213  }
214 
221  public function getRecurrenceId()
222  {
223  return $this->recurrence_id;
224  }
225 
226 
234  public function setEntryId($a_id)
235  {
236  $this->cal_id = $a_id;
237  }
238 
243  public function getEntryId()
244  {
245  return $this->cal_id;
246  }
247 
255  public function setRecurrence($a_type)
256  {
257  $this->recurrence_type = $a_type;
258  }
259 
267  public function isRecurrence()
268  {
269  return $this->recurrence_type == self::REC_RECURRENCE;
270  }
271 
279  public function setFrequenceType($a_type)
280  {
281  $this->freq_type = $a_type;
282  }
283 
290  public function getFrequenceType()
291  {
292  return $this->freq_type;
293  }
294 
301  public function getFrequenceUntilDate()
302  {
303  return is_object($this->freq_until_date) ? $this->freq_until_date : null;
304  }
305 
312  public function setFrequenceUntilDate(ilDateTime $a_date = null)
313  {
314  $this->freq_until_date = $a_date;
315  }
316 
324  public function setFrequenceUntilCount($a_count)
325  {
326  $this->freq_until_count = $a_count;
327  }
328 
336  public function getFrequenceUntilCount()
337  {
339  }
340 
348  public function setInterval($a_interval)
349  {
350  $this->interval = $a_interval;
351  }
352 
359  public function getInterval()
360  {
361  return $this->interval ? $this->interval : 1;
362  }
363 
371  public function setBYDAY($a_byday)
372  {
373  $this->byday = $a_byday;
374  }
375 
382  public function getBYDAY()
383  {
384  return $this->byday;
385  }
386 
393  public function getBYDAYList()
394  {
395  if(!trim($this->getBYDAY()))
396  {
397  return array();
398  }
399  foreach(explode(',',$this->getBYDAY()) as $byday)
400  {
401  $bydays[] = trim($byday);
402  }
403  return $bydays ? $bydays : array();
404  }
405 
413  public function setBYWEEKNO($a_byweekno)
414  {
415  $this->byweekno = $a_byweekno;
416  }
417 
424  public function getBYWEEKNOList()
425  {
426  if(!trim($this->getBYWEEKNO()))
427  {
428  return array();
429  }
430  foreach(explode(',',$this->getBYWEEKNO()) as $week_num)
431  {
432  $weeks[] = (int) $week_num;
433  }
434  return $weeks ? $weeks : array();
435  }
436 
437 
444  public function getBYWEEKNO()
445  {
446  return $this->byweekno;
447  }
448 
456  public function setBYMONTH($a_by)
457  {
458  $this->bymonth = $a_by;
459  }
460 
467  public function getBYMONTH()
468  {
469  return $this->bymonth;
470  }
471 
478  public function getBYMONTHList()
479  {
480  if(!trim($this->getBYMONTH()))
481  {
482  return array();
483  }
484  foreach(explode(',',$this->getBYMONTH()) as $month_num)
485  {
486  $months[] = (int) $month_num;
487  }
488  return $months ? $months : array();
489  }
490 
498  public function setBYMONTHDAY($a_by)
499  {
500  $this->bymonthday = $a_by;
501  }
502 
509  public function getBYMONTHDAY()
510  {
511  return $this->bymonthday;
512  }
513 
519  public function getBYMONTHDAYList()
520  {
521  if(!trim($this->getBYMONTHDAY()))
522  {
523  return array();
524  }
525  foreach(explode(',',$this->getBYMONTHDAY()) as $month_num)
526  {
527  $months[] = (int) $month_num;
528  }
529  return $months ? $months : array();
530 
531  }
532 
533 
541  public function setBYYEARDAY($a_by)
542  {
543  $this->byyearday = $a_by;
544  }
545 
552  public function getBYYEARDAY()
553  {
554  return $this->byyearday;
555  }
556 
563  public function getBYYEARDAYList()
564  {
565  if(!trim($this->getBYYEARDAY()))
566  {
567  return array();
568  }
569  foreach(explode(',',$this->getBYYEARDAY()) as $year_day)
570  {
571  $days[] = (int) $year_day;
572  }
573  return $days ? $days : array();
574  }
575 
583  public function setBYSETPOS($a_by)
584  {
585  $this->bysetpos = $a_by;
586  }
587 
594  public function getBYSETPOS()
595  {
596  return $this->bysetpos;
597  }
598 
605  public function getBYSETPOSList()
606  {
607  if(!trim($this->getBYSETPOS()))
608  {
609  return array();
610  }
611  foreach(explode(',',$this->getBYSETPOS()) as $pos)
612  {
613  $positions[] = (int) $pos;
614  }
615  return $positions ? $positions : array();
616  }
617 
618 
626  public function setWeekstart($a_start)
627  {
628  $this->weekstart = $a_start;
629  }
630 
637  public function getWeekstart()
638  {
639  return $this->weekstart;
640  }
641 
648  public function getTimeZone()
649  {
650  return $this->timezone;
651  }
652 
660  public function setTimeZone($a_tz)
661  {
662  $this->timezone = $a_tz;
663  }
664 
669  public function getExclusionDates()
670  {
671  return (array) $this->exclusion_dates;
672  }
673 
680  public function validate()
681  {
683  if(!in_array($this->getFrequenceType(),$valid_frequences))
684  {
685  return false;
686  }
687  if($this->getFrequenceUntilCount() < 0)
688  {
689  return false;
690  }
691  if($this->getInterval() <= 0)
692  {
693  return false;
694  }
695  return true;
696  }
697 
698 
705  public function save()
706  {
707  global $ilDB;
708 
709  $until_date = is_null($this->getFrequenceUntilDate()) ?
710  null :
711  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
712  $next_id = $ilDB->nextId('cal_recurrence_rules');
713 
714  $query = "INSERT INTO cal_recurrence_rules (rule_id,cal_id,cal_recurrence,freq_type,freq_until_date,freq_until_count,intervall, ".
715  "byday,byweekno,bymonth,bymonthday,byyearday,bysetpos,weekstart) ".
716  "VALUES( ".
717  $ilDB->quote($next_id,'integer').", ".
718  $this->db->quote($this->cal_id ,'integer').", ".
719  $ilDB->quote(1,'integer').", ".
720  $ilDB->quote((string) $this->getFrequenceType() ,'text').", ".
721  $this->db->quote($until_date,'timestamp').", ".
722  $this->db->quote((int) $this->getFrequenceUntilCount() ,'integer').", ".
723  $this->db->quote((int) $this->getInterval() ,'integer').", ".
724  $this->db->quote((string) $this->getBYDAY() ,'text').", ".
725  $this->db->quote((string) $this->getBYWEEKNO() ,'text').", ".
726  $this->db->quote((string) $this->getBYMONTH() ,'text').", ".
727  $this->db->quote((string) $this->getBYMONTHDAY() ,'text').", ".
728  $this->db->quote((string) $this->getBYYEARDAY() ,'text').", ".
729  $this->db->quote((string) $this->getBYSETPOS() ,'text').", ".
730  $this->db->quote((string) $this->getWeekstart() ,'text')." ".
731  ")";
732  $res = $ilDB->manipulate($query);
733  $this->recurrence_id = $next_id;
734  return true;
735  }
736 
743  public function update()
744  {
745  global $ilDB;
746 
747  $until_date = is_null($this->getFrequenceUntilDate()) ?
748  null :
749  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
750 
751  $query = "UPDATE cal_recurrence_rules SET ".
752  "cal_id = ".$this->db->quote($this->cal_id ,'integer').", ".
753  "cal_recurrence = 1,".
754  "freq_type = ".$this->db->quote($this->getFrequenceType() ,'text').", ".
755  "freq_until_date = ".$this->db->quote($until_date ,'timestamp').", ".
756  "freq_until_count = ".$this->db->quote($this->getFrequenceUntilCount() ,'integer').", ".
757  "intervall = ".$this->db->quote($this->getInterval() ,'integer').", ".
758  "byday = ".$this->db->quote($this->getBYDAY() ,'text').", ".
759  "byweekno = ".$this->db->quote($this->getBYWEEKNO() ,'text').", ".
760  "bymonth = ".$this->db->quote($this->getBYMONTH() ,'text').", ".
761  "bymonthday = ".$this->db->quote($this->getBYMONTHDAY() ,'text').", ".
762  "byyearday = ".$this->db->quote($this->getBYYEARDAY() ,'text').", ".
763  "bysetpos = ".$this->db->quote($this->getBYSETPOS() ,'text').", ".
764  "weekstart = ".$this->db->quote($this->getWeekstart() ,'text')." ".
765  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer')." ";
766  $res = $ilDB->manipulate($query);
767  return true;
768  }
769 
776  public function delete()
777  {
778  global $ilDB;
779 
780  $query = "DELETE FROM cal_recurrence_rules ".
781  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer');
782  $res = $ilDB->manipulate($query);
783  return true;
784  }
785 
792  private function read()
793  {
794  global $ilDB;
795 
796  $query = "SELECT * FROM cal_recurrence_rules ".
797  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer')." ";
798  $res = $this->db->query($query);
799  while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
800  {
801  $this->cal_id = $row->cal_id;
802  $this->recurrence_type = $row->cal_recurrence;
803  $this->freq_type = $row->freq_type;
804 
805  if($row->freq_until_date != null)
806  {
807  $this->freq_until_date = new ilDateTime($row->freq_until_date,IL_CAL_DATETIME,'UTC');
808  }
809  $this->freq_until_count = $row->freq_until_count;
810  $this->interval = $row->intervall;
811  $this->byday = $row->byday;
812  $this->byweekno = $row->byweekno;
813  $this->bymonth = $row->bymonth;
814  $this->bymonthday = $row->bymonthday;
815  $this->byyearday = $row->byyearday;
816  $this->bysetpos = $row->bysetpos;
817  $this->weekstart = $row->week_start;
818  }
819 
820  $this->exclusion_dates = ilCalendarRecurrenceExclusions::getExclusionDates($this->cal_id);
821  }
822 }
823 
824 
825 ?>
Set timezone
const IL_CAL_FREQ_MONTHLY
Model for a calendar entry.
const IL_CAL_DATETIME
getEntryId()
Get calendar entry id.
setBYWEEKNO($a_byweekno)
set by day
setFrequenceUntilCount($a_count)
set frequence count
static _getInstanceByUserId($a_user_id)
get singleton instance
setFrequenceUntilDate(ilDateTime $a_date=null)
set freq until date
getBYYEARDAYList()
get BYYEARDAY list
setFrequenceType($a_type)
set frequence type
setWeekstart($a_start)
set weekstart
getBYMONTHDAYList()
get BYMONTHDAY list
getExclusionDates()
Get exclusion dates.
$a_type
Definition: workflow.php:93
toICal($a_user_id)
Get ical presentation for calendar recurrence.
static delete($a_cal_id)
Delete exclusion dates of calendar entry.
setInterval($a_interval)
set interval
const IL_CAL_FREQ_DAILY
Model of calendar entry recurrcences.
getBYSETPOSList()
get bysetpos list
const IL_CAL_FREQ_YEARLY
getBYWEEKNOList()
get byweekno list
const IL_CAL_FKT_DATE
Date and time handling
__construct($a_rec_id=0)
Constructor.
Create styles array
The data for the language used.
static _delete($a_cal_id)
delete
getRecurrenceId()
get recurrence id
static getExclusionDates($a_cal_id)
Read exclusion dates.
setRecurrence($a_type)
set type of recurrence
global $ilDB
getFrequenceUntilCount()
get frequence until count
const IL_CAL_FREQ_WEEKLY