ILIAS  eassessment Revision 61809
 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  protected $filter_bookings = false;
56 
57  protected $start = null;
58  protected $end = null;
59  protected $user = null;
60  protected $user_settings = null;
61  protected $db = null;
62 
72  public function __construct(ilDate $seed,$a_type,$a_user_id = 0,$filter_bookings=false)
73  {
74  global $ilUser,$ilDB;
75 
76  $this->db = $ilDB;
77 
78  $this->type = $a_type;
79  $this->initPeriod($seed);
80 
81  if(!$a_user_id || $a_user_id == $ilUser->getId())
82  {
83  $this->user = $ilUser;
84  }
85  else
86  {
87  $this->user = new ilObjUser($a_user_id);
88  }
89  $this->filter_bookings = $filter_bookings;
90  $this->user_settings = ilCalendarUserSettings::_getInstanceByUserId($this->user->getId());
91  $this->weekstart = $this->user_settings->getWeekStart();
92  $this->timezone = $this->user->getTimeZone();
93 
94  $this->hidden_cat = ilCalendarHidden::_getInstanceByUserId($this->user->getId());
95  }
96 
102  public function addSubitemCalendars($a_status)
103  {
104  $this->subitems_enabled = $a_status;
105  }
106 
111  public function enabledSubitemCalendars()
112  {
113  return (bool) $this->subitems_enabled;
114  }
115 
123  public function getByDay(ilDate $a_start,$a_timezone)
124  {
126  $fstart = new ilDate($a_start->get(IL_CAL_UNIX),IL_CAL_UNIX);
127  $fend = clone $fstart;
128 
129  $f_unix_start = $fstart->get(IL_CAL_UNIX);
130  $fend->increment(ilDateTime::DAY,1);
131  $f_unix_end = $fend->get(IL_CAL_UNIX);
132 
133  $unix_start = $start->get(IL_CAL_UNIX);
134  $start->increment(ilDateTime::DAY,1);
135  $unix_end = $start->get(IL_CAL_UNIX);
136 
137  $counter = 0;
138 
139  $tmp_date = new ilDateTime($unix_start,IL_CAL_UNIX,$this->timezone);
140  $tmp_schedule = array();
141  foreach($this->schedule as $schedule)
142  {
143  if($schedule['fullday'])
144  {
145  if(($f_unix_start == $schedule['dstart']) or
146  $f_unix_start == $schedule['dend'] or
147  ($f_unix_start > $schedule['dstart'] and $f_unix_end <= $schedule['dend']))
148  {
149  $tmp_schedule[] = $schedule;
150  }
151  }
152  elseif(($schedule['dstart'] == $unix_start) or
153  (($schedule['dstart'] <= $unix_start) and ($schedule['dend'] > $unix_start)) or
154  (($schedule['dstart'] >= $unix_start) and ($schedule['dstart'] < $unix_end)))
155  {
156  $tmp_schedule[] = $schedule;
157  }
158  }
159  return $tmp_schedule;
160  }
161 
162 
168  public function calculate()
169  {
170  global $ilDB;
171 
172  $events = $this->getEvents();
173 
174  // we need category type for booking handling
175  $ids = array();
176  foreach($events as $event)
177  {
178  $ids[] = $event->getEntryId();
179  }
180  include_once('Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
182  include_once('Services/Calendar/classes/class.ilCalendarCategory.php');
183  $cat_types = array();
184  foreach(array_unique($cat_map) as $cat_id)
185  {
186  $cat = new ilCalendarCategory($cat_id);
187  $cat_types[$cat_id] = $cat->getType();
188  }
189 
190  $counter = 0;
191  foreach($events as $event)
192  {
193  // Calculdate recurring events
194  include_once('Services/Calendar/classes/class.ilCalendarRecurrences.php');
195  if($recs = ilCalendarRecurrences::_getRecurrences($event->getEntryId()))
196  {
197  $duration = $event->getEnd()->get(IL_CAL_UNIX) - $event->getStart()->get(IL_CAL_UNIX);
198  foreach($recs as $rec)
199  {
200  $calc = new ilCalendarRecurrenceCalculator($event,$rec);
201  foreach($calc->calculateDateList($this->start,$this->end)->get() as $rec_date)
202  {
203  $this->schedule[$counter]['event'] = $event;
204  $this->schedule[$counter]['dstart'] = $rec_date->get(IL_CAL_UNIX);
205  $this->schedule[$counter]['dend'] = $this->schedule[$counter]['dstart'] + $duration;
206  $this->schedule[$counter]['fullday'] = $event->isFullday();
207  $this->schedule[$counter]['category_id'] = $cat_map[$event->getEntryId()];
208  $this->schedule[$counter]['category_type'] = $cat_types[$cat_map[$event->getEntryId()]];
209 
210  switch($this->type)
211  {
212  case self::TYPE_DAY:
213  case self::TYPE_WEEK:
214  // store date info (used for calculation of overlapping events)
215  $tmp_date = new ilDateTime($this->schedule[$counter]['dstart'],IL_CAL_UNIX,$this->timezone);
216  $this->schedule[$counter]['start_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
217 
218  $tmp_date = new ilDateTime($this->schedule[$counter]['dend'],IL_CAL_UNIX,$this->timezone);
219  $this->schedule[$counter]['end_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
220  break;
221 
222  default:
223  break;
224  }
225  $counter++;
226  }
227  }
228  }
229  else
230  {
231  $this->schedule[$counter]['event'] = $event;
232  $this->schedule[$counter]['dstart'] = $event->getStart()->get(IL_CAL_UNIX);
233  $this->schedule[$counter]['dend'] = $event->getEnd()->get(IL_CAL_UNIX);
234  $this->schedule[$counter]['fullday'] = $event->isFullday();
235  $this->schedule[$counter]['category_id'] = $cat_map[$event->getEntryId()];
236  $this->schedule[$counter]['category_type'] = $cat_types[$cat_map[$event->getEntryId()]];
237 
238  if(!$event->isFullday())
239  {
240  switch($this->type)
241  {
242  case self::TYPE_DAY:
243  case self::TYPE_WEEK:
244  // store date info (used for calculation of overlapping events)
245  $tmp_date = new ilDateTime($this->schedule[$counter]['dstart'],IL_CAL_UNIX,$this->timezone);
246  $this->schedule[$counter]['start_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
247 
248  $tmp_date = new ilDateTime($this->schedule[$counter]['dend'],IL_CAL_UNIX,$this->timezone);
249  $this->schedule[$counter]['end_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
250  break;
251 
252  default:
253  break;
254  }
255  }
256  $counter++;
257  }
258  }
259  }
260 
269  public function getChangedEvents($a_include_subitem_calendars = false)
270  {
271  global $ilDB;
272 
273  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
274  $cats = ilCalendarCategories::_getInstance($this->user->getId())->getCategories($a_include_subitem_calendars);
275  $cats = $this->hidden_cat->filterHidden($cats,ilCalendarCategories::_getInstance($this->user->getId())->getCategoriesInfo());
276 
277  if(!count($cats))
278  {
279  return array();
280  }
281 
282  $start = new ilDate(date('Y-m-d',time()),IL_CAL_DATE);
283  $start->increment(IL_CAL_MONTH,-1);
284 
285  $query = "SELECT ce.cal_id cal_id FROM cal_entries ce ".
286  "JOIN cal_cat_assignments ca ON ca.cal_id = ce.cal_id ".
287  "WHERE last_update > ".$ilDB->quote($start->get(IL_CAL_DATETIME),'timestamp')." ".
288  "AND ".$ilDB->in('ca.cat_id',$cats,false,'integer').' '.
289  "ORDER BY last_update";
290  $res = $this->db->query($query);
291 
292  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
293  {
294  if(!$this->hidden_cat->isAppointmentVisible($row->cal_id))
295  {
296  $events[] = new ilCalendarEntry($row->cal_id);
297  }
298  }
299  return $events ? $events : array();
300  }
301 
302 
308  public function getEvents()
309  {
310  global $ilDB;
311 
312  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
313  $cats = ilCalendarCategories::_getInstance($this->user->getId())->getCategories($this->enabledSubitemCalendars());
314  if(!$this->filter_bookings)
315  {
316  $cats = $this->hidden_cat->filterHidden($cats,ilCalendarCategories::_getInstance($this->user->getId())->getCategoriesInfo());
317  }
318 
319  if(!count($cats))
320  {
321  return array();
322  }
323 
324  // TODO: optimize
325  $query = "SELECT ce.cal_id cal_id FROM cal_entries ce LEFT JOIN cal_recurrence_rules crr ON ce.cal_id = crr.cal_id ".
326  "JOIN cal_cat_assignments ca ON ca.cal_id = ce.cal_id ";
327 
328  if($this->type != self::TYPE_INBOX)
329  {
330  $query .= "WHERE ((starta <= ".$this->db->quote($this->end->get(IL_CAL_DATETIME,'','UTC'),'timestamp')." ".
331  "AND enda >= ".$this->db->quote($this->start->get(IL_CAL_DATETIME,'','UTC'),'timestamp').") ".
332  "OR (starta <= ".$this->db->quote($this->end->get(IL_CAL_DATETIME,'','UTC'),'timestamp')." ".
333  "AND NOT rule_id IS NULL)) ";
334  }
335  else
336  {
337  $date = new ilDateTime(mktime(0, 0, 0), IL_CAL_UNIX);
338  $query .= "WHERE starta >= ".$this->db->quote($date->get(IL_CAL_DATETIME,'','UTC'),'timestamp');
339  }
340 
341  $query .= "AND ".$ilDB->in('ca.cat_id',$cats,false,'integer')." ".
342  "ORDER BY starta";
343 
344  $res = $this->db->query($query);
345 
346  $events = array();
347  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
348  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
349  {
350  if(!$this->hidden_cat->isAppointmentVisible($row->cal_id) || $this->filter_bookings)
351  {
352  $event = new ilCalendarEntry($row->cal_id);
353  if(!$this->filter_bookings)
354  {
355  $events[] = $event;
356  }
357  else
358  {
359  $booking = new ilBookingEntry($event->getContextId());
360  if(!$booking->isBookedOut($row->cal_id, true))
361  {
362  $events[] = $event;
363  }
364  }
365  }
366 
367  }
368  /*
369  foreach($events as $event)
370  {
371  var_dump("<pre>",$event->getTitle(),"</pre>");
372  }
373  */
374  return $events;
375  }
376 
384  protected function initPeriod(ilDate $seed)
385  {
386  switch($this->type)
387  {
388  case self::TYPE_DAY:
389  $this->start = clone $seed;
390  $this->end = clone $seed;
391  $this->start->increment(IL_CAL_DAY,-2);
392  $this->end->increment(IL_CAL_DAY,2);
393  break;
394 
395  case self::TYPE_WEEK:
396  $this->start = clone $seed;
397  $start_info = $this->start->get(IL_CAL_FKT_GETDATE,'','UTC');
398  $day_diff = $this->weekstart - $start_info['isoday'];
399  if($day_diff == 7)
400  {
401  $day_diff = 0;
402  }
403  $this->start->increment(IL_CAL_DAY,$day_diff);
404  $this->start->increment(IL_CAL_DAY,-1);
405  $this->end = clone $this->start;
406  $this->end->increment(IL_CAL_DAY,9);
407  break;
408 
409  case self::TYPE_MONTH:
410  $year_month = $seed->get(IL_CAL_FKT_DATE,'Y-m','UTC');
411  list($year,$month) = explode('-',$year_month);
412 
413  $this->start = new ilDate($year_month.'-01',IL_CAL_DATE);
414  $this->start->increment(IL_CAL_DAY,-6);
415 
416  $this->end = new ilDate($year_month.'-'.ilCalendarUtil::_getMaxDayOfMonth($year,$month),IL_CAL_DATE);
417  $this->end->increment(IL_CAL_DAY,6);
418  break;
419  }
420 
421  return true;
422  }
423 }
424 ?>