ILIAS  Release_4_0_x_branch Revision 61816
 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 
36 define('IL_CAL_FREQ_DAILY',ilCalendarRecurrence::FREQ_DAILY);
37 define('IL_CAL_FREQ_WEEKLY',ilCalendarRecurrence::FREQ_WEEKLY);
38 define('IL_CAL_FREQ_MONTHLY',ilCalendarRecurrence::FREQ_MONTHLY);
39 define('IL_CAL_FREQ_YEARLY',ilCalendarRecurrence::FREQ_YEARLY);
40 
42 {
43  const REC_RECURRENCE = 0;
44  const REC_EXCLUSION = 1;
45 
46  const FREQ_DAILY = 'DAILY';
47  const FREQ_WEEKLY = 'WEEKLY';
48  const FREQ_MONTHLY = 'MONTHLY';
49  const FREQ_YEARLY = 'YEARLY';
50 
51 
52  protected $db;
53 
54  private $recurrence_id;
55  private $cal_id;
57 
58  private $freq_type;
59  private $freq_until_type;
60  private $freq_until_date = null;
62 
63  private $interval;
64  private $byday;
65  private $byweekno;
66  private $bymonth;
67  private $bymonthday;
68  private $byyearday;
69  private $bysetpos;
70  private $weekstart;
71 
72  private $timezone = 'Europe/Berlin';
73 
81  public function __construct($a_rec_id = 0)
82  {
83  global $ilDB;
84 
85  $this->db = $ilDB;
86  $this->recurrence_id = $a_rec_id;
87  if($a_rec_id)
88  {
89  $this->read();
90  }
91  }
92 
101  public static function _delete($a_cal_id)
102  {
103  global $ilDB;
104 
105  $query = "DELETE FROM cal_recurrence_rules ".
106  "WHERE cal_id = ".$ilDB->quote($a_cal_id ,'integer')." ";
107  $res = $ilDB->manipulate($query);
108  }
109 
110  public function toICal()
111  {
112  $ical = 'RRULE:';
113  $ical .= ('FREQ='.$this->getFrequenceType());
114 
115  if($this->getInterval())
116  {
117  $ical .= (';INTERVAL='.$this->getInterval());
118  }
119  if($this->getFrequenceUntilCount())
120  {
121  $ical .= (';COUNT='.$this->getFrequenceUntilCount());
122  }
123  if($this->getBYMONTH())
124  {
125  $ical .= (';BYMONTH='.$this->getBYMONTH());
126  }
127  if($this->getBYWEEKNO())
128  {
129  $ical .= (';BYWEEKNO='.$this->getBYWEEKNO());
130  }
131  if($this->getBYYEARDAY())
132  {
133  $ical .= (';BYYEARDAY='.$this->getBYYEARDAY());
134  }
135  if($this->getBYMONTHDAY())
136  {
137  $ical .= (';BYMONTHDAY='.$this->getBYMONTHDAY());
138  }
139  if($this->getBYDAY())
140  {
141  $ical .= (';BYDAY='.$this->getBYDAY());
142  }
143  if($this->getBYSETPOS())
144  {
145  $ical .= (';BYSETPOS='.$this->getBYSETPOS());
146  }
147  return $ical;
148  }
149 
150 
157  public function reset()
158  {
159  $this->setBYDAY('');
160  $this->setBYMONTHDAY('');
161  $this->setBYMONTH('');
162  $this->setBYSETPOS('');
163  $this->setBYWEEKNO('');
164  $this->setBYYEARDAY('');
165  $this->setFrequenceType('');
166  $this->setInterval(1);
167  $this->setFrequenceUntilCount(0);
168 
169  return true;
170  }
171 
178  public function getRecurrenceId()
179  {
180  return $this->recurrence_id;
181  }
182 
183 
191  public function setEntryId($a_id)
192  {
193  $this->cal_id = $a_id;
194  }
195 
203  public function setRecurrence($a_type)
204  {
205  $this->recurrence_type = $a_type;
206  }
207 
215  public function isRecurrence()
216  {
217  return $this->recurrence_type == self::REC_RECURRENCE;
218  }
219 
227  public function setFrequenceType($a_type)
228  {
229  $this->freq_type = $a_type;
230  }
231 
238  public function getFrequenceType()
239  {
240  return $this->freq_type;
241  }
242 
249  public function getFrequenceUntilDate()
250  {
251  return is_object($this->freq_until_date) ? $this->freq_until_date : null;
252  }
253 
260  public function setFrequenceUntilDate(ilDateTime $a_date)
261  {
262  $this->freq_until_date = $a_date;
263  }
264 
272  public function setFrequenceUntilCount($a_count)
273  {
274  $this->freq_until_count = $a_count;
275  }
276 
284  public function getFrequenceUntilCount()
285  {
287  }
288 
296  public function setInterval($a_interval)
297  {
298  $this->interval = $a_interval;
299  }
300 
307  public function getInterval()
308  {
309  return $this->interval ? $this->interval : 1;
310  }
311 
319  public function setBYDAY($a_byday)
320  {
321  $this->byday = $a_byday;
322  }
323 
330  public function getBYDAY()
331  {
332  return $this->byday;
333  }
334 
341  public function getBYDAYList()
342  {
343  if(!trim($this->getBYDAY()))
344  {
345  return array();
346  }
347  foreach(explode(',',$this->getBYDAY()) as $byday)
348  {
349  $bydays[] = trim($byday);
350  }
351  return $bydays ? $bydays : array();
352  }
353 
361  public function setBYWEEKNO($a_byweekno)
362  {
363  $this->byweekno = $a_byweekno;
364  }
365 
372  public function getBYWEEKNOList()
373  {
374  if(!trim($this->getBYWEEKNO()))
375  {
376  return array();
377  }
378  foreach(explode(',',$this->getBYWEEKNO()) as $week_num)
379  {
380  $weeks[] = (int) $week_num;
381  }
382  return $weeks ? $weeks : array();
383  }
384 
385 
392  public function getBYWEEKNO()
393  {
394  return $this->byweekno;
395  }
396 
404  public function setBYMONTH($a_by)
405  {
406  $this->bymonth = $a_by;
407  }
408 
415  public function getBYMONTH()
416  {
417  return $this->bymonth;
418  }
419 
426  public function getBYMONTHList()
427  {
428  if(!trim($this->getBYMONTH()))
429  {
430  return array();
431  }
432  foreach(explode(',',$this->getBYMONTH()) as $month_num)
433  {
434  $months[] = (int) $month_num;
435  }
436  return $months ? $months : array();
437  }
438 
446  public function setBYMONTHDAY($a_by)
447  {
448  $this->bymonthday = $a_by;
449  }
450 
457  public function getBYMONTHDAY()
458  {
459  return $this->bymonthday;
460  }
461 
467  public function getBYMONTHDAYList()
468  {
469  if(!trim($this->getBYMONTHDAY()))
470  {
471  return array();
472  }
473  foreach(explode(',',$this->getBYMONTHDAY()) as $month_num)
474  {
475  $months[] = (int) $month_num;
476  }
477  return $months ? $months : array();
478 
479  }
480 
481 
489  public function setBYYEARDAY($a_by)
490  {
491  $this->byyearday = $a_by;
492  }
493 
500  public function getBYYEARDAY()
501  {
502  return $this->byyearday;
503  }
504 
511  public function getBYYEARDAYList()
512  {
513  if(!trim($this->getBYYEARDAY()))
514  {
515  return array();
516  }
517  foreach(explode(',',$this->getBYYEARDAY()) as $year_day)
518  {
519  $days[] = (int) $year_day;
520  }
521  return $days ? $days : array();
522  }
523 
531  public function setBYSETPOS($a_by)
532  {
533  $this->bysetpos = $a_by;
534  }
535 
542  public function getBYSETPOS()
543  {
544  return $this->bysetpos;
545  }
546 
553  public function getBYSETPOSList()
554  {
555  if(!trim($this->getBYSETPOS()))
556  {
557  return array();
558  }
559  foreach(explode(',',$this->getBYSETPOS()) as $pos)
560  {
561  $positions[] = (int) $pos;
562  }
563  return $positions ? $positions : array();
564  }
565 
566 
574  public function setWeekstart($a_start)
575  {
576  $this->weekstart = $a_start;
577  }
578 
585  public function getWeekstart()
586  {
587  return $this->weekstart;
588  }
589 
596  public function getTimeZone()
597  {
598  return $this->timezone;
599  }
600 
608  public function setTimeZone($a_tz)
609  {
610  $this->timezone = $a_tz;
611  }
612 
619  public function validate()
620  {
622  if(!in_array($this->getFrequenceType(),$valid_frequences))
623  {
624  return false;
625  }
626  if($this->getFrequenceUntilCount() < 0)
627  {
628  return false;
629  }
630  if($this->getInterval() <= 0)
631  {
632  return false;
633  }
634  return true;
635  }
636 
637 
644  public function save()
645  {
646  global $ilDB;
647 
648  $until_date = is_null($this->getFrequenceUntilDate()) ?
649  null :
650  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
651  $next_id = $ilDB->nextId('cal_recurrence_rules');
652 
653  $query = "INSERT INTO cal_recurrence_rules (rule_id,cal_id,cal_recurrence,freq_type,freq_until_date,freq_until_count,intervall, ".
654  "byday,byweekno,bymonth,bymonthday,byyearday,bysetpos,weekstart) ".
655  "VALUES( ".
656  $ilDB->quote($next_id,'integer').", ".
657  $this->db->quote($this->cal_id ,'integer').", ".
658  $ilDB->quote(1,'integer').", ".
659  $ilDB->quote($this->getFrequenceType() ,'text').", ".
660  $this->db->quote($until_date,'timestamp').", ".
661  $this->db->quote($this->getFrequenceUntilCount() ,'integer').", ".
662  $this->db->quote($this->getInterval() ,'integer').", ".
663  $this->db->quote($this->getBYDAY() ,'text').", ".
664  $this->db->quote($this->getBYWEEKNO() ,'text').", ".
665  $this->db->quote($this->getBYMONTH() ,'text').", ".
666  $this->db->quote($this->getBYMONTHDAY() ,'text').", ".
667  $this->db->quote($this->getBYYEARDAY() ,'text').", ".
668  $this->db->quote($this->getBYSETPOS() ,'text').", ".
669  $this->db->quote($this->getWeekstart() ,'text')." ".
670  ")";
671  $res = $ilDB->manipulate($query);
672  $this->recurrence_id = $next_id;
673  return true;
674  }
675 
682  public function update()
683  {
684  global $ilDB;
685 
686  $until_date = is_null($this->getFrequenceUntilDate()) ?
687  null :
688  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
689 
690  $query = "UPDATE cal_recurrence_rules SET ".
691  "cal_id = ".$this->db->quote($this->cal_id ,'integer').", ".
692  "cal_recurrence = 1,".
693  "freq_type = ".$this->db->quote($this->getFrequenceType() ,'text').", ".
694  "freq_until_date = ".$this->db->quote($until_date ,'timestamp').", ".
695  "freq_until_count = ".$this->db->quote($this->getFrequenceUntilCount() ,'integer').", ".
696  "intervall = ".$this->db->quote($this->getInterval() ,'integer').", ".
697  "byday = ".$this->db->quote($this->getBYDAY() ,'text').", ".
698  "byweekno = ".$this->db->quote($this->getBYWEEKNO() ,'text').", ".
699  "bymonth = ".$this->db->quote($this->getBYMONTH() ,'text').", ".
700  "bymonthday = ".$this->db->quote($this->getBYMONTHDAY() ,'text').", ".
701  "byyearday = ".$this->db->quote($this->getBYYEARDAY() ,'text').", ".
702  "bysetpos = ".$this->db->quote($this->getBYSETPOS() ,'text').", ".
703  "weekstart = ".$this->db->quote($this->getWeekstart() ,'text')." ".
704  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer')." ";
705  $res = $ilDB->manipulate($query);
706  return true;
707  }
708 
715  public function delete()
716  {
717  global $ilDB;
718 
719  $query = "DELETE FROM cal_recurrence_rules ".
720  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer');
721  $res = $ilDB->manipulate($query);
722  return true;
723  }
724 
731  private function read()
732  {
733  global $ilDB;
734 
735  $query = "SELECT * FROM cal_recurrence_rules ".
736  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer')." ";
737  $res = $this->db->query($query);
738  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
739  {
740  $this->cal_id = $row->cal_id;
741  $this->recurrence_type = $row->cal_recurrence;
742  $this->freq_type = $row->freq_type;
743 
744  if($row->freq_until_date != null)
745  {
746  $this->freq_until_date = new ilDateTime($row->freq_until_date,IL_CAL_DATETIME,'UTC');
747  }
748  $this->freq_until_count = $row->freq_until_count;
749  $this->interval = $row->intervall;
750  $this->byday = $row->byday;
751  $this->byweekno = $row->byweekno;
752  $this->bymonth = $row->bymonth;
753  $this->bymonthday = $row->bymonthday;
754  $this->byyearday = $row->byyearday;
755  $this->bysetpos = $row->bysetpos;
756  $this->weekstart = $row->week_start;
757  }
758 
759  }
760 }
761 
762 
763 ?>