ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 = '';
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  $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 
120  public function toICal($a_user_id)
121  {
122  include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
123  $entry = new ilCalendarEntry($this->getEntryId());
124 
125  $ical = 'RRULE:';
126  $ical .= ('FREQ=' . $this->getFrequenceType());
127 
128  if ($this->getInterval()) {
129  $ical .= (';INTERVAL=' . $this->getInterval());
130  }
131  if ($this->getFrequenceUntilCount()) {
132  $ical .= (';COUNT=' . $this->getFrequenceUntilCount());
133  } elseif ($this->getFrequenceUntilDate()) {
134  if ($entry->isFullday()) {
135  $ical .= (';UNTIL=' . $this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE, 'Ymd'));
136  } else {
137  $his = $entry->getStart()->get(IL_CAL_FKT_DATE, 'His');
138  $ical .= (';UNTIL=' . $this->getFrequenceUntilDate()->get(IL_CAL_FKT_DATE, 'Ymd') . 'T' . $his);
139  }
140  }
141  if ($this->getBYMONTH()) {
142  $ical .= (';BYMONTH=' . $this->getBYMONTH());
143  }
144  if ($this->getBYWEEKNO()) {
145  $ical .= (';BYWEEKNO=' . $this->getBYWEEKNO());
146  }
147  if ($this->getBYYEARDAY()) {
148  $ical .= (';BYYEARDAY=' . $this->getBYYEARDAY());
149  }
150  if ($this->getBYMONTHDAY()) {
151  $ical .= (';BYMONTHDAY=' . $this->getBYMONTHDAY());
152  }
153  if ($this->getBYDAY()) {
154  $ical .= (';BYDAY=' . $this->getBYDAY());
155  }
156  if ($this->getBYSETPOS()) {
157  $ical .= (';BYSETPOS=' . $this->getBYSETPOS());
158  }
159 
160  // Required in outlook
161  if ($this->getBYDAY()) {
162  include_once './Services/Calendar/classes/class.ilCalendarUserSettings.php';
163  include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
165  if ($us->getWeekStart() == ilCalendarSettings::WEEK_START_MONDAY) {
166  $ical .= (';WKST=MO');
167  } else {
168  $ical .= (';WKST=SU');
169  }
170  }
171 
172  return $ical;
173  }
174 
175 
182  public function reset()
183  {
184  $this->setBYDAY('');
185  $this->setBYMONTHDAY('');
186  $this->setBYMONTH('');
187  $this->setBYSETPOS('');
188  $this->setBYWEEKNO('');
189  $this->setBYYEARDAY('');
190  $this->setFrequenceType('');
191  $this->setInterval(1);
192  $this->setFrequenceUntilCount(0);
193 
194  return true;
195  }
196 
203  public function getRecurrenceId()
204  {
205  return $this->recurrence_id;
206  }
207 
208 
216  public function setEntryId($a_id)
217  {
218  $this->cal_id = $a_id;
219  }
220 
225  public function getEntryId()
226  {
227  return $this->cal_id;
228  }
229 
237  public function setRecurrence($a_type)
238  {
239  $this->recurrence_type = $a_type;
240  }
241 
249  public function isRecurrence()
250  {
251  return $this->recurrence_type == self::REC_RECURRENCE;
252  }
253 
261  public function setFrequenceType($a_type)
262  {
263  $this->freq_type = $a_type;
264  }
265 
272  public function getFrequenceType()
273  {
274  return $this->freq_type;
275  }
276 
283  public function getFrequenceUntilDate()
284  {
285  return is_object($this->freq_until_date) ? $this->freq_until_date : null;
286  }
287 
294  public function setFrequenceUntilDate(ilDateTime $a_date = null)
295  {
296  $this->freq_until_date = $a_date;
297  }
298 
306  public function setFrequenceUntilCount($a_count)
307  {
308  $this->freq_until_count = $a_count;
309  }
310 
318  public function getFrequenceUntilCount()
319  {
321  }
322 
330  public function setInterval($a_interval)
331  {
332  $this->interval = $a_interval;
333  }
334 
341  public function getInterval()
342  {
343  return $this->interval ? $this->interval : 1;
344  }
345 
353  public function setBYDAY($a_byday)
354  {
355  $this->byday = $a_byday;
356  }
357 
364  public function getBYDAY()
365  {
366  return $this->byday;
367  }
368 
375  public function getBYDAYList()
376  {
377  if (!trim($this->getBYDAY())) {
378  return array();
379  }
380  foreach (explode(',', $this->getBYDAY()) as $byday) {
381  $bydays[] = trim($byday);
382  }
383  return $bydays ? $bydays : array();
384  }
385 
393  public function setBYWEEKNO($a_byweekno)
394  {
395  $this->byweekno = $a_byweekno;
396  }
397 
404  public function getBYWEEKNOList()
405  {
406  if (!trim($this->getBYWEEKNO())) {
407  return array();
408  }
409  foreach (explode(',', $this->getBYWEEKNO()) as $week_num) {
410  $weeks[] = (int) $week_num;
411  }
412  return $weeks ? $weeks : array();
413  }
414 
415 
422  public function getBYWEEKNO()
423  {
424  return $this->byweekno;
425  }
426 
434  public function setBYMONTH($a_by)
435  {
436  $this->bymonth = $a_by;
437  }
438 
445  public function getBYMONTH()
446  {
447  return $this->bymonth;
448  }
449 
456  public function getBYMONTHList()
457  {
458  if (!trim($this->getBYMONTH())) {
459  return array();
460  }
461  foreach (explode(',', $this->getBYMONTH()) as $month_num) {
462  $months[] = (int) $month_num;
463  }
464  return $months ? $months : array();
465  }
466 
474  public function setBYMONTHDAY($a_by)
475  {
476  $this->bymonthday = $a_by;
477  }
478 
485  public function getBYMONTHDAY()
486  {
487  return $this->bymonthday;
488  }
489 
495  public function getBYMONTHDAYList()
496  {
497  if (!trim($this->getBYMONTHDAY())) {
498  return array();
499  }
500  foreach (explode(',', $this->getBYMONTHDAY()) as $month_num) {
501  $months[] = (int) $month_num;
502  }
503  return $months ? $months : array();
504  }
505 
506 
514  public function setBYYEARDAY($a_by)
515  {
516  $this->byyearday = $a_by;
517  }
518 
525  public function getBYYEARDAY()
526  {
527  return $this->byyearday;
528  }
529 
536  public function getBYYEARDAYList()
537  {
538  if (!trim($this->getBYYEARDAY())) {
539  return array();
540  }
541  foreach (explode(',', $this->getBYYEARDAY()) as $year_day) {
542  $days[] = (int) $year_day;
543  }
544  return $days ? $days : array();
545  }
546 
554  public function setBYSETPOS($a_by)
555  {
556  $this->bysetpos = $a_by;
557  }
558 
565  public function getBYSETPOS()
566  {
567  return $this->bysetpos;
568  }
569 
576  public function getBYSETPOSList()
577  {
578  if (!trim($this->getBYSETPOS())) {
579  return array();
580  }
581  foreach (explode(',', $this->getBYSETPOS()) as $pos) {
582  $positions[] = (int) $pos;
583  }
584  return $positions ? $positions : array();
585  }
586 
587 
595  public function setWeekstart($a_start)
596  {
597  $this->weekstart = $a_start;
598  }
599 
606  public function getWeekstart()
607  {
608  return $this->weekstart;
609  }
610 
617  public function getTimeZone()
618  {
619  return $this->timezone;
620  }
621 
629  public function setTimeZone($a_tz)
630  {
631  $this->timezone = $a_tz;
632  }
633 
638  public function getExclusionDates()
639  {
640  return (array) $this->exclusion_dates;
641  }
642 
649  public function validate()
650  {
652  if (!in_array($this->getFrequenceType(), $valid_frequences)) {
653  return false;
654  }
655  if ($this->getFrequenceUntilCount() < 0) {
656  return false;
657  }
658  if ($this->getInterval() <= 0) {
659  return false;
660  }
661  return true;
662  }
663 
664 
671  public function save()
672  {
673  global $ilDB;
674 
675  $until_date = is_null($this->getFrequenceUntilDate()) ?
676  null :
677  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME, '', 'UTC');
678  $next_id = $ilDB->nextId('cal_recurrence_rules');
679 
680  $query = "INSERT INTO cal_recurrence_rules (rule_id,cal_id,cal_recurrence,freq_type,freq_until_date,freq_until_count,intervall, " .
681  "byday,byweekno,bymonth,bymonthday,byyearday,bysetpos,weekstart) " .
682  "VALUES( " .
683  $ilDB->quote($next_id, 'integer') . ", " .
684  $this->db->quote($this->cal_id, 'integer') . ", " .
685  $ilDB->quote(1, 'integer') . ", " .
686  $ilDB->quote((string) $this->getFrequenceType(), 'text') . ", " .
687  $this->db->quote($until_date, 'timestamp') . ", " .
688  $this->db->quote((int) $this->getFrequenceUntilCount(), 'integer') . ", " .
689  $this->db->quote((int) $this->getInterval(), 'integer') . ", " .
690  $this->db->quote((string) $this->getBYDAY(), 'text') . ", " .
691  $this->db->quote((string) $this->getBYWEEKNO(), 'text') . ", " .
692  $this->db->quote((string) $this->getBYMONTH(), 'text') . ", " .
693  $this->db->quote((string) $this->getBYMONTHDAY(), 'text') . ", " .
694  $this->db->quote((string) $this->getBYYEARDAY(), 'text') . ", " .
695  $this->db->quote((string) $this->getBYSETPOS(), 'text') . ", " .
696  $this->db->quote((string) $this->getWeekstart(), 'text') . " " .
697  ")";
698  $res = $ilDB->manipulate($query);
699  $this->recurrence_id = $next_id;
700  return true;
701  }
702 
709  public function update()
710  {
711  global $ilDB;
712 
713  $until_date = is_null($this->getFrequenceUntilDate()) ?
714  null :
715  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME, '', 'UTC');
716 
717  $query = "UPDATE cal_recurrence_rules SET " .
718  "cal_id = " . $this->db->quote($this->cal_id, 'integer') . ", " .
719  "cal_recurrence = 1," .
720  "freq_type = " . $this->db->quote($this->getFrequenceType(), 'text') . ", " .
721  "freq_until_date = " . $this->db->quote($until_date, 'timestamp') . ", " .
722  "freq_until_count = " . $this->db->quote($this->getFrequenceUntilCount(), 'integer') . ", " .
723  "intervall = " . $this->db->quote($this->getInterval(), 'integer') . ", " .
724  "byday = " . $this->db->quote($this->getBYDAY(), 'text') . ", " .
725  "byweekno = " . $this->db->quote($this->getBYWEEKNO(), 'text') . ", " .
726  "bymonth = " . $this->db->quote($this->getBYMONTH(), 'text') . ", " .
727  "bymonthday = " . $this->db->quote($this->getBYMONTHDAY(), 'text') . ", " .
728  "byyearday = " . $this->db->quote($this->getBYYEARDAY(), 'text') . ", " .
729  "bysetpos = " . $this->db->quote($this->getBYSETPOS(), 'text') . ", " .
730  "weekstart = " . $this->db->quote($this->getWeekstart(), 'text') . " " .
731  "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer') . " ";
732  $res = $ilDB->manipulate($query);
733  return true;
734  }
735 
742  public function delete()
743  {
744  global $ilDB;
745 
746  $query = "DELETE FROM cal_recurrence_rules " .
747  "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer');
748  $res = $ilDB->manipulate($query);
749  return true;
750  }
751 
758  private function read()
759  {
760  global $ilDB;
761 
762  $query = "SELECT * FROM cal_recurrence_rules " .
763  "WHERE rule_id = " . $this->db->quote($this->recurrence_id, 'integer') . " ";
764  $res = $this->db->query($query);
765  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
766  $this->cal_id = $row->cal_id;
767  $this->recurrence_type = $row->cal_recurrence;
768  $this->freq_type = $row->freq_type;
769 
770  if ($row->freq_until_date != null) {
771  $this->freq_until_date = new ilDateTime($row->freq_until_date, IL_CAL_DATETIME, 'UTC');
772  }
773  $this->freq_until_count = $row->freq_until_count;
774  $this->interval = $row->intervall;
775  $this->byday = $row->byday;
776  $this->byweekno = $row->byweekno;
777  $this->bymonth = $row->bymonth;
778  $this->bymonthday = $row->bymonthday;
779  $this->byyearday = $row->byyearday;
780  $this->bysetpos = $row->bysetpos;
781  $this->weekstart = $row->week_start;
782  }
783 
784  $this->exclusion_dates = ilCalendarRecurrenceExclusions::getExclusionDates($this->cal_id);
785  }
786 }
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:92
toICal($a_user_id)
Get ical presentation for calendar recurrence.
foreach($_POST as $key=> $value) $res
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
$query
__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