ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  const TYPE_PD_UPCOMING = 5;
48 
49  protected $limit_events = -1;
50  protected $schedule = array();
51  protected $timezone;
52  protected $weekstart;
53  protected $type = 0;
54 
55  protected $subitems_enabled = false;
56 
57  protected $start = null;
58  protected $end = null;
59  protected $user = null;
60  protected $user_settings = null;
61  protected $db = null;
62  protected $filters = array();
63 
73  public function __construct(ilDate $seed,$a_type,$a_user_id = 0)
74  {
75  global $ilUser,$ilDB;
76 
77  $this->db = $ilDB;
78 
79  $this->type = $a_type;
80  $this->initPeriod($seed);
81 
82  if(!$a_user_id || $a_user_id == $ilUser->getId())
83  {
84  $this->user = $ilUser;
85  }
86  else
87  {
88  $this->user = new ilObjUser($a_user_id);
89  }
90  $this->user_settings = ilCalendarUserSettings::_getInstanceByUserId($this->user->getId());
91  $this->weekstart = $this->user_settings->getWeekStart();
92  $this->timezone = $this->user->getTimeZone();
93 
94 
95  // category / event filters
96 
97  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
98 
99  // portfolio does custom filter handling (booking group ids)
101  {
102  // consultation hour calendar views do not mind calendar category visibility
104  {
105  // this is the "default" filter which handles currently hidden categories for the user
106  include_once('./Services/Calendar/classes/class.ilCalendarScheduleFilterHidden.php');
107  $this->addFilter(new ilCalendarScheduleFilterHidden($this->user->getId()));
108  }
109  else
110  {
111  // handle booking visibility (target object, booked out)
112  include_once('./Services/Calendar/classes/class.ilCalendarScheduleFilterBookings.php');
113  $this->addFilter(new ilCalendarScheduleFilterBookings($this->user->getId()));
114  }
115 
116  // exercise
117  include_once './Services/Calendar/classes/class.ilCalendarScheduleFilterExercise.php';
118  $this->addFilter(new ilCalendarScheduleFilterExercise($this->user->getId()));
119  }
120 
121  }
122 
127  protected function areEventsLimited()
128  {
129  return $this->limit_events != -1;
130  }
131 
136  public function getEventsLimit()
137  {
138  return $this->limit_events;
139  }
140 
145  public function setEventsLimit($a_limit)
146  {
147  $this->limit_events = $a_limit;
148  }
149 
155  public function addSubitemCalendars($a_status)
156  {
157  $this->subitems_enabled = $a_status;
158  }
159 
164  public function enabledSubitemCalendars()
165  {
166  return (bool) $this->subitems_enabled;
167  }
168 
174  public function addFilter(ilCalendarScheduleFilter $a_filter)
175  {
176  $this->filters[] = $a_filter;
177  }
178 
186  public function getByDay(ilDate $a_start,$a_timezone)
187  {
189  $fstart = new ilDate($a_start->get(IL_CAL_UNIX),IL_CAL_UNIX);
190  $fend = clone $fstart;
191 
192  $f_unix_start = $fstart->get(IL_CAL_UNIX);
193  $fend->increment(ilDateTime::DAY,1);
194  $f_unix_end = $fend->get(IL_CAL_UNIX);
195 
196  $unix_start = $start->get(IL_CAL_UNIX);
197  $start->increment(ilDateTime::DAY,1);
198  $unix_end = $start->get(IL_CAL_UNIX);
199 
200  $counter = 0;
201 
202  $tmp_date = new ilDateTime($unix_start,IL_CAL_UNIX,$this->timezone);
203  $tmp_schedule = array();
204  foreach($this->schedule as $schedule)
205  {
206  if($schedule['fullday'])
207  {
208  if(($f_unix_start == $schedule['dstart']) or
209  $f_unix_start == $schedule['dend'] or
210  ($f_unix_start > $schedule['dstart'] and $f_unix_end <= $schedule['dend']))
211  {
212  $tmp_schedule[] = $schedule;
213  }
214  }
215  elseif(($schedule['dstart'] == $unix_start) or
216  (($schedule['dstart'] <= $unix_start) and ($schedule['dend'] > $unix_start)) or
217  (($schedule['dstart'] >= $unix_start) and ($schedule['dstart'] < $unix_end)))
218  {
219  $tmp_schedule[] = $schedule;
220  }
221  }
222  return $tmp_schedule;
223  }
224 
225 
231  public function calculate()
232  {
233  $events = $this->getEvents();
234 
235  // we need category type for booking handling
236  $ids = array();
237  foreach($events as $event)
238  {
239  $ids[] = $event->getEntryId();
240  }
241 
242  include_once('Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
244  include_once('Services/Calendar/classes/class.ilCalendarCategory.php');
245  $cat_types = array();
246  foreach(array_unique($cat_map) as $cat_id)
247  {
248  $cat = new ilCalendarCategory($cat_id);
249  $cat_types[$cat_id] = $cat->getType();
250  }
251 
252  $counter = 0;
253  foreach($events as $event)
254  {
255  // Calculdate recurring events
256  include_once('Services/Calendar/classes/class.ilCalendarRecurrences.php');
257  if($recs = ilCalendarRecurrences::_getRecurrences($event->getEntryId()))
258  {
259  $duration = $event->getEnd()->get(IL_CAL_UNIX) - $event->getStart()->get(IL_CAL_UNIX);
260  foreach($recs as $rec)
261  {
262  $calc = new ilCalendarRecurrenceCalculator($event,$rec);
263  foreach($calc->calculateDateList($this->start,$this->end)->get() as $rec_date)
264  {
265  if($this->type == self::TYPE_PD_UPCOMING &&
266  $rec_date->get(IL_CAL_UNIX) < time())
267  {
268  continue;
269  }
270 
271  $this->schedule[$counter]['event'] = $event;
272  $this->schedule[$counter]['dstart'] = $rec_date->get(IL_CAL_UNIX);
273  $this->schedule[$counter]['dend'] = $this->schedule[$counter]['dstart'] + $duration;
274  $this->schedule[$counter]['fullday'] = $event->isFullday();
275  $this->schedule[$counter]['category_id'] = $cat_map[$event->getEntryId()];
276  $this->schedule[$counter]['category_type'] = $cat_types[$cat_map[$event->getEntryId()]];
277 
278  switch($this->type)
279  {
280  case self::TYPE_DAY:
281  case self::TYPE_WEEK:
282  // store date info (used for calculation of overlapping events)
283  $tmp_date = new ilDateTime($this->schedule[$counter]['dstart'],IL_CAL_UNIX,$this->timezone);
284  $this->schedule[$counter]['start_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
285 
286  $tmp_date = new ilDateTime($this->schedule[$counter]['dend'],IL_CAL_UNIX,$this->timezone);
287  $this->schedule[$counter]['end_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
288  break;
289 
290  default:
291  break;
292  }
293  $counter++;
294  if($this->type != self::TYPE_PD_UPCOMING &&
295  $this->areEventsLimited() && $counter >= $this->getEventsLimit())
296  {
297  break;
298  }
299 
300  }
301  }
302  }
303  else
304  {
305  $this->schedule[$counter]['event'] = $event;
306  $this->schedule[$counter]['dstart'] = $event->getStart()->get(IL_CAL_UNIX);
307  $this->schedule[$counter]['dend'] = $event->getEnd()->get(IL_CAL_UNIX);
308  $this->schedule[$counter]['fullday'] = $event->isFullday();
309  $this->schedule[$counter]['category_id'] = $cat_map[$event->getEntryId()];
310  $this->schedule[$counter]['category_type'] = $cat_types[$cat_map[$event->getEntryId()]];
311 
312  if(!$event->isFullday())
313  {
314  switch($this->type)
315  {
316  case self::TYPE_DAY:
317  case self::TYPE_WEEK:
318  // store date info (used for calculation of overlapping events)
319  $tmp_date = new ilDateTime($this->schedule[$counter]['dstart'],IL_CAL_UNIX,$this->timezone);
320  $this->schedule[$counter]['start_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
321 
322  $tmp_date = new ilDateTime($this->schedule[$counter]['dend'],IL_CAL_UNIX,$this->timezone);
323  $this->schedule[$counter]['end_info'] = $tmp_date->get(IL_CAL_FKT_GETDATE,'',$this->timezone);
324  break;
325 
326  default:
327  break;
328  }
329  }
330  $counter++;
331  if($this->type != self::TYPE_PD_UPCOMING &&
332  $this->areEventsLimited() && $counter >= $this->getEventsLimit())
333  {
334  break;
335  }
336  }
337  }
338 
339  if($this->type == self::TYPE_PD_UPCOMING)
340  {
341  $this->schedule = ilUtil::sortArray($this->schedule, "dstart", "asc", true);
342  if($this->areEventsLimited() && sizeof($this->schedule) >= $this->getEventsLimit())
343  {
344  $this->schedule = array_slice($this->schedule, 0, $this->getEventsLimit());
345  }
346  }
347  }
348 
349  public function getScheduledEvents()
350  {
351  return (array) $this->schedule;
352  }
353 
354  protected function filterCategories(array $a_cats)
355  {
356  if(!sizeof($a_cats))
357  {
358  return;
359  }
360 
361  foreach($this->filters as $filter)
362  {
363  if(sizeof($a_cats))
364  {
365  $a_cats = $filter->filterCategories($a_cats);
366  }
367  }
368 
369  return $a_cats;
370  }
371 
372  protected function modifyEventByFilters(ilCalendarEntry $event)
373  {
374  foreach($this->filters as $filter)
375  {
376  $res = $filter->modifyEvent($event);
377  if(!$res)
378  {
379  ilLoggerFactory::getLogger('crs')->debug('filtering failed for ' . get_class($filter));
380  return FALSE;
381  }
382  $event = $res;
383  }
384  return $event;
385  }
386 
387  protected function addCustomEvents(ilDate $start, ilDate $end, array $categories)
388  {
389  $new_events = array();
390  foreach($this->filters as $filter)
391  {
392  $events_by_filter = $filter->addCustomEvents($start, $end, $categories);
393  if($events_by_filter)
394  {
395  $new_events = array_merge($new_events, $events_by_filter);
396  }
397  }
398  return $new_events;
399  }
400 
409  public function getChangedEvents($a_include_subitem_calendars = false)
410  {
411  global $ilDB;
412 
413  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
414  $cats = ilCalendarCategories::_getInstance($this->user->getId())->getCategories($a_include_subitem_calendars);
415  $cats = $this->filterCategories($cats);
416 
417  if(!count($cats))
418  {
419  return array();
420  }
421 
422  $start = new ilDate(date('Y-m-d',time()),IL_CAL_DATE);
423  $start->increment(IL_CAL_MONTH,-1);
424 
425  $query = "SELECT ce.cal_id cal_id FROM cal_entries ce ".
426  "JOIN cal_cat_assignments ca ON ca.cal_id = ce.cal_id ".
427  "WHERE last_update > ".$ilDB->quote($start->get(IL_CAL_DATETIME),'timestamp')." ".
428  "AND ".$ilDB->in('ca.cat_id',$cats,false,'integer').' '.
429  "ORDER BY last_update";
430  $res = $this->db->query($query);
431 
432  while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
433  {
434  $event = new ilCalendarEntry($row->cal_id);
435  $valid_event = $this->modifyEventByFilters($event);
436  if($valid_event)
437  {
438  $events[] = $valid_event;
439  }
440  }
441 
442  foreach($this->addCustomEvents($this->start, $this->end, $cats) as $event)
443  {
444  $events[] = $event;
445  }
446 
447  return $events ? $events : array();
448  }
449 
450 
456  public function getEvents()
457  {
458  global $ilDB;
459 
460  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
461  $cats = ilCalendarCategories::_getInstance($this->user->getId())->getCategories($this->enabledSubitemCalendars());
462  $cats = $this->filterCategories($cats);
463 
464  if(!count($cats))
465  {
466  return array();
467  }
468 
469  // TODO: optimize
470  $query = "SELECT ce.cal_id cal_id".
471  " FROM cal_entries ce".
472  " LEFT JOIN cal_recurrence_rules crr ON (ce.cal_id = crr.cal_id)".
473  " JOIN cal_cat_assignments ca ON (ca.cal_id = ce.cal_id)";
474 
475  if($this->type != self::TYPE_INBOX)
476  {
477  $query .= " WHERE ((starta <= ".$this->db->quote($this->end->get(IL_CAL_DATETIME,'','UTC'),'timestamp').
478  " AND enda >= ".$this->db->quote($this->start->get(IL_CAL_DATETIME,'','UTC'),'timestamp').")".
479  " OR (starta <= ".$this->db->quote($this->end->get(IL_CAL_DATETIME,'','UTC'),'timestamp').
480  " AND NOT rule_id IS NULL))";
481  }
482  else
483  {
484  $date = new ilDateTime(mktime(0, 0, 0), IL_CAL_UNIX);
485  $query .= " WHERE starta >= ".$this->db->quote($date->get(IL_CAL_DATETIME,'','UTC'),'timestamp');
486  }
487 
488  $query .= " AND ".$ilDB->in('ca.cat_id',$cats,false,'integer').
489  " ORDER BY starta";
490 
491  $res = $this->db->query($query);
492 
493  $events = array();
494  while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
495  {
496  $event = new ilCalendarEntry($row->cal_id);
497  $valid_event = $this->modifyEventByFilters($event);
498  if($valid_event)
499  {
500  $events[] = $valid_event;
501  }
502  }
503 
504  foreach($this->addCustomEvents($this->start, $this->end, $cats) as $event)
505  {
506  $events[] = $event;
507  }
508 
509  return $events;
510  }
511 
519  protected function initPeriod(ilDate $seed)
520  {
521  switch($this->type)
522  {
523  case self::TYPE_DAY:
524  $this->start = clone $seed;
525  $this->end = clone $seed;
526  $this->start->increment(IL_CAL_DAY,-2);
527  $this->end->increment(IL_CAL_DAY,2);
528  break;
529 
530  case self::TYPE_WEEK:
531  $this->start = clone $seed;
532  $start_info = $this->start->get(IL_CAL_FKT_GETDATE,'','UTC');
533  $day_diff = $this->weekstart - $start_info['isoday'];
534  if($day_diff == 7)
535  {
536  $day_diff = 0;
537  }
538  $this->start->increment(IL_CAL_DAY,$day_diff);
539  $this->start->increment(IL_CAL_DAY,-1);
540  $this->end = clone $this->start;
541  $this->end->increment(IL_CAL_DAY,9);
542  break;
543 
544  case self::TYPE_MONTH:
545  $year_month = $seed->get(IL_CAL_FKT_DATE,'Y-m','UTC');
546  list($year,$month) = explode('-',$year_month);
547 
548  $this->start = new ilDate($year_month.'-01',IL_CAL_DATE);
549  $this->start->increment(IL_CAL_DAY,-6);
550 
551  $this->end = new ilDate($year_month.'-'.ilCalendarUtil::_getMaxDayOfMonth($year,$month),IL_CAL_DATE);
552  $this->end->increment(IL_CAL_DAY,6);
553  break;
554 
555  case self::TYPE_PD_UPCOMING:
556  case self::TYPE_INBOX:
557  $this->start = $seed;
558  $this->end = clone $this->start;
559  $this->end->increment(IL_CAL_MONTH,3);
560  break;
561  }
562 
563  return true;
564  }
565 }
566 ?>
Set timezone
static _getRecurrences($a_cal_id)
get all recurrences of an appointment
Model for a calendar entry.
areEventsLimited()
Check if events are limited.
modifyEventByFilters(ilCalendarEntry $event)
getEventsLimit()
get current limit of events
Calendar schedule filter for consultation hour bookings.
const IL_CAL_DATETIME
static _getInstanceByUserId($a_user_id)
get singleton instance
enabledSubitemCalendars()
Are subitem calendars enabled.
addSubitemCalendars($a_status)
Enable subitem calendars (session calendars for courses)
const IL_CAL_MONTH
Stores calendar categories.
setEventsLimit($a_limit)
Set events limit.
const IL_CAL_UNIX
getByDay(ilDate $a_start, $a_timezone)
get byday
static sortArray($array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
user()
Definition: user.php:4
get($a_format, $a_format_str='', $a_tz='')
get formatted date
static _getMaxDayOfMonth($a_year, $a_month)
get max day of month 2008,2 => 29
$counter
$a_type
Definition: workflow.php:93
const IL_CAL_DAY
Calendar schedule filter for hidden categories.
Calendar schedule filter interface.
Class for single dates.
Calculates an ilDateList for a given calendar entry and recurrence rule.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
const IL_CAL_FKT_DATE
Date and time handling
$ilUser
Definition: imgupload.php:18
Calendar schedule filter for exercises.
get($a_format, $a_format_str='', $a_tz='')
get formatted date
increment($a_type, $a_count=1)
increment
static _getInstance($a_usr_id=0)
get singleton instance
getEvents()
Read events (will be moved to another class, since only active and/or visible calendars are shown) ...
Create styles array
The data for the language used.
initPeriod(ilDate $seed)
init period of events
const IL_CAL_FKT_GETDATE
const IL_CAL_DATE
__construct(ilDate $seed, $a_type, $a_user_id=0)
Constructor.
addFilter(ilCalendarScheduleFilter $a_filter)
Add filter.
getChangedEvents($a_include_subitem_calendars=false)
get new/changed events
global $ilDB
static getLogger($a_component_id)
Get component logger.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
addCustomEvents(ilDate $start, ilDate $end, array $categories)
Represents a list of calendar appointments (including recurring events) for a specific user in a give...
static _getAppointmentCalendars($a_cal_ids)
lookup calendars for appointment ids