ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
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;
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 
116  public function toICal()
117  {
118  $ical = 'RRULE:';
119  $ical .= ('FREQ='.$this->getFrequenceType());
120 
121  if($this->getInterval())
122  {
123  $ical .= (';INTERVAL='.$this->getInterval());
124  }
125  if($this->getFrequenceUntilCount())
126  {
127  $ical .= (';COUNT='.$this->getFrequenceUntilCount());
128  }
129  elseif($this->getFrequenceUntilDate())
130  {
131  $ical .= (';UNTIL='.$this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE,'Ymd'));
132  }
133  if($this->getBYMONTH())
134  {
135  $ical .= (';BYMONTH='.$this->getBYMONTH());
136  }
137  if($this->getBYWEEKNO())
138  {
139  $ical .= (';BYWEEKNO='.$this->getBYWEEKNO());
140  }
141  if($this->getBYYEARDAY())
142  {
143  $ical .= (';BYYEARDAY='.$this->getBYYEARDAY());
144  }
145  if($this->getBYMONTHDAY())
146  {
147  $ical .= (';BYMONTHDAY='.$this->getBYMONTHDAY());
148  }
149  if($this->getBYDAY())
150  {
151  $ical .= (';BYDAY='.$this->getBYDAY());
152  }
153  if($this->getBYSETPOS())
154  {
155  $ical .= (';BYSETPOS='.$this->getBYSETPOS());
156  }
157  return $ical;
158  }
159 
160 
167  public function reset()
168  {
169  $this->setBYDAY('');
170  $this->setBYMONTHDAY('');
171  $this->setBYMONTH('');
172  $this->setBYSETPOS('');
173  $this->setBYWEEKNO('');
174  $this->setBYYEARDAY('');
175  $this->setFrequenceType('');
176  $this->setInterval(1);
177  $this->setFrequenceUntilCount(0);
178 
179  return true;
180  }
181 
188  public function getRecurrenceId()
189  {
190  return $this->recurrence_id;
191  }
192 
193 
201  public function setEntryId($a_id)
202  {
203  $this->cal_id = $a_id;
204  }
205 
213  public function setRecurrence($a_type)
214  {
215  $this->recurrence_type = $a_type;
216  }
217 
225  public function isRecurrence()
226  {
227  return $this->recurrence_type == self::REC_RECURRENCE;
228  }
229 
237  public function setFrequenceType($a_type)
238  {
239  $this->freq_type = $a_type;
240  }
241 
248  public function getFrequenceType()
249  {
250  return $this->freq_type;
251  }
252 
259  public function getFrequenceUntilDate()
260  {
261  return is_object($this->freq_until_date) ? $this->freq_until_date : null;
262  }
263 
270  public function setFrequenceUntilDate(ilDateTime $a_date = null)
271  {
272  $this->freq_until_date = $a_date;
273  }
274 
282  public function setFrequenceUntilCount($a_count)
283  {
284  $this->freq_until_count = $a_count;
285  }
286 
294  public function getFrequenceUntilCount()
295  {
297  }
298 
306  public function setInterval($a_interval)
307  {
308  $this->interval = $a_interval;
309  }
310 
317  public function getInterval()
318  {
319  return $this->interval ? $this->interval : 1;
320  }
321 
329  public function setBYDAY($a_byday)
330  {
331  $this->byday = $a_byday;
332  }
333 
340  public function getBYDAY()
341  {
342  return $this->byday;
343  }
344 
351  public function getBYDAYList()
352  {
353  if(!trim($this->getBYDAY()))
354  {
355  return array();
356  }
357  foreach(explode(',',$this->getBYDAY()) as $byday)
358  {
359  $bydays[] = trim($byday);
360  }
361  return $bydays ? $bydays : array();
362  }
363 
371  public function setBYWEEKNO($a_byweekno)
372  {
373  $this->byweekno = $a_byweekno;
374  }
375 
382  public function getBYWEEKNOList()
383  {
384  if(!trim($this->getBYWEEKNO()))
385  {
386  return array();
387  }
388  foreach(explode(',',$this->getBYWEEKNO()) as $week_num)
389  {
390  $weeks[] = (int) $week_num;
391  }
392  return $weeks ? $weeks : array();
393  }
394 
395 
402  public function getBYWEEKNO()
403  {
404  return $this->byweekno;
405  }
406 
414  public function setBYMONTH($a_by)
415  {
416  $this->bymonth = $a_by;
417  }
418 
425  public function getBYMONTH()
426  {
427  return $this->bymonth;
428  }
429 
436  public function getBYMONTHList()
437  {
438  if(!trim($this->getBYMONTH()))
439  {
440  return array();
441  }
442  foreach(explode(',',$this->getBYMONTH()) as $month_num)
443  {
444  $months[] = (int) $month_num;
445  }
446  return $months ? $months : array();
447  }
448 
456  public function setBYMONTHDAY($a_by)
457  {
458  $this->bymonthday = $a_by;
459  }
460 
467  public function getBYMONTHDAY()
468  {
469  return $this->bymonthday;
470  }
471 
477  public function getBYMONTHDAYList()
478  {
479  if(!trim($this->getBYMONTHDAY()))
480  {
481  return array();
482  }
483  foreach(explode(',',$this->getBYMONTHDAY()) as $month_num)
484  {
485  $months[] = (int) $month_num;
486  }
487  return $months ? $months : array();
488 
489  }
490 
491 
499  public function setBYYEARDAY($a_by)
500  {
501  $this->byyearday = $a_by;
502  }
503 
510  public function getBYYEARDAY()
511  {
512  return $this->byyearday;
513  }
514 
521  public function getBYYEARDAYList()
522  {
523  if(!trim($this->getBYYEARDAY()))
524  {
525  return array();
526  }
527  foreach(explode(',',$this->getBYYEARDAY()) as $year_day)
528  {
529  $days[] = (int) $year_day;
530  }
531  return $days ? $days : array();
532  }
533 
541  public function setBYSETPOS($a_by)
542  {
543  $this->bysetpos = $a_by;
544  }
545 
552  public function getBYSETPOS()
553  {
554  return $this->bysetpos;
555  }
556 
563  public function getBYSETPOSList()
564  {
565  if(!trim($this->getBYSETPOS()))
566  {
567  return array();
568  }
569  foreach(explode(',',$this->getBYSETPOS()) as $pos)
570  {
571  $positions[] = (int) $pos;
572  }
573  return $positions ? $positions : array();
574  }
575 
576 
584  public function setWeekstart($a_start)
585  {
586  $this->weekstart = $a_start;
587  }
588 
595  public function getWeekstart()
596  {
597  return $this->weekstart;
598  }
599 
606  public function getTimeZone()
607  {
608  return $this->timezone;
609  }
610 
618  public function setTimeZone($a_tz)
619  {
620  $this->timezone = $a_tz;
621  }
622 
627  public function getExclusionDates()
628  {
629  return (array) $this->exclusion_dates;
630  }
631 
638  public function validate()
639  {
641  if(!in_array($this->getFrequenceType(),$valid_frequences))
642  {
643  return false;
644  }
645  if($this->getFrequenceUntilCount() < 0)
646  {
647  return false;
648  }
649  if($this->getInterval() <= 0)
650  {
651  return false;
652  }
653  return true;
654  }
655 
656 
663  public function save()
664  {
665  global $ilDB;
666 
667  $until_date = is_null($this->getFrequenceUntilDate()) ?
668  null :
669  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
670  $next_id = $ilDB->nextId('cal_recurrence_rules');
671 
672  $query = "INSERT INTO cal_recurrence_rules (rule_id,cal_id,cal_recurrence,freq_type,freq_until_date,freq_until_count,intervall, ".
673  "byday,byweekno,bymonth,bymonthday,byyearday,bysetpos,weekstart) ".
674  "VALUES( ".
675  $ilDB->quote($next_id,'integer').", ".
676  $this->db->quote($this->cal_id ,'integer').", ".
677  $ilDB->quote(1,'integer').", ".
678  $ilDB->quote($this->getFrequenceType() ,'text').", ".
679  $this->db->quote($until_date,'timestamp').", ".
680  $this->db->quote($this->getFrequenceUntilCount() ,'integer').", ".
681  $this->db->quote($this->getInterval() ,'integer').", ".
682  $this->db->quote($this->getBYDAY() ,'text').", ".
683  $this->db->quote($this->getBYWEEKNO() ,'text').", ".
684  $this->db->quote($this->getBYMONTH() ,'text').", ".
685  $this->db->quote($this->getBYMONTHDAY() ,'text').", ".
686  $this->db->quote($this->getBYYEARDAY() ,'text').", ".
687  $this->db->quote($this->getBYSETPOS() ,'text').", ".
688  $this->db->quote($this->getWeekstart() ,'text')." ".
689  ")";
690  $res = $ilDB->manipulate($query);
691  $this->recurrence_id = $next_id;
692  return true;
693  }
694 
701  public function update()
702  {
703  global $ilDB;
704 
705  $until_date = is_null($this->getFrequenceUntilDate()) ?
706  null :
707  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
708 
709  $query = "UPDATE cal_recurrence_rules SET ".
710  "cal_id = ".$this->db->quote($this->cal_id ,'integer').", ".
711  "cal_recurrence = 1,".
712  "freq_type = ".$this->db->quote($this->getFrequenceType() ,'text').", ".
713  "freq_until_date = ".$this->db->quote($until_date ,'timestamp').", ".
714  "freq_until_count = ".$this->db->quote($this->getFrequenceUntilCount() ,'integer').", ".
715  "intervall = ".$this->db->quote($this->getInterval() ,'integer').", ".
716  "byday = ".$this->db->quote($this->getBYDAY() ,'text').", ".
717  "byweekno = ".$this->db->quote($this->getBYWEEKNO() ,'text').", ".
718  "bymonth = ".$this->db->quote($this->getBYMONTH() ,'text').", ".
719  "bymonthday = ".$this->db->quote($this->getBYMONTHDAY() ,'text').", ".
720  "byyearday = ".$this->db->quote($this->getBYYEARDAY() ,'text').", ".
721  "bysetpos = ".$this->db->quote($this->getBYSETPOS() ,'text').", ".
722  "weekstart = ".$this->db->quote($this->getWeekstart() ,'text')." ".
723  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer')." ";
724  $res = $ilDB->manipulate($query);
725  return true;
726  }
727 
734  public function delete()
735  {
736  global $ilDB;
737 
738  $query = "DELETE FROM cal_recurrence_rules ".
739  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer');
740  $res = $ilDB->manipulate($query);
741  return true;
742  }
743 
750  private function read()
751  {
752  global $ilDB;
753 
754  $query = "SELECT * FROM cal_recurrence_rules ".
755  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer')." ";
756  $res = $this->db->query($query);
757  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
758  {
759  $this->cal_id = $row->cal_id;
760  $this->recurrence_type = $row->cal_recurrence;
761  $this->freq_type = $row->freq_type;
762 
763  if($row->freq_until_date != null)
764  {
765  $this->freq_until_date = new ilDateTime($row->freq_until_date,IL_CAL_DATETIME,'UTC');
766  }
767  $this->freq_until_count = $row->freq_until_count;
768  $this->interval = $row->intervall;
769  $this->byday = $row->byday;
770  $this->byweekno = $row->byweekno;
771  $this->bymonth = $row->bymonth;
772  $this->bymonthday = $row->bymonthday;
773  $this->byyearday = $row->byyearday;
774  $this->bysetpos = $row->bysetpos;
775  $this->weekstart = $row->week_start;
776  }
777 
778  $this->exclusion_dates = ilCalendarRecurrenceExclusions::getExclusionDates($this->cal_id);
779  }
780 }
781 
782 
783 ?>