ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCalendarSchedule.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.ilCalendarUserSettings.php');
25 include_once('./Services/Calendar/classes/class.ilDateTime.php');
26 include_once('./Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php');
27 include_once('./Services/Calendar/classes/class.ilCalendarEntry.php');
28 include_once('./Services/Calendar/classes/class.ilCalendarHidden.php');
29 
42 {
43  const TYPE_DAY = 1;
44  const TYPE_WEEK = 2;
45  const TYPE_MONTH = 3;
46  const TYPE_INBOX = 4;
47 
48  protected $schedule = array();
49  protected $timezone;
50  protected $weekstart;
51  protected $hidden_cat = null;
52  protected $type = 0;
53 
54  protected $subitems_enabled = false;
55 
56  protected $start = null;
57  protected $end = null;
58  protected $user = null;
59  protected $user_settings = null;
60  protected $db = null;
61 
71  public function __construct(ilDate $seed,$a_type,$a_user_id = 0)
72  {
73  global $ilUser,$ilDB;
74 
75  $this->db = $ilDB;
76 
77  $this->type = $a_type;
78  $this->initPeriod($seed);
79 
80  if(!$a_user_id)
81  {
82  $this->user = $ilUser;
83  }
84  $this->user_settings = ilCalendarUserSettings::_getInstanceByUserId($this->user->getId());
85  $this->weekstart = $this->user_settings->getWeekStart();
86  $this->timezone = $ilUser->getTimeZone();
87 
88  $this->hidden_cat = ilCalendarHidden::_getInstanceByUserId($this->user->getId());
89  }
90 
96  public function addSubitemCalendars($a_status)
97  {
98  $this->subitems_enabled = $a_status;
99  }
100 
105  public function enabledSubitemCalendars()
106  {
107  return (bool) $this->subitems_enabled;
108  }
109 
117  public function getByDay(ilDate $a_start,$a_timezone)
118  {
120  $fstart = new ilDate($a_start->get(IL_CAL_UNIX),IL_CAL_UNIX);
121  $fend = clone $fstart;
122 
123  $f_unix_start = $fstart->get(IL_CAL_UNIX);
124  $fend->increment(ilDateTime::DAY,1);
125  $f_unix_end = $fend->get(IL_CAL_UNIX);
126 
127  $unix_start = $start->get(IL_CAL_UNIX);
128  $start->increment(ilDateTime::DAY,1);
129  $unix_end = $start->get(IL_CAL_UNIX);
130 
131  $counter = 0;
132 
133  $tmp_date = new ilDateTime($unix_start,IL_CAL_UNIX,$this->timezone);
134  foreach($this->schedule as $schedule)
135  {
136  if($schedule['fullday'])
137  {
138  if(($f_unix_start == $schedule['dstart']) or
139  $f_unix_start == $schedule['dend'] or
140  ($f_unix_start > $schedule['dstart'] and $f_unix_end <= $schedule['dend']))
141  {
142  $tmp_schedule[] = $schedule;
143  }
144  }
145  elseif(($schedule['dstart'] == $unix_start) or
146  (($schedule['dstart'] <= $unix_start) and ($schedule['dend'] > $unix_start)) or
147  (($schedule['dstart'] >= $unix_start) and ($schedule['dstart'] < $unix_end)))
148  {
149  $tmp_schedule[] = $schedule;
150  }
151  }
152  return $tmp_schedule ? $tmp_schedule : array();
153  }
154 
155 
161  public function calculate()
162  {
163  $counter = 0;
164  foreach($this->getEvents() as $event)
165  {
166  // Calculdate recurring events
167  include_once('Services/Calendar/classes/class.ilCalendarRecurrences.php');
168  if($recs = ilCalendarRecurrences::_getRecurrences($event->getEntryId()))
169  {
170  $duration = $event->getEnd()->get(IL_CAL_UNIX) - $event->getStart()->get(IL_CAL_UNIX);
171  foreach($recs as $rec)
172  {
173  $calc = new ilCalendarRecurrenceCalculator($event,$rec);
174  foreach($calc->calculateDateList($this->start,$this->end)->get() as $rec_date)
175  {
176  $this->schedule[$counter]['event'] = $event;
177  $this->schedule[$counter]['dstart'] = $rec_date->get(IL_CAL_UNIX);
178  $this->schedule[$counter]['dend'] = $this->schedule[$counter]['dstart'] + $duration;
179  $this->schedule[$counter]['fullday'] = $event->isFullday();
180 
181  switch($this->type)
182  {
183  case self::TYPE_DAY:
184  case self::TYPE_WEEK:
185  // store date info (used for calculation of overlapping events)
186  $tmp_date = new ilDateTime($this->schedule[$counter]['dstart'],IL_CAL_UNIX,$this->timezone);
187  $this->schedule[$counter]['start_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
188 
189  $tmp_date = new ilDateTime($this->schedule[$counter]['dend'],IL_CAL_UNIX,$this->timezone);
190  $this->schedule[$counter]['end_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
191  break;
192 
193  default:
194  break;
195  }
196  $counter++;
197  }
198  }
199  }
200  else
201  {
202  $this->schedule[$counter]['event'] = $event;
203  $this->schedule[$counter]['dstart'] = $event->getStart()->get(IL_CAL_UNIX);
204  $this->schedule[$counter]['dend'] = $event->getEnd()->get(IL_CAL_UNIX);
205  $this->schedule[$counter]['fullday'] = $event->isFullday();
206 
207  if(!$event->isFullday())
208  {
209  switch($this->type)
210  {
211  case self::TYPE_DAY:
212  case self::TYPE_WEEK:
213  // store date info (used for calculation of overlapping events)
214  $tmp_date = new ilDateTime($this->schedule[$counter]['dstart'],IL_CAL_UNIX,$this->timezone);
215  $this->schedule[$counter]['start_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
216 
217  $tmp_date = new ilDateTime($this->schedule[$counter]['dend'],IL_CAL_UNIX,$this->timezone);
218  $this->schedule[$counter]['end_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
219  break;
220 
221  default:
222  break;
223  }
224  }
225  $counter++;
226  }
227  }
228  }
229 
238  public function getChangedEvents($a_include_subitem_calendars = false)
239  {
240  global $ilUser,$ilDB;
241 
242  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
243  $cats = ilCalendarCategories::_getInstance($ilUser->getId())->getCategories($a_include_subitem_calendars);
244  $cats = $this->hidden_cat->filterHidden($cats,ilCalendarCategories::_getInstance($ilUser->getId())->getCategoriesInfo());
245 
246  if(!count($cats))
247  {
248  return array();
249  }
250 
251  $start = new ilDate(date('Y-m-d',time()),IL_CAL_DATE);
252  $start->increment(IL_CAL_MONTH,-1);
253 
254  $query = "SELECT ce.cal_id cal_id FROM cal_entries ce ".
255  "JOIN cal_cat_assignments ca ON ca.cal_id = ce.cal_id ".
256  "WHERE last_update > ".$ilDB->quote($start->get(IL_CAL_DATETIME),'timestamp')." ".
257  "AND ".$ilDB->in('ca.cat_id',$cats,false,'integer').' '.
258  "ORDER BY last_update";
259  $res = $this->db->query($query);
260 
261  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
262  {
263  if(!$this->hidden_cat->isAppointmentVisible($row->cal_id))
264  {
265  $events[] = new ilCalendarEntry($row->cal_id);
266  }
267  }
268  return $events ? $events : array();
269  }
270 
271 
277  protected function getEvents()
278  {
279  global $ilUser,$ilDB;
280 
281  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
282  $cats = ilCalendarCategories::_getInstance($ilUser->getId())->getCategories($this->enabledSubitemCalendars());
283  $cats = $this->hidden_cat->filterHidden($cats,ilCalendarCategories::_getInstance($ilUser->getId())->getCategoriesInfo());
284 
285  if(!count($cats))
286  {
287  return array();
288  }
289 
290  // TODO: optimize
291  $query = "SELECT ce.cal_id cal_id FROM cal_entries ce LEFT JOIN cal_recurrence_rules crr ON ce.cal_id = crr.cal_id ".
292  "JOIN cal_cat_assignments ca ON ca.cal_id = ce.cal_id ".
293  "WHERE ((starta <= ".$this->db->quote($this->end->get(IL_CAL_DATETIME,'','UTC'),'timestamp')." ".
294  "AND enda >= ".$this->db->quote($this->start->get(IL_CAL_DATETIME,'','UTC'),'timestamp').") ".
295  "OR (starta <= ".$this->db->quote($this->end->get(IL_CAL_DATETIME,'','UTC'),'timestamp')." ".
296  "AND NOT rule_id IS NULL)) ".
297  "AND ".$ilDB->in('ca.cat_id',$cats,false,'integer')." ".
298  "ORDER BY starta";
299  $res = $this->db->query($query);
300 
301  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
302  {
303  if(!$this->hidden_cat->isAppointmentVisible($row->cal_id))
304  {
305  $events[] = new ilCalendarEntry($row->cal_id);
306  }
307  }
308  /*
309  foreach($events as $event)
310  {
311  var_dump("<pre>",$event->getTitle(),"</pre>");
312  }
313  */
314  return $events ? $events : array();
315  }
316 
324  protected function initPeriod(ilDate $seed)
325  {
326  switch($this->type)
327  {
328  case self::TYPE_DAY:
329  $this->start = clone $seed;
330  $this->end = clone $seed;
331  $this->start->increment(IL_CAL_DAY,-2);
332  $this->end->increment(IL_CAL_DAY,2);
333  break;
334 
335  case self::TYPE_WEEK:
336  $this->start = clone $seed;
337  $start_info = $this->start->get(IL_CAL_FKT_GETDATE,'','UTC');
338  $day_diff = $this->weekstart - $start_info['isoday'];
339  if($day_diff == 7)
340  {
341  $day_diff = 0;
342  }
343  $this->start->increment(IL_CAL_DAY,$day_diff);
344  $this->start->increment(IL_CAL_DAY,-1);
345  $this->end = clone $this->start;
346  $this->end->increment(IL_CAL_DAY,9);
347  break;
348 
349  case self::TYPE_MONTH:
350  $year_month = $seed->get(IL_CAL_FKT_DATE,'Y-m','UTC');
351  list($year,$month) = explode('-',$year_month);
352 
353  $this->start = new ilDate($year_month.'-01',IL_CAL_DATE);
354  $this->start->increment(IL_CAL_DAY,-6);
355 
356  $this->end = new ilDate($year_month.'-'.ilCalendarUtil::_getMaxDayOfMonth($year,$month),IL_CAL_DATE);
357  $this->end->increment(IL_CAL_DAY,6);
358  break;
359  }
360 
361  return true;
362  }
363 }
364 ?>