ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCalendarEntry.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once('Services/Calendar/classes/class.ilDate.php');
5 include_once('./Services/Calendar/interfaces/interface.ilDatePeriod.php');
6 
7 define('IL_CAL_TRANSLATION_NONE',0);
8 define('IL_CAL_TRANSLATION_SYSTEM',1);
9 
10 
20 class ilCalendarEntry implements ilDatePeriod
21 {
22  protected $log;
23  protected $db;
24 
25 
26  protected $entry_id;
27  protected $last_update;
28  protected $title;
29  protected $subtitle;
30  protected $description;
31  protected $location;
33  protected $start = null;
34  protected $fullday;
35  protected $end = null;
36  protected $is_auto_generated = false;
37  protected $context_id = 0;
39  protected $is_milestone = false;
40  protected $completion = 0;
41 
42  protected $notification = false;
43 
51  public function __construct($a_id = 0)
52  {
53  global $ilDB,$ilLog;
54 
55  $this->log = $ilLog;
56  $this->db = $ilDB;
57 
58  if($this->entry_id = $a_id)
59  {
60  $this->read();
61  }
62  }
63 
67  public function __clone()
68  {
69  $this->entry_id = NULL;
70  }
71 
79  public static function _delete($a_entry_id)
80  {
81  global $ilDB;
82 
83  include_once('./Services/Calendar/classes/class.ilCalendarRecurrence.php');
84  ilCalendarRecurrence::_delete($a_entry_id);
85 
86  $query = "DELETE FROM cal_entries ".
87  "WHERE cal_id = ".$ilDB->quote($a_entry_id ,'integer')." ";
88  $res = $ilDB->manipulate($query);
89 
90  return true;
91  }
92 
99  public function getEntryId()
100  {
101  return $this->entry_id;
102  }
103 
111  public function getLastUpdate()
112  {
113  return $this->last_update ? $this->last_update : new ilDateTime(time(),IL_CAL_UNIX);
114  }
115 
123  public function setLastUpdate($a_date)
124  {
125  $this->last_update = $a_date;
126  }
127 
128 
135  public function getStart()
136  {
137  return $this->start ? $this->start : $this->start = new ilDateTime();
138 
139  }
140 
147  public function setStart(ilDateTime $a_start)
148  {
149  $this->start = $a_start;
150  }
151 
157  public function getEnd()
158  {
159  return $this->end ? $this->end : $this->end = new ilDateTime();
160  }
161 
167  public function setEnd($a_end)
168  {
169  $this->end = $a_end;
170  }
171 
179  public function setTitle($a_title)
180  {
181  $this->title = $a_title;
182  }
183 
190  public function getTitle()
191  {
192  return $this->title;
193  }
194 
202  public function getPresentationTitle($a_shorten = true)
203  {
204  global $lng;
205 
207  {
208  $title = $this->getTitle();
209  }
210  elseif(strlen($this->getSubtitle()))
211  {
212  // parse dynamic title?
213  if(preg_match("/#([a-z]+)#/", $this->getSubtitle(), $matches))
214  {
215  $subtitle = $this->parseDynamicTitle($matches[1]);
216  }
217  else
218  {
219  $subtitle = $lng->txt($this->getSubtitle());
220  }
221  $title = $this->getTitle().
222  (strlen($subtitle)
223  ? ' ('.$subtitle.')'
224  : '');
225  }
226  else
227  {
228  $title = $this->getTitle();
229  }
230 
231  if($a_shorten)
232  {
233  return ilUtil::shortenText(ilUtil::shortenWords($title,20),40,true);
234  }
235  return $title;
236  }
237 
238  public function getPresentationStyle()
239  {
240  // see parseDynamicTitle()
241  return $this->presentation_style;
242  }
243 
244  protected function parseDynamicTitle($a_type)
245  {
246  global $lng;
247 
248  $title = $style = "";
249  switch($a_type)
250  {
251  case "consultationhour":
252  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
253  $entry = new ilBookingEntry($this->getContextId());
254  if($entry)
255  {
256  if($entry->isOwner())
257  {
258  $max = (int)$entry->getNumberOfBookings();
259  $current = (int)$entry->getCurrentNumberOfBookings($this->getEntryId());
260  if(!$current)
261  {
262  $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: green';
263  $title = $lng->txt('cal_book_free');
264  }
265  elseif($current >= $max)
266  {
267  $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: red';
268  $title = $lng->txt('cal_booked_out');
269  }
270  else
271  {
272  $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: yellow';
273  $title = $current.'/'.$max;
274  }
275  }
276  else
277  {
278  /*
279  * if($entry->hasBooked($this->getEntryId()))
280  */
281  include_once 'Services/Calendar/classes/ConsultationHours/class.ilConsultationHourAppointments.php';
282  $apps = ilConsultationHourAppointments::getAppointmentIds($entry->getObjId(), $this->getContextId(), $this->getStart());
283  $orig_event = $apps[0];
284  if($entry->hasBooked($orig_event))
285  {
286  $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: green';
287  $title = $lng->txt('cal_date_booked');
288  }
289  }
290  }
291  break;
292  }
293 
294  if($style)
295  {
296  $this->presentation_style = $style;
297  }
298  return $title;
299  }
300 
310  public function setSubtitle($a_subtitle)
311  {
312  $this->subtitle = $a_subtitle;
313  }
314 
321  public function getSubtitle()
322  {
323  return $this->subtitle;
324  }
325 
333  public function setDescription($a_description)
334  {
335  $this->description = $a_description;
336  }
337 
343  public function getDescription()
344  {
345  return $this->description;
346  }
347 
355  public function setLocation($a_location)
356  {
357  $this->location = $a_location;
358  }
359 
365  public function getLocation()
366  {
367  return $this->location;
368  }
369 
377  public function setFurtherInformations($a_informations)
378  {
379  $this->further_informations = $a_informations;
380  }
381 
387  public function getFurtherInformations()
388  {
390  }
391 
401  public function setFullday($a_fullday)
402  {
403  $this->fullday = (bool) $a_fullday;
404  }
405 
411  public function isFullday()
412  {
413  return (bool) $this->fullday;
414  }
415 
423  public function isAutoGenerated()
424  {
425  return (bool) $this->is_auto_generated;
426  }
427 
435  public function setAutoGenerated($a_status)
436  {
437  $this->is_auto_generated = $a_status;
438  }
439 
447  public function isMilestone()
448  {
449  return (bool) $this->is_milestone;
450  }
451 
459  public function setMilestone($a_status)
460  {
461  $this->is_milestone = $a_status;
462  }
463 
469  function setCompletion($a_completion)
470  {
471  $this->completion = $a_completion;
472  }
473 
479  function getCompletion()
480  {
481  return $this->completion;
482  }
483 
491  public function setContextId($a_context_id)
492  {
493  $this->context_id = $a_context_id;
494  }
495 
502  public function getContextId()
503  {
504  return $this->context_id;
505  }
506 
514  public function setTranslationType($a_type)
515  {
516  $this->translation_type = $a_type;
517  }
518 
525  public function getTranslationType()
526  {
528  }
529 
534  public function enableNotification($a_status)
535  {
536  $this->notification = $a_status;
537  }
538 
543  public function isNotificationEnabled()
544  {
545  return (bool) $this->notification;
546  }
547 
554  public function update()
555  {
556  global $ilDB;
557 
558  $now = new ilDateTime(time(),IL_CAL_UNIX);
559  $utc_timestamp = $now->get(IL_CAL_DATETIME,'',ilTimeZone::UTC);
560 
561 
562  $query = "UPDATE cal_entries ".
563  "SET title = ".$this->db->quote($this->getTitle() ,'text').", ".
564  "last_update = ".$ilDB->quote($utc_timestamp,'timestamp').", ".
565  "subtitle = ".$this->db->quote($this->getSubtitle() ,'text').", ".
566  "description = ".$this->db->quote($this->getDescription(),'text').", ".
567  "location = ".$this->db->quote($this->getLocation() ,'text').", ".
568  "fullday = ".$ilDB->quote($this->isFullday() ? 1 : 0,'integer').", ".
569  "starta = ".$this->db->quote($this->getStart()->get(IL_CAL_DATETIME,'','UTC'),'timestamp').", ".
570  "enda = ".$this->db->quote($this->getEnd()->get(IL_CAL_DATETIME,'','UTC'),'timestamp').", ".
571  "informations = ".$this->db->quote($this->getFurtherInformations() ,'text').", ".
572  "auto_generated = ".$this->db->quote($this->isAutoGenerated() ,'integer').", ".
573  "translation_type = ".$this->db->quote($this->getTranslationType() ,'integer').", ".
574  "context_id = ".$this->db->quote($this->getContextId() ,'integer').", ".
575  "completion = ".$this->db->quote($this->getCompletion(), 'integer').", ".
576  "is_milestone = ".$this->db->quote($this->isMilestone() ? 1 : 0, 'integer').", ".
577  'notification = '.$this->db->quote($this->isNotificationEnabled() ? 1 : 0,'integer').' '.
578  "WHERE cal_id = ".$this->db->quote($this->getEntryId() ,'integer')." ";
579  $res = $ilDB->manipulate($query);
580 
581  return true;
582  }
583 
590  public function save()
591  {
592  global $ilDB;
593 
594  $next_id = $ilDB->nextId('cal_entries');
595  $now = new ilDateTime(time(),IL_CAL_UNIX);
596  $utc_timestamp = $now->get(IL_CAL_DATETIME,'',ilTimeZone::UTC);
597 
598  $query = "INSERT INTO cal_entries (cal_id,title,last_update,subtitle,description,location,fullday,starta,enda, ".
599  "informations,auto_generated,context_id,translation_type, completion, is_milestone, notification) ".
600  "VALUES( ".
601  $ilDB->quote($next_id,'integer').", ".
602  $this->db->quote($this->getTitle(),'text').", ".
603  $ilDB->quote($utc_timestamp,'timestamp').", ".
604  $this->db->quote($this->getSubtitle(),'text').", ".
605  $this->db->quote($this->getDescription() ,'text').", ".
606  $this->db->quote($this->getLocation() ,'text').", ".
607  $ilDB->quote($this->isFullday() ? 1 : 0,'integer').", ".
608  $this->db->quote($this->getStart()->get(IL_CAL_DATETIME,'','UTC'),'timestamp').", ".
609  $this->db->quote($this->getEnd()->get(IL_CAL_DATETIME,'','UTC'),'timestamp').", ".
610  $this->db->quote($this->getFurtherInformations() ,'text').", ".
611  $this->db->quote($this->isAutoGenerated() ,'integer').", ".
612  $this->db->quote($this->getContextId() ,'integer').", ".
613  $this->db->quote($this->getTranslationType() ,'integer').", ".
614  $this->db->quote($this->getCompletion(), 'integer').", ".
615  $this->db->quote($this->isMilestone() ? 1 : 0, 'integer').", ".
616  $this->db->quote($this->isNotificationEnabled() ? 1 : 0,'integer').' '.
617  ")";
618  $res = $ilDB->manipulate($query);
619 
620  $this->entry_id = $next_id;
621  return true;
622  }
623 
630  public function delete()
631  {
632  global $ilDB;
633 
634  include_once('./Services/Calendar/classes/class.ilCalendarRecurrence.php');
636 
637  $query = "DELETE FROM cal_entries ".
638  "WHERE cal_id = ".$this->db->quote($this->getEntryId() ,'integer')." ";
639  $res = $ilDB->manipulate($query);
640 
641  include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
643 
644  return true;
645  }
646 
653  public function validate()
654  {
655  global $ilErr,$lng;
656 
657  $success = true;
658  $ilErr->setMessage('');
659  if(!strlen($this->getTitle()))
660  {
661  $success = false;
662  $ilErr->appendMessage($lng->txt('err_missing_title'));
663  }
664  if(ilDateTime::_before($this->getEnd(),$this->getStart(),''))
665  {
666  $success = false;
667  $ilErr->appendMessage($lng->txt('err_end_before_start'));
668  }
669  return $success;
670  }
671 
672 
673 
679  protected function read()
680  {
681  global $ilDB;
682 
683  $query = "SELECT * FROM cal_entries WHERE cal_id = ".$this->db->quote($this->getEntryId() ,'integer')." ";
684  $res = $this->db->query($query);
685  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
686  {
687  $this->setLastUpdate(new ilDateTime($row->last_update,IL_CAL_DATETIME,'UTC'));
688  $this->setTitle($row->title);
689  $this->setSubtitle($row->subtitle);
690  $this->setDescription($row->description);
691  $this->setLocation($row->location);
692  $this->setFurtherInformations($row->informations);
693  $this->setFullday((bool) $row->fullday);
694  $this->setAutoGenerated($row->auto_generated);
695  $this->setContextId($row->context_id);
696  $this->setTranslationType($row->translation_type);
697  $this->setCompletion($row->completion);
698  $this->setMilestone($row->is_milestone);
699  $this->enableNotification((bool) $row->notification);
700 
701  if($this->isFullday())
702  {
703  $this->start = new ilDate($row->starta,IL_CAL_DATETIME);
704  $this->end = new ilDate($row->enda,IL_CAL_DATETIME);
705  }
706  else
707  {
708  $this->start = new ilDateTime($row->starta,IL_CAL_DATETIME,'UTC');
709  $this->end = new ilDateTime($row->enda,IL_CAL_DATETIME,'UTC');
710  }
711  }
712 
713  }
714 
720  public function appointmentToMailString($lng)
721  {
722  $body = $lng->txt('cal_details');
723  $body .= "\n\n";
724  $body .= $lng->txt('title').': '.$this->getTitle()."\n";
725 
727  $body .= $lng->txt('date').': '.ilDatePresentation::formatPeriod($this->getStart(), $this->getEnd())."\n";
729 
730  if(strlen($this->getLocation()))
731  {
732  $body .= $lng->txt('cal_where').': '.$this->getLocation()."\n";
733  }
734 
735  if(strlen($this->getDescription()))
736  {
737  $body .= $lng->txt('description').': '.$this->getDescription()."\n";
738  }
739  return $body;
740  }
741 
742 
746  function writeResponsibleUsers($a_users)
747  {
748  global $ilDB;
749 
750  $ilDB->manipulateF("DELETE FROM cal_entry_responsible WHERE cal_id = %s",
751  array("integer"), array($this->getEntryId()));
752 
753  if (is_array($a_users))
754  {
755  foreach ($a_users as $user_id)
756  {
757  $ilDB->manipulateF("INSERT INTO cal_entry_responsible (cal_id, user_id) ".
758  " VALUES (%s,%s)", array("integer", "integer"),
759  array($this->getEntryId(), $user_id));
760  }
761  }
762 
763  $this->responsible_users = $a_users;
764  }
765 
770  {
771  global $ilDB;
772 
773  $set = $ilDB->queryF("SELECT * FROM cal_entry_responsible WHERE cal_id = %s",
774  array("integer"), array($this->getEntryId()));
775 
776  $return = array();
777  while($rec = $ilDB->fetchAssoc($set))
778  {
779  $n = ilObjUser::_lookupName($rec["user_id"]);
780  $return[] = array_merge($n,
781  array("login" => ilObjUser::_lookupLogin($rec["user_id"])));
782  }
783 
784  return $return;
785  }
786 }
787 ?>