ILIAS  eassessment Revision 61809
 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 
37 define('IL_CAL_FREQ_DAILY',ilCalendarRecurrence::FREQ_DAILY);
38 define('IL_CAL_FREQ_WEEKLY',ilCalendarRecurrence::FREQ_WEEKLY);
39 define('IL_CAL_FREQ_MONTHLY',ilCalendarRecurrence::FREQ_MONTHLY);
40 define('IL_CAL_FREQ_YEARLY',ilCalendarRecurrence::FREQ_YEARLY);
41 
43 {
44  const REC_RECURRENCE = 0;
45  const REC_EXCLUSION = 1;
46 
47  const FREQ_DAILY = 'DAILY';
48  const FREQ_WEEKLY = 'WEEKLY';
49  const FREQ_MONTHLY = 'MONTHLY';
50  const FREQ_YEARLY = 'YEARLY';
51 
52 
53  protected $db;
54 
55  private $recurrence_id;
56  private $cal_id;
58 
59  private $freq_type = '';
60  private $freq_until_type;
61  private $freq_until_date = null;
63 
64  private $interval;
65  private $byday;
66  private $byweekno;
67  private $bymonth;
68  private $bymonthday;
69  private $byyearday;
70  private $bysetpos;
71  private $weekstart;
72 
73  private $exclusion_dates = array();
74 
75  private $timezone = 'Europe/Berlin';
76 
84  public function __construct($a_rec_id = 0)
85  {
86  global $ilDB;
87 
88  $this->db = $ilDB;
89  $this->recurrence_id = $a_rec_id;
90  if($a_rec_id)
91  {
92  $this->read();
93  }
94  }
95 
104  public static function _delete($a_cal_id)
105  {
106  global $ilDB;
107 
108  $query = "DELETE FROM cal_recurrence_rules ".
109  "WHERE cal_id = ".$ilDB->quote($a_cal_id ,'integer')." ";
110  $res = $ilDB->manipulate($query);
111 
113  }
114 
115  public function toICal()
116  {
117  $ical = 'RRULE:';
118  $ical .= ('FREQ='.$this->getFrequenceType());
119 
120  if($this->getInterval())
121  {
122  $ical .= (';INTERVAL='.$this->getInterval());
123  }
124  if($this->getFrequenceUntilCount())
125  {
126  $ical .= (';COUNT='.$this->getFrequenceUntilCount());
127  }
128  elseif($this->getFrequenceUntilDate())
129  {
130  $ical .= (';UNTIL='.$this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE,'Ymd'));
131  }
132  if($this->getBYMONTH())
133  {
134  $ical .= (';BYMONTH='.$this->getBYMONTH());
135  }
136  if($this->getBYWEEKNO())
137  {
138  $ical .= (';BYWEEKNO='.$this->getBYWEEKNO());
139  }
140  if($this->getBYYEARDAY())
141  {
142  $ical .= (';BYYEARDAY='.$this->getBYYEARDAY());
143  }
144  if($this->getBYMONTHDAY())
145  {
146  $ical .= (';BYMONTHDAY='.$this->getBYMONTHDAY());
147  }
148  if($this->getBYDAY())
149  {
150  $ical .= (';BYDAY='.$this->getBYDAY());
151  }
152  if($this->getBYSETPOS())
153  {
154  $ical .= (';BYSETPOS='.$this->getBYSETPOS());
155  }
156  return $ical;
157  }
158 
159 
166  public function reset()
167  {
168  $this->setBYDAY('');
169  $this->setBYMONTHDAY('');
170  $this->setBYMONTH('');
171  $this->setBYSETPOS('');
172  $this->setBYWEEKNO('');
173  $this->setBYYEARDAY('');
174  $this->setFrequenceType('');
175  $this->setInterval(1);
176  $this->setFrequenceUntilCount(0);
177 
178  return true;
179  }
180 
187  public function getRecurrenceId()
188  {
189  return $this->recurrence_id;
190  }
191 
192 
200  public function setEntryId($a_id)
201  {
202  $this->cal_id = $a_id;
203  }
204 
212  public function setRecurrence($a_type)
213  {
214  $this->recurrence_type = $a_type;
215  }
216 
224  public function isRecurrence()
225  {
226  return $this->recurrence_type == self::REC_RECURRENCE;
227  }
228 
236  public function setFrequenceType($a_type)
237  {
238  $this->freq_type = $a_type;
239  }
240 
247  public function getFrequenceType()
248  {
249  return $this->freq_type;
250  }
251 
258  public function getFrequenceUntilDate()
259  {
260  return is_object($this->freq_until_date) ? $this->freq_until_date : null;
261  }
262 
269  public function setFrequenceUntilDate(ilDateTime $a_date = null)
270  {
271  $this->freq_until_date = $a_date;
272  }
273 
281  public function setFrequenceUntilCount($a_count)
282  {
283  $this->freq_until_count = $a_count;
284  }
285 
293  public function getFrequenceUntilCount()
294  {
296  }
297 
305  public function setInterval($a_interval)
306  {
307  $this->interval = $a_interval;
308  }
309 
316  public function getInterval()
317  {
318  return $this->interval ? $this->interval : 1;
319  }
320 
328  public function setBYDAY($a_byday)
329  {
330  $this->byday = $a_byday;
331  }
332 
339  public function getBYDAY()
340  {
341  return $this->byday;
342  }
343 
350  public function getBYDAYList()
351  {
352  if(!trim($this->getBYDAY()))
353  {
354  return array();
355  }
356  foreach(explode(',',$this->getBYDAY()) as $byday)
357  {
358  $bydays[] = trim($byday);
359  }
360  return $bydays ? $bydays : array();
361  }
362 
370  public function setBYWEEKNO($a_byweekno)
371  {
372  $this->byweekno = $a_byweekno;
373  }
374 
381  public function getBYWEEKNOList()
382  {
383  if(!trim($this->getBYWEEKNO()))
384  {
385  return array();
386  }
387  foreach(explode(',',$this->getBYWEEKNO()) as $week_num)
388  {
389  $weeks[] = (int) $week_num;
390  }
391  return $weeks ? $weeks : array();
392  }
393 
394 
401  public function getBYWEEKNO()
402  {
403  return $this->byweekno;
404  }
405 
413  public function setBYMONTH($a_by)
414  {
415  $this->bymonth = $a_by;
416  }
417 
424  public function getBYMONTH()
425  {
426  return $this->bymonth;
427  }
428 
435  public function getBYMONTHList()
436  {
437  if(!trim($this->getBYMONTH()))
438  {
439  return array();
440  }
441  foreach(explode(',',$this->getBYMONTH()) as $month_num)
442  {
443  $months[] = (int) $month_num;
444  }
445  return $months ? $months : array();
446  }
447 
455  public function setBYMONTHDAY($a_by)
456  {
457  $this->bymonthday = $a_by;
458  }
459 
466  public function getBYMONTHDAY()
467  {
468  return $this->bymonthday;
469  }
470 
476  public function getBYMONTHDAYList()
477  {
478  if(!trim($this->getBYMONTHDAY()))
479  {
480  return array();
481  }
482  foreach(explode(',',$this->getBYMONTHDAY()) as $month_num)
483  {
484  $months[] = (int) $month_num;
485  }
486  return $months ? $months : array();
487 
488  }
489 
490 
498  public function setBYYEARDAY($a_by)
499  {
500  $this->byyearday = $a_by;
501  }
502 
509  public function getBYYEARDAY()
510  {
511  return $this->byyearday;
512  }
513 
520  public function getBYYEARDAYList()
521  {
522  if(!trim($this->getBYYEARDAY()))
523  {
524  return array();
525  }
526  foreach(explode(',',$this->getBYYEARDAY()) as $year_day)
527  {
528  $days[] = (int) $year_day;
529  }
530  return $days ? $days : array();
531  }
532 
540  public function setBYSETPOS($a_by)
541  {
542  $this->bysetpos = $a_by;
543  }
544 
551  public function getBYSETPOS()
552  {
553  return $this->bysetpos;
554  }
555 
562  public function getBYSETPOSList()
563  {
564  if(!trim($this->getBYSETPOS()))
565  {
566  return array();
567  }
568  foreach(explode(',',$this->getBYSETPOS()) as $pos)
569  {
570  $positions[] = (int) $pos;
571  }
572  return $positions ? $positions : array();
573  }
574 
575 
583  public function setWeekstart($a_start)
584  {
585  $this->weekstart = $a_start;
586  }
587 
594  public function getWeekstart()
595  {
596  return $this->weekstart;
597  }
598 
605  public function getTimeZone()
606  {
607  return $this->timezone;
608  }
609 
617  public function setTimeZone($a_tz)
618  {
619  $this->timezone = $a_tz;
620  }
621 
626  public function getExclusionDates()
627  {
628  return (array) $this->exclusion_dates;
629  }
630 
637  public function validate()
638  {
640  if(!in_array($this->getFrequenceType(),$valid_frequences))
641  {
642  return false;
643  }
644  if($this->getFrequenceUntilCount() < 0)
645  {
646  return false;
647  }
648  if($this->getInterval() <= 0)
649  {
650  return false;
651  }
652  return true;
653  }
654 
655 
662  public function save()
663  {
664  global $ilDB;
665 
666  $until_date = is_null($this->getFrequenceUntilDate()) ?
667  null :
668  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
669  $next_id = $ilDB->nextId('cal_recurrence_rules');
670 
671  $query = "INSERT INTO cal_recurrence_rules (rule_id,cal_id,cal_recurrence,freq_type,freq_until_date,freq_until_count,intervall, ".
672  "byday,byweekno,bymonth,bymonthday,byyearday,bysetpos,weekstart) ".
673  "VALUES( ".
674  $ilDB->quote($next_id,'integer').", ".
675  $this->db->quote($this->cal_id ,'integer').", ".
676  $ilDB->quote(1,'integer').", ".
677  $ilDB->quote($this->getFrequenceType() ,'text').", ".
678  $this->db->quote($until_date,'timestamp').", ".
679  $this->db->quote($this->getFrequenceUntilCount() ,'integer').", ".
680  $this->db->quote($this->getInterval() ,'integer').", ".
681  $this->db->quote($this->getBYDAY() ,'text').", ".
682  $this->db->quote($this->getBYWEEKNO() ,'text').", ".
683  $this->db->quote($this->getBYMONTH() ,'text').", ".
684  $this->db->quote($this->getBYMONTHDAY() ,'text').", ".
685  $this->db->quote($this->getBYYEARDAY() ,'text').", ".
686  $this->db->quote($this->getBYSETPOS() ,'text').", ".
687  $this->db->quote($this->getWeekstart() ,'text')." ".
688  ")";
689  $res = $ilDB->manipulate($query);
690  $this->recurrence_id = $next_id;
691  return true;
692  }
693 
700  public function update()
701  {
702  global $ilDB;
703 
704  $until_date = is_null($this->getFrequenceUntilDate()) ?
705  null :
706  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
707 
708  $query = "UPDATE cal_recurrence_rules SET ".
709  "cal_id = ".$this->db->quote($this->cal_id ,'integer').", ".
710  "cal_recurrence = 1,".
711  "freq_type = ".$this->db->quote($this->getFrequenceType() ,'text').", ".
712  "freq_until_date = ".$this->db->quote($until_date ,'timestamp').", ".
713  "freq_until_count = ".$this->db->quote($this->getFrequenceUntilCount() ,'integer').", ".
714  "intervall = ".$this->db->quote($this->getInterval() ,'integer').", ".
715  "byday = ".$this->db->quote($this->getBYDAY() ,'text').", ".
716  "byweekno = ".$this->db->quote($this->getBYWEEKNO() ,'text').", ".
717  "bymonth = ".$this->db->quote($this->getBYMONTH() ,'text').", ".
718  "bymonthday = ".$this->db->quote($this->getBYMONTHDAY() ,'text').", ".
719  "byyearday = ".$this->db->quote($this->getBYYEARDAY() ,'text').", ".
720  "bysetpos = ".$this->db->quote($this->getBYSETPOS() ,'text').", ".
721  "weekstart = ".$this->db->quote($this->getWeekstart() ,'text')." ".
722  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer')." ";
723  $res = $ilDB->manipulate($query);
724  return true;
725  }
726 
733  public function delete()
734  {
735  global $ilDB;
736 
737  $query = "DELETE FROM cal_recurrence_rules ".
738  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer');
739  $res = $ilDB->manipulate($query);
740  return true;
741  }
742 
749  private function read()
750  {
751  global $ilDB;
752 
753  $query = "SELECT * FROM cal_recurrence_rules ".
754  "WHERE rule_id = ".$this->db->quote($this->recurrence_id ,'integer')." ";
755  $res = $this->db->query($query);
756  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
757  {
758  $this->cal_id = $row->cal_id;
759  $this->recurrence_type = $row->cal_recurrence;
760  $this->freq_type = $row->freq_type;
761 
762  if($row->freq_until_date != null)
763  {
764  $this->freq_until_date = new ilDateTime($row->freq_until_date,IL_CAL_DATETIME,'UTC');
765  }
766  $this->freq_until_count = $row->freq_until_count;
767  $this->interval = $row->intervall;
768  $this->byday = $row->byday;
769  $this->byweekno = $row->byweekno;
770  $this->bymonth = $row->bymonth;
771  $this->bymonthday = $row->bymonthday;
772  $this->byyearday = $row->byyearday;
773  $this->bysetpos = $row->bysetpos;
774  $this->weekstart = $row->week_start;
775  }
776 
777  $this->exclusion_dates = ilCalendarRecurrenceExclusions::getExclusionDates($this->cal_id);
778  }
779 }
780 
781 
782 ?>