ILIAS  release_4-3 Revision
 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 = 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 
116  public function toICal($a_user_id)
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 
158  // Required in outlook
159  if($this->getBYDAY())
160  {
161  include_once './Services/Calendar/classes/class.ilCalendarUserSettings.php';
162  include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
164  if($us->getWeekStart() == ilCalendarSettings::WEEK_START_MONDAY)
165  {
166  $ical .= (';WKST=MO');
167  }
168  else
169  {
170  $ical .= (';WKST=SU');
171  }
172  }
173 
174  return $ical;
175  }
176 
177 
184  public function reset()
185  {
186  $this->setBYDAY('');
187  $this->setBYMONTHDAY('');
188  $this->setBYMONTH('');
189  $this->setBYSETPOS('');
190  $this->setBYWEEKNO('');
191  $this->setBYYEARDAY('');
192  $this->setFrequenceType('');
193  $this->setInterval(1);
194  $this->setFrequenceUntilCount(0);
195 
196  return true;
197  }
198 
205  public function getRecurrenceId()
206  {
207  return $this->recurrence_id;
208  }
209 
210 
218  public function setEntryId($a_id)
219  {
220  $this->cal_id = $a_id;
221  }
222 
230  public function setRecurrence($a_type)
231  {
232  $this->recurrence_type = $a_type;
233  }
234 
242  public function isRecurrence()
243  {
244  return $this->recurrence_type == self::REC_RECURRENCE;
245  }
246 
254  public function setFrequenceType($a_type)
255  {
256  $this->freq_type = $a_type;
257  }
258 
265  public function getFrequenceType()
266  {
267  return $this->freq_type;
268  }
269 
276  public function getFrequenceUntilDate()
277  {
278  return is_object($this->freq_until_date) ? $this->freq_until_date : null;
279  }
280 
287  public function setFrequenceUntilDate(ilDateTime $a_date = null)
288  {
289  $this->freq_until_date = $a_date;
290  }
291 
299  public function setFrequenceUntilCount($a_count)
300  {
301  $this->freq_until_count = $a_count;
302  }
303 
311  public function getFrequenceUntilCount()
312  {
314  }
315 
323  public function setInterval($a_interval)
324  {
325  $this->interval = $a_interval;
326  }
327 
334  public function getInterval()
335  {
336  return $this->interval ? $this->interval : 1;
337  }
338 
346  public function setBYDAY($a_byday)
347  {
348  $this->byday = $a_byday;
349  }
350 
357  public function getBYDAY()
358  {
359  return $this->byday;
360  }
361 
368  public function getBYDAYList()
369  {
370  if(!trim($this->getBYDAY()))
371  {
372  return array();
373  }
374  foreach(explode(',',$this->getBYDAY()) as $byday)
375  {
376  $bydays[] = trim($byday);
377  }
378  return $bydays ? $bydays : array();
379  }
380 
388  public function setBYWEEKNO($a_byweekno)
389  {
390  $this->byweekno = $a_byweekno;
391  }
392 
399  public function getBYWEEKNOList()
400  {
401  if(!trim($this->getBYWEEKNO()))
402  {
403  return array();
404  }
405  foreach(explode(',',$this->getBYWEEKNO()) as $week_num)
406  {
407  $weeks[] = (int) $week_num;
408  }
409  return $weeks ? $weeks : array();
410  }
411 
412 
419  public function getBYWEEKNO()
420  {
421  return $this->byweekno;
422  }
423 
431  public function setBYMONTH($a_by)
432  {
433  $this->bymonth = $a_by;
434  }
435 
442  public function getBYMONTH()
443  {
444  return $this->bymonth;
445  }
446 
453  public function getBYMONTHList()
454  {
455  if(!trim($this->getBYMONTH()))
456  {
457  return array();
458  }
459  foreach(explode(',',$this->getBYMONTH()) as $month_num)
460  {
461  $months[] = (int) $month_num;
462  }
463  return $months ? $months : array();
464  }
465 
473  public function setBYMONTHDAY($a_by)
474  {
475  $this->bymonthday = $a_by;
476  }
477 
484  public function getBYMONTHDAY()
485  {
486  return $this->bymonthday;
487  }
488 
494  public function getBYMONTHDAYList()
495  {
496  if(!trim($this->getBYMONTHDAY()))
497  {
498  return array();
499  }
500  foreach(explode(',',$this->getBYMONTHDAY()) as $month_num)
501  {
502  $months[] = (int) $month_num;
503  }
504  return $months ? $months : array();
505 
506  }
507 
508 
516  public function setBYYEARDAY($a_by)
517  {
518  $this->byyearday = $a_by;
519  }
520 
527  public function getBYYEARDAY()
528  {
529  return $this->byyearday;
530  }
531 
538  public function getBYYEARDAYList()
539  {
540  if(!trim($this->getBYYEARDAY()))
541  {
542  return array();
543  }
544  foreach(explode(',',$this->getBYYEARDAY()) as $year_day)
545  {
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  {
584  return array();
585  }
586  foreach(explode(',',$this->getBYSETPOS()) as $pos)
587  {
588  $positions[] = (int) $pos;
589  }
590  return $positions ? $positions : array();
591  }
592 
593 
601  public function setWeekstart($a_start)
602  {
603  $this->weekstart = $a_start;
604  }
605 
612  public function getWeekstart()
613  {
614  return $this->weekstart;
615  }
616 
623  public function getTimeZone()
624  {
625  return $this->timezone;
626  }
627 
635  public function setTimeZone($a_tz)
636  {
637  $this->timezone = $a_tz;
638  }
639 
644  public function getExclusionDates()
645  {
646  return (array) $this->exclusion_dates;
647  }
648 
655  public function validate()
656  {
658  if(!in_array($this->getFrequenceType(),$valid_frequences))
659  {
660  return false;
661  }
662  if($this->getFrequenceUntilCount() < 0)
663  {
664  return false;
665  }
666  if($this->getInterval() <= 0)
667  {
668  return false;
669  }
670  return true;
671  }
672 
673 
680  public function save()
681  {
682  global $ilDB;
683 
684  $until_date = is_null($this->getFrequenceUntilDate()) ?
685  null :
686  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
687  $next_id = $ilDB->nextId('cal_recurrence_rules');
688 
689  $query = "INSERT INTO cal_recurrence_rules (rule_id,cal_id,cal_recurrence,freq_type,freq_until_date,freq_until_count,intervall, ".
690  "byday,byweekno,bymonth,bymonthday,byyearday,bysetpos,weekstart) ".
691  "VALUES( ".
692  $ilDB->quote($next_id,'integer').", ".
693  $this->db->quote($this->cal_id ,'integer').", ".
694  $ilDB->quote(1,'integer').", ".
695  $ilDB->quote((string) $this->getFrequenceType() ,'text').", ".
696  $this->db->quote($until_date,'timestamp').", ".
697  $this->db->quote((int) $this->getFrequenceUntilCount() ,'integer').", ".
698  $this->db->quote((int) $this->getInterval() ,'integer').", ".
699  $this->db->quote((string) $this->getBYDAY() ,'text').", ".
700  $this->db->quote((string) $this->getBYWEEKNO() ,'text').", ".
701  $this->db->quote((string) $this->getBYMONTH() ,'text').", ".
702  $this->db->quote((string) $this->getBYMONTHDAY() ,'text').", ".
703  $this->db->quote((string) $this->getBYYEARDAY() ,'text').", ".
704  $this->db->quote((string) $this->getBYSETPOS() ,'text').", ".
705  $this->db->quote((string) $this->getWeekstart() ,'text')." ".
706  ")";
707  $res = $ilDB->manipulate($query);
708  $this->recurrence_id = $next_id;
709  return true;
710  }
711 
718  public function update()
719  {
720  global $ilDB;
721 
722  $until_date = is_null($this->getFrequenceUntilDate()) ?
723  null :
724  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
725 
726  $query = "UPDATE cal_recurrence_rules SET ".
727  "cal_id = ".$this->db->quote($this->cal_id ,'integer').", ".
728  "cal_recurrence = 1,".
729  "freq_type = ".$this->db->quote($this->getFrequenceType() ,'text').", ".
730  "freq_until_date = ".$this->db->quote($until_date ,'timestamp').", ".
731  "freq_until_count = ".$this->db->quote($this->getFrequenceUntilCount() ,'integer').", ".
732  "intervall = ".$this->db->quote($this->getInterval() ,'integer').", ".
733  "byday = ".$this->db->quote($this->getBYDAY() ,'text').", ".
734  "byweekno = ".$this->db->quote($this->getBYWEEKNO() ,'text').", ".
735  "bymonth = ".$this->db->quote($this->getBYMONTH() ,'text').", ".
736  "bymonthday = ".$this->db->quote($this->getBYMONTHDAY() ,'text').", ".
737  "byyearday = ".$this->db->quote($this->getBYYEARDAY() ,'text').", ".
738  "bysetpos = ".$this->db->quote($this->getBYSETPOS() ,'text').", ".
739  "weekstart = ".$this->db->quote($this->getWeekstart() ,'text')." ".
740  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer')." ";
741  $res = $ilDB->manipulate($query);
742  return true;
743  }
744 
751  public function delete()
752  {
753  global $ilDB;
754 
755  $query = "DELETE FROM cal_recurrence_rules ".
756  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer');
757  $res = $ilDB->manipulate($query);
758  return true;
759  }
760 
767  private function read()
768  {
769  global $ilDB;
770 
771  $query = "SELECT * FROM cal_recurrence_rules ".
772  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer')." ";
773  $res = $this->db->query($query);
774  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
775  {
776  $this->cal_id = $row->cal_id;
777  $this->recurrence_type = $row->cal_recurrence;
778  $this->freq_type = $row->freq_type;
779 
780  if($row->freq_until_date != null)
781  {
782  $this->freq_until_date = new ilDateTime($row->freq_until_date,IL_CAL_DATETIME,'UTC');
783  }
784  $this->freq_until_count = $row->freq_until_count;
785  $this->interval = $row->intervall;
786  $this->byday = $row->byday;
787  $this->byweekno = $row->byweekno;
788  $this->bymonth = $row->bymonth;
789  $this->bymonthday = $row->bymonthday;
790  $this->byyearday = $row->byyearday;
791  $this->bysetpos = $row->bysetpos;
792  $this->weekstart = $row->week_start;
793  }
794 
795  $this->exclusion_dates = ilCalendarRecurrenceExclusions::getExclusionDates($this->cal_id);
796  }
797 }
798 
799 
800 ?>