ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCalendarEntry.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
27 class ilCalendarEntry implements ilDatePeriod
28 {
29  public const TRANSLATION_NONE = 0;
30  public const TRANSLATION_SYSTEM = 1;
31 
32  protected ilLogger $log;
33  protected ilDBInterface $db;
34  protected ilLanguage $lng;
36 
37  protected int $entry_id = 0;
38  protected ?ilDateTime $last_update = null;
39  protected string $title = '';
40  protected string $presentation_style = '';
41  protected string $subtitle = '';
42  protected string $description = '';
43  protected string $location = '';
44  protected string $further_informations = '';
45  protected ?ilDateTime $start = null;
46  protected bool $fullday = false;
47  protected ?ilDateTime $end = null;
48  protected bool $is_auto_generated = false;
49  protected int $context_id = 0;
50  protected string $context_info = '';
52  protected bool $notification = false;
53 
54  public function __construct(int $a_id = 0)
55  {
56  global $DIC;
57 
58  $this->log = $DIC->logger()->cal();
59  $this->lng = $DIC->language();
60  $this->db = $DIC->database();
61  $this->error = $DIC['ilErr'];
62  $this->entry_id = $a_id;
63  if ($this->entry_id > 0) {
64  $this->read();
65  }
66  }
67 
71  public function __clone()
72  {
73  $this->entry_id = 0;
74  }
75 
76  public static function _delete(int $a_entry_id): void
77  {
78  global $DIC;
79 
80  $ilDB = $DIC['ilDB'];
81  ilCalendarRecurrence::_delete($a_entry_id);
82 
83  $query = "DELETE FROM cal_entries " .
84  "WHERE cal_id = " . $ilDB->quote($a_entry_id, 'integer') . " ";
85  $res = $ilDB->manipulate($query);
86  }
87 
88  public function setContextInfo(string $a_info): void
89  {
90  $this->context_info = $a_info;
91  }
92 
93  public function getContextInfo(): string
94  {
95  return $this->context_info;
96  }
97 
98  public function getEntryId(): int
99  {
100  return $this->entry_id;
101  }
102 
103  public function getLastUpdate(): ilDateTime
104  {
105  return $this->last_update ?: new ilDateTime(time(), IL_CAL_UNIX);
106  }
107 
108  public function setLastUpdate(ilDateTime $a_date): void
109  {
110  $this->last_update = $a_date;
111  }
112 
113  public function getStart(): ?ilDateTime
114  {
115  return $this->start;
116  }
117 
118  public function setStart(?ilDateTime $a_start): void
119  {
120  $this->start = $a_start;
121  }
122 
123  public function getEnd(): ?ilDateTime
124  {
125  return $this->end;
126  }
127 
128  public function setEnd(?ilDateTime $a_end): void
129  {
130  $this->end = $a_end;
131  }
132 
133  public function setTitle(string $a_title): void
134  {
135  $this->title = $a_title;
136  }
137 
138  public function getTitle(): string
139  {
140  return $this->title;
141  }
142 
143  public function getPresentationTitle(bool $a_shorten = true): string
144  {
146  $title = $this->getTitle();
147  } elseif (strlen($this->getSubtitle())) {
148  // parse dynamic title?
149  if (preg_match("/#([a-z]+)#/", $this->getSubtitle(), $matches)) {
150  $subtitle = $this->parseDynamicTitle($matches[1]);
151  } else {
152  $subtitle = $this->lng->txt($this->getSubtitle());
153  }
154  $title = $this->getTitle() .
155  (strlen($subtitle)
156  ? ' (' . $subtitle . ')'
157  : '');
158  } else {
159  $title = $this->lng->txt($this->getTitle());
160  }
161 
162  if ($a_shorten) {
163  return ilStr::shortenTextExtended(ilStr::shortenWords($title, 20), 40, true);
164  }
165  return $title;
166  }
167 
168  protected function parseDynamicTitle(string $a_type): string
169  {
170  $title = $style = "";
171  switch ($a_type) {
172  case "consultationhour":
173  $entry = new ilBookingEntry($this->getContextId());
174  if ($entry) {
175  if ($entry->isOwner()) {
176  $max = $entry->getNumberOfBookings();
177  $current = $entry->getCurrentNumberOfBookings($this->getEntryId());
178  if (!$current) {
179  $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: green';
180  $title = $this->lng->txt('cal_book_free');
181  } elseif ($current >= $max) {
182  $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: red';
183  $title = $this->lng->txt('cal_booked_out');
184  } else {
185  $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: yellow';
186  $title = $current . '/' . $max;
187  }
188  } else {
190  $entry->getObjId(),
191  $this->getContextId(),
192  $this->getStart()
193  );
194  if ($apps === []) {
195  $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: red';
196  $title = $this->lng->txt('cal_booked_out');
197  } else {
198  $orig_event = $apps[0];
199  $max = $entry->getNumberOfBookings();
200  $current = $entry->getCurrentNumberOfBookings($this->getEntryId());
201  if ($entry->hasBooked($orig_event)) {
202  $title = $this->lng->txt('cal_date_booked');
203  } elseif ($current >= $max) {
204  $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: red';
205  $title = $this->lng->txt('cal_booked_out');
206  } else {
207  $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: green';
208  $title = $this->lng->txt('cal_book_free');
209  }
210  }
211  }
212  }
213  break;
214  }
215  if (strlen($style)) {
216  $this->presentation_style = $style;
217  }
218 
219  return $title;
220  }
221 
222  public function getPresentationStyle(): string
223  {
225  }
226 
232  public function setSubtitle(string $a_subtitle): void
233  {
234  $this->subtitle = $a_subtitle;
235  }
236 
237  public function getSubtitle(): string
238  {
239  return $this->subtitle;
240  }
241 
242  public function setDescription(string $a_description): void
243  {
244  $this->description = $a_description;
245  }
246 
247  public function getDescription(): string
248  {
249  return $this->description;
250  }
251 
252  public function setLocation(string $a_location): void
253  {
254  $this->location = $a_location;
255  }
256 
257  public function getLocation(): string
258  {
259  return $this->location;
260  }
261 
262  public function setFurtherInformations(string $a_informations): void
263  {
264  $this->further_informations = $a_informations;
265  }
266 
267  public function getFurtherInformations(): string
268  {
270  }
271 
277  public function setFullday(bool $a_fullday): void
278  {
279  $this->fullday = $a_fullday;
280  }
281 
282  public function isFullday(): bool
283  {
284  return $this->fullday;
285  }
286 
287  public function isAutoGenerated(): bool
288  {
290  }
291 
292  public function setAutoGenerated(bool $a_status): void
293  {
294  $this->is_auto_generated = $a_status;
295  }
296 
297  public function setContextId(int $a_context_id): void
298  {
299  $this->context_id = $a_context_id;
300  }
301 
302  public function getContextId(): int
303  {
304  return $this->context_id;
305  }
306 
307  public function setTranslationType(int $a_type): void
308  {
309  $this->translation_type = $a_type;
310  }
311 
312  public function getTranslationType(): int
313  {
315  }
316 
317  public function enableNotification(bool $a_status): void
318  {
319  $this->notification = $a_status;
320  }
321 
322  public function isNotificationEnabled(): bool
323  {
324  return $this->notification;
325  }
326 
327  public function update(): void
328  {
329  $now = new ilDateTime(time(), IL_CAL_UNIX);
330  $utc_timestamp = $now->get(IL_CAL_DATETIME, '', ilTimeZone::UTC);
331  $query = "UPDATE cal_entries " .
332  /*
333  * The title needs to be truncated to fit into the table column. This is a pretty
334  * brute force method for doing so, but right now I can't find a better place for it.
335  */
336  "SET title = " . $this->db->quote(mb_substr($this->getTitle(), 0, 128), 'text') . ", " .
337  "last_update = " . $this->db->quote($utc_timestamp, 'timestamp') . ", " .
338  "subtitle = " . $this->db->quote($this->getSubtitle(), 'text') . ", " .
339  "description = " . $this->db->quote($this->getDescription(), 'text') . ", " .
340  "location = " . $this->db->quote($this->getLocation(), 'text') . ", " .
341  "fullday = " . $this->db->quote($this->isFullday() ? 1 : 0, 'integer') . ", " .
342  "starta = " . $this->db->quote($this->getStart()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
343  "enda = " . $this->db->quote($this->getEnd()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
344  "informations = " . $this->db->quote($this->getFurtherInformations(), 'text') . ", " .
345  "auto_generated = " . $this->db->quote($this->isAutoGenerated(), 'integer') . ", " .
346  "translation_type = " . $this->db->quote($this->getTranslationType(), 'integer') . ", " .
347  "context_id = " . $this->db->quote($this->getContextId(), 'integer') . ", " .
348  'context_info = ' . $this->db->quote($this->getContextInfo(), 'text') . ', ' .
349  'notification = ' . $this->db->quote($this->isNotificationEnabled() ? 1 : 0, 'integer') . ' ' .
350  "WHERE cal_id = " . $this->db->quote($this->getEntryId(), 'integer') . " ";
351  $res = $this->db->manipulate($query);
352  }
353 
354  public function save(): void
355  {
356  $next_id = $this->db->nextId('cal_entries');
357  $now = new ilDateTime(time(), IL_CAL_UNIX);
358  $utc_timestamp = $now->get(IL_CAL_DATETIME, '', ilTimeZone::UTC);
359 
360  $query = "INSERT INTO cal_entries (cal_id,title,last_update,subtitle,description,location,fullday,starta,enda, " .
361  "informations,auto_generated,context_id,context_info,translation_type, notification) " .
362  "VALUES( " .
363  $this->db->quote($next_id, 'integer') . ", " .
364  /*
365  * The title needs to be truncated to fit into the table column. This is a pretty
366  * brute force method for doing so, but right now I can't find a better place for it.
367  */
368  $this->db->quote(mb_substr($this->getTitle(), 0, 128), 'text') . ", " .
369  $this->db->quote($utc_timestamp, 'timestamp') . ", " .
370  $this->db->quote($this->getSubtitle(), 'text') . ", " .
371  $this->db->quote($this->getDescription(), 'text') . ", " .
372  $this->db->quote($this->getLocation(), 'text') . ", " .
373  $this->db->quote($this->isFullday() ? 1 : 0, 'integer') . ", " .
374  $this->db->quote($this->getStart()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
375  $this->db->quote($this->getEnd()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
376  $this->db->quote($this->getFurtherInformations(), 'text') . ", " .
377  $this->db->quote($this->isAutoGenerated(), 'integer') . ", " .
378  $this->db->quote($this->getContextId(), 'integer') . ", " .
379  $this->db->quote($this->getContextInfo(), 'text') . ', ' .
380  $this->db->quote($this->getTranslationType(), 'integer') . ", " .
381  $this->db->quote($this->isNotificationEnabled() ? 1 : 0, 'integer') . ' ' .
382  ")";
383  $res = $this->db->manipulate($query);
384 
385  $this->entry_id = $next_id;
386  }
387 
388  public function delete(): void
389  {
391 
392  $query = "DELETE FROM cal_entries " .
393  "WHERE cal_id = " . $this->db->quote($this->getEntryId(), 'integer') . " ";
394  $res = $this->db->manipulate($query);
395 
397  }
398 
399  public function validate(): bool
400  {
401  $success = true;
402  $this->error->setMessage('');
403  if (!strlen($this->getTitle())) {
404  $success = false;
405  $this->error->appendMessage($this->lng->txt('err_missing_title'));
406  }
407  if (!$this->getStart() || !$this->getEnd()) {
408  $success = false;
409  } elseif (ilDateTime::_before($this->getEnd(), $this->getStart(), '')) {
410  $success = false;
411  $this->error->appendMessage($this->lng->txt('err_end_before_start'));
412  }
413  return $success;
414  }
415 
416  protected function read(): void
417  {
418  $query = "SELECT * FROM cal_entries WHERE cal_id = " . $this->db->quote($this->getEntryId(), 'integer') . " ";
419  $res = $this->db->query($query);
420  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
421  $this->setLastUpdate(new ilDateTime((string) $row->last_update, IL_CAL_DATETIME, 'UTC'));
422  $this->setTitle((string) $row->title);
423  $this->setSubtitle((string) $row->subtitle);
424  $this->setDescription((string) $row->description);
425  $this->setLocation((string) $row->location);
426  $this->setFurtherInformations((string) $row->informations);
427  $this->setFullday((bool) $row->fullday);
428  $this->setAutoGenerated((bool) $row->auto_generated);
429  $this->setContextId((int) $row->context_id);
430  $this->setContextInfo((string) $row->context_info);
431  $this->setTranslationType((int) $row->translation_type);
432  $this->enableNotification((bool) $row->notification);
433 
434  if ($this->isFullday()) {
435  $this->start = new ilDate((string) $row->starta, IL_CAL_DATETIME);
436  $this->end = new ilDate((string) $row->enda, IL_CAL_DATETIME);
437  } else {
438  $this->start = new ilDateTime((string) $row->starta, IL_CAL_DATETIME, 'UTC');
439  $this->end = new ilDateTime((string) $row->enda, IL_CAL_DATETIME, 'UTC');
440  }
441  }
442  }
443 
444  public function appointmentToMailString(ilLanguage $lng): string
445  {
446  $body = $lng->txt('cal_details');
447  $body .= "\n\n";
448  $body .= $lng->txt('title') . ': ' . $this->getTitle() . "\n";
449 
451  $body .= $lng->txt('date') . ': ' . ilDatePresentation::formatPeriod($this->getStart(), $this->getEnd()) . "\n";
453 
454  if (strlen($this->getLocation())) {
455  $body .= $lng->txt('cal_where') . ': ' . $this->getLocation() . "\n";
456  }
457 
458  if (strlen($this->getDescription())) {
459  $body .= $lng->txt('description') . ': ' . $this->getDescription() . "\n";
460  }
461  return $body;
462  }
463 }
ilErrorHandling $error
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setTitle(string $a_title)
const IL_CAL_DATETIME
appointmentToMailString(ilLanguage $lng)
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
__clone()
clone instance
static _before(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
setAutoGenerated(bool $a_status)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setContextId(int $a_context_id)
const IL_CAL_UNIX
setStart(?ilDateTime $a_start)
setSubtitle(string $a_subtitle)
set subtitle Used for automatic generated appointments.
getPresentationTitle(bool $a_shorten=true)
setLocation(string $a_location)
static _delete(int $a_entry_id)
global $DIC
Definition: feed.php:28
setFullday(bool $a_fullday)
set fullday event Fullday events do not change their time in different timezones. ...
static getAppointmentIds(int $a_user_id, int $a_context_id=null, ?ilDateTime $a_start=null, ?int $a_type=null, bool $a_check_owner=true)
getEnd()
Get end of period.
static _deleteByAppointmentId(int $a_app_id)
Delete appointment assignment.
setFurtherInformations(string $a_informations)
isFullday()
is event a fullday period
setTranslationType(int $a_type)
setContextInfo(string $a_info)
parseDynamicTitle(string $a_type)
setEnd(?ilDateTime $a_end)
getStart()
Get start of date period.
Error Handling & global info handling.
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
enableNotification(bool $a_status)
setLastUpdate(ilDateTime $a_date)
static shortenTextExtended(string $a_str, int $a_len, bool $a_dots=false, bool $a_next_blank=false, bool $a_keep_extension=false)
setDescription(string $a_description)
static _delete(int $a_cal_id)
static setUseRelativeDates(bool $a_status)
set use relative dates
static shortenWords(string $a_str, int $a_len=30, bool $a_dots=true)
Ensure that the maximum word lenght within a text is not longer than $a_len.