ILIAS  Release_3_10_x_branch Revision 61812
 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;
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 
255  $query = "SELECT ce.cal_id AS cal_id FROM cal_entries AS ce ".
256  "JOIN cal_category_assignments AS ca ON ca.cal_id = ce.cal_id ".
257  "WHERE last_update > '".$start->get(IL_CAL_DATETIME)."' ".
258  "AND ca.cat_id IN (".implode(',',ilUtil::quoteArray($cats)).') '.
259  "ORDER BY last_update";
260  $res = $this->db->query($query);
261 
262  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
263  {
264  if(!$this->hidden_cat->isAppointmentVisible($row->cal_id))
265  {
266  $events[] = new ilCalendarEntry($row->cal_id);
267  }
268  }
269  return $events ? $events : array();
270  }
271 
272 
278  protected function getEvents()
279  {
280  global $ilUser;
281 
282  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
283  $cats = ilCalendarCategories::_getInstance($ilUser->getId())->getCategories($this->enabledSubitemCalendars());
284  $cats = $this->hidden_cat->filterHidden($cats,ilCalendarCategories::_getInstance($ilUser->getId())->getCategoriesInfo());
285 
286  if(!count($cats))
287  {
288  return array();
289  }
290 
291  // TODO: optimize
292  $query = "SELECT ce.cal_id AS cal_id FROM cal_entries AS ce LEFT JOIN cal_recurrence_rules AS crr USING (cal_id) ".
293  "JOIN cal_category_assignments AS ca ON ca.cal_id = ce.cal_id ".
294  "WHERE ((start <= ".$this->db->quote($this->end->get(IL_CAL_DATETIME,'','UTC'))." ".
295  "AND end >= ".$this->db->quote($this->start->get(IL_CAL_DATETIME,'','UTC')).") ".
296  "OR (start <= ".$this->db->quote($this->end->get(IL_CAL_DATETIME,'','UTC'))." ".
297  "AND NOT rule_id IS NULL)) ".
298  "AND ca.cat_id IN (".implode(',',ilUtil::quoteArray($cats)).') '.
299  "ORDER BY start";
300  $res = $this->db->query($query);
301 
302  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
303  {
304  if(!$this->hidden_cat->isAppointmentVisible($row->cal_id))
305  {
306  $events[] = new ilCalendarEntry($row->cal_id);
307  }
308  }
309  /*
310  foreach($events as $event)
311  {
312  var_dump("<pre>",$event->getTitle(),"</pre>");
313  }
314  */
315  return $events ? $events : array();
316  }
317 
325  protected function initPeriod(ilDate $seed)
326  {
327  switch($this->type)
328  {
329  case self::TYPE_DAY:
330  $this->start = clone $seed;
331  $this->end = clone $seed;
332  $this->start->increment(IL_CAL_DAY,-2);
333  $this->end->increment(IL_CAL_DAY,2);
334  break;
335 
336  case self::TYPE_WEEK:
337  $this->start = clone $seed;
338  $start_info = $this->start->get(IL_CAL_FKT_GETDATE,'','UTC');
339  $day_diff = $this->weekstart - $start_info['isoday'];
340  if($day_diff == 7)
341  {
342  $day_diff = 0;
343  }
344  $this->start->increment(IL_CAL_DAY,$day_diff);
345  $this->start->increment(IL_CAL_DAY,-1);
346  $this->end = clone $this->start;
347  $this->end->increment(IL_CAL_DAY,9);
348  break;
349 
350  case self::TYPE_MONTH:
351  $year_month = $seed->get(IL_CAL_FKT_DATE,'Y-m','UTC');
352  list($year,$month) = explode('-',$year_month);
353 
354  $this->start = new ilDate($year_month.'-01',IL_CAL_DATE);
355  $this->start->increment(IL_CAL_DAY,-6);
356 
357  $this->end = new ilDate($year_month.'-'.ilCalendarUtil::_getMaxDayOfMonth($year,$month),IL_CAL_DATE);
358  $this->end->increment(IL_CAL_DAY,6);
359  break;
360  }
361 
362  return true;
363  }
364 }
365 
366 ?>